Sep-07-2019, 12:44 PM
Hi there,
the below code work as long as i remove the lines number 8 and 26.
But i need to create another loop using j as I need the multiplier in row 26 to change dynamically between 0 and 5.
Now once it is achieved, i will need to print a new 5 new columns for j=0, j=1, j=2, etc till j=5.
Under each column i want to display the 'Profit/Loss' but according to that specific value of j at each interaction.
I am getting crazy and can't find the way.
Appreciate your help,
Thanks a lot.
Marco.
the below code work as long as i remove the lines number 8 and 26.
But i need to create another loop using j as I need the multiplier in row 26 to change dynamically between 0 and 5.
Now once it is achieved, i will need to print a new 5 new columns for j=0, j=1, j=2, etc till j=5.
Under each column i want to display the 'Profit/Loss' but according to that specific value of j at each interaction.
I am getting crazy and can't find the way.
Appreciate your help,
Thanks a lot.
Marco.
# assign to variable n the total number of rows n = df_dj30.shape[0]-1 new_df = pd.DataFrame() #loop cycle and add new values to the new dataframe new_df for j in range(0, 5): for i in range(0, n): new_df.loc[i, 'Date[i]'] = (df_dj30['Date'][i]) new_df.loc[i, 'Delta[i]'] = (df_dj30['Delta'][i]) if (df_dj30['Delta'][i])>0: new_df.loc[i, 'Action[i+1]'] = 'buy', else: new_df.loc[i, 'Action[i+1]'] = 'sell' new_df.loc[i, 'Open[i+1]'] = (df_dj30['Open'][i+1]) new_df.loc[i, 'Close[i+1]'] = (df_dj30['Close'][i+1]) new_df.loc[i, 'High[i]'] = (df_dj30['High'][i]) new_df.loc[i, 'Low[i]'] = (df_dj30['Low'][i]) new_df.loc[i, 'High[i+1]'] = (df_dj30['High'][i+1]) new_df.loc[i, 'Low[i+1]'] = (df_dj30['Low'][i+1]) if new_df.loc[i, 'Action[i+1]'] == 'sell': # new_df.loc[i, 'SL[i+1]'] = (max(new_df.loc[i, 'High[i]'], new_df.loc[i, 'Open[i+1]']))-0 , new_df.loc[i, 'SL[i+1]'] = (new_df.loc[i, 'Open[i+1]'])*(1+j/1000) , new_df.loc[i, 'TP[i+1]'] = new_df.loc[i, 'Close[i+1]'] , if new_df.loc[i, 'High[i+1]'] >= new_df.loc[i, 'SL[i+1]']: new_df.loc[i, 'Profit/Loss'] = new_df.loc[i, 'Open[i+1]'] - new_df.loc[i, 'SL[i+1]'], new_df.loc[i, 'Profit/Loss-2'] = new_df.loc[i, 'Close[i+1]'] - new_df.loc[i, 'SL[i+1]'], else: new_df.loc[i, 'Profit/Loss'] = new_df.loc[i, 'Open[i+1]'] - new_df.loc[i, 'Close[i+1]'], else: # new_df.loc[i, 'SL[i+1]'] = (min(new_df.loc[i, 'Low[i]'], new_df.loc[i, 'Open[i+1]']))+0, new_df.loc[i, 'SL[i+1]'] = (new_df.loc[i, 'Open[i+1]'])*0.99, new_df.loc[i, 'TP[i+1]'] = new_df.loc[i, 'Close[i+1]'], if new_df.loc[i, 'Low[i+1]'] <= new_df.loc[i, 'SL[i+1]']: new_df.loc[i, 'Profit/Loss'] = new_df.loc[i, 'SL[i+1]'] - new_df.loc[i, 'Open[i+1]'], new_df.loc[i, 'Profit/Loss-2'] = new_df.loc[i, 'SL[i+1]'] - new_df.loc[i, 'Close[i+1]'], else: new_df.loc[i, 'Profit/Loss'] = new_df.loc[i, 'Close[i+1]'] - new_df.loc[i, 'Open[i+1]'] new_df.loc[i, 'Delta[i+1]'] = (df_dj30['Delta'][i+1]) #temporary as it might changes according to the SL triggering if (df_dj30['Delta'][i])>0 and new_df.loc[i, 'Delta[i+1]']>0: new_df.loc[i, 'Outcome[i+1]'] = 'profit', elif (df_dj30['Delta'][i])<0 and new_df.loc[i, 'Delta[i+1]']<0: new_df.loc[i, 'Outcome[i+1]'] = 'profit', else: new_df.loc[i, 'Outcome[i+1]'] = 'loss'
Output: Date[i] Delta[i] Action[i+1] Open[i+1] Close[i+1] High[i] Low[i] High[i+1] Low[i+1] SL[i+1] TP[i+1] Profit/Loss Delta[i+1] Outcome[i+1] Profit/Loss-2
0 2002-04-08 -11.0 sell 10290.0 10222.0 10280.0 10105.0 10312.0 10204.0 10392.90 10222.0 68.00 -68.0 profit NaN
1 2002-04-09 -68.0 sell 10229.0 10387.0 10312.0 10204.0 10399.0 10215.0 10331.29 10387.0 -102.29 158.0 loss 55.71
2 2002-04-10 158.0 buy 10378.0 10170.0 10399.0 10215.0 10378.0 10158.0 10274.22 10170.0 -103.78 -208.0 loss 104.22
3 2002-04-11 -208.0 sell 10210.0 10194.0 10378.0 10158.0 10240.0 10140.0 10312.10 10194.0 16.00 -16.0 profit NaN
4 2002-04-12 -16.0 sell 10196.0 10093.0 10240.0 10140.0 10230.0 10068.0 10297.96 10093.0 103.00 -103.0 profit NaN