EMA Crossover Strategy

Author: ChaoZhang, Date: 2023-09-28 11:22:39


This strategy builds a trading system based on the EMA crossover principle to automatically trade and capture market trends. It mainly uses the crossover of fast EMA and slow EMA to determine buy and sell signals.

Strategy Logic

This strategy is mainly built on the crossover principle of two moving averages, EMAs. One is the 20-period slow EMA, and the other is the 9-period fast EMA. When the fast EMA (EMA9) crosses above the slow EMA (EMA20), a buy signal is generated. When EMA9 crosses below EMA20, a sell signal is generated.

Specifically, the strategy calculates the values of two EMAs and compares their magnitude relationship to determine if a crossover happens. When EMA9 is greater than EMA20, it indicates a golden cross happens and the boolean variable bullish is set to true, meaning a buy signal is generated. When EMA9 is less than EMA20, it indicates a dead cross happens and the boolean variable bearish is set to true, meaning a sell signal is generated.

At the same time, the strategy also uses the cross function to detect crossovers between EMA9 and EMA20. When an upward crossover happens, i.e. EMA9 crosses above EMA20, bullish is also set to true. When a downward crossover happens, i.e. EMA9 crosses below EMA20, bearish is also set to true.

This dual validation helps avoid missing signals. Finally, the strategy enters long or short logic based on the values of bullish and bearish to complete the automated trading system.

Advantage Analysis

This strategy has the following advantages:

  1. Using EMA crossover principle effectively detects market trend reversal points and captures trends.

  2. The fast and slow EMA combo smoothes out trends and catches reversals.

  3. The classic golden cross to buy and dead cross to sell is simple and intuitive.

  4. Added crossover detection logic avoids missing signals.

  5. Fully automated system, no manual intervention needed, good backtest results.

  6. Customizable EMA periods allows optimizing the strategy.

Risk Analysis

This strategy also has some risks:

  1. EMA crossover trend detection can be late and miss reversal points.

  2. Whipsaw effect can trigger false signals on short-term corrections.

  3. Fixed EMA periods cannot adapt to market changes.

  4. Unable to gauge trend strength, may get whipsawed in ranging markets.

  5. No stop loss means losses could expand.

  6. Backtest overfitting of automated systems, questionable live performance.

To address the risks, optimizations can be made in:

  1. Add other indicators for trend confirmation to avoid whipsaws.

  2. Implement stop loss to limit downside.

  3. Introduce parameter optimization for dynamic EMA periods.

  4. Add trend strength determination to avoid ranging market trades.

  5. Utilize ensemble models to improve robustness.

Optimization Directions

This strategy can be optimized in several aspects:

  1. Dynamic EMA Periods: The fixed 20 and 9 periods can be made adaptive to better track evolving market trends.

  2. Multi Timeframe Validation: Currently only one timeframe, can verify signals on multiple timeframes to avoid false signals.

  3. Combine Other Indicators: Incorporate indicators like MACD, KD to filter crossover signals and improve accuracy.

  4. Stop Loss: Currently no stop loss, can add fixed or trailing stop loss to limit downside.

  5. Parameter Optimization: Optimize EMA periods to find best combinations. Or walk-forward optimize for dynamic parameters.

  6. Ensemble Models: Build ensemble of sub-strategies with different parameters for robustness.

  7. Machine Learning: Use neural networks to train and recognize crossovers for an intelligent system.


This strategy builds an automated system based on the classical EMA crossover principle. The overall logic is simple and clear. But stability issues exist. By introducing dynamic parameters, multi-indicator combos, stop losses, ensemble models etc., significant improvements can be made in live performance and robustness. EMA crossover strategies warrant further research and application.

start: 2022-09-21 00:00:00
end: 2023-09-27 00:00:00
period: 4d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]

//For TRI'ers with a stinky trading view account.
//Some reccomended moving averages including the institutional moving averages.
//Much love to Brian for changing our lives.

strategy (title="Crossing Ema 20:9 by Sedkur", overlay=false)

src = close

ema20 = ema(src, 20)
ema9 = ema(src, 9)

plot( ema20, color=color.orange, style=plot.style_line, title="EMA20", linewidth=2)
plot( ema9, color=color.blue, style=plot.style_line, title="EMA9", linewidth=2)

//bullish = (ema9>ema20)?true:false
bullish = cross(ema9, ema20) and (ema9>ema20)?true:false
bearish = cross(ema9, ema20) and (ema20>ema9)?true:false
plotshape(bullish, style=shape.triangleup , location=location.belowbar, color=color.lime,size=size.tiny)
plotshape(bearish, style=shape.triangledown , location=location.abovebar, color=color.red,size=size.tiny)
alertcondition(bullish, title="Bullish", message="AL verdi")

if (bullish)
    strategy.entry("buy", strategy.long, comment="al", when = year>2016)
if (bearish)
    strategy.entry("sell", strategy.short, comment="sat", when = year>2016)