user3368250
user3368250

Reputation: 1

Pine Script overwriting data on function with loop and array

So, i have this code, it's a little example of my problem:

`//@version=5
indicator("Bands", "", true)

output = array.new_int(0)
fun(len1,len2,index)=>
    ema1 = ta.ema(close,len1*(index+1))
    ema2 = ta.ema(close,len2*(index+1))
    out = ema1 > ema2 ? 1:-1
    out
var i = 0
i:=0
while i <10
    array.push(output,fun(1+i,2+i,i))
    i+=1
if barstate.islast  
    label.new(bar_index,high,array.join(output,','))`

I want to collect all the "out" data on this "output" array, but the only thing i obtain is something like a overwritting of the last loop all over the array. I have even try to put the "array.push" function in my "fun()" but the result doesn't chage at all....

What i expect is to obtain all the different value and not the array filled with the same value. I have try to use bigger and different lenght period but the result doesn't change.

Upvotes: 0

Views: 177

Answers (2)

user3368250
user3368250

Reputation: 1

For example, i'm tryng this:

   fun(len1,len2,index)=>
       n1 = 1*index+1)
       n2 = 2*index+1)
       array.push(n1arr,n1)
       array.push(n2arrarr,n2)
   
if barstate.islast  
    label.new(bar_index,low,array.join(n1arr,','))
    label.new(bar_index,high,array.join(n2arr,','))

If i use this function, i can plot the exact value of this multiply for each cycle and i have the correct answer. But if i try to use something built in in Pine Script like Ema, i still have all the same value in the array. Coul it be a problem using Array and Indicator together?

Upvotes: 0

G.Lebret
G.Lebret

Reputation: 3108

You have a problem with the ema2 value, it is always evaluate to Nan (Not a Number).

Upvotes: 0

Related Questions