1

Im trying to identify highest high candles and save the difference between the open and close for the last 10 occurrences of these candles into an array, and then get the average of the array. However, I'm having some problems since nan values are being stored in the array, so i'm getting wrong results.

highestHigh = highest(high, 20)
is_high = high >= highestHigh

var float[] HHarray = array.new_float(10) // Array size 10 i.e last 10 candles

if is_high
    array.push(HHarray, (close-open))

ArrayAverage = array.sum(HHarray) / array.size (HHarray)

Any idea how this can be fixed? Also, is there a way to view all values stored in an array?

Thanks.

user2334436
  • 949
  • 5
  • 13
  • 34

1 Answers1

0

You have a different problem. The size parameter defines the initial size, not the maximal one. See ref here.
So you're initializing the array with 10 na values and just push the rest to it.
The 4th example of tradingview in the docs even solves your problem. Code copied with some tweak for you:

//@version=5
indicator("array.new<float> example")
length = 10
var a = array.new<float>(length, close)
if array.size(a) == length and is_high
    array.remove(a, 0)
    array.push(a, close-open)
// ...
elod008
  • 1,227
  • 1
  • 5
  • 15