0
con = ta.crossover(ta.rsi(close, 10), 20)

ser1 = 0
ser2 = 0

if con[1]
    ser1 := math.round(math.random(-5, 5))
    ser2 := math.round(math.random(-5, 5))

I have two series ser1 & ser2 which are always zero except when condition con[1] is reached. ser1 & ser2 is given a random number from -5 to 5.

arr1 = array.new_int()
array.push(arr1, ser1)
array.push(arr1, ser2)

if con[1]
    label.new(bar_index, high, str.tostring(arr1))

Series ser1 & ser2 is pushed inside array arr1.

enter image description here

Calculating cumulative of ser1 and ser1 when outside of array arr1:

cumSer1 = ta.cum(ser1)
cumSer2 = ta.cum(ser2)

if barstate.islast
    label.new(bar_index+50, high, str.tostring(cumSer1) + ", " + str.tostring(cumSer2))

Calculating cumulative of ser1 and ser1 when inside of array arr1:

arr2 = array.new_float()
for [i, ser] in arr1
    array.push(arr2, ta.cum(array.get(arr1, i)))

if barstate.islast
    label.new(bar_index, high, str.tostring(arr2))

I tried calculating the cumulative of both series when they're outside and inside of the array and I got varying results. I get the wrong answer when I try to calculate when the series are inside of the array. Calculating the cumulative of series the outside the array gives the correct answer.

enter image description here

How do I get the cumulative of series ser1 and ser2 when they are inside of the array arr1?

bbnm
  • 1,034
  • 1
  • 4
  • 14

0 Answers0