Search code examples
pine-scripttradingalgorithmic-tradingpine-script-v4

Converting multiple iff V1 Function to V5 (Tradingview - Pine Script)


I tried to convert pine v1 iff function to v3 (and afterwards convert it automatically to V5) but I kept getting this error:

extraneous input 'if' expecting 'end of line without line continuation'

This is the V1 code


bcolor = iff( val > 0, 
            iff( val > nz(val[1]), lime, green),
            iff( val < nz(val[1]), red, maroon))

This is what I tried to convert it to V3


bcolor = if (val > 0) and (val > nz(val[1]))
    lime
else if
    (val < 0) and (val > nz(val[1]))
    green
else if
    (val > 0) and (val < nz(val[1]))
    red
else
    maroon

Does this even make sense in a logical way? Im not quite sure if I did it correctly so that the result is the same like in the iff function.


Solution

  • iff() designed this way:

    iff(condition, result1, result2)
    

    If condition is true, it will return result1. Else, it will return result2.

    The correct way of converting that block would be:

    bcolor = if (val > 0)
        if (val > nz(val[1]))
            lime
        else
            green
    else
        if (val < nz(val[1]))
            red
        else
            maroon