vendredi 8 mai 2020

Nested loops with if statements aren't working properly

-input is a data frame of OHLC for USDJPY pair

-ssuport is a list of potential support price I have calculated

I have a support list where I need to run every candlestick to this list to see if it hits on any of them , then calculate buy price , takeprofit, stoploss. the problem is when I output the following code, I get this

buy price     [108.528, nan, 107.193001, nan, 107.496002, nan]
sell price    [nan, 108.466003, nan, 107.089996, nan, 107.847]

which means elif statement isn't properly working, why ?!

I want to extend my code even further to identify short sells but I don't know how! My code

sbuy=[]
takeprofit = []
stoploss = []
ssell = []
open_orders = 0

for i in range(len(df['Close'])): #loop through candlestics
  for j in range (len(ssupport)) : # loop through support list
    if open_orders != 1 :

        if ((ssupport[j] * .98) <= df['Low'][i-1] <= (ssupport[j] * 1.02 )) & (df['Low'][i] >= df['High'] 
 [i-1]) :

            sbuy.append (df['High'][i])
            ssell.append(np.nan)
            buyprice = df['High'][i]
            stoploss = (df['Low'][i] * 0.95)
            open_orders = 1
            if (j+1 < len(ssupport)) :
                takeprofit = (ssupport[j+1])
                break
            else:
                takeprofit = (ssupport[j] * 1.05)
                break
        break           

    elif open_orders == 1 :
        if (df['High'][i] > takeprofit) | (df['Low'][i] < stoploss) :
           sellprice = df['High'][i]
           ssell.append(df['High'][i])
           sbuy.append (np.nan)
           open_orders = 0
           break
        break

print (sbuy) print (ssell)

Aucun commentaire:

Enregistrer un commentaire