Strategy vault / Trend following
Dual Moving Average Trend Detection with Volatility Based Stop Loss Strategy
This strategy is a trend-following trading system that combines Exponential Moving Averages (EMA) with a stop-loss mechanism based on the Average True Range (ATR). The strategy uses 9-period and 21-period EMAs to…

Overview
This strategy is a trend-following trading system that combines Exponential Moving Averages (EMA) with a stop-loss mechanism based on the Average True Range (ATR). The strategy uses 9-period and 21-period EMAs to identify market trends while utilizing ATR to dynamically adjust stop-loss positions, achieving an organic combination of trend following and risk control.
Strategy Principle
The core logic of the strategy consists of two main components: trend determination and risk control. For trend determination, market trends are identified by monitoring the crossover between the fast EMA (9-period) and slow EMA (21-period). A long signal is triggered when the fast line crosses above the slow line, and a short signal is triggered when the fast line crosses below the slow line. For risk control, the strategy uses the ATR indicator to calculate dynamic stop-loss positions. Specifically, the stop-loss point for long positions is set at the entry price minus 1.5 times the ATR value, while the stop-loss point for short positions is set at the entry price plus 1.5 times the ATR value.
Strategy Advantages
- High trend identification accuracy: By using two EMAs with different periods, the strategy effectively filters market noise and improves trend judgment accuracy.
- Flexible risk control: The ATR-based dynamic stop-loss mechanism can adaptively adjust according to market volatility, providing wider stop-loss space during increased volatility and tighter stops during reduced volatility.
- Strong parameter adaptability: Key parameters (EMA periods, ATR period, ATR multiplier) can be optimized for different market characteristics and trading timeframes.
- Simple and understandable implementation: The strategy logic is clear, and the code structure is concise, making it easy to understand and maintain.
Strategy Risks
- Sideways market risk: In ranging markets, frequent EMA crossover signals may lead to overtrading and consecutive stop-losses.
- Lag risk: EMAs inherently have some lag, which may result in delayed reactions to rapid market reversals.
- Stop-loss setting risk: The choice of ATR multiplier requires balancing between stop-loss space and profit opportunity; improper settings may lead to premature stops or excessive risk exposure.
Strategy Optimization Directions
- Introduce trend strength confirmation: Add trend strength indicators (such as ADX) as trading filters to enter only during clear trends.
- Dynamic adjustment of ATR multiplier: Automatically adjust the ATR multiplier based on market volatility cycles to improve stop-loss adaptability.
- Add profit targets: Set dynamic profit targets based on ATR to achieve dynamic risk-reward ratio management.
- Include volume confirmation: Add volume analysis for entry signal confirmation to improve trading signal reliability.
Summary
This strategy builds a complete trend-following trading system by combining EMA crossover trend determination with ATR dynamic stop-loss. The strategy's strengths lie in its objective judgment criteria and flexible risk control, but attention must be paid to sideways market risks and signal lag issues. There is significant room for improvement through adding trend strength confirmation, optimizing stop-loss settings, and other enhancements. Overall, this is a trend-following strategy with solid foundations and clear logic, suitable as a basis for building more complex trading systems.
Source Pine Script
/*backtest
start: 2024-02-20 00:00:00
end: 2024-05-31 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"TRB_USDT"}]
*/
//@version=5
strategy("EMA 9/21 + ATR SL Strategy", shorttitle="EMA+ATR", overlay=true)
// ===== Input Parameters ===== //
emaFastLen = input.int(9, "Fast EMA")
emaSlowLen = input.int(21, "Slow EMA")
atrLen = input.int(14, "ATR Length")
atrMult = input.float(1.5, "ATR Multiplier")
// ===== EMA Calculation ===== //
emaFast = ta.ema(close, emaFastLen)
emaSlow = ta.ema(close, emaSlowLen)
// ===== ATR Calculation ===== //
atrValue = ta.atr(atrLen)
// ===== Conditions for Entry ===== //
longCondition = ta.crossover(emaFast, emaSlow) // Long when 9 EMA crosses above 21 EMA
shortCondition = ta.crossunder(emaFast, emaSlow) // Short when 9 EMA crosses below 21 EMA
// ===== Entry Commands ===== //
if longCondition
strategy.entry("Long", strategy.long)
if shortCondition
strategy.entry("Short", strategy.short)
// ===== Set Stop-Loss Using ATR ===== //
//
// For LONG: stop-loss = entry price - (atrMult * ATR)
// For SHORT: stop-loss = entry price + (atrMult * ATR)
//
// Note: You can adjust the atrMult values based on market volatility
//
if strategy.position_size > 0
// If holding LONG, define stop-loss below the entry price
strategy.exit("Exit Long", "Long", stop = strategy.position_avg_price - atrMult * atrValue)
if strategy.position_size < 0
// If holding SHORT, define stop-loss above the entry price
strategy.exit("Exit Short", "Short", stop = strategy.position_avg_price + atrMult * atrValue)
Published by ianzeng123 on the FMZ strategy square · last modified 2025-02-27. 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/Dual-Moving-Average-Trend-Detection-with-Volatility-Based-Stop-Loss-Strategy.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
- Dual EMA Filtered ATR Adaptive Trend Following StrategyPine Script · Trend following · 1h
- Dynamic EMA Trend Detection with ATR Threshold StrategyPine Script · Trend following · 1h
- Multi Exponential Moving Average Crossover Strategy with Volume Based ATR Dynamic Stop Loss OptimizationPine Script · Trend following · 2d
- Multi Factor Trend Price Action Strategy with Dynamic Risk Management SystemPine Script · Trend following · 2h
- Multi Indicator Dynamic Trend Following Strategy Based on EMA and SMAPine Script · Trend following · 1d
- Three Line Strike Trend Following Strategy with EMA Dynamic Filter and ATR Risk Management SystemPine Script · Trend following · 2h