Dynamic Dual EMA Trailing Stop Strategy
This strategy aims to exploit potential trend reversals or continuations using Exponential Moving Averages (EMAs) and a trailing stop based on the Chande Dynamic Convergence Divergence (CDC) Average True Range…

Overview
This strategy aims to exploit potential trend reversals or continuations using Exponential Moving Averages (EMAs) and a trailing stop based on the Chande Dynamic Convergence Divergence (CDC) Average True Range method. The strategy combines multiple indicators to determine entry timing and sets stop loss and take profit levels based on market volatility to control risk while capturing new trends.
Strategy Logic
This strategy uses 60-period and 90-period dual EMAs to determine trend direction. A crossover where the shorter period EMA moves above the longer period EMA gives a bullish signal. At the same time, a MACD line crossover above its signal line can confirm the bullish view. Entry requires the price to be above the previously calculated CDC trailing stop level.
The exit rules are: close the position when price hits the ATR-based take profit level or falls below the CDC trailing stop loss level.
Advantage Analysis
This strategy combines dual EMAs to judge the main trend direction and MACD to confirm entry timing, avoiding false breakouts. Both the trailing stop and profit target levels are calculated based on market volatility for effective risk management. Whether a trend reverses or continues, this strategy can seize opportunities in a timely manner.
In addition, the input parameters of this strategy are customizable. Users can adjust the EMA periods, ATR period and CDC multiplier according to their own trading style.
Risk Analysis
The biggest risk of this strategy is incorrect trend judgment. When the market is consolidating, EMAs can easily give wrong signals. At this time, MACD's confirmation role is especially important. In addition, appropriately increasing the CDC stop loss multiplier is needed to deal with large price gaps caused by sudden events.
Optimization Directions
- Test different combinations of EMA period parameters to find the optimal setting
- Test different CDC stop loss multiplier sizes
- Try incorporating other indicators to filter entry timing
- Add mechanisms to handle sudden market events
Summary
This strategy makes good use of the advantages of trend and volatility indicators to identify potential opportunities in securities. Through parameter optimization and mechanism improvements, this strategy has the potential to further enhance stability and profitability. It provides quantitative traders with a reliable and scalable strategic framework.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 60 | EMA 60 Period |
| v_input_2 | 90 | EMA 90 Period |
| v_input_3 | 24 | CDC ATR Period |
| v_input_4 | 4 | CDC Multiplier |
| v_input_5 | 2 | Profit Target Multiplier (ATR) |
Source Pine Script
/*backtest
start: 2023-01-17 00:00:00
end: 2024-01-23 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Improved EMA & CDC Trailing Stop Strategy", overlay=true)
// Define the inputs
ema60Period = input(60, title="EMA 60 Period")
ema90Period = input(90, title="EMA 90 Period")
atrPeriod = input(24, title="CDC ATR Period")
multiplier = input(4.0, title="CDC Multiplier")
profitTargetMultiplier = input(2.0, title="Profit Target Multiplier (ATR)")
// Calculate EMAs
ema60 = ta.ema(close, ema60Period)
ema90 = ta.ema(close, ema90Period)
// Calculate ATR
atr = ta.atr(atrPeriod)
// MACD calculation
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)
// Define the trailing stop and profit target
longStop = close - multiplier * atr
shortStop = close + multiplier * atr
longProfitTarget = close + profitTargetMultiplier * atr
shortProfitTarget = close - profitTargetMultiplier * atr
// Entry conditions
longCondition = close > ema60 and ema60 > ema90 and macdLine > signalLine and close > longStop
shortCondition = close < ema60 and ema60 < ema90 and macdLine < signalLine and close < shortStop
// Exit conditions based on profit target
longProfitCondition = close >= longProfitTarget
shortProfitCondition = close <= shortProfitTarget
// Plot the EMAs, Stops, and MACD for visualization
plot(ema60, color=color.blue, title="60 EMA")
plot(ema90, color=color.red, title="90 EMA")
plot(longStop, color=color.green, title="Long Stop", style=plot.style_linebr)
plot(shortStop, color=color.red, title="Short Stop", style=plot.style_linebr)
hline(0, "Zero Line", color=color.gray)
plot(macdLine - signalLine, color=color.blue, title="MACD Histogram")
// Strategy execution using conditional blocks
if longCondition
strategy.entry("Long", strategy.long)
if shortCondition
strategy.entry("Short", strategy.short)
// Exit based on profit target and trailing stop
if longProfitCondition or close < longStop
strategy.close("Long")
if shortProfitCondition or close > shortStop
strategy.close("Short")
Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-24. 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/Dynamic-Dual-EMA-Trailing-Stop-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 Breakout Volatility Channel StrategyPine Script · Breakout · 1h
- Kana Candle Breakout Strategy Based on Moving Average and Support ResistancePine Script · Breakout · 1h
- MA Trendline Breakthrough StrategyPine Script · Breakout · 1d
- Momentum Breakout Moving Average StrategyPine Script · Breakout · 1d
- Momentum Breakout Trading Strategy 440964Pine Script · Breakout · 5m
- Moving Average Crossover and Upper Rail Breakthrough Impulse StrategyPine Script · Breakout · 1m