# RSI Divergence: Out-of-Sample Optimizer

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

A research dashboard that backtests price-RSI divergence trading across dozens of RSI periods, selects the best performer in-sample, then validates it on out-of-sample and forward data. A sensitivity heatmap reveals whether the winning period sits in a robust cluster or stands alone as a curve-fit outlier.

- Indicator in the LuxAlgo Library, by LuxAlgo, published Jun 9, 2026.
- Access: free.
- Concepts: [Regular Bullish/bearish Divergence](https://www.luxalgo.com/library/concept/regular-bullish-bearish-divergence/) (primary), [RSI](https://www.luxalgo.com/library/concept/rsi/), [In-sample / Out-of-sample Split](https://www.luxalgo.com/library/concept/in-sample-out-of-sample-split/)
- Open on Vela: https://vela.luxalgo.com/chart?pineScriptCodeId=cmq6t1dg6000qkt04ti00mw3n
- Page: https://www.luxalgo.com/library/indicator/rsi-divergence-out-of-sample-optimizer/

**RSI Divergence: Out-of-Sample Optimizer** turns divergence trading into a controlled experiment: it simulates regular [bullish and bearish divergences](https://www.luxalgo.com/library/concept/regular-bullish-bearish-divergence/) between price and RSI across an entire range of RSI periods, ranks them by your chosen metric, and tests whether the winner survives on data it never saw. Divergences follow the standard definitions (a lower low in price paired with a higher RSI low, or the bearish mirror), confirmed by pivot lookbacks.

Three chronological phases mirror the [in-sample/out-of-sample split](https://www.luxalgo.com/library/concept/in-sample-out-of-sample-split/): In-Sample optimization scans every period, Out-of-Sample validation locks the best one and replays it on unseen data, and Forward testing carries the validated setting to the current bar.

## How to Trade the RSI Divergence: Out-of-Sample Optimizer?

- **In-Sample winner**: the RSI period topping your Optimization Metric (ten choices, including Net Profit, Profit Factor, Sharpe Ratio, Win Rate, Drawdown, and Z-Score).
- **Out-of-Sample verdict**: whether the edge persisted out of sample or evaporated as curve fit.
- **Sensitivity Table**: a heatmap of every tested period. A broad green cluster suggests robustness, a lone spike is a warning.
- **Simulated trade rules**: entries fill at the close of the bar on which a divergence confirms; exits use ATR-multiplied stops and targets, with optional Exit on Opposite Signal.

## RSI Divergence: Out-of-Sample Optimizer Settings

- **In-Sample Start/End / Out-of-Sample Start/End**: the optimization and validation windows.
- **Min/Max RSI Period**: the range of RSI lengths simulated.
- **Optimization Metric**: the KPI used to rank periods.
- **Pivot Left/Right Bars**: bars required either side to confirm an RSI pivot.
- **Max Divergence Bars**: maximum pivot distance for a valid divergence.
- **Stop Loss/Take Profit ATR Multiplier and Exit on Opposite Signal**: the simulated exit logic.
- **Extra Dashboard Metric 1/2, Dashboard Position/Size**: extra statistics and dashboard placement.

## Frequently Asked Questions

### Does a strong in-sample result mean the setting will keep working?

No, that is why the tool exists. The out-of-sample and forward phases expose settings fitted to historical noise, so read all three phases together before trusting a period.

### Is there a lighter way to avoid committing to one RSI length?

Yes. [RSI Multi Length](https://www.luxalgo.com/library/indicator/rsi-multi-length/) sidesteps the choice by averaging RSIs across a whole band of lengths and showing how many agree. Optimization picks a period; aggregation hedges across them.

### How do I access the optimizer?

Free, via the LuxAlgo Library. It also runs in Quant, our coding agent, straight from this page, fitting for a tool whose whole point is testing discipline. You can also copy the source code from this page and run it anywhere Pine Script® runs.

## Related indicators

- [Oscillator Matrix](https://www.luxalgo.com/library/indicator/luxalgo-oscillator-matrix/) (Regular Bullish/bearish Divergence): markdown at https://www.luxalgo.com/library/indicator/luxalgo-oscillator-matrix.md
- [HTF Reversal Divergences](https://www.luxalgo.com/library/indicator/htf-reversal-divergences/) (Regular Bullish/bearish Divergence): markdown at https://www.luxalgo.com/library/indicator/htf-reversal-divergences.md
- [LuxAlgo - Screener (OSC)](https://www.luxalgo.com/library/indicator/luxalgo-screener-osc/) (Regular Bullish/bearish Divergence): markdown at https://www.luxalgo.com/library/indicator/luxalgo-screener-osc.md
- [RSI Candlestick Oscillator](https://www.luxalgo.com/library/indicator/rsi-candlestick-oscillator/) (Regular Bullish/bearish Divergence): markdown at https://www.luxalgo.com/library/indicator/rsi-candlestick-oscillator.md
- [Triangular Momentum Oscillator & Real Time Divergences](https://www.luxalgo.com/library/indicator/triangular-momentum-oscillator-real-time-divergences/) (Regular Bullish/bearish Divergence): markdown at https://www.luxalgo.com/library/indicator/triangular-momentum-oscillator-real-time-divergences.md
- [RSI Multi Length](https://www.luxalgo.com/library/indicator/rsi-multi-length/) (RSI): markdown at https://www.luxalgo.com/library/indicator/rsi-multi-length.md
- [RSI Prediction by Range Segmentation](https://www.luxalgo.com/library/indicator/rsi-prediction-by-range-segmentation/) (RSI): markdown at https://www.luxalgo.com/library/indicator/rsi-prediction-by-range-segmentation.md
- [Divergence Variants & Confirmation](https://www.luxalgo.com/library/indicator/divergence-variants-and-confirmation/) (Momentum & Oscillators): markdown at https://www.luxalgo.com/library/indicator/divergence-variants-and-confirmation.md

---

Source: https://www.luxalgo.com/library/indicator/rsi-divergence-out-of-sample-optimizer/ (LuxAlgo Library). Free to use with attribution: https://www.luxalgo.com/library/license/
