MACD Crossover with Signal Strategy

Author: ChaoZhang, Date: 2023-12-13 16:58:15
Tags:

img

Overview

The MACD Crossover with Signal strategy generates trading signals when the MACD crosses above or below the signal line. The strategy combines the idea of double moving averages to capture the turning points of medium-term trends in stock prices, belonging to a typical trailing stop loss strategy.

Strategy Principle

The strategy first calculates the fast moving average line fastMA and the slow moving average line slowMA. The fast moving average parameter is 12 days, and the slow moving average parameter is 26 days. Then calculate the difference between the two moving average lines to form the MACD. Then calculate the 9-day moving average of the MACD to get the signal line. Trading signals are generated when the MACD crosses above or below the signal line.

The advantage of the strategy is to capture the turning point of the medium-term trend of stock prices. The combination of fast and slow moving averages filters out short-term price fluctuations and noise, and can capture medium-term price trends. When the stock price undergoes a medium-term reversal, the MACD will break through the signal line and generate relatively clear trading signals.

Advantage Analysis

The MACD Crossover with Signal strategy combines the idea of double moving averages to filter out short-term noise and only capture the turning points of long and medium-term trends. Compared with a single price indicator, it can generate fewer false signals.

MACD itself is more sensitive and can respond sensitively to price trend changes. The addition of the signal line can filter out more short-term false signals. Only when the medium-term trend changes significantly, the MACD breaks through the signal line up and down, will a signal be generated.

In a sustainable uptrend, the MACD maintains above the signal line most of the time, which can capture multiple opportunities along the way. Similarly, in a sustained downtrend, the MACD can also maintain a long/short pattern and give short signals in a timely manner.

Risk Analysis

Since the strategy buy and sell signals rely entirely on the crossover of the moving averages, if the market fluctuates greatly, more false signals will be generated, resulting in frequent stop loss. The actual profit and loss of the strategy may not meet expectations.

Breaking through the signal line does not necessarily ensure that the medium-term trend has changed. Relying solely on a single technical indicator as a buy signal has a certain blindness, and the timing may not be accurate enough.

In the ever-changing market, using the crossover of double moving averages alone as the threshold may miss more trading opportunities. In more complex strong trends, this strategy will also lag significantly.

Optimization Directions

  1. Add liquidity and volatility filtering indicators to reduce opening frequency and avoid ineffective trading. Such as adding trading volume, dynamically adjusting moving average parameters, etc.

  2. Combine multiple other technical indicators to form an indicator portfolio to improve signal quality. The combination of short-term and medium and long-term indicators can more comprehensively judge the market structure.

  3. Add machine learning algorithms to train parameters and attention thresholds that are more suitable for the current market environment, reducing human intervention.

  4. Combine VIX and other fear indices to predict market trends and volatility, and dynamically adjust parameters to better take advantage of MACD opportunities.

Conclusion

The MACD Crossover with Signal strategy uses the principle of double moving average crossover to generate trading signals. Drawing the price graph by the difference between fast and slow moving averages, medium-term price trend changes will have obvious characteristics. The addition of the signal line also effectively filters out some noisy signals. The strategy has some advantages in capturing relatively clear medium-term trend reversals. However, the crossover of MACD and signal line cannot fully determine the fundamental change in market structure, and trading signals need to be treated with caution. It is recommended to use in combination with other technical indicators, and machine learning optimization can also be considered.


/*backtest
start: 2022-12-06 00:00:00
end: 2023-12-12 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@author : SudeepBisht
//@version=2
strategy(title="MACD with Signal", shorttitle="MACD_with_Signal")
source = close
useCurrentRes = input(true, title="Use Current Chart Resolution?")
resCustom = input(title="Use Different Timeframe? Uncheck Box Above", defval="60")
smd = input(true, title="Show MacD & Signal Line? Also Turn Off Dots Below")
sd = input(true, title="Show Dots When MacD Crosses Signal Line?")
sh = input(true, title="Show Histogram?")
macd_colorChange = input(true,title="Change MacD Line Color-Signal Line Cross?")
hist_colorChange = input(true,title="MacD Histogram 4 Colors?")

res = useCurrentRes ? timeframe.period : resCustom

fastLength = input(12, minval=1), slowLength=input(26,minval=1)
signalLength=input(9,minval=1)

fastMA = ema(source, fastLength)
slowMA = ema(source, slowLength)

macd = fastMA - slowMA
signal = sma(macd, signalLength)
hist = macd - signal

outMacD = request.security(syminfo.tickerid, res, macd)
outSignal = request.security(syminfo.tickerid, res, signal)
outHist = request.security(syminfo.tickerid, res, hist)

histA_IsUp = outHist > outHist[1] and outHist > 0
histA_IsDown = outHist < outHist[1] and outHist > 0
histB_IsDown = outHist < outHist[1] and outHist <= 0
histB_IsUp = outHist > outHist[1] and outHist <= 0

//MacD Color Definitions
macd_IsAbove = outMacD >= outSignal
macd_IsBelow = outMacD < outSignal

plot_color = hist_colorChange ? histA_IsUp ? green : histA_IsDown ? lime : histB_IsDown ? red : histB_IsUp ? maroon :yellow :gray
macd_color = macd_colorChange ? macd_IsAbove ? green : red : red
signal_color = macd_colorChange ? macd_IsAbove ? yellow : yellow : lime

circleYPosition = outSignal
 
plot(smd and outMacD ? outMacD : na, title="MACD", color=macd_color, linewidth=3)
plot(smd and outSignal ? outSignal : na, title="Signal", color=blue, style=line ,linewidth=1)
plot(sh and outHist ? outHist : na, title="", color=plot_color, style=columns, linewidth=4)
//plot(sd and cross(outMacD, outSignal) ? circleYPosition : na, title="Cross", style=circles, linewidth=4, color=macd_color)
hline(0, '0 Line',  linewidth=2, color=white)

macd_chk=smd and outMacD ? outMacD : na
checker=smd and outSignal ? outSignal : na
if (crossover(macd_chk,checker))
    strategy.entry("BBandLE", strategy.long)

if (crossunder(macd_chk, checker))
    strategy.entry("BBandSE", strategy.short)


More