Mean Reversion Strategy
Mar 23, 2020

The Mean Reversion Strategy indicator identifies overextended price movements relative to a linear regression curve to capitalize on potential returns to the mean. It provides a comprehensive framework for backtesting mean reversion setups with built-in trend filtering and customizable exit logic.
Usage
The Usage section describes how the script can be used to identify entry and exit points based on statistical deviations. Traders can use the tool to detect "oversold" or "overbought" conditions when the price crosses specific standard deviation thresholds relative to a linear regression line.
- Long Entries: Occur when the price is below the lower threshold, below the exit moving average, and optionally aligned with the long-term SMA trend.
- Short Entries: Occur when the price is above the upper threshold, above the exit moving average, and optionally aligned with the long-term SMA trend.
- Exits: Positions can be closed when the price touches a chosen mean (Linear Regression or Simple Moving Average) or when the percentage-based Profit Target or Stop Loss is hit.
Details
The strategy calculates a Z-score style value by subtracting the Linear Regression value from the source price and dividing it by the standard deviation over a user-defined lookback period. This normalized value determines the distance from the mean. The strategy also incorporates a trend filter using a 200-period SMA (default) to ensure trades are taken in the direction of the broader market bias, or against it, depending on the user's preference.
Settings
Core Logic
- Long Only or Short Only or Both?: Filters the strategy to execute only long trades, only short trades, or both directions.
- Length: The lookback period used for calculating the Linear Regression and Standard Deviation.
- Upper threshold: The statistical level above the mean that triggers short entries.
- Lower threshold: The statistical level below the mean that triggers long entries.
- Source: The price data used for calculations (default is Close).
Exit & Trend
- Linear Regression Exit or Moving Average Exit?: Selects whether the "mean" used for closing positions is a Linear Regression curve or a Simple Moving Average.
- MA/LR Exit Length: The lookback period for the chosen exit mean.
- Trend SMA Length: The period for the trend-filtering Simple Moving Average.
- Above or Below Trend SMA?: Filters entries based on whether the price is above or below the trend SMA.
Risk Management
- Profit Target On/Off: Enables or disables the percentage-based profit target.
- Profit Target %: The percentage gain required to trigger an automated exit.
- Stop Loss On/Off: Enables or disables the percentage-based stop loss.
- Stop Loss %: The percentage loss allowed before triggering an automated exit.
FAQ
How do I use the trend filter effectively?
You can set the "Above or Below Trend SMA" setting to "Above" to only take mean reversion longs during an uptrend, or set it to "Don't Include" if you want to trade every mean reversion signal regardless of the primary trend.
What is the difference between the LR and MA exit?
The Linear Regression (LR) exit tends to follow price more reactively, whereas the Moving Average (MA) exit provides a smoother, more traditional mean for the price to return to.
How can I access this tool?
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.