# Bagging

Bagging is an indicator on [Vela](https://vela.luxalgo.com/chart), our next-generation charting platform, written in Pine Script®.

A working bootstrap-aggregating ensemble on the chart: 50 k-NN base models, each trained on its own resample of a 100-bar window of three standardized features, combined by averaging or majority vote. It plots the bagged prediction against an unbagged reference line, bands member dispersion, and reports out-of-bag error in a dashboard.

- Indicator in the LuxAlgo Library, by LuxAlgo, published Apr 30, 2026.
- Access: free.
- Concepts: [Bagging](https://www.luxalgo.com/library/concept/bagging/) (primary)
- Open on Vela: https://vela.luxalgo.com/chart?pineScriptCodeId=cj2bfp4053n0x96kqvgqbmcj
- Page: https://www.luxalgo.com/library/indicator/bagging/

**Bagging** implements [bagging](https://www.luxalgo.com/library/concept/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](https://www.luxalgo.com/library/indicator/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.

## Related indicators

- [Model Stacking](https://www.luxalgo.com/library/indicator/model-stacking/) (Machine Learning): markdown at https://www.luxalgo.com/library/indicator/model-stacking.md
- [Ensemble Voting of Signals](https://www.luxalgo.com/library/indicator/ensemble-voting-of-signals/) (Machine Learning): markdown at https://www.luxalgo.com/library/indicator/ensemble-voting-of-signals.md
- [SuperTrend AI (Clustering)](https://www.luxalgo.com/library/indicator/supertrend-ai-clustering/) (Machine Learning): markdown at https://www.luxalgo.com/library/indicator/supertrend-ai-clustering.md
- [KNN Supertrend Horizon](https://www.luxalgo.com/library/indicator/knn-supertrend-horizon/) (Machine Learning): markdown at https://www.luxalgo.com/library/indicator/knn-supertrend-horizon.md
- [Nadaraya-Watson Envelope](https://www.luxalgo.com/library/indicator/nadaraya-watson-envelope/) (Machine Learning): markdown at https://www.luxalgo.com/library/indicator/nadaraya-watson-envelope.md
- [kNN Market Architecture](https://www.luxalgo.com/library/indicator/knn-market-architecture/) (Machine Learning): markdown at https://www.luxalgo.com/library/indicator/knn-market-architecture.md
- [Dynamic Time Warping Similarity](https://www.luxalgo.com/library/indicator/dynamic-time-warping-similarity/) (Machine Learning): markdown at https://www.luxalgo.com/library/indicator/dynamic-time-warping-similarity.md
- [Machine Learning Moving Average](https://www.luxalgo.com/library/indicator/machine-learning-moving-average/) (Machine Learning): markdown at https://www.luxalgo.com/library/indicator/machine-learning-moving-average.md

---

Source: https://www.luxalgo.com/library/indicator/bagging/ (LuxAlgo Library). Free to use with attribution: https://www.luxalgo.com/library/license/
