GitHub で見る

定向運動戦略

概要

この戦略はMetaTraderのDirected Movementエキスパートアドバイザーを再現します。移動平均で2回平滑化された相対力指数(RSI)を適用します。最初の平滑化が高速ラインを形成し、2回目の平滑化がより遅いラインを作成します。

トレードの意思決定は、高速ラインと低速ラインの逆張りクロスオーバーに基づいています:

  • 買い:高速ラインが低速ラインを下抜けしたとき。
  • 売り:高速ラインが低速ラインを上抜けしたとき。

オプションのストップロスとテイクプロフィットレベルはエントリー価格のパーセンテージとして適用されます。

インジケーター

  • RelativeStrengthIndex – 基本モメンタムインジケーター。
  • MovingAverage – RSIの最初の平滑化(高速ライン)。
  • MovingAverage – 高速ラインの2回目の平滑化(低速ライン)。

トレードルール

  1. ローソク足の終値からRSIを計算します。
  2. 最初の移動平均でRSIを平滑化して高速ラインを取得します。
  3. 2番目の移動平均で高速ラインを平滑化して低速ラインを取得します。
  4. 高速ラインが低速ラインを下抜けしたときにロングポジションを建てます。新しいロングを建てる前に既存のショートポジションをクローズします。
  5. 高速ラインが低速ラインを上抜けしたときにショートポジションを建てます。新しいショートを建てる前に既存のロングポジションをクローズします。
  6. パラメーターがゼロより大きい場合、ストップロスとテイクプロフィットの保護を適用します。

パラメーター

名前 説明
CandleType 計算に使用するローソク足シリーズ。
RsiPeriod RSI計算期間。
FirstMaType 高速ラインに使用する移動平均の種類。
FirstMaLength 高速移動平均の期間。
SecondMaType 低速ラインに使用する移動平均の種類。
SecondMaLength 低速移動平均の期間。
StopLossPercent エントリー価格のパーセンテージでのストップロス。
TakeProfitPercent エントリー価格のパーセンテージでのテイクプロフィット。
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>
/// Directed Movement Strategy - RSI cross system with two MA smoothing.
/// </summary>
public class DirectedMovementStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<int> _fastMaLength;
	private readonly StrategyParam<int> _slowMaLength;

	private ExponentialMovingAverage _fastMa;
	private ExponentialMovingAverage _slowMa;
	private decimal _prevFast;
	private decimal _prevSlow;

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
	public int FastMaLength { get => _fastMaLength.Value; set => _fastMaLength.Value = value; }
	public int SlowMaLength { get => _slowMaLength.Value; set => _slowMaLength.Value = value; }

	public DirectedMovementStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");

		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetDisplay("RSI Period", "RSI calculation period", "Indicators");

		_fastMaLength = Param(nameof(FastMaLength), 12)
			.SetDisplay("Fast MA Length", "Period of fast moving average", "Indicators");

		_slowMaLength = Param(nameof(SlowMaLength), 5)
			.SetDisplay("Slow MA Length", "Period of slow moving average", "Indicators");
	}

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

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

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

		_prevFast = 0m;
		_prevSlow = 0m;

		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
		_fastMa = new ExponentialMovingAverage { Length = FastMaLength };
		_slowMa = new ExponentialMovingAverage { Length = SlowMaLength };

		Indicators.Add(_fastMa);
		Indicators.Add(_slowMa);

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

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

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

		var t = candle.ServerTime;

		var fastResult = _fastMa.Process(new DecimalIndicatorValue(_fastMa, rsiValue, t) { IsFinal = true });
		if (!_fastMa.IsFormed)
			return;

		var fast = fastResult.GetValue<decimal>();

		var slowResult = _slowMa.Process(new DecimalIndicatorValue(_slowMa, fast, t) { IsFinal = true });
		if (!_slowMa.IsFormed)
		{
			_prevFast = fast;
			_prevSlow = fast;
			return;
		}

		var slow = slowResult.GetValue<decimal>();

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

		// Crossover: fast crosses below slow -> buy
		if (_prevFast > _prevSlow && fast <= slow && Position <= 0)
			BuyMarket();
		// Crossover: fast crosses above slow -> sell
		else if (_prevFast < _prevSlow && fast >= slow && Position >= 0)
			SellMarket();

		_prevFast = fast;
		_prevSlow = slow;
	}
}