EMA Crossover Strategy with Trailing Stop Loss

Author: ChaoZhang, Date: 2023-12-20 17:39:30



This strategy uses the crossover of a fast EMA (9-period) and slow EMA (21-period) as entry signals, and incorporates a trailing stop loss to lock in profits and avoid excessive drawdowns.

Strategy Logic

When the fast EMA crosses above the slow EMA from below, a buy signal is generated. When the fast EMA crosses below the slow EMA from above, a sell signal is triggered.

Once entered, the strategy tracks the highest high in real-time and triggers a trailing stop loss when the current price falls 2% below the highest high, locking in profits.

Advantage Analysis

  • Utilizes EMA’s trend following and signal generation ability to effectively capture medium-long term trends
  • Trailing stop loss locks in most profits, avoiding entire gains being swallowed
  • Adjustable EMA parameters cater to different market environments
  • Clear buy and sell signal rules, easy to implement

Risk Analysis

  • EMA has lagging, may miss short-term opportunities
  • Improper trailing stop loss distance setting may prematurely stop loss or render it ineffective
  • Parameter mismatch with market may cause excessive trading or insufficient signals

Risk Solutions:

  • Choose appropriate EMA parameter combination
  • Test and evaluate stop loss parameter
  • Adjust parameters to match market volatility dynamics

Optimization Directions

  • Dynamically adjust trailing stop distance based on market volatility and risk appetite
  • Add other filters to reduce false signals
  • Optimize EMA period parameters
  • Incorporate trend indicators to avoid counter-trend trading


This strategy integrates the advantages of trend identification and risk control. Through parameter tuning and optimization, it can be adapted to different market types and trading instruments, and is worth further testing and practice.

start: 2023-12-12 00:00:00
end: 2023-12-19 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]

strategy("EMA Crossover with Trailing Stop-Loss", overlay=true)

fastEMA = ema(close, 9)
slowEMA = ema(close, 21)

// Entry conditions
longCondition = crossover(fastEMA, slowEMA)
shortCondition = crossunder(fastEMA, slowEMA)

// Trailing stop-loss calculation
var float trailingStop = na
var float highestHigh = na

if (longCondition)
    highestHigh := na
    trailingStop := na

if (longCondition and high > highestHigh)
    highestHigh := high

if (strategy.position_size > 0)
    trailingStop := highestHigh * (1 - 0.02)  // Adjust the trailing percentage as needed

// Execute trades
strategy.entry("Long", strategy.long, when=longCondition)
strategy.entry("Short", strategy.short, when=shortCondition)

// Apply trailing stop-loss to long positions
strategy.exit("Long", from_entry="Long", loss=trailingStop)

// Plot EMAs and Trailing Stop-Loss
plot(fastEMA, color=color.green, title="Fast EMA")
plot(slowEMA, color=color.red, title="Slow EMA")
plot(trailingStop, color=color.orange, title="Trailing Stop-Loss", linewidth=2)