MACD 背离
MACD 背离策略寻找价格走势与 MACD 指标之间的不一致。当价格创出更高高点而 MACD 高点却更低时,表明动能减弱(看跌背离);相反,价格创出更低低点而 MACD 低点抬高,则可能预示着看涨反转。
测试表明年均收益约为 70%,该策略在股票市场表现最佳。
发现背离后,系统等待 MACD 上穿其信号线再入场。若 MACD 再次反向穿越或触发止损则平仓。
细节
- 入场条件:出现看涨或看跌背离并且 MACD 穿越信号线。
- 多/空:双向。
- 退出条件:MACD 反向穿越或触发止损。
- 止损:有。
- 默认值:
FastMacdPeriod= 12SlowMacdPeriod= 26SignalPeriod= 9DivergencePeriod= 5CandleType= TimeSpan.FromMinutes(15)StopLossPercent= 2.0m
- 过滤条件:
- 类别: 背离
- 方向: 双向
- 指标: MACD
- 止损: 有
- 复杂度: 中等
- 时间框架: 日内
- 季节性: 无
- 神经网络: 无
- 背离: 是
- 风险级别: 中等
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// MACD Divergence strategy.
/// Detects divergences between price and MACD for reversal signals.
/// Bullish: price falling but MACD rising.
/// Bearish: price rising but MACD falling.
/// </summary>
public class MacdDivergenceStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _cooldownBars;
private decimal _prevPrice;
private decimal _prevMacd;
private int _cooldown;
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Cooldown bars between trades.
/// </summary>
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public MacdDivergenceStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
_cooldownBars = Param(nameof(CooldownBars), 500)
.SetRange(1, 1000)
.SetDisplay("Cooldown Bars", "Bars to wait between trades", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, _candleType.Value)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevPrice = default;
_prevMacd = default;
_cooldown = default;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevPrice = 0;
_prevMacd = 0;
_cooldown = 0;
var macd = new MovingAverageConvergenceDivergenceSignal();
var subscription = SubscribeCandles(_candleType.Value);
subscription
.BindEx(macd, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, macd);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue macdValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!macdValue.IsFormed)
return;
var macdTyped = (MovingAverageConvergenceDivergenceSignalValue)macdValue;
if (macdTyped.Macd is not decimal macdLine || macdTyped.Signal is not decimal signal)
return;
if (_prevPrice == 0)
{
_prevPrice = candle.ClosePrice;
_prevMacd = macdLine;
return;
}
if (_cooldown > 0)
{
_cooldown--;
_prevPrice = candle.ClosePrice;
_prevMacd = macdLine;
return;
}
// Bullish divergence: price down but MACD up
var bullishDiv = candle.ClosePrice < _prevPrice && macdLine > _prevMacd;
// Bearish divergence: price up but MACD down
var bearishDiv = candle.ClosePrice > _prevPrice && macdLine < _prevMacd;
if (Position == 0 && bullishDiv && macdLine > signal)
{
BuyMarket();
_cooldown = CooldownBars;
}
else if (Position == 0 && bearishDiv && macdLine < signal)
{
SellMarket();
_cooldown = CooldownBars;
}
else if (Position > 0 && macdLine < signal)
{
SellMarket();
_cooldown = CooldownBars;
}
else if (Position < 0 && macdLine > signal)
{
BuyMarket();
_cooldown = CooldownBars;
}
_prevPrice = candle.ClosePrice;
_prevMacd = macdLine;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import MovingAverageConvergenceDivergenceSignal
from StockSharp.Algo.Strategies import Strategy
class macd_divergence_strategy(Strategy):
"""
MACD Divergence strategy.
Detects divergences between price and MACD for reversal signals.
Bullish: price falling but MACD rising.
Bearish: price rising but MACD falling.
"""
def __init__(self):
super(macd_divergence_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(1))).SetDisplay("Candle Type", "Type of candles to use", "General")
self._cooldown_bars = self.Param("CooldownBars", 500).SetDisplay("Cooldown Bars", "Bars to wait between trades", "General")
self._prev_price = 0.0
self._prev_macd = 0.0
self._cooldown = 0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(macd_divergence_strategy, self).OnReseted()
self._prev_price = 0.0
self._prev_macd = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(macd_divergence_strategy, self).OnStarted2(time)
self._prev_price = 0.0
self._prev_macd = 0.0
self._cooldown = 0
macd = MovingAverageConvergenceDivergenceSignal()
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(macd, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, macd)
self.DrawOwnTrades(area)
def _process_candle(self, candle, macd_value):
if candle.State != CandleStates.Finished:
return
if not macd_value.IsFormed:
return
macd_line = macd_value.Macd
signal = macd_value.Signal
if macd_line is None or signal is None:
return
macd_f = float(macd_line)
signal_f = float(signal)
close = float(candle.ClosePrice)
if self._prev_price == 0:
self._prev_price = close
self._prev_macd = macd_f
return
if self._cooldown > 0:
self._cooldown -= 1
self._prev_price = close
self._prev_macd = macd_f
return
cd = self._cooldown_bars.Value
# Bullish divergence: price down but MACD up
bullish_div = close < self._prev_price and macd_f > self._prev_macd
# Bearish divergence: price up but MACD down
bearish_div = close > self._prev_price and macd_f < self._prev_macd
if self.Position == 0 and bullish_div and macd_f > signal_f:
self.BuyMarket()
self._cooldown = cd
elif self.Position == 0 and bearish_div and macd_f < signal_f:
self.SellMarket()
self._cooldown = cd
elif self.Position > 0 and macd_f < signal_f:
self.SellMarket()
self._cooldown = cd
elif self.Position < 0 and macd_f > signal_f:
self.BuyMarket()
self._cooldown = cd
self._prev_price = close
self._prev_macd = macd_f
def CreateClone(self):
return macd_divergence_strategy()