Search code examples
pine-scriptpine-script-v5tradingview-api

'IF' Statement Not Working Properly (Pinescript)


I am trying to use an if statement to make my projections better in pinescript. However, the IF statement isn't working has it's supposed too.

The statement:

NCV = if close < companyValue and bias_on
    companyValue = companyValue - bias
else
    companyValue = totalNPVFCF + year10FCFValue + cashAndEquivalents - totalLiabilities

When I plug this in, it will use the "CompanyValue - bias" for the entire thing when bias is on, instead of just when CompanyValue is above close, and bias is on.

Screenshots:

Without Bias On

With Bias On

I've tried switching the <> signs, and flipping the "if" statement, but neither worked.


Solution

  • NCV = close < companyValue and bias_on ? companyValue - bias : totalNPVFCF + year10FCFValue + cashAndEquivalents - totalLiabilities
    

    This should do what you where asking in the comments.

    I am still wondering why no rule when bias is false, cause this would change the whole calculation I guess.

    Explanation...

    You where using variable = if else which will create a locale scope where you edited the value of companyValue. I changed it to a one-liner which wont change companyValue at all and the return value will be NCV. For your approach the return value of NCV I am not sure what it would be, cause there is missing part in the code to find that out. Anyway, my assumption is you wont edit companyValue at all and use NCV to show the changes. This is what it should look like then.

    Addition...

    BTW when you have companyValue declared before, then you have to use := to put a new value into an existing variable.