GitHub で見る

ColorX2MA Digit戦略

この戦略はMQL5エキスパート Exp_ColorX2MA_Digit のポートです。 元のアルゴリズムは、二重平滑化移動平均線をその傾きに応じて異なる色で描画し、その色を取引シグナルの生成に使用します。 このC#バージョンでは、2本の単純移動平均線でその動作を近似し、それらのクロスオーバーで取引を行います。

取引ロジック

  • 速い移動平均線が価格系列を平滑化します。
  • 遅い移動平均線が速い移動平均の結果を平滑化します。
  • 速い平均線が遅い平均線を上抜けると、戦略はロングポジションを開き、既存のショートポジションを決済します。
  • 速い平均線が遅い平均線を下抜けると、戦略はショートポジションを開き、既存のロングポジションを決済します。
  • シグナルは足が確定した後にのみ処理されます。

パラメーター

  • FastLength – 最初の平滑化の長さ(デフォルト12)。
  • SlowLength – 2番目の平滑化の長さ(デフォルト5)。
  • CandleType – 計算に使用する足の時間軸。

この戦略は高水準APIのみを使用します:インジケーターにデータを供給するために SubscribeCandlesBind を使用し、ポジション管理には BuyMarket/SellMarket を使用します。コード内のコメントはメンテナンスを容易にするために英語で記述されています。

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>
/// Trend following strategy based on two sequential moving averages.
/// Trades on crossovers of fast and slow SMAs.
/// </summary>
public class ColorX2MaDigitStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevFast;
	private decimal? _prevSlow;

	public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
	public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ColorX2MaDigitStrategy()
	{
		_fastLength = Param(nameof(FastLength), 8)
			.SetGreaterThanZero()
			.SetDisplay("Fast MA Length", "Length of the first smoothing", "Parameters");

		_slowLength = Param(nameof(SlowLength), 21)
			.SetGreaterThanZero()
			.SetDisplay("Slow MA Length", "Length of the second smoothing", "Parameters");

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

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

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

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

		_prevFast = null;
		_prevSlow = null;

		var fastMa = new ExponentialMovingAverage { Length = FastLength };
		var slowMa = new ExponentialMovingAverage { Length = SlowLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fastMa, slowMa, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, fastMa);
			DrawIndicator(area, slowMa);
			DrawOwnTrades(area);
		}
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (_prevFast is null || _prevSlow is null)
		{
			_prevFast = fastMa;
			_prevSlow = slowMa;
			return;
		}

		var wasAbove = _prevFast > _prevSlow;
		var isAbove = fastMa > slowMa;

		// Fast MA crossed above slow MA -> buy
		if (!wasAbove && isAbove && Position <= 0)
			BuyMarket();
		// Fast MA crossed below slow MA -> sell
		else if (wasAbove && !isAbove && Position >= 0)
			SellMarket();

		_prevFast = fastMa;
		_prevSlow = slowMa;
	}
}