Linear Regression (Backtest / Trailing Stop)
Feb 16, 2019

The Linear Regression (Backtest / Trailing Stop) tool provides a comprehensive automated strategy framework that utilizes linear regression channels and volatility-based bands to identify trend reversals and execute trades with advanced trailing exit logic.
Usage
The Linear Regression (Backtest / Trailing Stop) strategy is used to identify overbought and oversold conditions within a statistical trend. Traders can use the visual cues and backtesting engine to evaluate performance across different market conditions.
- Trend Identification: The script plots a central linear regression mean and deviation bands. When price interacts with these boundaries, it signals potential exhaustion or trend continuation.
- Signal Generation: Buy signals are triggered when the price crosses back into a defined range from an oversold condition (based on the "Oversold" setting). Sell signals occur when the price retreats from an overbought condition.
- Multi-Timeframe Analysis: By enabling the "Use another Timeframe?" setting, users can calculate the regression bands on a higher timeframe (e.g., 60-minute) while executing trades on a lower timeframe chart to ensure alignment with the broader trend.
- Exit Management: The strategy incorporates a sophisticated exit mechanism. Users can set a "Fixed SL" for initial protection and a "Trailing TP/SL" to lock in profits as the trade moves favorably.
Details
The script is constructed using two primary layers of analysis:
- Linear Regression Mean: Calculates the "best fit" line for a given lookback period using the
ta.linregfunction. - Volatility Bands: Utilizes the Average True Range (ATR) and Standard Deviation to create dynamic envelopes around the mean. The
bbr(Bollinger Band Range) calculation determines the relative position of the price within these envelopes to identify extremes.
The strategy logic is credited to original concepts by RafaelZioni. It implements a specific entry filter where signals only trigger when the price action confirms a reversal from the "pintaVerde" (green) or "pintaRojo" (red) price levels, which represent historically significant pivot points derived from the bands.
Settings
General Settings
- Linear Length: Sets the lookback period for the primary linear regression calculation.
- Upper/Lower Deviation: Toggles the visibility of the upper and lower ATR-based bands.
- Ratio 2: Multiplier for the ATR-based deviation bands.
Bands & Timeframe
- Linear Length (Bands): The lookback period specifically for the Standard Deviation bands used in signal generation.
- Linear Deviation: The multiplier for the Standard Deviation bands.
- Overbought/Oversold: Threshold levels for the normalized price position; values above the overbought or below the oversold level trigger signal preparation.
- Use another Timeframe?: Enables the calculation of bands based on a different period than the current chart.
- Select The Timeframe: Defines which timeframe to use for the multi-timeframe calculations.
Strategy & Backtesting
- SL Activation: The fixed stop-loss distance in ticks or points.
- SL Trigger / TP Trigger: The point at which the trailing mechanism begins to follow the price.
- TP Activation: The target distance to activate the trailing profit take.
- From/To Date (Month/Day/Year): Defines the specific window for backtesting performance.
FAQ
How do I adjust the sensitivity of the signals? You can adjust the "Linear Length (Bands)" or the "Overbought/Oversold" thresholds. Increasing the length typically results in fewer, more lag-sensitive signals, while tightening the thresholds increases signal frequency.
What is the difference between SL Activation and SL Trigger? SL Activation is the initial hard stop-loss used to protect the capital. The SL Trigger (Trail SL) determines the offset for the trailing stop once the trade moves into profit, allowing the exit price to move dynamically with the market.
How can I access the Linear Regression (Backtest / Trailing Stop) script? You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.
Free indicator
Get free access to this indicator on the platforms below.
Unlock the entire LuxAlgo Library
Every indicator, every strategy, full charts, and complete access to Quant — our AI agent.