Search code examples
pine-script

Pinescript Conversion from version 4 to 5


Can anyone help me converting this code into PineScript Version 5?

periods=input(21, minval=1, title="MA Period")
pc = input.bool(true, title="MA BAND")


hld = iff(close > ta.sma(high,periods)[1], 1, iff(close<ta.sma(low,periods)[1],-1, 0))
hlv = ta.valuewhen(hld != 0, hld, 1)

I tried this, but the result iw wrong...:

sma_1 = ta.sma(high, periods)
sma_2 = ta.sma(low, periods)
iff_1 = close < sma_2[1] ? -1 : 0
hld = close > sma_1[1] ? 1 : iff_1

Solution

  • You can nest the conditionals:

    Blockquote hld = iff(close > ta.sma(high,periods)[1], 1, iff(close<ta.sma(low,periods)[1],-1, 0))

    hld = close>ta.sma(high,periods)[1]? 1 : close<ta.sma(low,periods)[1]?-1 : 0