Search code examples
pine-scriptpine-script-v5pine-script-v4

How is the Aqua colored line formed in this pine script?


Can you explain the code - how blue(aqua) colored line is formed.

I'm was able to determine that the aqua line depends on the `xATRTrailingStop`, but I'm unable to understand this line.

xATRTrailingStop := iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss), iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss), iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))

Complete Code below:

//@version=4

study(title="JL ATR + Volume Spikes", overlay=true)

atrPeriod = input(20, "Period")

atrMultiplier = input(4, "Multiplier", type=input.float, minval=0.5, maxval=1000, step=0.1)

atr = atr(atrPeriod)

nLoss = atrMultiplier * atr

xATRTrailingStop = float(na)

xATRTrailingStop := iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss), iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss), iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))

pos = int(na)

pos := iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 1, iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0)))

isLong = false

isLong := nz(isLong[1], false)

isShort = false

isShort := nz(isShort[1], false)

LONG = not isLong and pos == 1

SHORT = not isShort and pos == -1

if LONG

isLong := true

isShort := false

isShort

if SHORT

isLong := false

isShort := true

isShort

plotshape(SHORT, title="Sell", style=shape.labeldown, location=location.abovebar, size=size.normal, text="Sell", transp=0, textcolor=color.white, color=color.red)

plotshape(LONG, title="Buy", style=shape.labelup, location=location.belowbar, size=size.normal, text="Buy", textcolor=color.white, color=color.green, transp=0)

len2 = input(20, minval=1, title="Smooth")

src = input(close, title="Source")

out = vwma(src, len2)

avg1 = avg(out, xATRTrailingStop)

plot(avg1, color=color.aqua, transp=0, title="ATR")

vol_length = input(20, "Volume SMA length", minval=1)

vol_avg = sma(volume, vol_length)

unusual_vol_down = volume > vol_avg * 1.2 and close < open

unusual_vol_up = volume > vol_avg * 1.2 and close > open

barHighlightColor = unusual_vol_down or unusual_vol_up ? color.yellow : na

barcolor(barHighlightColor)

Thanks!

I tried using understanding the formation of blue line in the following way:

  1. plot(avg1, color=color.aqua, transp=0, title="ATR") ("7th last line")

  2. avg1 = avg(out, xATRTrailingStop)

  3. xATRTrailingStop := iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss), iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss), iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))

More precisely, blue line depends on the value of xATRTrailingStop (3rd point above), but, I'm unable to determine its value?


Solution

  • It is bunch of conditions.

    iff() is basically If ... then ... else ....

    iff(condition, then, _else)
    

    You can write the same statement with the ternary operator as below:

    condition ? then : _else
    

    Or with the if blocks:

    if (condition)
        then
    else
        _else
    

    You need to break down all those conditions in order to understand what is going on.

    If you convert it to v5, it will break it down a few steps for you:

    jatr_xATRTrailingStop = float(na)
    iff_1 = close > nz(jatr_xATRTrailingStop[1], 0) ? close - jatr_nLoss : close + jatr_nLoss
    iff_2 = close < nz(jatr_xATRTrailingStop[1], 0) and close[1] < nz(jatr_xATRTrailingStop[1], 0) ? math.min(nz(jatr_xATRTrailingStop[1]), close + jatr_nLoss) : iff_1
    jatr_xATRTrailingStop := close > nz(jatr_xATRTrailingStop[1], 0) and close[1] > nz(jatr_xATRTrailingStop[1], 0) ? math.max(nz(jatr_xATRTrailingStop[1]), close - jatr_nLoss) : iff_2
    

    After that, just follow each line and check out the conditions and their results.

    Edit:

    jatr_xATRTrailingStop = float(na)
    iff_1 = close > nz(jatr_xATRTrailingStop[1], 0) ? close - jatr_nLoss : close + jatr_nLoss
    

    In the above statement, jatr_xATRTrailingStop will be na in the first line. Then there is close > nz(jatr_xATRTrailingStop[1], 0) check.

    nz() will replace the nan valuee with zeros. Since, jatr_xATRTrailingStop is na, it will replace that with 0.

    So, it will be: close > 0 which is true. Therefore, the result will be close - jatr_nLoss.

    Then on the next iteration (2nd bar), close > nz(jatr_xATRTrailingStop[1], 0) will become close > (close - jatr_nLoss)[1].