GitHub で見る

MA Rounding Candle戦略

概要

この戦略は、MQL5のエキスパートアドバイザー「MA Rounding Candle」のオリジナルを解釈したものです。ローソク足の始値と終値に適用された2本の平滑化移動平均を使用します。これらの平均の相対的な位置が合成ローソク足の色を決定します。平滑化した終値が始値より高い場合は緑、低い場合は赤、等しい場合は灰色です。前のバーからの色の変化がトレードシグナルを生成します。

アルゴリズム

  1. 完成したすべてのローソク足に対して、始値と終値を設定可能な長さの単純移動平均で平滑化します。
  2. ローソク足の色は平滑化値を比較して決定されます:
    • 上昇ローソク足 – 平滑化終値が平滑化始値より高い。
    • 下降ローソク足 – 平滑化終値が平滑化始値より低い。
    • 中立 – 両方の値が等しい。
  3. 前のローソク足が上昇で、現在のローソク足が上昇でない場合、戦略はロングポジションを建て、ショートポジションをすべて決済します。
  4. 前のローソク足が下降で、現在のローソク足が下降でない場合、戦略はショートポジションを建て、ロングポジションをすべて決済します。

パラメーター

  • MaLength – 平滑化移動平均の期間(デフォルト12)。
  • CandleType – 処理するローソク足の時間軸。

備考

この戦略は、StockSharpの組み込みツールのみを使用してカスタムインジケーターのシグナルを再現する方法を示しています。ストップロスやテイクプロフィットは適用されません。逆シグナルが現れると即座にポジションが反転します。

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>
/// MA Rounding Candle strategy.
/// Opens a long position when a smoothed candle is bullish and a short position when it is bearish.
/// </summary>
public class MaRoundingCandleStrategy : Strategy
{
	private readonly StrategyParam<int> _maLength;
	private readonly StrategyParam<DataType> _candleType;

	private ExponentialMovingAverage _openMa;
	private ExponentialMovingAverage _closeMa;
	private int _prevColor = 1;

	public MaRoundingCandleStrategy()
	{
		_maLength = Param(nameof(MaLength), 12)
			.SetDisplay("MA Length", "Moving average length", "Parameters");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame());
	}

	public int MaLength { get => _maLength.Value; set => _maLength.Value = value; }

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_openMa = default;
		_closeMa = default;
		_prevColor = 1;
	}

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

		_openMa = new ExponentialMovingAverage { Length = MaLength };
		_closeMa = new ExponentialMovingAverage { Length = MaLength };

		Indicators.Add(_openMa);
		Indicators.Add(_closeMa);

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

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

		StartProtection(null, null);
	}

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

		var openVal = _openMa.Process(candle.OpenPrice, candle.OpenTime, true).ToDecimal();
		var closeVal = _closeMa.Process(candle.ClosePrice, candle.OpenTime, true).ToDecimal();

		if (!_openMa.IsFormed || !_closeMa.IsFormed)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var color = openVal < closeVal ? 2 : openVal > closeVal ? 0 : 1;

		if (_prevColor == 2 && color != 2 && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (_prevColor == 0 && color != 0 && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevColor = color;
	}
}