Search code examples
pine-scriptpine-script-v5

Pine Script language syntax


I just start to learning Pine Script. And I cant find any information about this issue in language documentation or google. This code is working and I find it in someones indicator script.

I want to understand what this means:

  1. if (float)

float = float ? float : float
    
float ph = ta.pivothigh(prd, prd)
float pl = ta.pivotlow(prd, prd)
    
float lastpp = ph ? ph : pl ? pl : na
    
if lastpp
    ...

Solution

  • ?: is called ternary operator.

    The way it works is, it will check a condition and return value1 if the condition is true and return value2 otherwise.

    condition ? value1: value2
    

    You can write the same check with an if block as below:

    if (condition)
        value1
    else
        value2
    

    Edit:

    ta.pivothigh will return NaN if there is no pivot. NaN is not a number so when you run it in a condition it will return false.

    Essentially, what ph ? checks is to see if there is a pivot high or not.