Search code examples
pine-scriptnested-if

pine v2 if statements meaning


I have been studying pine(tradingview) recently and I've encountered a nested if statement in pine v2 that I don't understand.

//@version=2 
study("name", shorttitle="name", overlay=true)
phi = 0.001

calculate() =>
    last = security(tickerid, '60', close[1])
    actual = security(tickerid, '60', close)
    result = actual - last
    p = result / last

diff = calculate()

q = diff > phi ? true : diff < -phi ? false : q[1]

long_condition = crossover (diff, phi)
plotshape(long_condition, style=shape.triangleup, title = "Buy", text="Buy", location=location.belowbar, size=size.small, color=green, transp=0)

short_condition = crossunder (diff, phi)
plotshape(short_condition, style=shape.triangledown, title = "Sell", text="Sell", location=location.abovebar, size=size.small, color=red, transp=0)


So I don't understand what q is and how do I convert this nested if statement to pine v4 nested if


Solution

  • q is a boolean variable.

    [] in pine is called History Reference Operator. With that, you can access to historical values of a variable. So q[1] returns q‘s previous value.

    If you want to convert this to v4, you need to define the variable q first:

    bool q = false
    q := diff > phi ? true : diff < -phi ? false : q[1]