Search code examples
pine-scriptvarindicator

How to use var in pine script?


This is an indicator:

//box 
boxp=input(5, "BOX LENGTH")
LL = lowest(low,boxp)
k1=highest(high,boxp)
k2=highest(high,boxp-1)
k3=highest(high,boxp-2)
NH =  valuewhen(high>k1[1],high,0)
box1 =k3<k2
TopBox = valuewhen(barssince(high>k1[1])==boxp-2 and box1, NH, 0)
BottomBox = valuewhen(barssince(high>k1[1])==boxp-2 and box1, LL, 0)

and I use variable: var longstopprice = BottomBox[0]

but it isn't available to display the value when I plot longstopprice. Can anyone help me?


Solution

  • var is the keyword used for assigning and one-time initializing of the variable.

    When you do longstopprice = BottomBox[0], it will initialize ´longstoppricewith the value ofBottomBox` on the very first bar and it will never change its value unless you explicitly do that.

    It will take BottomBox several bars to get any none na value because you are using functions like highest() and barssince().

    If you plot BottomBox, you will see that it will take a few bars until it gets a valid number value.

    enter image description here