GitHub で見る

MADX-07 ADX MA 戦略

この戦略はMQL4エキスパートアドバイザーMADX-07から変換されました。H4ローソク足で取引し、2本の移動平均線とフィルターとしてのAverage Directional Index (ADX)を組み合わせています。

ロジック

  • ロングエントリー: 価格が遅いMAの上にあり、速いMAが遅いMAの上にあり、直近2本のローソク足で価格が速い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 – ローソク足の時間軸(デフォルトは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;
	}
}