All indicators

RSI Divergence: Out-of-Sample Optimizer

By LuxAlgoJun 9, 2026

RSI Divergence: Out-of-Sample Optimizer is an indicator on Vela, our next-generation charting platform, written in Pine Script®.

RSI Divergence: Out-of-Sample Optimizer turns divergence trading into a controlled experiment: it simulates regular bullish and bearish divergences 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: 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

Free to use

Run it on Vela. Go further with a paid plan.

  • Up to 500 strategy alerts, with webhook automation
  • Up to 10× the chart history of Free
  • Orderflow lookback up to full history
  • More Quant usage to customize it, plus extra usage when you need it
  • Futures data alongside crypto, stocks, forex and commodities
See plans

Also works on

TradingView
Original indicatorBuilt in-house by LuxAlgo

Run it on Vela. Make it yours with Quant.

Open this indicator free on Vela, our next-generation charting platform. Then ask Quant, our coding agent built into Vela, to retune it or turn it into a backtested strategy of your own.