How to resample and calculate amount weighted average price in Pandas?
Question:
I have a dataframe of trades in which each trade has a corresponding amount and price. How can I resample the dataframe in a 1 minute period and calculate the amount weighted average prices of each period ?
>df
amount price
datetime
2023-06-23 10:55:40.420 0.04657 30106.01
2023-06-23 10:55:42.348 0.00085 30104.54
2023-06-23 10:55:42.491 0.00368 30104.54
2023-06-23 10:55:43.211 0.03008 30104.54
2023-06-23 10:55:45.910 0.00035 30101.56
... ... ...
2023-06-23 10:58:06.401 0.00863 30108.00
2023-06-23 10:58:06.661 0.00829 30108.00
2023-06-23 10:58:07.474 0.00305 30108.00
2023-06-23 10:58:07.599 0.00048 30108.00
2023-06-23 10:58:08.393 0.00041 30108.00
[428 rows x 2 columns]
I can resample trade amounts with :
>>> df['amount'].resample("1Min").sum()
datetime
2023-06-23 10:55:00 0.78885
2023-06-23 10:56:00 12.84216
2023-06-23 10:57:00 9.56456
2023-06-23 10:58:00 0.08334
Freq: T, Name: amount, dtype: float64
But what is the best solution to calculate the average price of each periods based on the amount of each trade ?
Answers:
You can compute the average of amount*price and divide by the sum of amount per period:
(df.eval('product = amount*price')[['amount', 'product']]
.resample("1Min").sum()
.eval('product/amount')
)
Output:
datetime
2023-06-23 10:55:00 30105.366872
2023-06-23 10:56:00 NaN
2023-06-23 10:57:00 NaN
2023-06-23 10:58:00 30108.000000
Freq: T, dtype: float64
Intermediate before the last step (.eval('product/amount')
):
amount product
datetime
2023-06-23 10:55:00 0.08153 2454.490561
2023-06-23 10:56:00 0.00000 0.000000
2023-06-23 10:57:00 0.00000 0.000000
2023-06-23 10:58:00 0.02086 628.052880
I have a dataframe of trades in which each trade has a corresponding amount and price. How can I resample the dataframe in a 1 minute period and calculate the amount weighted average prices of each period ?
>df
amount price
datetime
2023-06-23 10:55:40.420 0.04657 30106.01
2023-06-23 10:55:42.348 0.00085 30104.54
2023-06-23 10:55:42.491 0.00368 30104.54
2023-06-23 10:55:43.211 0.03008 30104.54
2023-06-23 10:55:45.910 0.00035 30101.56
... ... ...
2023-06-23 10:58:06.401 0.00863 30108.00
2023-06-23 10:58:06.661 0.00829 30108.00
2023-06-23 10:58:07.474 0.00305 30108.00
2023-06-23 10:58:07.599 0.00048 30108.00
2023-06-23 10:58:08.393 0.00041 30108.00
[428 rows x 2 columns]
I can resample trade amounts with :
>>> df['amount'].resample("1Min").sum()
datetime
2023-06-23 10:55:00 0.78885
2023-06-23 10:56:00 12.84216
2023-06-23 10:57:00 9.56456
2023-06-23 10:58:00 0.08334
Freq: T, Name: amount, dtype: float64
But what is the best solution to calculate the average price of each periods based on the amount of each trade ?
You can compute the average of amount*price and divide by the sum of amount per period:
(df.eval('product = amount*price')[['amount', 'product']]
.resample("1Min").sum()
.eval('product/amount')
)
Output:
datetime
2023-06-23 10:55:00 30105.366872
2023-06-23 10:56:00 NaN
2023-06-23 10:57:00 NaN
2023-06-23 10:58:00 30108.000000
Freq: T, dtype: float64
Intermediate before the last step (.eval('product/amount')
):
amount product
datetime
2023-06-23 10:55:00 0.08153 2454.490561
2023-06-23 10:56:00 0.00000 0.000000
2023-06-23 10:57:00 0.00000 0.000000
2023-06-23 10:58:00 0.02086 628.052880