Rolling Returns, Momentum, Correlation, and Volatility in Pandas
Summary
The document reviews simple rolling calculations for quantitative analysis in Pandas. It presents functions for rolling correlation, return volatility based on percentage changes, momentum defined as the percentage change across a chosen lookback, and a proposed trend calculation using autocorrelation. The discussion clarifies that the momentum function is appropriate if momentum means the change from the start to the end of the lookback period.
A follow-up offers a different definition: the rolling mean of one-period percentage changes. These approaches measure different things, so the choice depends on the researcher's intended meaning of momentum. The document does not resolve whether the proposed trend or rolling-return functions are correct, nor does it compare outputs against a library or reference implementation. It provides a useful starting distinction among common rolling measures, but leaves window conventions, missing data, annualization, and interpretation to the user.
Key ideas
- Rolling correlation can be calculated between two series over a moving window.
- Rolling volatility is estimated from the standard deviation of percentage changes in the window.
- Momentum can mean either a lookback-period percentage change or an average of one-period changes.
- The document's proposed autocorrelation-based trend measure is not evaluated or validated.
Tags
Full text
# How to calculate basic components like trend, momentum, correlation and volatility in Pandas(Python)
# How to calculate basic components like trend, momentum, correlation and volatility in Pandas(Python)
I am new to quant. finance and trying to calculate `trend`, `momentum`, `correlation` and `volatility`. Below are the functions I have created
```
def roll_correlation(first_df, second_df, rolling_period):
"""
Rolling correlation
"""
return first_df.rolling(rolling_period).corr(second_df)
def roll_volatility(df, rolling_period):
"""
Rolling volatility
"""
return df.pct_change().rolling(rolling_period).std(ddof=0)
# ASK: is rolling momentum calculation is correct
def roll_momentum(df, rolling_period):
"""
Rolling momentum
"""
return df.pct_change(rolling_period)
def roll_returns(df, rolling_period):
"""
Rolling returns
"""
return df.rolling(rolling_period).apply(lambda x: x.iloc[[0, -1]].pct_change()[-1])
def trend(df, rolling_period):
"""
Identify trend
"""
return df.apply(lambda x: x.autocorr(rolling_period))
```
Apart from `volatility` calculation I am not sure about others are they look ok? Is there any external or builtin `pandas` library to calculate these basic components?
## Answer by Khanh (score 0)
https://quant.stackexchange.com/a/66137
It depends on how you define rolling momentum. If you define it as the percentage change between the end and the begin of a period, your code is correct.
## Answer by Derby9421 (score 0)
https://quant.stackexchange.com/a/77642
Quick follow-up if you want to calculate the rolling momentum as the mean of the percentage change, you can modify the roll_momentum function accordingly. You may try,
```
def roll_momentum_mean(df, rolling_period):
return df.pct_change().rolling(rolling_period).mean()
```Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.