request.security 在更改时间范围时获得不同的值

问题描述 投票:0回答:3

我正在尝试在数组中收集随机 rsi 100 和 0 值并将其用于策略(或指标)。我无法在时间范围内获得它的固定值。当我使用与收集时间范围相同的时间范围时,它工作正常,但当我将其更改为实时图表上的较低时间范围时,它不会收集正确的值。下面的示例仅在时间范围为“月”时获取正确的数据,但在周或天等较低时间范围内则无法获取正确的数据。 有人可以帮忙解决这个问题吗?

//@version=5 
indicator(title="test", shorttitle="test", format=format.price, precision=2, overlay=true) 
// general options
displayMonthlySto = input(true, "Show Monthly Stoch", group="General Options")

// options for monthly stoRSI
smoothKMon = input.int(3, "K", minval=1, group="Monthly Time Frame Options") 
smoothDMon = input.int(3, "D", minval=1, group="Monthly Time Frame Options") 
lengthRSIMon = input.int(14, "RSI Length", minval=1, group="Monthly Time Frame Options") 
lengthStochMon = input.int(14, "Stochastic Length", minval=1, group="Monthly Time Frame Options") 
srcMon = input(close, title="RSI Source", group="Monthly Time Frame Options") 
rsi1Mon = ta.rsi(srcMon, lengthRSIMon) 
kMon = ta.sma(ta.stoch(rsi1Mon, rsi1Mon, rsi1Mon, lengthStochMon), smoothKMon) 
dMon = ta.sma(kMon, smoothDMon) 

// timeframe for month
stoMonth = request.security(syminfo.tickerid, "M", kMon)

// initialize arrys for stoch
var sto100MonArr = array.new_float() 
var sto0MonArr = array.new_float()

// collect sto100 and 0 data
if ta.change(time_close("1M")) and displayMonthlySto
    if stoMonth == 100 
        array.push(sto100MonArr, close)
    if stoMonth == 0
        array.push(sto0MonArr, close)

plot(array.size(sto100MonArr)>0 ? array.get(sto100MonArr, array.size(sto100MonArr)-1) : na)

pine-script pine-script-v5
3个回答
0
投票

Request.security 在加载较低 TF 数据时给出不一致的结果 对于 Lower TF 数据,您必须使用:https://www.tradingview.com/pine-script-reference/v5/#fun_request{dot}security_lower_tf

此函数返回较低 TF 值的数组

想要锁定更大的TF值,可以试试这个

stoMonth = request.security(syminfo.tickerid, "M", kMon[1], barmerge.gaps_off, barmerge.lookahead_on)

0
投票

如果您计算 request.security 函数中的所有内容,它会给出更准确的结果,就像这个例子对我有用。它在不同的时间范围内给出相同的结果..

request.security(
  syminfo.tickerid,
  '15',
  ta.sma(ta.stoch(
    ta.rsi(close, lengthRSI),
    ta.rsi(close, lengthRSI),
    ta.rsi(close, lengthRSI),
    lengthStoch
  ), smoothK),
  barmerge.gaps_off,
  barmerge.lookahead_on
)

0
投票

当间隙参数使用 barmerge.gaps_on 时,该函数会在所有尚未从请求的上下文中确认新数据的图表条上返回 na 结果。否则,当参数使用 barmerge.gaps_off 时,该函数会使用历史柱上的最后确认值和实时柱上的最新发展值来填充请求数据中的空白。

daily_range = request.security(syminfo.tickerid, "D", Range(high, low),barmerge.gaps_on,barmerge.lookahead_off)

更多参考:- https://www.tradingview.com/pine-script-docs/concepts/other-timeframes-and-data/#gaps

© www.soinside.com 2019 - 2024. All rights reserved.