Strategy vault / Trend following
EMA Cross Over Trend Following Strategy with ATR Dynamic Stop Loss Optimization
This strategy is a trend-following system based on moving average crossovers and dynamic stop-loss management. The core logic is to capture the beginning of uptrends through the golden cross of fast moving average…

Overview
This strategy is a trend-following system based on moving average crossovers and dynamic stop-loss management. The core logic is to capture the beginning of uptrends through the golden cross of fast moving average (EMA5) and slow moving average (EMA200), combined with ATR-based dynamic stop-loss for profit protection. The strategy also incorporates a fixed percentage take-profit target to balance risk and reward.
Strategy Principles
The strategy operates on the following core mechanisms:
- Entry signals are triggered when EMA5 crosses above EMA200, indicating short-term momentum breakthrough
- Dynamic stop-loss is calculated based on the ATR indicator, set at close price minus ATR value multiplied by a factor
- Take-profit target is set at a fixed percentage (default 5%) above entry price
- During position holding, ATR stop-loss moves up with price movement, forming a trailing stop
- The strategy automatically closes positions when price hits either stop-loss or take-profit levels
Strategy Advantages
- Strong trend capture capability - EMA crossover system effectively identifies early trend stages
- Flexible risk management - ATR dynamic stop-loss adapts to market volatility
- Stable execution - Systematic entry and exit rules avoid emotional interference
- High parameter adaptability - Moving average periods, ATR multiplier, and take-profit percentage can be optimized
- Clear operational logic - Strategy rules are simple and easy to understand and execute
Strategy Risks
- False breakout risk - Ranging markets may produce multiple invalid cross signals
- Drawdown risk - Sudden trend reversals may lead to significant drawdowns
- Slippage risk - Stop-loss or take-profit orders may face slippage in volatile markets
- Parameter sensitivity - Optimal parameters may vary significantly across different market conditions
- Money management risk - Fixed position sizing may be too risky in certain situations
Strategy Optimization Directions
- Add trend filters - Incorporate trend strength indicators like ADX to filter weak trends
- Optimize stop-loss mechanism - Consider combining support levels or volatility percentages
- Dynamic take-profit adjustment - Adjust take-profit targets based on market volatility or trend strength
- Add time filters - Avoid highly volatile time periods
- Improve position management - Introduce dynamic position sizing based on market risk levels
Summary
This is a trend-following strategy combining classic technical indicators with modern risk management. It captures trends through moving average crossovers and protects profits using ATR dynamic stop-loss, performing well in trending markets. While there are risks of false signals, strategy stability can be significantly improved through parameter optimization and additional filters. The core advantages lie in its systematic operational logic and flexible risk management mechanism, making it suitable as a foundation framework for medium to long-term trend trading.
Source Pine Script
/*backtest
start: 2024-02-21 00:00:00
end: 2025-02-18 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"ETH_USDT"}]
*/
// -----------------------------------------------------------
// Title: EMA5 Cross-Up EMA200 with ATR Trailing Stop & Take-Profit
// Author: ChatGPT
// Version: 1.1 (Pine Script v6)
// Notes: Enter Long when EMA(5) crosses above EMA(200).
// Exit on either ATR-based trailing stop or
// specified % Take-Profit.
// -----------------------------------------------------------
//@version=6
strategy(title="EMA5 Cross-Up EMA200 ATR Stop", shorttitle="EMA5x200_ATRStop_v6", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity,default_qty_value=100)
// -- 1) Inputs
emaFastLength = input.int(5, "Fast EMA Length")
emaSlowLength = input.int(200, "Slow EMA Length")
atrPeriod = input.int(14, "ATR Period")
atrMult = input.float(2.0,"ATR Multiplier", step=0.1)
takeProfitPerc = input.float(5.0,"Take-Profit %", step=0.1)
// -- 2) Indicator Calculations
emaFast = ta.ema(close, emaFastLength)
emaSlow = ta.ema(close, emaSlowLength)
atrValue = ta.atr(atrPeriod)
// -- 3) Entry Condition: EMA5 crosses above EMA200
emaCrossUp = ta.crossover(emaFast, emaSlow)
// -- 4) Determine a dynamic ATR-based stop loss (for trailing)
longStopPrice = close - (atrValue * atrMult)
// -- 5) Take-Profit Price
// We store it in a variable so we can update it when in position.
var float takeProfitPrice = na
var float avgEntryPrice = na
if strategy.position_size > 0
// If there is an open long, get the average fill price:
avgEntryPrice := strategy.position_avg_price
takeProfitPrice := avgEntryPrice * (1 + takeProfitPerc / 100)
else
// If no open position, reset
takeProfitPrice := na
avgEntryPrice := na
// -- 6) Submit Entry Order
if emaCrossUp
strategy.entry(id="Long", direction=strategy.long)
// -- 7) Submit Exit Orders (Stop or Take-Profit)
strategy.exit(id = "Exit Long",stop = longStopPrice,limit = takeProfitPrice)
// -- 8) (Optional) Plotting for Visuals
plot(emaFast, color=color.new(color.yellow, 0), linewidth=2, title="EMA Fast")
plot(emaSlow, color=color.new(color.blue, 0), linewidth=2, title="EMA Slow")
plot(longStopPrice, color=color.red, linewidth=2, title="ATR Trailing Stop")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/EMA-Cross-Over-Trend-Following-Strategy-with-ATR-Dynamic-Stop-Loss-Optimization.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