stefano introini
stefano introini

Reputation: 37

<Pinescript> Indicator error using bar prices in arrays

I'm using this simple code to learn arrays in pine v5:

var float[] my_arr = array.new_float(0)
if barstate.islast
    array.push(my_arr, close[1])
a = array.get (my_arr, 0)
plot(a)

I'm trying to insert the previous close in my_arr and plot its value. I think the value of the close[1] should be entered in the array at the index 0 using: array.push() But when I plot that value, It appears to me this message: enter image description here

Upvotes: 0

Views: 187

Answers (1)

Bjorgum
Bjorgum

Reputation: 2310

The barstate last call will not run until the last bar of the chart. When you load a script it runs on all historical bars first beginning at the first bar in time. The array.get call is trying to pull a value from an array that is not populated. You can put in a ternary check to make sure the array size is greater than 0, or you could initialize your array with a size and a value like (1) which would give the first populated value na until populated by your of statement. If you just want to plot close[1] you can skip the array and just plot that series

Cheers

Upvotes: 1

Related Questions