在 GitHub 上查看

MADX-07 ADX MA 策略

该策略由 MQL4 平台的 MADX-07 EA 转换而来,使用 H4 K 线,将两条移动平均线与平均趋向指数 (ADX) 结合作为过滤器。

逻辑

  • 做多:价格高于慢速 MA,快速 MA 高于慢速 MA,最近两根 K 线的价格至少比快速 MA 高 MaDifference 点,ADX 上升并高于 AdxMainLevel,+DI 上升,-DI 下降。
  • 做空:条件相反。
  • 当盈利达到 CloseProfit 点或挂单在 TakeProfit 距离被执行时,仓位将平仓。

参数

  • BigMaPeriod (25) – 慢速 MA 的周期。
  • BigMaType – 慢速 MA 的类型。
  • SmallMaPeriod (5) – 快速 MA 的周期。
  • SmallMaType – 快速 MA 的类型。
  • MaDifference (5) – 价格与快速 MA 之间的最小距离(点)。
  • AdxPeriod (11) – ADX 计算周期。
  • AdxMainLevel (13) – ADX 的最小值。
  • AdxPlusLevel (13) – +DI 的最小值。
  • AdxMinusLevel (14) – -DI 的最小值。
  • TakeProfit (299) – 止盈距离(点)。
  • CloseProfit (13) – 提前平仓的利润点数。
  • Volume (0.1) – 交易量。
  • CandleType – K 线时间框(默认 H4)。
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>
/// Strategy combining EMA crossover for trend following.
/// </summary>
public class Madx07AdxMaStrategy : Strategy
{
	private readonly StrategyParam<int> _bigMaPeriod;
	private readonly StrategyParam<int> _smallMaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevSmall;
	private decimal _prevBig;
	private bool _hasPrev;

	public int BigMaPeriod { get => _bigMaPeriod.Value; set => _bigMaPeriod.Value = value; }
	public int SmallMaPeriod { get => _smallMaPeriod.Value; set => _smallMaPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public Madx07AdxMaStrategy()
	{
		_bigMaPeriod = Param(nameof(BigMaPeriod), 25)
			.SetGreaterThanZero()
			.SetDisplay("Big MA Period", "Period of the slower MA", "MA");

		_smallMaPeriod = Param(nameof(SmallMaPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Small MA Period", "Period of the faster MA", "MA");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevSmall = 0;
		_prevBig = 0;
		_hasPrev = false;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var bigMa = new ExponentialMovingAverage { Length = BigMaPeriod };
		var smallMa = new ExponentialMovingAverage { Length = SmallMaPeriod };

		SubscribeCandles(CandleType)
			.Bind(bigMa, smallMa, ProcessCandle)
			.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal bigMaVal, decimal smallMaVal)
	{
		if (candle.State != CandleStates.Finished) return;

		if (!_hasPrev)
		{
			_prevSmall = smallMaVal;
			_prevBig = bigMaVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevSmall <= _prevBig && smallMaVal > bigMaVal;
		var crossDown = _prevSmall >= _prevBig && smallMaVal < bigMaVal;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevSmall = smallMaVal;
		_prevBig = bigMaVal;
	}
}