Search code examples
pine-scriptpine-script-v4

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")

This part of the function to get my pivot low with a set bar index, works but after 10-20s i get an runtime error.

Pine cannot determine the referencing length of a series. Try using max_bars_back

why does this error accure? and any suggestions what i have to change?


Solution

  • This article explains it: https://www.tradingview.com/chart/?solution=43000587849

    Basically its because you are using low[n] and high[n] with some n that is unknown and might go outside history buffer of this variables. Did you try to play with max_bars_back parameter?


    UPD:

    When you calculating on bars with the low resolution, your barset number is bigger than 10001 it is impossible to use something like low[targetBarIndex], where targetBarIndex = 10000, because max history length of series variables is 10000. You need to rewrite your script.

    1. I suggest you to add additional condition which will make your script to calculate only around desired targetBarIndex (if bar_index > targetBarIndex and bar_index<targetBarIndex+2 ), because, as I see, pivotLowHigh value calculating only in this area.
    //@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. Second approach, if you really need to calculate past values in the every bar, then you can use arrays to push high and low values, and calculate values from them. Array size can be 10 times bigger than the history of the serial variable.