Strategy vault / Trend following
ATR Dynamic Trend Following and EMA Crossover Trading Strategy
This is a trend following strategy based on the ATR (Average True Range) indicator, combining dynamic stop-loss and EMA crossover signals. The strategy calculates ATR to determine market volatility and uses this…

Overview
This is a trend following strategy based on the ATR (Average True Range) indicator, combining dynamic stop-loss and EMA crossover signals. The strategy calculates ATR to determine market volatility and uses this information to establish a dynamic trailing stop line. Trading signals are generated when price and EMA (Exponential Moving Average) break through the ATR trailing stop line. The strategy also offers the option to use regular or Heikin Ashi candles for calculations, adding flexibility.
Strategy Principles
The core logic of the strategy is based on the following key calculations:
- Using ATR indicator to measure market volatility with adjustable period
- Calculating dynamic stop-loss distance based on ATR value, adjusted by sensitivity parameter a
- Building ATR trailing stop line that dynamically adjusts with price movement
- Using 1-period EMA crossover with ATR trailing stop line to determine trading signals
- Opening long positions when EMA breaks above ATR trailing stop line, short when breaking below
- Option to use regular closing price or Heikin Ashi HLC3 price as calculation basis
Strategy Advantages
- Strong Dynamic Adaptability: ATR trailing stop automatically adjusts to market volatility, maintaining strategy stability in different market conditions
- Comprehensive Risk Control: Continuous position protection through dynamic stop-loss line
- Good Parameter Adjustability: Can adapt to different market characteristics by adjusting ATR period and sensitivity
- Clear and Reliable Signals: Provides clear entry and exit signals through EMA crossover
- Concise Calculation Logic: Clear strategy logic, easy to understand and maintain
- Good Visualization: Provides graphical display of trading signals and trends
Strategy Risks
- Choppy Market Risk: May generate frequent false breakout signals in sideways markets
- Slippage Impact: May face significant slippage in fast markets, affecting strategy performance
- Parameter Sensitivity: Different parameter combinations may lead to large performance variations
- Trend Dependency: Strategy may not perform well in non-trending markets
- Stop-Loss Range: Abnormal ATR values may lead to unreasonable stop-loss positions
Strategy Optimization Directions
- Add Trend Filter: Introduce additional trend judgment indicators to reduce false signals in choppy markets
- Optimize Parameter Adaptation: Develop mechanism to automatically optimize ATR period and sensitivity
- Improve Signal Confirmation: Add volume or other technical indicators for signal confirmation
- Enhance Stop-Loss Mechanism: Combine fixed and trailing stops with ATR-based stops
- Add Position Management: Dynamically adjust position size based on market volatility
Summary
This is a complete trading strategy combining dynamic trailing stops and moving average systems. It captures market volatility characteristics through the ATR indicator and provides trading signals using EMA crossover, forming a logically rigorous trading system. The strategy's strengths lie in its dynamic adaptability and risk control capabilities, but attention needs to be paid to its performance in sideways markets. Through the suggested optimization directions, there is room for further improvement of the strategy.
Source Pine Script
/*backtest
start: 2024-05-15 00:00:00
end: 2024-08-08 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"ETH_USDT"}]
*/
//@version=6
strategy(title="UT Bot Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Inputs
a = input.float(1, title="Key Value. 'This changes the sensitivity'")
c = input.int(10, title="ATR Period")
h = input.bool(false, title="Signals from Heikin Ashi Candles")
// Calculate ATR
xATR = ta.atr(c)
nLoss = a * xATR
// Source for calculations
src = h ? request.security(syminfo.tickerid, timeframe.period, hlc3) : close
// ATR Trailing Stop logic
var float xATRTrailingStop = na
if (not na(xATRTrailingStop[1]) and src > xATRTrailingStop[1] and src[1] > xATRTrailingStop[1])
xATRTrailingStop := math.max(xATRTrailingStop[1], src - nLoss)
else if (not na(xATRTrailingStop[1]) and src < xATRTrailingStop[1] and src[1] < xATRTrailingStop[1])
xATRTrailingStop := math.min(xATRTrailingStop[1], src + nLoss)
else
xATRTrailingStop := src > xATRTrailingStop[1] ? src - nLoss : src + nLoss
// Position logic
var int pos = 0
if (not na(xATRTrailingStop[1]) and src[1] < xATRTrailingStop[1] and src > xATRTrailingStop[1])
pos := 1
else if (not na(xATRTrailingStop[1]) and src[1] > xATRTrailingStop[1] and src < xATRTrailingStop[1])
pos := -1
else
pos := pos[1]
xcolor = pos == -1 ? color.red : pos == 1 ? color.green : color.blue
// Entry and Exit Signals
ema = ta.ema(src, 1)
above = ta.crossover(ema, xATRTrailingStop)
below = ta.crossover(xATRTrailingStop, ema)
buy = src > xATRTrailingStop and above
sell = src < xATRTrailingStop and below
// Strategy Execution
if (buy)
strategy.entry("UT Long", strategy.long)
if (sell)
strategy.entry("UT Short", strategy.short)
// Plotting and Alerts
plotshape(buy, title="Buy", text='Buy', style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, size=size.tiny)
plotshape(sell, title="Sell", text='Sell', style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, size=size.tiny)
barcolor(src > xATRTrailingStop ? color.green : src < xATRTrailingStop ? color.red : na)
alertcondition(buy, title="UT Long", message="UT Long")
alertcondition(sell, title="UT Short", message="UT Short")
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/ATR-Dynamic-Trend-Following-and-EMA-Crossover-Trading-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
- ATR RSI Enhanced Trend Following Trading SystemPine Script · Trend following
- DPO EMA Trend Crossover Quantitative Strategy ResearchPine Script · Trend following · 1d
- Dynamic ATR Trailing Stop with EMA Cross Combination StrategyPine Script · Trend following · 2h
- Dynamic Moving Average Tracking Strategy 433137Pine Script · Trend following · 1d
- Kama and Moving Average Based Trend Following StrategyPine Script · Trend following · 45m