Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Double 'for' loop and writing in a new columns dataframe
#1
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.



# 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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How modify the DataFrame columns SriRajesh 2 93 Sep-12-2019, 03:14 PM
Last Post: SriRajesh
  Creating A List of DataFrames & Manipulating Columns in Each DataFrame firebird 1 151 Jul-31-2019, 04:04 AM
Last Post: scidam
  [pandas] How to re-arrange DataFrame columns SriMekala 8 782 Jun-22-2019, 12:55 AM
Last Post: scidam
  Selecting Few Columns from a dataframe Shivi_Bhatia 2 378 Mar-24-2019, 12:20 PM
Last Post: Shivi_Bhatia
  compare and modify columns in dataframe DionisiO 1 322 Feb-23-2019, 11:07 PM
Last Post: tiredAcademic
  build pandas dataframe from a for loop vaison 4 30,165 Apr-14-2018, 04:34 PM
Last Post: vaison
  Get rows with same value from dataframe of particular columns angelwings 1 648 Apr-11-2018, 02:40 AM
Last Post: scidam
  Stack dataframe columns into rows klllmmm 0 1,149 Sep-03-2017, 02:26 AM
Last Post: klllmmm

Forum Jump:


Users browsing this thread: 1 Guest(s)