Search code examples
debuggingpine-scripttrading

barstate.islast invalidades series[1]


Original Question

Apparently, when using barstate.islast the values before zero in a series are not returned.

Is this the expected behavior? Or is it a problem?


//@version=5
indicator("barstate.islast invalidades series[1]", max_bars_back=5000)

index = bar_index

countNonNa(indices) =>
    count = 0
    for i = 0 to 4999 by 1
        index = indices[i]
        if not(na(index))
            count += 1
    count
    
    
count00 = countNonNa(index) 
plot(count00, "count00", color = color.blue) // ok

count01 = barstate.isconfirmed ?  countNonNa(index) : 0 
plot(count01, "count01", color = color.black) // ok

count02 = barstate.islast ?  countNonNa(index) : 0 
plot(count02, "count02", color = color.red) // error expected 5000 in the last bar, but gets 1

Image of the example


Solution

  • You should execute your function on every bar, as stated in the console.
    This will work:

    //@version=5
    indicator("barstate.islast invalidades series[1]", max_bars_back=5000)
    
    index = bar_index
    
    countNonNa(indices) =>
        count = 0
        for i = 0 to 4999 by 1
            idx = indices[i]
            if not(na(idx))
                count += 1
        count
        
    c = countNonNa(index)     
    
    count00 = c
    plot(count00, "count00", color = color.blue) // ok
    
    count01 = barstate.isconfirmed ?  c : 0 
    plot(count01, "count01", color = color.black) // ok
    
    count02 = barstate.islast ?  c : 0 
    plot(count02, "count02", color = color.red) // error expected 5000 in the last bar, but gets 1