I have a requirement where I need to calculate the percent change for a order group. What I have done so far works well if there are equal number of rows for sub group within primary group. I need to take into consideration the quantity as well.
time txn_type symbol qty price
27/12/21 10:32 BUY XYZ 1 4054.5
27/12/21 10:26 SELL XYZ 2 4053.65
27/12/21 10:00 BUY XYZ 1 4072.25
27/12/21 09:56 BUY XYZ 1 4045.15
27/12/21 09:50 SELL XYZ 1 4034.25
27/12/21 09:40 BUY XYZ 1 4006
27/12/21 09:20 SELL XYZ 1 3978.1
27/12/21 10:55 SELL MNO 1 1714.95
27/12/21 10:25 BUY PQR 1 768.7
27/12/21 10:05 SELL PQR 1 765.05
27/12/21 09:57 SELL PQR 1 764
27/12/21 09:40 BUY PQR 1 769
27/12/21 09:28 SELL PQR 1 765.8
27/12/21 09:20 BUY PQR 1 768.95
27/12/21 09:20 BUY MNO 1 1703.55
symbol_orders_df = order_df.groupby(['symbol', 'txn_type']).agg({
'symbol': 'first',
'txn_type': 'first',
'price': np.sum
})
symbol_percent_df = symbol_orders_df.groupby(level=[0]).transform(
lambda g: round(((g.shift(-1) - g) / g) * 100, 2))
symbol_percent_df.reset_index(inplace=True)
symbol_percent_df = symbol_percent_df[symbol_percent_df['txn_type'] == "BUY"]
symbol_percent_df.sort_values(by=['price'], ascending=False, inplace=True)
symbol_pct_dict: dict = symbol_percent_df.set_index('symbol')['price'].to_dict()
Above code works well for MNO, PQR but gives incorrect result for XYZ as qty varied for one row at 10:26.
What I need is symbol wise percent change in a dictionary.