GitHub で見る

シンプル・マルチタイムフレーム移動平均戦略

この戦略は simple_multiple_time_frame_moving_average.mq4 のロジックを再現します。2つの時間軸にわたってシンプル移動平均を使用することでトレンドを一致させます。

戦略ロジック

  • 1時間足と4時間足のローソク足に対して期間 Length のSMAを計算します。
  • 両方のSMAが上昇しているときにロングエントリーします。
  • 両方のSMAが下落しているときにショートエントリーします。
  • どちらかのSMAが下向きに転換した場合、ロングポジションをクローズします。
  • どちらかのSMAが上向きに転換した場合、ショートポジションをクローズします。
  • 同時にアクティブにできるポジションは1つだけです。

パラメーター

  • MA Length (Length): 両方の移動平均に使用する期間。
  • Short Time Frame (ShortCandleType): 最初のSMAの時間軸(デフォルト: 1時間)。
  • Long Time Frame (LongCandleType): 2番目のSMAの時間軸(デフォルト: 4時間)。

取引数量は戦略の Volume プロパティから取得します。

注意事項

この実装は、オリジナルのMQLバージョンで使用されている1時間足と4時間足の平均に焦点を当てており、使用されていない上位時間軸の計算は省略しています。

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 using fast and slow SMA slopes for trade direction.
/// Buys when both SMAs are rising, sells when both are falling.
/// </summary>
public class SimpleMultipleTimeFrameMovingAverageStrategy : 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 SimpleMultipleTimeFrameMovingAverageStrategy()
	{
		_fastLength = Param(nameof(FastLength), 5)
			.SetDisplay("Fast MA", "Fast moving average period", "General");

		_slowLength = Param(nameof(SlowLength), 20)
			.SetDisplay("Slow MA", "Slow moving average period", "General");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = _prevSlow = null;
	}

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

		var fastSma = new ExponentialMovingAverage { Length = FastLength };
		var slowSma = new ExponentialMovingAverage { Length = SlowLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fastSma, slowSma, ProcessCandle)
			.Start();

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevFast = fast;
			_prevSlow = slow;
			return;
		}

		if (_prevFast is decimal pf && _prevSlow is decimal ps)
		{
			var fastUp = fast > pf;
			var fastDown = fast < pf;
			var slowUp = slow > ps;
			var slowDown = slow < ps;

			if (fastUp && slowUp && Position <= 0)
				BuyMarket();
			else if (fastDown && slowDown && Position >= 0)
				SellMarket();
		}

		_prevFast = fast;
		_prevSlow = slow;
	}
}