I have DataFrame where 4 unique orders are split into row 3-12. As you can see below in step 1, 2 and 3 I am using groupby to make it so that 1 order = 1 row.
I am missing one crucial step however, calculating the weighted average price for each order. Currently step 2 is calculating the mean price instead.
What I want to do:
Create a function/lambda that can calculate the weighted average price for each order (maybe based on groupby 'Time' column).
Formula for weighted average price = ((first price * amount) + (second price * amount)) / total amount
Weighted average price for order 1 = ((660.33 * 0.0130) + (659.58 * 0.0070)) / 0.02 = 660.06750
Step 1 - Original DataFrame:
| 1| Time | Market | Type | Price | Amount | Total | Fee | Acc |
| 2|-----------|-----------|-------|----------|---------|----------|----------|---------|
| 3| 22:12:15 | Market 1 | Buy | 660.33 | 0.0130 | 8.58429 | 0.00085 | MXG_33 |
| 4| 22:12:15 | Market 1 | Buy | 659.58 | 0.0070 | 4.61706 | 0.00055 | MXG_33 |
| 5| 19:36:08 | Market 1 | Sell | 670.00 | 0.0082 | 5.49400 | 0.00070 | MXG_33 |
| 6| 19:36:08 | Market 1 | Sell | 670.33 | 0.0058 | 3.88791 | 0.00048 | MXG_33 |
| 7| 19:36:08 | Market 1 | Sell | 671.23 | 0.0060 | 4.02738 | 0.00054 | MXG_33 |
| 8| 13:01:41 | Market 1 | Buy | 667.15 | 0.0015 | 1.00073 | 0.00011 | MXG_33 |
| 9| 13:01:41 | Market 1 | Buy | 667.10 | 0.0185 | 12.3414 | 0.00132 | MXG_33 |
|10| 07:14:36 | Market 1 | Sell | 657.55 | 0.0107 | 7.03579 | 0.00079 | MXG_33 |
|11| 07:14:36 | Market 1 | Sell | 657.08 | 0.0005 | 0.32854 | 0.00004 | MXG_33 |
|12| 07:14:36 | Market 1 | Sell | 656.59 | 0.0088 | 5.77799 | 0.00071 | MXG_33 |
Step 2: Merging orders back into 1 row pr order:
d_agg = {'Market':'first'
,'Type':'first'
,'Price':'mean'
,'Amount':'sum'
,'Total':'sum'
,'Fee':'sum'
,'Acc':'first'}
(df.groupby('Time', sort=False)['Market','Type','Price','Amount','Total','Fee','Acc'].agg(d_agg).reset_index())
Step 3 - Final result: (But 'Price' column shows mean prices instead of weighted average prices).
| 1| Time | Market | Type | Price | Amount | Total | Fee | Acc |
| 2|-----------|-----------|-------|----------|---------|-----------|----------|---------|
| 3| 22:12:15 | Market 1 | Buy | 659.955 | 0.0200 | 13.20135 | 0.00140 | MXG_33 |
| 4| 19:36:08 | Market 1 | Sell | 670.520 | 0.0200 | 13.40929 | 0.00172 | MXG_33 |
| 5| 13:01:41 | Market 1 | Buy | 667.125 | 0.0200 | 13.34213 | 0.00242 | MXG_33 |
| 6| 07:14:36 | Market 1 | Sell | 657.073 | 0.0200 | 13.14232 | 0.00154 | MXG_33 |
The .apply method of groupby object would allow you to precess data at group level and return a dataframe.
def fn(group):
group['weighted_avg'] = group['Price'] * group['Amount'] / group['Amount'].sum()
return group
d_agg = {'Market':'first'
,'Type':'first'
,'weighted_avg':'sum'
,'Amount':'sum'
,'Total':'sum'
,'Fee':'sum'
,'Acc':'first'}
df.groupby('Time', sort=False).apply(fn).groupby('Time').agg(d_agg)
# if you don't understand what the code is doing, try:
print(df.groupby('Time', sort=False).apply(fn))