Machine Learning: Optimal RSI

Oct 23, 2023

Static chart image
Signals
Oscillators
Machine Learning
Dashboard
Volatility

The Machine Learning: Optimal RSI indicator identifies the most profitable RSI length based on historical performance and applies machine learning logic to create an adaptive, smoothed oscillator. It dynamically scans multiple RSI lengths to determine which RSI-to-MA cross produces the highest return, offering a data-driven alternative to static RSI periods.

Usage

The indicator can be used to identify trend shifts and overbought/oversold conditions using an optimized RSI length that adapts to current market volatility.

  • Optimal RSI Selection: The script scans a range of RSI lengths (starting from a user-defined minimum) over a specific lookback period. It tracks the simulated profitability of every RSI/MA cross and selects the length that performed best.
  • Machine Learning Integration: Once the optimal RSI is found, it is processed through a Machine Learning (ML) model—either a Simple Average or a k-Nearest Neighbor (KNN) algorithm. This creates a smoothed version of the RSI that filters out noise while remaining responsive to price action.
  • Dynamic Bollinger Bands: Unlike static levels (30/70), the tool provides RSI-based Bollinger Bands. These act as adaptive support and resistance levels for the RSI line, helping traders spot exhaustion points that static levels might miss.
  • Filtering: Users can toggle "Filter RSI and RSI MA," which ensures the ML calculation only considers data points where the trend direction (RSI vs. RSI MA) matches the current optimal RSI direction.

Details

The script's core logic revolves around an "Optimal RSI" selection process. Traders often default to a 14-period RSI, but different assets and timeframes often respond better to different lengths.

By default, the script uses All Crossings to evaluate profit, but it can be set to Extremity Crossings. In the latter mode, the script only considers profitability when an RSI cross occurs at extreme levels (below 40 for longs or above 60 for shorts).

The KNN (k-Nearest Neighbor) functionality allows the indicator to "learn" from historical distances between fast and slow moving averages of the RSI. It identifies similar historical patterns to predict the current smoothed value, offering a more sophisticated alternative to simple moving averages. To prevent script execution timeouts on lower timeframes or data-heavy charts, the indicator includes an "Auto" adjustment feature for the lookback length and scan count.

Settings

RSI Settings

  • Show Crossing Signals: Toggles visual buy/sell markers when the RSI and its MA cross.
  • Show Tables: Displays a dashboard showing the currently selected optimal RSI length and its calculated profit.
  • Show Bollinger Bands: Displays adaptive volatility bands around the RSI.
  • Optimal RSI Type: Choose between "All Crossings" or "Extremity Crossings" for profit evaluation.
  • Adjust Optimal RSI Lookback and RSI Count: Set to "Auto" to let the script manage performance or "Manual" for custom lookback periods.
  • Optimal RSI Lookback Length: The number of bars the script analyzes to find the best-performing RSI length.
  • RSI Count: The total number of RSI lengths to test (e.g., if min is 4 and count is 30, it tests lengths 4 through 34).
  • RSI Minimum Length: The starting period for the RSI scan.
  • RSI MA Length: The period of the Moving Average used for RSI crossing logic.
  • Extremity Crossings RSI Backup Length: The default length used if no optimal length is found during extremity scans.

Machine Learning

  • Use Rational Quadratics: Applies a rational quadratic kernel to the price source for smoother ML input.
  • Filter RSI and RSI MA: If enabled, ML calculations only include historical data with the same bullish/bearish RSI-to-MA relationship.
  • Machine Learning Type: Select the smoothing algorithm: KNN Average, KNN Exponential Average, Simple Average, or None.
  • KNN Distance Type: Determines whether the KNN algorithm looks for Min, Max, or Both distances between data points.
  • Machine Learning Length: The lookback window for the ML data set.
  • k-Nearest Neighbour (KNN) Length: The number of "neighbors" (similar historical points) the KNN algorithm considers.
  • Fast/Slow ML Data Length: Periods used to calculate the distances for KNN logic.

FAQ

How does the "Auto" adjustment for Lookback work? The script detects your timeframe and bar count to prevent TradingView timeout errors. It automatically lowers the scan depth on lower timeframes and increases it on higher timeframes to ensure the script executes within the platform's processing limits.

What is the benefit of using KNN over a Simple Average? A Simple Average gives equal weight to all recent data points, whereas KNN looks for historical instances that specifically resemble the current market structure (based on the distance between fast and slow RSI averages), potentially providing a more context-aware smoothing.

How can I access the Machine Learning: Optimal RSI indicator? You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.

Free access on the following platforms
tradingviewSymbolTradingView
Open in your browser

This in-app browser is not compatible with Get Access from the library.

Tap the menu (...) at the top of your screen and select "Open in Browser", "Open in Safari", or "Open in Chrome" to continue.

ninjatraderNinjaTrader
Open in your browser

This in-app browser is not compatible with Get Access from the library.

Tap the menu (...) at the top of your screen and select "Open in Browser", "Open in Safari", or "Open in Chrome" to continue.

metatrader4MetaTrader 4/5
Open in your browser

This in-app browser is not compatible with Get Access from the library.

Tap the menu (...) at the top of your screen and select "Open in Browser", "Open in Safari", or "Open in Chrome" to continue.

thinkorswimThinkorswim
Open in your browser

This in-app browser is not compatible with Get Access from the library.

Tap the menu (...) at the top of your screen and select "Open in Browser", "Open in Safari", or "Open in Chrome" to continue.

Trading & investing are risky and many will lose money in connection with trading and investing activities. All content on this site is not intended to, and should not be, construed as financial advice. Decisions to buy, sell, hold or trade in securities, commodities and other investments involve risk and are best made based on the advice of qualified financial professionals. Past performance does not guarantee future results.

Hypothetical or Simulated performance results have certain limitations. Unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, including, but not limited to, lack of liquidity. Simulated trading programs in general are designed with the benefit of hindsight, and are based on historical information. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

Testimonials appearing on this website may not be representative of other clients or customers and is not a guarantee of future performance or success.

As a provider of technical analysis tools and strategies, we do not have access to the personal trading accounts or brokerage statements of our customers. As a result, we have no reason to believe our customers perform better or worse than traders as a whole based on any content, tool, or platform feature we provide.

Charts used on this site are by TradingView in which the majority of our technical indicators are built on. TradingView® is a registered trademark of TradingView, Inc. www.TradingView.com TradingView® has no affiliation with the owner, developer, or provider of the Services described herein.

Market data is provided by CBOE, CME Group, BarChart, Massive, CoinAPI. Select U.S. equities data is provided through Massive. CBOE BZX real-time U.S. equities data is licensed from CBOE and provided through BarChart. Real-time futures data is licensed from CME Group and provided through BarChart. Select cryptocurrency data, including major coins, is provided through CoinAPI. All data is provided “as is” and should be verified independently for trading purposes.

This does not represent our full Disclaimer. Please read our full disclaimer.

© 2026 LuxAlgo Global, LLC.