Strategy vault / Mean reversion
Enhanced Mean Reversion Strategy with MACD ATR Implementation
This strategy is a quantitative trading system that combines mean reversion principles with technical indicators MACD and ATR. It uses Bollinger Bands to identify price deviations, MACD for momentum confirmation, and…

Overview
This strategy is a quantitative trading system that combines mean reversion principles with technical indicators MACD and ATR. It uses Bollinger Bands to identify price deviations, MACD for momentum confirmation, and ATR for dynamic risk management. The core concept is to capture mean reversion opportunities when prices show significant deviation, validated through multiple technical indicators.
Strategy Principles
The strategy employs three technical indicators working in conjunction: First, Bollinger Bands determine significant price deviations; second, MACD validates price momentum, ensuring trade direction aligns with market trends; finally, ATR sets dynamic stop-loss and take-profit levels. Specifically, long signals are generated when price breaks below the lower Bollinger Band with MACD line above its signal line, while short signals occur when price breaks above the upper Bollinger Band with MACD line below its signal line. ATR dynamically adjusts stop-loss and take-profit levels based on market volatility.
Strategy Advantages
- Multi-dimensional signal confirmation mechanism significantly reduces false breakout risks
- Dynamic stop-loss and take-profit settings better adapt to market volatility
- Combines mean reversion and trend following characteristics, capturing both short-term opportunities and major trends
- Strategy parameters can be flexibly adjusted for different market environments
- Comprehensive risk management mechanism effectively controls drawdowns
Strategy Risks
- May trigger frequent stop-losses in highly volatile markets
- Risk of overfitting through excessive parameter optimization
- Multiple indicators might lead to delayed signals
- Mean reversion assumption may fail in trending markets
- Improper stop-loss placement can affect overall returns
Optimization Directions
- Introduce adaptive Bollinger Bands parameters that automatically adjust to market volatility
- Add market environment recognition module to use different parameter combinations in different market conditions
- Optimize MACD parameters to improve signal timeliness and accuracy
- Enhance stop-loss strategy by incorporating trailing stops
- Consider integrating timeframe analysis to validate signals across different time periods
Summary
This strategy combines classical technical analysis with modern quantitative trading methods. Through the coordinated use of multiple indicators, it maintains the core advantages of mean reversion while overcoming the limitations of single indicators. The strategy is highly extensible, capable of continuous improvement through parameter optimization and additional functional modules. Meanwhile, its comprehensive risk control mechanism ensures stability.
Source Pine Script
/*backtest
start: 2024-11-12 00:00:00
end: 2024-12-11 08:00:00
period: 3h
basePeriod: 3h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Enhanced Mean Reversion with MACD and ATR", overlay=true)
// Nastavenia Bollinger Bands
bbLength = input(20, title="Bollinger Bands Length")
bbMult = input(2, title="Bollinger Bands Multiplier")
basis = ta.sma(close, bbLength)
dev = ta.stdev(close, bbLength)
upperBand = basis + bbMult * dev
lowerBand = basis - bbMult * dev
// MACD indikátor
macdShort = input(12, title="MACD Short Length")
macdLong = input(26, title="MACD Long Length")
macdSignal = input(9, title="MACD Signal Length")
[macdLine, signalLine, _] = ta.macd(close, macdShort, macdLong, macdSignal)
// ATR pre dynamický Stop Loss a Take Profit
atrLength = input(14, title="ATR Length")
atrMultiplier = input(1.5, title="ATR Multiplier")
atrValue = ta.atr(atrLength)
// Vstupné podmienky pre long pozície
longCondition = ta.crossover(close, lowerBand) and macdLine > signalLine
if (longCondition)
strategy.entry("Long", strategy.long)
// Vstupné podmienky pre short pozície
shortCondition = ta.crossunder(close, upperBand) and macdLine < signalLine
if (shortCondition)
strategy.entry("Short", strategy.short)
// Dynamický Stop Loss a Take Profit na základe ATR
longSL = strategy.position_avg_price - atrValue * atrMultiplier
longTP = strategy.position_avg_price + atrValue * atrMultiplier * 2
shortSL = strategy.position_avg_price + atrValue * atrMultiplier
shortTP = strategy.position_avg_price - atrValue * atrMultiplier * 2
// Pridanie stop loss a take profit
if (strategy.position_size > 0)
strategy.exit("Take Profit/Stop Loss", "Long", stop=longSL, limit=longTP)
if (strategy.position_size < 0)
strategy.exit("Take Profit/Stop Loss", "Short", stop=shortSL, limit=shortTP)
// Vizualizácia Bollinger Bands a MACD
plot(upperBand, color=color.red, title="Upper Bollinger Band")
plot(lowerBand, color=color.green, title="Lower Bollinger Band")
plot(basis, color=color.blue, title="Bollinger Basis")
hline(0, "MACD Zero Line", color=color.gray)
plot(macdLine - signalLine, color=color.blue, title="MACD Histogram")
plot(macdLine, color=color.red, title="MACD Line")
plot(signalLine, color=color.green, title="Signal Line")
// Generovanie alertov
alertcondition(longCondition, title="Long Alert", message="Long Entry Signal")
alertcondition(shortCondition, title="Short Alert", message="Short Entry Signal")
Published by ChaoZhang on the FMZ strategy square · last modified 2024-12-13. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Enhanced-Mean-Reversion-Strategy-with-MACD-ATR-Implementation.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- CCTBBO Reversal Tracking StrategyPine Script · Mean reversion · 45m
- Multi Timeframe Gold Reversal Tracking StrategyPine Script · Mean reversion · 2h
- MACD RSI and EMA StrategyPine Script · Mean reversion · 1d
- Mean Reversion Trading Strategy Based on Bollinger Bands and Golden RatioPine Script · Mean reversion · 1h
- MTDAT Multi Technical Indicator Dynamic Adaptive Trading Strategy MTDATPine Script · Mean reversion · 15m
- A Multi Period Trading Strategy Based on Moving AveragesPine Script · Mean reversion · 1h