Recursive Least Squares Forecast [LuxAlgo]The Recursive Least Squares Forecast indicator uses an adaptive linear regression algorithm to estimate price trends in real-time, projecting future movements via a "Ghost Line" and providing dynamic bands and mean reversion signals for identifying market extremes.
By continuously updating its internal model with every new bar, the script provides a highly responsive framework for both trend forecasting and volatility-adjusted trading.
🔶 USAGE
The indicator aims to identify trend direction and potential exhaustion points. The central RLS mean line represents the current equilibrium price based on the adaptive model, while the bands represent volatility-adjusted extremes.
Users can utilize the tool for both trend following and mean reversion strategies:
Trend Following: Observe the slope and direction of the RLS Mean and the "Ghost Line" projection to determine the prevailing market bias.
Mean Reversion: Use the dynamic bands to identify when price has deviated significantly from its adaptive equilibrium.
Responsiveness: Adjust the Forgetting Factor (λ) to control the model's memory. A lower value (e.g., 0.95) makes the model react quickly to new price pivots, while a higher value (e.g., 0.99) provides a smoother, more stable trend line.
🔹 Mean Reversion Signals
The indicator identifies mean reversion opportunities using a two-step process:
Overextension: A setup begins when the price crosses outside the Upper or Lower Band, indicating an overbought or oversold state.
Entry Signal: A "BUY" or "SELL" signal is triggered when the price crosses back inside the band, suggesting a return to the RLS mean.
Targets: The RLS mean line serves as the primary take-profit target for these mean reversion setups.
🔶 DETAILS
The model assumes a linear relationship where the intercept and slope are updated recursively. The RLS algorithm is an adaptive filter that effectively "learns" the trend at every bar. It uses a state-space approach where the transition matrix is updated using a gain vector, ensuring the most efficient estimate of the current trend trajectory.
Unlike standard Moving Averages, the Recursive Least Squares (RLS) algorithm minimizes the sum of squared prediction errors by giving more weight to recent data. This allows the mean line to pivot quickly when market conditions change without the lag associated with traditional smoothing techniques.
The "Ghost Line" extends from the last bar into the future, providing a linear projection of where the current trend is headed. Surrounding this projection are "Standard Deviation Forecast Bands," which indicate the expected range of price movement based on the current model state.
🔶 SETTINGS
Forgetting Factor (λ): Controls how quickly the model forgets old data. Values closer to 1.0 make the model stable, while lower values make it more adaptive to recent price changes.
Band Multiplier: Standard deviation multiplier for the forecast bands, controlling the width of the mean reversion zones.
Forecast Horizon: Number of bars to project the "Ghost Line" and uncertainty bands into the future.
Show Ghost Line & Bands: Toggles the visibility of the future projection polylines.
Show Mean Reversion Signals: Toggles the visibility of the BUY/SELL labels on the chart.
Pine Script® indicator






















