Bagging
By LuxAlgoApr 30, 2026
Bagging implements bagging — bootstrap aggregating — end to end on the chart: 50 k-nearest-neighbors models, each fit on its own resample of the last 100 labeled observations, combine into one directional prediction between −1 and +1. Three standardized features feed every member — momentum, RSI and a price-stretch z-score; labels record whether close rose or fell over a 5-bar horizon.
Two layers make the method's point visible: an unbagged reference line shows the same k-NN trained once without resampling, and a dispersion band widens when members disagree — when the output depends on which observations were drawn.
How to Trade the Bagging?
- Bagged Prediction above zero: the ensemble leans bullish; below zero, bearish — zero crosses fire the alerts.
- Wide dispersion band: the prediction is sample-sensitive; an alert fires when dispersion crosses 0.5.
- ±1 dotted levels: unanimous membership.
- OOB Error: out-of-bag scoring in the dashboard; readings near 50% say the ensemble finds no edge here.
Bagging Settings
- Bootstrap Replicates (B) (default 50): number of resamples and members.
- Training Window (n) (default 100): the training set; resamples are drawn at size n.
- Neighbors (k) (default 5): each member's k-NN; small k is the high-variance regime bagging stabilizes.
- Label Horizon (default 5): bars ahead used for labels.
- Aggregation (default Average (Regression)): or Majority Vote (Classification).
- Bootstrap Type (default Standard (IID)) with Block Length (default 10) for the block variant.
- Random Seed (default 1): redraws the ensemble deterministically.
- Disagreement Alert Level (default 0.5): dispersion that fires the alert.
- Momentum Length (default 10), RSI Length (default 14), Mean Reversion Length (default 20): feature lookbacks.
- Style toggles cover the gradient fill, dispersion band, single-model reference and the dashboard.
Frequently Asked Questions
How is bagging different from a random forest?
A Random Forest adds one more randomization to bagging: it restricts each split to a few randomly drawn features. Plain bagging, as here, gives every member the full feature set and varies only the sample.
When should I switch to the block bootstrap?
When observations overlap or trend together, as financial series usually do: IID resampling assumes independent draws, while the block variant keeps contiguous chunks intact — the standard adjustment for autocorrelated data.
What does the OOB Share figure mean?
Each member never samples roughly a third of the observations — 36.8% in theory — and only those members score it. OOB Share reports the realized fraction.
The Library is free. Quant makes it yours.
Pull any concept or indicator into Quant: rebuild it, retune it, or turn it into a backtested strategy of your own.
