Python Forum
Double 'for' loop and writing in a new columns dataframe
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add columns to polars dataframe sayyedkamran 1 1,763 Nov-03-2023, 03:01 PM
Last Post: gulshan212
  concat 3 columns of dataframe to one column flash77 2 839 Oct-03-2023, 09:29 PM
Last Post: flash77
  Convert several columns to int in dataframe Krayna 2 2,393 May-21-2021, 08:55 AM
Last Post: Krayna
  Outputs "NaN" after "DataFrame columns" function? epsilon 7 3,649 Jan-27-2021, 10:59 AM
Last Post: epsilon
  Adapting a dataframe to the some of columns flyway 2 2,048 Aug-12-2020, 07:21 AM
Last Post: flyway
  Difference of two columns in Pandas dataframe zinho 2 3,360 Jun-17-2020, 03:36 PM
Last Post: zinho
  DataFrame: To print a column value which is not null out of 5 columns mani 2 2,112 Mar-18-2020, 06:07 AM
Last Post: mani
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 2,458 Mar-14-2020, 09:19 AM
Last Post: scidam
  Interate for loop over certain columns in dataframe Finpyth 2 1,949 Mar-06-2020, 08:34 AM
Last Post: Finpyth
  newbie: loop, modify dataframe cells expat_th 5 3,690 Mar-03-2020, 09:05 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020