在 GitHub 上查看

Macd Momentum Reversal 策略

该策略利用MACD柱状图检测动量反转。 当阳线实体增大但MACD柱下降时做空。 当阴线实体增大但MACD柱上升时做多。

细节

  • 入场条件:较大的蜡烛实体与减弱的MACD动量。
  • 多空:双向。
  • 出场条件:相反信号。
  • 止损:无。
  • 默认值:
    • FastLength = 12
    • SlowLength = 26
    • SignalLength = 9
    • CandleType = TimeSpan.FromMinutes(1)
  • 筛选:
    • 分类:动量
    • 方向:双向
    • 指标:MACD
    • 止损:无
    • 复杂度:基础
    • 时间框架:日内 (1m)
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:中等
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 momentum reversal strategy.
/// A bullish candle with a larger body than the previous candle while the MACD histogram declines goes short; a bearish candle with a
/// larger body than the previous candle while the histogram rises goes long. An opposite signal reverses the position.
/// </summary>
public class MacdMomentumReversalStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<int> _signalLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevBody;
	private decimal? _prevHist;

	/// <summary>
	/// Fast EMA length of MACD.
	/// </summary>
	public int FastLength
	{
		get => _fastLength.Value;
		set => _fastLength.Value = value;
	}

	/// <summary>
	/// Slow EMA length of MACD.
	/// </summary>
	public int SlowLength
	{
		get => _slowLength.Value;
		set => _slowLength.Value = value;
	}

	/// <summary>
	/// Signal line length of MACD.
	/// </summary>
	public int SignalLength
	{
		get => _signalLength.Value;
		set => _signalLength.Value = value;
	}

	/// <summary>
	/// Candle type.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Constructor.
	/// </summary>
	public MacdMomentumReversalStrategy()
	{
		_fastLength = Param(nameof(FastLength), 12)
			.SetGreaterThanZero()
			.SetDisplay("Fast Length", "Fast EMA length of MACD", "MACD");

		_slowLength = Param(nameof(SlowLength), 26)
			.SetGreaterThanZero()
			.SetDisplay("Slow Length", "Slow EMA length of MACD", "MACD");

		_signalLength = Param(nameof(SignalLength), 9)
			.SetGreaterThanZero()
			.SetDisplay("Signal Length", "Signal line length of MACD", "MACD");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevBody = null;
		_prevHist = null;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		_prevBody = null;
		_prevHist = null;

		var macd = new MovingAverageConvergenceDivergenceSignal
		{
			Macd =
			{
				ShortMa = { Length = FastLength },
				LongMa = { Length = SlowLength },
			},
			SignalMa = { Length = SignalLength }
		};

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(macd, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawOwnTrades(area);

			var oscillators = CreateChartArea();
			if (oscillators != null)
				DrawIndicator(oscillators, macd);
		}
	}

	private void ProcessCandle(ICandleMessage candle, IIndicatorValue macdValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		var body = Math.Abs(candle.ClosePrice - candle.OpenPrice);
		var prevBody = _prevBody;
		_prevBody = body;

		if (!macdValue.IsFormed || macdValue is not MovingAverageConvergenceDivergenceSignalValue { Macd: decimal macd, Signal: decimal signal })
			return;

		var hist = macd - signal;
		var prevHist = _prevHist;
		_prevHist = hist;

		if (prevBody is not decimal pb || prevHist is not decimal ph)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var grows = body > pb;

		if (grows && candle.ClosePrice > candle.OpenPrice && hist < ph && Position >= 0)
			SellMarket(Volume + Math.Abs(Position));
		else if (grows && candle.ClosePrice < candle.OpenPrice && hist > ph && Position <= 0)
			BuyMarket(Volume + Math.Abs(Position));
	}
}