The moving average crossover MACD trading strategy is a quantitative trading strategy that tracks the crossover situations of short-term and long-term exponential moving averages (EMA) and makes buy and sell operations when golden cross and dead cross occur. This strategy combines the MACD indicator for trading signal judgment.
This strategy mainly relies on 12-day EMA, 26-day EMA and MACD indicator. The specific logic is:
Additionally, this strategy also sets some filtering conditions:
This strategy combines moving average crossover and MACD indicator, which can effectively capture the inflection points of market short-term and medium-term trends. The main advantages are:
This strategy also has some risks:
Corresponding mitigation methods:
The main aspects for optimizing this strategy include:
The moving average crossover MACD trading strategy generates trading signals through simple trend tracking and effectively controls risks with appropriate filtering conditions. It is an effective quantitative trading strategy. The strategy can be improved in ways like parameter optimization, adding stop loss mechanisms, incorporating more auxiliary indicators etc.
/*backtest
start: 2024-01-01 00:00:00
end: 2024-01-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("EMMA", max_bars_back = 200)
var up1 = #26A69A
var up2 = #B2DFDB
var down1 = #FF5252
var down2 = #FFCDD2
var confirmationLength = 2
var earliest = timestamp("20 Jan 2024 00:00 +0000")
// Regn u
shortEMA = ta.ema(close, 12)
longEMA = ta.ema(close, 26)
macd = shortEMA - longEMA
signal = ta.ema(macd, 9)
delta = macd - signal
absDelta = math.abs(delta)
previousDelta = delta[1]
signalCrossover = ta.crossover(macd, signal)
signalCrossunder = ta.crossunder(macd, signal)
harskiftetdag = hour(time[confirmationLength]) > hour(time)
enterLongSignal = signalCrossover[confirmationLength] and (macd > signal) and (absDelta >= 0.08)
exitLongSignal = signalCrossunder[confirmationLength] and (macd < signal)
enterShortSignal = signalCrossunder[confirmationLength] and (macd < signal) and (absDelta >= 0.08)
exitShortSignal = signalCrossover[confirmationLength] and (macd > signal)
// Så er det tid til at købe noe
qty = math.floor(strategy.equity / close)
if time >= earliest and not harskiftetdag
if exitLongSignal
strategy.close("long")
else if enterLongSignal
strategy.close("short")
strategy.entry("long", strategy.long, qty = qty)
if exitShortSignal
strategy.close("short")
else if enterShortSignal
strategy.close("long")
strategy.entry("short", strategy.short, qty = qty)
// Så er det tid til at vise noe
plot(macd, color=color.blue)
plot(signal, color=color.orange)
// bgcolor(color = delta > 0.1 ? color.new(color.green, 90) : color.new(color.green, 100))
// bgcolor(color = signalCrossover ? color.purple : signalCrossunder ? color.aqua : color.new(color.green, 100))
histogramColor = delta > 0 ? (previousDelta < delta ? up1 : up2) : (previousDelta > delta ? down1 : down2)
plot(
delta,
style=plot.style_columns,
color=histogramColor
)