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])
asked Jul 9, 2022 at 17:59
Gokhan Sahin
1851 gold badge4 silver badges17 bronze badges
-
Please help me it does not work rightlyGokhan Sahin– Gokhan Sahin2022年07月11日 15:31:00 +00:00Commented Jul 11, 2022 at 15:31
1 Answer 1
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
answered Jul 12, 2022 at 17:27
Ricardo Spinoza
1991 silver badge4 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py