Apologies in advance as technically i have a few questions regarding the problem i would like to solve with python but since they are related i am putting it all in one post (at least i am hoping it will be a worthy challenge for whoever is able to help me with this).
I have the following pandas dataframe called df example:
REF Period Product Price Type QTY
T001 Jan-20 EQ 69.87 Sell -10
T001 Feb-21 EQ 69.77 Buy 10
T002 Apr-20 BN 10.77 Buy 15
T003 Jul-20 EQ 71.25 Sell -20
T003 Aug-20 EQ 70.89 Buy 40
T003 Sep-20 EQ 70.69 Sell -20
T004 Nov-20 BN 20.83 Buy 10
T004 Dec-20 EQ 40.01 Sell 12
T005 Sep-20 FD 31.25 Buy -20
T005 Mar-21 FD 36.89 Sell 40
T005 Sep-21 FD 40.69 Buy -20
As you can see the column REF refers to the trade reference of a portfolio that i would like to analyse.
I have been struggling with finding a solution for the following data analysis problems:
I would like python to go over each row of the trade reference column and:
Add a column
TRADE_TYPEandDIRECTION(this part i got covered of course)If the
REFis unique (no duplicates in the column) thenTRADE_TYPEshould be = "Flat" + df[Period] (i.e. "Flat Apr-20") andDIRECTIONshould be = df[Type] of that rowIf the
REFis the same across 2 rows,Productis the same across these rows,Periodis different in these rows, andTypeis different (one is Sell the other is Buy) thenTRADE_TYPEin those rows should = "Spread" andDIRECTIONshould be equal to the Type of the first row (i.e. if the first row of the 2 says Sell then its a Sell and vice versa)If the
REFis the same across 3 rows,Productis the same across these rows,Periodis different across these rows, and the sum of theQTYof these rows equals ZERO thenTRADE_TYPEshould be = "Trio" andDIRECTIONshould be equal to the Type of the first row (i.e. if the first row of the 3 is SELL second is BUY and third is SELL, then it should pick the value of the first one)If the
REFis the same across 2 rows but theProductis different thenTRADE_TYPEshould be = "Arbitrage" andDIRECTIONshould be equal to theTypeof the first row (i.e. if the first row of the 2 says Sell then its a Sell and vice versa)
The final result should be something like this if using the above table as example:
REF Period Product Price Type QTY TRADE_TYPE DIRECTION
T001 Jan-20 EQ 69.87 Sell -10 Spread Sell
T001 Feb-21 EQ 69.77 Buy 10 Spread Sell
T002 Apr-20 BN 10.77 Buy 15 Flat Apr Buy
T003 Jul-20 EQ 71.25 Sell -20 Trio Sell
T003 Aug-20 EQ 70.89 Buy 40 Trio Sell
T003 Sep-20 EQ 70.69 Sell -20 Trio Sell
T004 Nov-20 BN 20.83 Buy 10 Arbitrage Buy
T004 Dec-20 EQ 40.01 Sell 12 Arbitrage Buy
T005 Sep-20 FD 31.25 Buy -20 Trio Buy
T005 Mar-21 FD 36.89 Sell 40 Trio Buy
T005 Sep-21 FD 40.69 Buy -20 Trio Buy
Anyone that could possibly help with this problem or point me in the right direction? (how to iterate and use if conditionals at the same time over rows and columns etc).
Thank you so much for your help in advance!