0bps

Strategy vault / Trend following

Advanced EMA Crossover Trend Following Strategy with ATR Based Dynamic Stop Management System

This strategy is a trend following trading system that combines EMA crossover signals with dynamic risk management. It uses fast and slow Exponential Moving Averages (EMA) to identify market trends and incorporates…

Author
ChaoZhang
Complexity
Starter · 50 lines
Parameters
0
Backtest bar
2m · intraday
Tested on
BTC/USDT · futures

IMG

Overview

This strategy is a trend following trading system that combines EMA crossover signals with dynamic risk management. It uses fast and slow Exponential Moving Averages (EMA) to identify market trends and incorporates the Average True Range (ATR) indicator to optimize entry timing. The strategy also integrates three layers of protection: percentage-based stop loss, take profit, and trailing stop.

Strategy Principles

The core logic is based on the following key elements:

  1. Uses 5-period and 20-period EMA crossovers to determine trend direction
  2. Enhances signal reliability through ATR multiplier filtering
  3. Triggers trading signals when EMA crosses occur and price breaks ATR channel
  4. Sets immediate 1% fixed stop loss and 5% profit target upon position entry
  5. Employs ATR-based trailing stop to protect profits
  6. Trades both long and short directions to capture all market opportunities

Strategy Advantages

  1. Signal system combines trend and volatility indicators for improved accuracy
  2. Dynamic ATR channel adapts to volatility characteristics in different market conditions
  3. Triple risk control mechanism provides comprehensive protection
  4. Highly adjustable parameters for optimization across different market characteristics
  5. High level of automation reduces emotional interference in trading decisions

Strategy Risks

  1. EMA crossovers may lag in volatile markets, potentially missing optimal entry points
  2. Fixed percentage stops may lack flexibility during high volatility periods
  3. Frequent trading might incur significant transaction costs
  4. May generate frequent false signals in ranging markets
  5. Trailing stops might exit positions prematurely during quick retracements

Optimization Directions

  1. Incorporate volume indicators to validate trend strength
  2. Add market regime identification mechanism for parameter adaptation
  3. Optimize ATR multiplier with adaptive dynamic parameter system
  4. Integrate additional technical indicators to filter false signals
  5. Develop more flexible capital management solutions

Summary

This is a well-designed trend following strategy with clear logic. It captures trends through EMA crossovers, manages risk using ATR, and incorporates multiple stop loss mechanisms to form a complete trading system. The strategy's main advantages lie in its comprehensive risk control and high customizability, but attention must be paid to false signals and transaction costs in live trading. Through the suggested optimization directions, there is room for further improvement in the strategy's performance.

Source Pine Script

/*backtest
start: 2024-12-29 00:00:00
end: 2025-01-05 00:00:00
period: 2m
basePeriod: 2m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jesusperezguitarra89

//@version=6
strategy("High Profit Buy/Sell Signals", overlay=true)

// Parámetros ajustables
fastLength = input.int(5, title="Fast EMA Length")
slowLength = input.int(20, title="Slow EMA Length")
atrLength = input.int(10, title="ATR Length")
atrMultiplier = input.float(2.5, title="ATR Multiplier")
stopLossPercent = input.float(1.0, title="Stop Loss %")
takeProfitPercent = input.float(5.0, title="Take Profit %")
trailingStop = input.float(2.0, title="Trailing Stop %")

// Cálculo de EMAs
fastEMA = ta.ema(close, fastLength)
slowEMA = ta.ema(close, slowLength)

// Cálculo del ATR
atr = ta.atr(atrLength)

// Señales de compra y venta
longCondition = ta.crossover(fastEMA, slowEMA) and close > slowEMA + atrMultiplier * atr
shortCondition = ta.crossunder(fastEMA, slowEMA) and close < slowEMA - atrMultiplier * atr

// Dibujar señales en el gráfico
plotshape(series=longCondition, location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=shortCondition, location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")

// Estrategia de backtesting para marcos de tiempo en minutos
if longCondition
    strategy.entry("Buy", strategy.long)
    strategy.exit("Take Profit", from_entry="Buy", limit=close * (1 + takeProfitPercent / 100), stop=close * (1 - stopLossPercent / 100), trail_points=atr * trailingStop)
if shortCondition
    strategy.entry("Sell", strategy.short)
    strategy.exit("Take Profit", from_entry="Sell", limit=close * (1 - takeProfitPercent / 100), stop=close * (1 + stopLossPercent / 100), trail_points=atr * trailingStop)

// Mostrar EMAs
plot(fastEMA, color=color.blue, title="Fast EMA")
plot(slowEMA, color=color.orange, title="Slow EMA")

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.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Advanced-EMA-Crossover-Trend-Following-Strategy-with-ATR-Based-Dynamic-Stop-Management-System.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open