
MACD趋势跟踪策略是一种基于MACD指标的量化交易策略。该策略通过识别MACD指标金叉和死叉信号,来判断市场趋势,实现追踪股价趋势。
MACD趋势跟踪策略的核心逻辑是:
通过这种趋势跟踪机制,该策略能及时捕捉市场趋势转折,实现盈利。
MACD趋势跟踪策略具有以下优势:
MACD趋势跟踪策略也存在以下风险:
针对上述风险,可以采取以下优化措施:
MACD趋势跟踪策略可以从以下方面进行优化:
优化MACD指标参数,降低虚假信号率。可以测试不同周期参数的MACD。
增加成交量等其他指标过滤信号。可以设置最小成交量条件。
设置动态追踪止损机制。可以根据波动率实时调整止损点。
优化打开头寸的信号判定逻辑。可以设置更严格的信号触发条件。
结合机器学习模型过滤信号。可以训练模型判断信号的可靠性。
MACD趋势跟踪策略整体而言是一种较为成熟的量化策略。该策略利用MACD指标判断市场趋势方向,配合止损机制控制风险,能够有效跟踪股价趋势。但MACD指标本身也存在一定缺陷,容易产生虚假信号。因此该策略还有进一步优化的空间,主要集中在指标参数、止损机制、信号过滤等方面。
/*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