Choosing Rolling Windows for Cointegrated Pair Trading
Summary
The discussion asks how to choose the number of historical candles used to estimate a spread z-score in a crypto perpetual swap pairs strategy. The question notes that different pairs appear to perform best with different window lengths in both backtests and live trading, and asks whether spread volatility, trading costs, or cointegration test results should guide the choice.
The main response cautions that selecting a window because it produces a profitable backtest or the lowest cointegration p-value can overfit historical data. It recommends first establishing why the pair relationship should exist, when it may break down, and whether typical spread divergences are large enough to cover fees and slippage. The discussion also highlights financing costs and market structure: obvious relationships may be difficult for slower or higher-cost traders to exploit. Another reply suggests using decision trees or Bayesian optimization to adapt window choice, but offers no evidence or implementation details. The advice is conceptual and does not establish a validated dynamic window method or demonstrate profitability.
Key ideas
- Choosing a window based on backtest returns can overfit and may fail in live trading.
- A cointegration test result alone does not establish that a pair is economically tradable.
- Compare typical spread moves with entry and exit fees, slippage, and financing costs.
- Identify why the relationship exists and the conditions under which it may break down.
- Adaptive model selection is suggested, but the discussion provides no tested procedure.
Tags
Full text
# Pair trading - history length for spread calculation # Pair trading - history length for spread calculation I run a pair trading system on perpetual swaps (crypto). It runs fine but there is a concept I can't figure out. The cointegration test looks fine, but when and how to chose a given history length (or number of candles) for the spread zscore modelling ? Some pairs would yield good return on 500 historical candles, other would do it rather on 200 candles (live trading + backtest) Why is that, how can I make it more dynamic ? - Is it due to the spread volatility ? Spread width not large enough to cover the trading fees / slippage ? - Should I run a cointegration test on several length and chose the one that provides the lowest p-values maybe ? I've been scratching my head off aout this. Any idea or feedback from your own expirentation is much welcome. ## Answer by THATS MY QUANT MY QUANTITATIVE (score 0) https://quant.stackexchange.com/a/80953 For the same reason I can run a profitable backtest on the cointegration of SPX and DAX with a 6 hour window, but lose money on live trades. Before running a backtest, you should be asking the question, why should a 500 candle window be profitable? (Specificity on the window size). Backtest are used to measure an idea, not for research purposes. Otherwise you can come up with any window size to pass a backtest (over-fitting), but it will more than likely fail live. > Is it due to the spread volatility ? Spread width not large enough to cover the trading fees / slippage ? Potentially, how would we know? I'm guessing you're trading the divergence between the spot and the perp. If true, it is probably never going to work since the exchange's market-maker will be doing exactly what you're doing but will have a better fee structure than you. You should be measuring whether trading this pair is actually possible in the first place. That is; measuring the size of their divergence and comparing it to the fees associated with entering and exiting the trade + potential slippage. > Should I run a cointegration test on several length and chose the one that provides the lowest p-values maybe ? No, to my first point. You should be asking the questions: Why does this pair relationship exist? (This can potentially lead you to finding when their rate of convergence is at its greatest, which would beat spread/financing costs) If it exists, under what conditions does it breakdown? A simple example: Pairs trading the SPX and DAX. On average they're correlated and follow each other. If they start to diverge, you can long and short, which profits when they converge again. The cointegration of the pair breaks down when there are earnings i.e. NVDA, TSLA, GOOGL have really good/bad earnings. Or as seen today, SAP (German company) beat earnings so the DAX increased, whilst US markets declined. Trading on that event would have wiped away all profit from previous days when their relationship was strong. General rule of thumb with pairs trading. The more obvious a pair is, it turns into an engineering/speed/fees problem (i.e. having direct market access, being a market-maker etc). So if you're wanting to go in this direction, you should be trying to find a way to reduce fees, being the first to recognise the divergence/convergence and trying to near instantaneously hedge the position before anyone else can ## Answer by Content_Quantinsti (score 0) https://quant.stackexchange.com/a/80956 Use machine learning models (like decision trees) or Bayesian optimization to dynamically choose the best history length. These models can learn from the past performance of various window lengths and suggest the best one for current market conditions ## Answer by Sixk (score 0) https://quant.stackexchange.com/a/80987 if anyone interested, I updated the logic, not live yet but it seems way less sensible to the rolling window size. Here is the explanation as a picture, let me know if it's not clear:
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.