for-loopcompiler-errorspine-scriptundeclared-identifier

Negative index caused by undeclared identifier in 'for' statement


I'm trying to test my script, and I wanted to plot some of the variables I have on it. The problem is that everything that is being ploted before the 'for' statement works, but nothing comes to work beyond that. I made a test to plot the 'bar' variable, and when I do so, it pops an error: Undeclared identifier 'bar'. This happens when I try to plot the 'bar' variable.

The 'SwingIndex' variable contains the result that I finally want to plot in the indicator, but when I try to plot it, it comes an error on the indicator window that says 'Study Error Negative index -1'. Something is causing a problem somewhere inside the 'for' statement.

Personally, I see that since the 'bar' variable is not working (declared in line 39 of my code), nothing else is. But I don't know why is not working.

Can anyone help me to find where is my mistake? I'll leave the code down here.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TD_DaFen

//@version=5
// Indicator name
indicator("DAF_Swing_Index", shorttitle= 'DAF_SwInd', overlay=false)
// Input
T = input.int(30000, title = 'Ratio de escala', minval = 1000, maxval = 150000)
Shift = input.int(0, title = 'Desplazamiento horizontal', minval = 0, maxval = 100)

// Other variable
var float SwingIndex = 0
var int StartBars = 1
Prev_calculated = bar_index
Rates_total = bar_index + 1
var float SH1 = 0
var float SI = 0
var float R = 0

// Array
Open = array.new_float(Rates_total)
Open1 = array.new_float(Rates_total)
Close = array.new_float(Rates_total)
Close1 = array.new_float(Rates_total)
High = array.new_float(Rates_total)
Low = array.new_float(Rates_total)

// Initial bar verification
if Rates_total < StartBars 
    SwingIndex := 0

Primero = 1
if Prev_calculated > Rates_total or Prev_calculated <= 0
    Primero := 1
else
    Primero := Prev_calculated -1

// Main process
for bar= Primero to Rates_total-1
    array.push(Open, high[bar])
    array.push(Open1, open[bar-1])
    array.push(Close, close[bar])
    array.push(Close1, close[bar-1])
    array.push(High, high[bar])
    array.push(Low, low[bar])

    K = math.max(math.abs(array.get(High, bar)- array.get(Close1, bar)), math.abs(array.get(Low, bar) - array.get(Close1, bar)))
    TR = math.max(math.max(math.abs(array.get(High, bar) - array.get(Close1, bar)), math.abs(array.get(Low, bar) - array.get(Close1, bar))), math.abs(array.get(High, bar) - array.get(Low, bar)))
    
    ER = 0.0
    
    if array.get(Close1, bar) > array.get(High, bar)
        ER := math.abs(array.get(High, bar) - array.get(Close1, bar))
    if array.get(Close1, bar) < array.get(Low, bar)
        ER := math.abs(array.get(Low, bar) - array.get(Close1, bar))
    
    
    SH1 := math.abs(array.get(Close1, bar) - array.get(Open1, bar))
    R := TR - 0.5 * ER + 0.25 * SH1
    SI := 0.0
    if R != 0
        SI := 50 * ((array.get(Close, bar) - array.get(Close1, bar)) + 0.5 * (array.get(Close, bar) - array.get(Open1, bar))) * (K / T) / R
    
    SwingIndex := SI

// ploting partial results to see if its working

plot(SwingIndex, title = 'Swing Index', style = plot.style_line,  color = color.rgb(193, 150, 51, 10))


Solution

  • Your problem is in your for loop. Error message tells you that you are trying to access an element at negative index -1.

    Following two array.push calls are causing the problem:

    for bar= Primero to Rates_total-1
    array.push(Open1, open[bar-1])
    array.push(Close1, close[bar-1])
    

    That is because at the second bar of the chart, Primero becomes zero, making your for loop start from zero (for bar = Primero). Then you subtract 1 from zero and therefore try to access an element at negative index -1.

    How do we know that?

    Remember your script will be executed for every bar. Let's analyze the following segments of your code for the very first and second bars.

    Prev_calculated = bar_index
    Rates_total = bar_index + 1
    
    Primero = 1
    if Prev_calculated > Rates_total or Prev_calculated <= 0
        Primero := 1
    else
        Primero := Prev_calculated -1
    

    Very first bar (bar_index = 0)

    Prev_calculated = 0
    Rates_total = 1
    Primero = 1 (if branch -> Prev_calculated <= 0 is true)
    

    Second bar (bar_index = 1)

    Prev_calculated = 1
    Rates_total = 2
    Primero = 0 (else branch)
    

    Also, this Prev_calculated > Rates_total check will always be false.