Trend Reversal Strategy Based on EMA and SMA Crossover

Author: ChaoZhang, Date: 2024-01-04 17:59:04



The purpose of this strategy is to identify potential trend reversal points by observing the crossover between the 20-period Exponential Moving Average (EMA) and the 20-period Simple Moving Average (SMA). It decides to go long or go short based on the direction of the crossover.

Strategy Logic

  1. When the 20-period EMA crosses above the 20-period SMA and the closing price is above the 20-period EMA, go long.
  2. When the 20-period EMA crosses below the 20-period SMA and the closing price is below the 20-period EMA, go short.
  3. For long positions, close the trade when the 20-period EMA crosses below the 20-period SMA.
  4. For short positions, close the trade when the 20-period EMA crosses above the 20-period SMA.

The strategy uses the crossover and crossunder functions from the ta library to detect moving average crossovers.

Advantage Analysis

The strategy combines the trend following capability of moving averages and the signal generation of crossover events, having the following advantages:

  1. Moving averages can effectively filter out some market noise and identify medium-to-long term trends.
  2. Crossovers are easy to operate and clearly identify turns in market momentum.
  3. The 20-period parameter works well for most stocks and timeframes without needing frequent adjustment.
  4. Using the closing price in relation to the EMA avoids some false signals.
  5. The rules are simple and easy to understand, suitable for less sophisticated investors.

Risk Analysis

The strategy also has the following risks:

  1. Moving averages have lag and may miss short-term, abrupt trend reversals.
  2. Crossovers can generate noisy signals, impacting stability.
  3. The fixed 20-period parameter may not work well for some stocks, needing tuning.
  4. There is no stop loss, allowing large losing trades.


  1. Shorten the moving average periods to increase responsiveness.
  2. Add filters to avoid false signals.
  3. Test and optimize parameters and stock categories.
  4. Incorporate stop loss to control risk.

Optimization Directions

The strategy can also be improved in the following aspects:

  1. Add other indicators to build a composite strategy, e.g. volume, RSI.
  2. Test and optimize periods and symbols, set adaptive parameters.
  3. Build dynamic exit mechanisms like trailing stop loss, time-based stop loss.
  4. Add algorithmic trading capabilities for automation.
  5. Incorporate machine learning for adaptive optimization.


The strategy is relatively simple and practical overall, identifying potential trend reversal points through moving average crossover theory. But there is also room for improvement via additional indicators, dynamic parameters, stop losses, algorithmic trading etc. to make the strategy more robust, reliable and automated. In summary, it provides a good template for getting started with quantitative trading.

start: 2022-12-28 00:00:00
end: 2024-01-03 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]

strategy("EMA-SMA Crossover Strategy", overlay=true)

// Define the length of the moving averages
emaLength = 20
smaLength = 20

// Calculate moving averages
emaValue = ta.ema(close, emaLength)
smaValue = ta.sma(close, smaLength)

// Buy condition
buyCondition = ta.crossover(emaValue, smaValue) and close > emaValue

// Short sell condition
sellCondition = ta.crossunder(emaValue, smaValue) and close < emaValue

// Exit conditions for both Buy and Short sell
exitBuyCondition = ta.crossunder(emaValue, smaValue)
exitSellCondition = ta.crossover(emaValue, smaValue)

// Strategy logic
if (buyCondition)
    strategy.entry("Buy", strategy.long)

if (sellCondition)
    strategy.entry("Sell", strategy.short)

if (exitBuyCondition)

if (exitSellCondition)

// Plot the moving averages
plot(emaValue,, title="20 EMA")
plot(smaValue,, title="20 SMA")