EMA-MACD Trend Reversal Quantitative Trading Strategy

FMZQuant - Feb 26 - - Dev Community

Image description

Image description

Overview
This strategy is a trend reversal trading system based on moving averages and MACD indicators. It combines Fast Exponential Moving Average (EMA), Simple Moving Average (SMA), and MACD indicator to capture profit opportunities during market trend changes. The strategy focuses on price breakouts above moving averages and MACD bottom reversal signals below the zero line to position trades before potential market reversals.

Strategy Principle
The strategy uses EMA(10) and MA(20) as trend judgment benchmarks, combined with MACD indicator (12,26,9) for signal confirmation. Specifically, entry signals require the following conditions to be met simultaneously:

  1. EMA(10) crosses above MA(20), indicating short-term momentum is becoming stronger than medium-term trend
  2. Both MACD and signal lines are below the zero line, but MACD line is above the signal line, showing potential bottom reversal signals

The exit condition is triggered when the MACD delta crosses below 0 while both MACD and signal lines are above the zero line, suggesting the uptrend may have ended.

Strategy Advantages

  1. Multiple technical indicators cross-validation improves signal reliability
  2. Combines trend and momentum indicators for both trend capture and precise entry timing
  3. Uses both EMA and SMA to balance market sensitivity and false signal filtering
  4. Clear exit conditions help secure profits and avoid being trapped

Strategy Risks

  1. May generate frequent false breakout signals in ranging markets
  2. Moving average system has inherent lag, potentially missing optimal entry points
  3. MACD indicator may generate lagging signals in volatile markets
  4. Lacks explicit stop-loss mechanism, potentially leading to significant losses in volatile markets

Strategy Optimization Directions

  1. Introduce volatility indicators (like ATR) for dynamic position sizing and stop-loss placement
  2. Add trend strength filters to avoid trading in weak trends
  3. Optimize moving average parameters based on different market characteristics
  4. Add volume indicator verification to improve signal reliability
  5. Build a more comprehensive money management system, including staged position building and dynamic position adjustment

Summary
This strategy constructs a relatively complete trend reversal trading system through the combination of moving average system and MACD indicator. Although it has certain inherent lag and false signal risks, it still holds practical value through reasonable parameter optimization and risk control measures. When implementing in live trading, it's recommended to adjust strategy parameters based on market conditions and personal risk preference.

Strategy source code

/*backtest
start: 2024-02-25 00:00:00
end: 2025-01-20 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"SOL_USDT"}]
*/

//@version=5
strategy("MACD Strategy", overlay=true)

//Macd parameters
fastLength = input(12, title="Fast line length")
slowLength = input(26, title="Slow line length")
MACDLength = input(9, title="MACD signal line length")

// Calculate MACD
MACD = ta.ema(close, fastLength) - ta.ema(close, slowLength)
aMACD = ta.ema(MACD, MACDLength)
delta = MACD - aMACD


// Calculation of EMA(10) and MA(20)
ema10 = ta.ema(close, 10)
ma20 = ta.sma(close, 20)
// Plotting EMA(10) and MA(20) on the chart for debugging purposes
plot(ema10, title="EMA 10", color=color.blue, linewidth=2)
plot(ma20, title="MA 20", color=color.red, linewidth=2)

// Real-time check conditions
// Check if EMA(10) is above MA(20)
bool emaAboveMa = ema10 > ma20

// Check if MACD is above the signal line and both MACD and signal line are below the 0 axis
bool macdCondition = (MACD > aMACD) and (MACD < 0) and (aMACD < 0)

// Add debug information - draw graphics when conditions are met
plotshape(emaAboveMa, title="EMA Above MA Condition",  size=size.small, text="eam")
plotshape(macdCondition, title="MACD Condition", size=size.small, text="macd")

// When both conditions are met, a buy operation is triggered
if (emaAboveMa and macdCondition)
    strategy.entry("Long position", strategy.long, comment="Buy signal")
    // Displays the label for the buy signal
    label.new(bar_index, high, "Buy", textcolor=color.white, style=label.style_label_up, size=size.normal)

// Closing conditions
if (ta.crossunder(delta, 0) and MACD > 0 and aMACD > 0)
    strategy.close("MacdLE", comment="Close Long")
//if (ta.crossunder(delta, 0))
//  strategy.entry("MacdSE", strategy.short, comment="MacdSE")
//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)
Enter fullscreen mode Exit fullscreen mode

Strategy Parameters

Image description

The original address: EMA-MACD Trend Reversal Quantitative Trading Strategy

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .