1

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
1
  • Please help me it does not work rightly Commented Jul 11, 2022 at 15:31

1 Answer 1

2
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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.