Home > Software design >  how pinescript code expression "?:" "if else" to python conversion
how pinescript code expression "?:" "if else" to python conversion

Time:01-31

Sorry for my bad english. I am trying to set Pinescript code to Python. But I'm stuck on line 15.

part of the original code

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// author © KivancOzbilgic
// developer © KivancOzbilgic
//@version=5
indicator('AlphaTrend', shorttitle='AT', overlay=true, format=format.price, precision=2, timeframe='')
coeff = input.float(1, 'Multiplier', step=0.1)
AP = input(14, 'Common Period')
ATR = ta.sma(ta.tr, AP)
src = input(close)
showsignalsk = input(title='Show Signals?', defval=true)
novolumedata = input(title='Change calculation (no volume data)?', defval=false)
upT = low - ATR * coeff
downT = high   ATR * coeff
AlphaTrend = 0.0

# This i'm stuck on line
AlphaTrend := (novolumedata ? ta.rsi(src, AP) >= 50 : ta.mfi(hlc3, AP) >= 50) ? upT < nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : upT : downT > nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : downT

How do i convert? How your did it would you tell me?

I tried to code

AlphaTrend = 0.0
if no_volume_data is True and mfi(high,low,close,volume) >= 50 and upT < nz(AlphaTrend[1]) and nz(AlphaTrend[1])
    #......................

CodePudding user response:

Lets break it down.

AlphaTrend := (novolumedata ? ta.rsi(src, AP) >= 50 : ta.mfi(hlc3, AP) >= 50) ? upT < nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : upT : downT > nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : downT

Is easier to parse as:

cond1 := novolumedata ? ta.rsi(src, AP) >= 50 : ta.mfi(hlc3, AP) >= 50
cond2 := upT < nz(AlphaTrend[1])
cond3 := downT > nz(AlphaTrend[1])
val1 := nz(AlphaTrend[1])
val2 := upT
val3 := nz(AlphaTrend[1])
val4 := downT

AlphaTrend := cond1 ? cond2 ? val1 : val2 : cond3 ? val3 : val4

Putting braces to make it more obvious:

AlphaTrend := cond1 ? (cond2 ? val1 : val2) : (cond3 ? val3 : val4)

Writing this in python would look like:

AlphaTrend = (val1 if cond2 else val2) if cond1 else (val3 if cond3 else val4)

Or as a much more readable format:

if cond1:
    if cond2:
        AlphaTrend = val1
    else:
        AlphaTrend = val2
else:
    if cond3:
        AlphaTrend = val3
    else:
        AlphaTrend = val4

Plugging the variables back in:

if ta.rsi(src, AP) >= 50 if novolumedata else ta.mfi(hlc3, AP) >= 50:
    if upT < nz(AlphaTrend[1]):
        AlphaTrend = nz(AlphaTrend[1])
    else:
        AlphaTrend = upT
else:
    if downT > nz(AlphaTrend[1]):
        AlphaTrend = nz(AlphaTrend[1])
    else:
        AlphaTrend = downT

CodePudding user response:

In Pine Script, the ternary expression takes the form of:

condition ? valueWhenConditionIsTrue : valueWhenConditionIsFalse

In Python, the ternary expression takes the form of:

valueWhenConditionIsTrue if condition else valueWhenConditionIsFalse

Simple Python example:

num = 500
print("big number!" if num > 100 else "small number!")

Output:

big number!
  • Related