GitHub で見る

ColorMETRO XRSX戦略

この戦略は、オリジナルのMQL5エキスパートアドバイザー「Exp_ColorMETRO_XRSX」にインスパイアされたStockSharp実装です。2本の平滑化移動平均を使用してトレンドの変化を検出します。速い平均が遅い平均を上抜けたときにロングポジションを建て、速い平均が遅い平均を下抜けたときにショートポジションを建てます。

パラメーター

  • Fast Period – 速い移動平均の期間。
  • Slow Period – 遅い移動平均の期間。
  • Candle Type – 計算に使用するローソク足の時間軸。

動作の仕組み

  1. 戦略は選択したローソク足シリーズをサブスクライブします。
  2. 終値に対して異なる期間の2つのSmaインジケーターが計算されます。
  3. 速いSMAが遅いSMAを上抜けると、ショートポジションがあればクローズし、ロングポジションを建てます。
  4. 速いSMAが遅いSMAを下抜けると、ロングポジションがあればクローズし、ショートポジションを建てます。
  5. クロスを一度だけ検出するため、平均の前回値が保存されます。

注意事項

  • 戦略はインジケーター処理にBindを使用した高レベルAPIを採用しています。
  • StartProtectionが有効化され、保護メカニズムを管理します。
  • この実装は簡略化された変換であり、オリジナルのカスタムインジケーターは使用していません。
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy based on the cross of two smoothed lines inspired by the ColorMETRO XRSX indicator.
/// Opens a long position when the fast line crosses above the slow line and
/// opens a short position when the fast line crosses below the slow line.
/// </summary>
public class ColorMetroXrsxStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevFast;
	private decimal? _prevSlow;

	/// <summary>
	/// Fast moving average period.
	/// </summary>
	public int FastPeriod
	{
		get => _fastPeriod.Value;
		set => _fastPeriod.Value = value;
	}

	/// <summary>
	/// Slow moving average period.
	/// </summary>
	public int SlowPeriod
	{
		get => _slowPeriod.Value;
		set => _slowPeriod.Value = value;
	}

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

	/// <summary>
	/// Initializes a new instance of the <see cref="ColorMetroXrsxStrategy"/> class.
	/// </summary>
	public ColorMetroXrsxStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast moving average length", "Parameters")
			;

		_slowPeriod = Param(nameof(SlowPeriod), 30)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow moving average length", "Parameters")
			;

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

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

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

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

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fast, slow, ProcessCandle)
			.Start();

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

	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (_prevFast is decimal prevFast && _prevSlow is decimal prevSlow)
		{
			if (prevFast <= prevSlow && fast > slow && Position <= 0)
				BuyMarket();

			if (prevFast >= prevSlow && fast < slow && Position >= 0)
				SellMarket();
		}

		_prevFast = fast;
		_prevSlow = slow;
	}
}