Spread Bollinger Bands for a Two-Leg Mean-Reversion Strategy
Summary
This strategy forms a spread from two instruments’ bar closes, weighted by configurable leg ratios. It updates the spread at five-minute intervals, keeps a rolling history, and calculates a moving average with upper and lower bands based on the spread’s standard deviation. After the history reaches the configured window, it opens opposite positions when the spread crosses an outer band and exits when it returns to the midpoint.
The code shows how tick data can be aggregated into bars, how positions are tracked as paired targets, and how orders are priced with a fixed adjustment from the bar close. It provides no backtest, profitability evidence, or statistical test that the pair’s spread is stable. The example also omits the bar callback’s implementation and uses fixed position targets, so sizing, execution risk, and behavior under missed or asynchronous leg data require further work.
Key ideas
- The spread is calculated as the difference between the two leg prices after applying configurable ratios.
- Outer standard-deviation bands trigger opposite positions in the two instruments.
- Positions are closed when the spread returns to its rolling mean.
- The strategy evaluates completed bars at five-minute intervals and offsets order prices from the close.
- The code supplies no evidence that the spread is stationary or that the rules are profitable.
Tags
From a private course collection; the original is not published.