OPEN-SOURCE SCRIPT
Updated Linear Mean Reversion Strategy

📘 Strategy Introduction: Linear Mean Reversion with Fixed Stop
This strategy implements a simple yet powerful mean reversion model that assumes price tends to oscillate around a dynamic average over time. It identifies statistically significant deviations from the moving average using a z-score, and enters trades expecting a return to the mean.
🧠 Core Logic:
A z-score is calculated by comparing the current price to its moving average, normalized by standard deviation, over a user-defined half-life window.
Trades are entered when the z-score crosses a threshold (e.g., ±1), signaling overbought or oversold conditions.
The strategy exits positions either when price reverts back near the mean (z-score close to 0), or if a fixed stop loss of 100 points is hit, whichever comes first.
⚙️ Key Features:
Dynamic mean and volatility estimation using moving average and standard deviation
Configurable z-score thresholds for entry and exit
Position size scaling based on z-score magnitude
Fixed stop loss to control risk and avoid prolonged drawdowns
🧪 Use Case:
Ideal for range-bound markets or assets that exhibit stationary behavior around a mean, this strategy is especially useful on assets with mean-reverting characteristics like currency pairs, ETFs, or large-cap stocks. It is best suited for traders looking for short-term reversions rather than long-term trends.
This strategy implements a simple yet powerful mean reversion model that assumes price tends to oscillate around a dynamic average over time. It identifies statistically significant deviations from the moving average using a z-score, and enters trades expecting a return to the mean.
🧠 Core Logic:
A z-score is calculated by comparing the current price to its moving average, normalized by standard deviation, over a user-defined half-life window.
Trades are entered when the z-score crosses a threshold (e.g., ±1), signaling overbought or oversold conditions.
The strategy exits positions either when price reverts back near the mean (z-score close to 0), or if a fixed stop loss of 100 points is hit, whichever comes first.
⚙️ Key Features:
Dynamic mean and volatility estimation using moving average and standard deviation
Configurable z-score thresholds for entry and exit
Position size scaling based on z-score magnitude
Fixed stop loss to control risk and avoid prolonged drawdowns
🧪 Use Case:
Ideal for range-bound markets or assets that exhibit stationary behavior around a mean, this strategy is especially useful on assets with mean-reverting characteristics like currency pairs, ETFs, or large-cap stocks. It is best suited for traders looking for short-term reversions rather than long-term trends.
Release Notes
The idea is simple: if an instrument’s price is truly mean-reverting, we can expect it to return to its long-term average. Most assets, however, behave more like random walks and show little tendency to revert. The VIX—an index of S&P 500 volatility—is a notable exception, exhibiting clear mean-reversion.
For this strategy, I’ve assumed a trading cost of 0.1%, which seems reasonable given the typical bid-ask spread on the VIX is about 0.18 points.
A z-score is calculated by comparing the current price to its moving average, normalized by standard deviation, over a user-defined half-life window.
Trades are entered when the z-score crosses a threshold (e.g., ±1), signaling overbought or oversold conditions.
The strategy exits positions either when price reverts back near the mean (z-score close to 0), or if a fixed stop loss is hit, whichever comes first.
Designed for a 1 H window, where the VIX’s mean reversion and assumed costs tend to align well.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.