移动平均线金叉死叉交易策略是一种追踪短期和长期移动平均线(EMA)的交叉情况,在金叉和死叉时进行买入和卖出操作的量化交易策略。该策略结合MACD指标进行交易信号判断。
该策略主要依赖12日EMA、26日EMA和MACD指标。具体逻辑是:
另外,该策略还设置了一些过滤条件:
该策略结合移动平均线交叉和MACD指标,可以有效捕捉市场短期和中期趋势的转折点。主要优势有:
该策略也存在一些风险:
对应缓解方法:
该策略主要可从以下方面进行优化:
移动平均线金叉死叉结合MACD交易策略,通过简单的趋势跟踪形成交易信号,易于实现,并结合适当的过滤条件控制风险,是一种行之有效的量化交易策略。该策略可通过参数优化、止损机制增加、结合更多辅助指标等方式得到改进。
/*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 )