
The MACD Trend Following Strategy is a quantitative trading strategy based on the MACD indicator. This strategy identifies MACD golden cross and death cross signals to determine market trends and track price trends.
The core logic of the MACD Trend Following Strategy is:
Through this trend following mechanism, the strategy can timely capture turns of market trends and make profits.
The MACD Trend Following Strategy has the following advantages:
The MACD Trend Following Strategy also has the following risks:
To address the above risks, the following optimization measures can be adopted:
The MACD Trend Following Strategy can be optimized in the following aspects:
Optimize MACD indicator parameters to reduce false signal rate. Different cycle parameters of MACD can be tested.
Add other indicators like trading volume to filter out signals. Minimum trading volume conditions can be set.
Set up dynamic trailing stop loss mechanism. Stop loss points can be adjusted dynamically based on volatility.
Optimize the signal determination logic for opening positions. More rigorous trigger conditions can be set.
Incorporate machine learning models to filter out signals. Models can be trained to judge reliability of signals.
In general, the MACD Trend Following Strategy is a relatively mature quantitative strategy. It utilizes the MACD indicator to determine market trend directions, and controls risks with stop loss mechanism, which can effectively track price trends. But the MACD indicator itself also has some flaws, easy to generate false signals. So there are rooms for further optimization of this strategy, mainly on aspects like indicator parameters, stop loss mechanism, signal filtering etc.
/*backtest
start: 2023-11-10 00:00:00
end: 2023-12-10 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("MACD Cross Strategy", overlay=true)
// Get MACD values
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)
var float entryLongPrice = na
var float entryShortPrice = na
var float highestLongProfit = 0
var float highestShortProfit = 0
var float highestMACD = 0
var float lowestMACD = 0
var bool haveOpenedLong = false
var bool haveOpenedShort = false
var float stoploss = 0.04 // To be adjust for different investment
var float minProfit = 0.05 // To be adjust for different investment
if macdLine > 0
lowestMACD := 0
highestMACD := math.max(highestMACD, macdLine)
haveOpenedShort := false
else
highestMACD := 0
lowestMACD := math.min(lowestMACD, macdLine)
haveOpenedLong := false
// Enter long position when MACD line crosses above the signal line
if ta.crossover(macdLine, signalLine) and macdLine < highestMACD and macdLine > 0 and haveOpenedLong == false
strategy.entry("Long", strategy.long)
strategy.exit("Exit Long", from_entry = "Long", stop=close*(1 - stoploss))
entryLongPrice := close
haveOpenedLong := true
if ta.crossunder(macdLine, signalLine) and macdLine > lowestMACD and macdLine < 0 and haveOpenedShort == false
strategy.entry("Short", strategy.short)
strategy.exit("Exit Short", from_entry = "Short", stop=close*(1 + stoploss))
entryShortPrice := close
haveOpenedShort := true
// log.info("entryLongPrice:{0}", entryLongPrice)
if strategy.position_size > 0
profit = close - entryLongPrice
log.info("profit:{0}", profit)
if profit > 0
highestLongProfit := math.max(highestLongProfit, profit)
if profit / entryLongPrice > minProfit and highestLongProfit * 0.8 > profit
strategy.close("Long")
highestLongProfit := 0
if strategy.position_size < 0
profit = entryShortPrice - close
if profit > 0
highestShortProfit := math.max(highestShortProfit, profit)
log.info("highestShortProfit={0}, profit={1}", highestShortProfit, profit)
if profit / entryShortPrice > minProfit and highestShortProfit * 0.8 > profit
strategy.close("Short")
highestShortProfit := 0