I am using Pine Script which is used in tradingview.com.
My question is: how I can get the last value that equals the current value? I thought about using a for
loop or something else.
I tried this code but it returns an error:
getval(x,y) =>
for i = 1 to 100
val = valuewhen(i, y, 1)
val2 = valuewhen(x=i, val, 1)
val2
To get the last value that equals the current value (using pine script version 3), you should write something like this :
getval(x,y) =>
val = 0.0
val2 = 0.0
for i = 1 to 100 //has to be indented as well
val := valuewhen(i,y, 1)
val2 := valuewhen(x == i, val, 1) //== for a condition, = is to assign a value to a variable
val2 ? val2 : val //if val2 exists, return val2, else return val)
It works, I tried it, when you will be calling your function, don't forget to give it the parameters, for example :
getval(1, 3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With