为什么我会收到 bar_index 的运行时错误?

why do i get an runtime error with bar_index?

structureLookback   = input(title="Lookback", type=input.integer, defval=7,  group=trs)
targetBarIndex      = input(title="Bar Index", type=input.integer, defval=0,  group=trs)


//bar_index low high


n = bar_index < targetBarIndex ? na : bar_index - targetBarIndex
pivotLowHigh = tradeType == "Long" ? (bar_index < targetBarIndex ? na : low[n]) : bar_index < targetBarIndex ? na : high[n]
    
//Fib Trailing variablen

var Price = 0.0
t_Price = tradeType == "Long" ? highest(high, structureLookback) : lowest(low, structureLookback)

//Berechnung des Fib-Levels

fib_m23 = Price-(Price-pivotLowHigh)*(-0.236)


//Update Price aufgrund von Price Action

if ((bar_index >= targetBarIndex and  targetBarIndex != 0)or targetBarIndex==0)
    //long version
    if (t_Price > Price or Price == 0.0) and tradeType == "Long"
        Price := t_Price
plot(pivotLowHigh, color=color.gray, title="SwingLow")

这部分函数通过设定的柱线索引使我的轴心变低,有效,但在 10-20 秒后我收到运行时错误。

Pine 无法确定系列的引用长度。尝试使用 max_bars_back

为什么会出现这个错误?以及我必须更改的任何建议?

本文对此进行了解释:https://www.tradingview.com/chart/?solution=43000587849

基本上是因为您正在使用 low[n]high[n] 以及一些未知的 n 并且可能会超出此变量的历史缓冲区。您是否尝试使用 max_bars_back 参数?


更新:

当你在低分辨率的bar上计算时,你的barset数大于10001是无法使用的 类似于 low[targetBarIndex],其中 targetBarIndex = 10000,因为系列变量的最大历史长度为 10000。 您需要重写脚本。

  1. 我建议您添加额外的条件,这将使您的脚本仅在所需的 targetBarIndex (if bar_index > targetBarIndex and bar_index<targetBarIndex+2 ) 附近计算,因为正如我所见,pivotLowHigh 值仅在这个区域。
//@version=4
strategy("My Strategy", overlay=false, margin_long=100, margin_short=100, max_bars_back = 5000)
structureLookback   = input(title="Lookback", type=input.integer, defval=7)
targetBarIndex      = input(title="Bar Index", type=input.integer, defval=0)

tradeType = "Long"
// //bar_index low high
var float pivotLowHigh = na
var int n = na

if bar_index > targetBarIndex and bar_index<targetBarIndex+2
    n := bar_index - targetBarIndex
    pivotLowHigh := tradeType == "Long" ? low[n] : high[n]
    
    
//Fib Trailing variablen

var Price = 0.0
t_Price = tradeType == "Long" ? highest(high, structureLookback) : lowest(low, structureLookback)

//Berechnung des Fib-Levels

fib_m23 = Price-(Price-pivotLowHigh)*(-0.236)


//Update Price aufgrund von Price Action

if ((bar_index >= targetBarIndex and  targetBarIndex != 0)or targetBarIndex==0)
    //long version
    if (t_Price > Price or Price == 0.0) and tradeType == "Long"
        Price := t_Price
plot(pivotLowHigh, color=color.gray, title="SwingLow")

  1. 第二种方法,如果您确实需要计算每个柱的过去值,那么您可以使用数组推送 highlow 值,并从中计算值。数组大小可以比串行变量的历史大 10 倍。