AutoCorrelation — Auto-Correlation
Correlation of returns with their own lag. Positive means momentum persists bar to bar; negative means the series reverses itself — the most direct statistical test of whether a mean-reversion or a trend rule is the right shape for this instrument.
Signature
AutoCorrelation(period, lag, on=<source>)| Argument | Type | Required | Notes |
|---|---|---|---|
period |
int Param or literal | Yes | Return window. Default 60, bounds 3–400. |
lag |
int Param or literal | Yes | Lag in bars. Default 1, bounds 1–100. |
on= |
symbol, OHLCV field, or indicator | No | Source. Defaults to the trade symbol’s close. |
Formula
$$ \rho_k = \frac{\sum_{i} (r_i - \bar{r})(r_{i+k} - \bar{r})}{\sum_i (r_i - \bar{r})^2} $$
on log returns.
Warmup
period + 1 bars; 0 until then.
Reading the value
ac.valueac.value < -0.1 # the series is revertingac.value > 0.1 # the series is trendingExample
class RegimeSwitch(Strategy): """Only fade extremes when returns are actually negatively autocorrelated.""" trade = Symbol() sizing = FixedCapital()
def execute(self): ac = AutoCorrelation(60, 1, on=self.trade) z = ZScore(20, on=self.trade) if ac.value < -0.1 and z.value < -2 and self.trade.is_flat: self.buy(self.trade, reason=f"Reverting regime, z {z.value:.2f}") if z.value > 0 and self.trade.is_long: self.sell(self.trade, self.trade.position, reason="Mean reached")Notes
- Proven by an assert-via-trade run on SPY daily (stayed inside ±1 on every bar of a 140-bar run; zero alarm trades).
- Daily equity autocorrelation is small and unstable — a reading of ±0.1 is meaningful, ±0.5 would be extraordinary and worth double-checking before trusting.