'The study references too many candle

I am a super beginner and I am struggling with a problem that is certainly bigger than me. The basic idea is to have pivot lines only for the current price range + or - a 20% If the close crosses the upper line it sells, otherwise it buys. All combined with a cyclic analysis (which I have yet to define) but the following code doesn't work ... the problem should be in max_bars_back as I run checks in the past before bar 241. p.s. I also tried to reverse the reasoning but I don't like it so much ... "if bar_index> (last_bar_index-500)"

Another info please, when i have an line [] array.new_line I thought of doing a "push" of the line, doing the push also plots the line, at which point I thought of taking the data from the array inserting it in a variable line and then do a "line.delete", but doing so the data inside the array becomes "nan"

var line[] array_ph = array.new_line(0)
var line[] array_pl = array.new_line(0)
leftBars = input(2)
rightBars=input(2)
ph = ta.pivothigh(leftBars, rightBars)
pl = ta.pivotlow(close, leftBars, rightBars)

if barstate.isrealtime
    // for i=0 to 499
    //     line current_line_h = array.get(array_ph,i)
    //     line.set_x2(current_line_h, x= bar_index)
    //     line current_line_l = array.get(array_pl,i)
    //     line.set_x2(current_line_h, x= bar_index)
    
   
    label.new(bar_index,high*1.02, text=str.tostring(bar_index)+"sono qui")
    for ii=bar_index to  (bar_index - 499)
        // debug label.new(bar_index,high, text="sono qui")
        if array.size(array_ph)<=499 and ph[ii]>0  
            array.push(array_ph, line.new(bar_index[ii]-rightBars,ph[ii],last_bar_index,ph[ii]))


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source