Pine-script SECURITY 函数没有 return 一致的结果

Pine-script SECURITY function doesn't return consistent result

为了回测其中一个策略,我需要检索过去 5 天的日最高值,这在任何图表分辨率下都应该有效。

当图表分辨率为每日时,下面的代码工作正常,但如果我将图表分辨率更改为较低的时间范围,即 15 分钟或 30 分钟,我会在第 2、3、4 和 5 天获得当天的高值。

理想情况下,安全函数输出不应受到图表分辨率变化的影响,并且输出应该是不同的值。

// @version=4
study("Plot high prices for lat 5 days")

day_high = security(syminfo.tickerid, "D", high, false)
day1 = day_high[1]
day2 = day_high[2]
day3 = day_high[3]
day4 = day_high[4]
day5 = day_high[5]

//Check whether this is the first bar of the day? If yes, display highs for last 5 days
t = time("1440", session.regular)
is_first = na(t[1]) and not na(t) or t[1] < t

if (is_first)  
    label.new(bar_index, na, tostring(day5) + ", " + tostring(day4) + ", " + tostring(day3) + ", " + tostring(day2) + ", " + tostring(day1), style=label.style_cross, yloc=yloc.abovebar)

您需要为每个变量调用 security() 函数。

您可以阅读 How to avoid repainting when using security() - PineCoders FAQ 了解更多详情。

// @version=4
study("Plot high prices for lat 5 days")

day1 = security(syminfo.tickerid, "D", high[1], lookahead = barmerge.lookahead_on)
day2 = security(syminfo.tickerid, "D", high[2], lookahead = barmerge.lookahead_on)
day3 = security(syminfo.tickerid, "D", high[3], lookahead = barmerge.lookahead_on)
day4 = security(syminfo.tickerid, "D", high[4], lookahead = barmerge.lookahead_on)
day5 = security(syminfo.tickerid, "D", high[5], lookahead = barmerge.lookahead_on)

//Check whether this is the first bar of the day? If yes, display highs for last 5 days
t = time("1440", session.regular)
is_first = na(t[1]) and not na(t) or t[1] < t

if (is_first)  
    label.new(bar_index, na, tostring(day5) + ", " + tostring(day4) + ", " + tostring(day3) + ", " + tostring(day2) + ", " + tostring(day1), style=label.style_cross, yloc=yloc.abovebar)