Problem statement: Trying to calculate the simple moving average with Pandas groupby using different period for each of the group.
Example: I have continuous contracts of S&P E-mini, trying to find a simple moving average, but want to use different period. In the example below, I want to calculate 5 day SMA for C1, 7 day SMA for C2, 10d SMA for C3, etc. I get period values from a configuration.
Date |Contract| Close | Period| SMA
3/23/2020 | C1 | 2210.50 | 5 | 2335.58
3/22/2020 | C1 | 2191.50 | 5 | 2374.73
3/20/2020 | C1 | 2389.00 | 5 | 2473.21
3/19/2020 | C1 | 2401.40 | 5 | 2489.19
3/18/2020 | C1 | 2485.50 | 5 | 2502.69
3/17/2020 | C1 | 2406.25 | 5 | 2553.65
3/16/2020 | C1 | 2683.90 | 5 |
3/15/2020 | C1 | 2468.90 | 5 |
3/13/2020 | C1 | 2468.90 | 5 |
3/12/2020 | C1 | 2740.30 | 5 |
…..
3/23/2020 | C2 | 2219.45 | 7 | 2403.69
3/22/2020 | C2 | 2199.30 | 7 | 2440.39
3/20/2020 | C2 | 2396.50 | 7 | 2480.07
3/19/2020 | C2 | 2410.20 | 7 | 2530.51
3/18/2020 | C2 | 2493.90 | 7 |
3/17/2020 | C2 | 2413.90 | 7 |
3/16/2020 | C2 | 2692.60 | 7 |
3/15/2020 | C2 | 2476.35 | 7 |
3/13/2020 | C2 | 2477.05 | 7 |
3/12/2020 | C2 | 2749.55 | 7 |
I have tried to use rolling window, but unable to use dynamic/custom window period.
df['sma'] = df.groupby('Contract')['Close'].rolling(<<period - not able to use>>).mean().reset_index(0,drop=True)
Is there any method to use a configurable parameter for window
parameter?
If you want to keep the contract
dict_periods = {"C1": 5, "C2":7, "C3": 10}
period = lambda z: dict_periods[z['Contract'].iloc[0]]
ma = lambda df: df['Close'].rolling(period(df)).mean()
df.groupby('Contract', as_index=False).apply(ma)
Otherwise, back to your reset_index(0, drop=True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.