Strategy vault / Trend following
Dynamic ATR Adjusted EMA Crossover Strategy
This strategy is a trading system based on Exponential Moving Average (EMA) crossovers, combined with Average True Range (ATR) for dynamic risk management. The strategy uses short-term and long-term EMA lines to…

Overview
This strategy is a trading system based on Exponential Moving Average (EMA) crossovers, combined with Average True Range (ATR) for dynamic risk management. The strategy uses short-term and long-term EMA lines to capture momentum changes in price trends, while utilizing ATR to dynamically set take-profit and stop-loss levels, achieving precise control over trading risks.
Strategy Principle
The core logic of the strategy is based on crossover signals between two EMAs of different periods (9 and 21). A buy signal is generated when the short-term EMA crosses above the long-term EMA, while a sell signal is generated when the short-term EMA crosses below the long-term EMA. To better manage risk, the strategy incorporates a dynamic take-profit and stop-loss mechanism based on 14-period ATR, with take-profit levels set at 2x ATR and stop-loss levels at 1x ATR, ensuring sufficient profit potential while maintaining timely risk control.
Strategy Advantages
- Dynamic Risk Management: Adjusts take-profit and stop-loss levels dynamically through ATR, allowing better adaptation to market volatility changes.
- Trend Following Capability: The EMA crossover system effectively captures medium to long-term trends, reducing false signals.
- Optimized Risk-Reward Ratio: Take-profit distance is twice the stop-loss distance, adhering to sound risk-reward principles.
- Strong Adaptability: Strategy parameters can be adjusted for different market conditions, demonstrating high adaptability.
Strategy Risks
- Choppy Market Risk: May generate frequent false breakout signals in ranging markets, leading to consecutive losses.
- Slippage Risk: During high volatility periods, actual execution prices may significantly deviate from signal prices.
- Parameter Sensitivity: The choice of EMA periods significantly impacts strategy performance, potentially requiring different settings for different market environments.
Strategy Optimization Directions
- Implement Trend Filters: Add longer-period moving averages or ADX indicators to filter trend strength, trading only in strong trend environments.
- Optimize Position Sizing: Dynamically adjust position sizes based on ATR values, reducing positions during high volatility periods.
- Add Time Filters: Implement trading time filters to avoid trading during low liquidity periods.
Summary
This strategy creates a comprehensive trading system by combining the classic EMA crossover system with dynamic ATR risk management. Its main strengths lie in dynamic risk management capabilities and effective trend-following characteristics. Through the suggested optimization directions, there is room for further improvement. For live trading implementation, it is recommended to conduct thorough backtesting and parameter optimization, with appropriate adjustments based on specific market characteristics.
Source Pine Script
/*backtest
start: 2019-12-23 08:00:00
end: 2025-01-04 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Improved EMA Crossover Strategy", overlay=true)
// User-defined inputs for EMAs
shortTermLength = input(9, title="Short-Term EMA Length")
longTermLength = input(21, title="Long-Term EMA Length")
// Dynamic Take Profit and Stop Loss
atrLength = input(14, title="ATR Length")
atrMultiplierTP = input(2.0, title="ATR Multiplier for Take Profit")
atrMultiplierSL = input(1.0, title="ATR Multiplier for Stop Loss")
// Calculate EMAs and ATR
shortTermEMA = ta.ema(close, shortTermLength)
longTermEMA = ta.ema(close, longTermLength)
atr = ta.atr(atrLength)
// Plot the EMAs
plot(shortTermEMA, color=color.blue, title="Short-Term EMA")
plot(longTermEMA, color=color.red, title="Long-Term EMA")
// Generate Entry Conditions
longCondition = ta.crossover(shortTermEMA, longTermEMA)
shortCondition = ta.crossunder(shortTermEMA, longTermEMA)
// Optional Debugging: Print conditions (you can remove this later)
var label longLabel = na
var label shortLabel = na
if longCondition
longLabel := label.new(bar_index, high, "Buy Signal", color=color.green, style=label.style_label_down, textcolor=color.white)
if shortCondition
shortLabel := label.new(bar_index, low, "Sell Signal", color=color.red, style=label.style_label_up, textcolor=color.white)
if (longCondition)
strategy.entry("Long", strategy.long)
strategy.exit("Long Exit", "Long", limit=close + atr * atrMultiplierTP, stop=close - atr * atrMultiplierSL)
if (shortCondition)
strategy.entry("Short", strategy.short)
strategy.exit("Short Exit", "Short", limit=close - atr * atrMultiplierTP, stop=close + atr * atrMultiplierSL)Published by ChaoZhang on the FMZ strategy square · last modified 2025-01-06. 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-ATR-Adjusted-EMA-Crossover-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
- Dual Moving Average Trend Detection with Volatility Based Stop Loss StrategyPine Script · Trend following · 1d
- 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