Reputation: 329
I am having troubles with understanding PineScript ?
statement logic, there is such function in Pinescript on TradingView:
updown(s) =>
isEqual = s == s[1]
isGrowing = s > s[1]
ud = 0.0
ud := isEqual ? 0 : isGrowing ? (nz(ud[1]) <= 0 ? 1 : nz(ud[1])+1) : (nz(ud[1]) >= 0 ? -1 : nz(ud[1])-1)
ud
I have tried my best to crack the logic of the ?
expression above, but I am not sure if I am correct.
I have written the logic in pseudo code, but I need someone to tell me if I am correct.
if s == s.shift(1):
ud = 0
else:
if s > s.shift(1):
if (nz(ud[1]) <= 0:
ud = 1
else:
nz(ud[1]) + 1)
else:
if nz(ud[1]) >= 0:
ud = -1
else:
nz(ud[1]) - 1)
If I am correct or wrong please tell because I have no idea myself.
Upvotes: 0
Views: 233
Reputation: 21209
?:
is called ternary operator.
condition ? valueWhenConditionIsTrue : valueWhenConditionIsFalse
You can write the below statement:
ud := isEqual ? 0 : isGrowing ? (nz(ud[1]) <= 0 ? 1 : nz(ud[1])+1) : (nz(ud[1]) >= 0 ? -1 : nz(ud[1])-1)
as
ud := if (isEqual)
0
else
if (isGrowing)
if ((nz(ud[1]) <= 0)
1
else
nz(ud[1])+1)
else
if ((nz(ud[1]) >= 0)
-1
else
nz(ud[1])-1)
Upvotes: 2