Reputation: 185
I'm trying to convert a pine script to python but I'm confused about the following code because some results are not same with pine script. For example, at the moment, only last 5 values are right, others are wrong. Please check my code.
Pine Script:
up=src-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
converted code:
up = src - (Multiplier*atr)
up1 = up.copy()
for i in range(len(up1)):
if(i>0):
up1[i] = up[i-1]
for i in range(len(up)):
if(i > 0 and close[i-1] > up1[i]):
up[i] = max(up[i], up1[I])
Upvotes: 1
Views: 2692
Reputation: 199
def nz(x, y=None):
'''
RETURNS
Two args version: returns x if it's a valid (not NaN) number, otherwise y
One arg version: returns x if it's a valid (not NaN) number, otherwise 0
ARGUMENTS
x (val) Series of values to process.
y (float) Value that will be inserted instead of all NaN values in x series.
'''
if isinstance(x, np.generic):
return x.fillna(y or 0)
if x != x:
if y is not None:
return y
return 0
return x
ref. https://community.backtrader.com/topic/2671/converting-pinescript-indicators/3
Upvotes: 2