Auf GitHub ansehen

Gerichtete-Bewegung-Strategie

Überblick

Diese Strategie repliziert den Expertenberater Directed Movement aus MetaTrader. Sie wendet einen Relative Strength Index (RSI) an, der zweimal durch gleitende Durchschnitte geglättet wird. Die erste Glättung bildet eine schnelle Linie, während die zweite Glättung eine langsamere Linie erzeugt.

Handelsentscheidungen basieren auf dem Kreuzungsverhalten der schnellen und langsamen Linie im konträren Stil:

  • Kaufen, wenn die schnelle Linie unter die langsame Linie kreuzt.
  • Verkaufen, wenn die schnelle Linie über die langsame Linie kreuzt.

Optionale Stop-Loss- und Take-Profit-Niveaus werden als Prozentsätze des Einstiegspreises angewendet.

Indikatoren

  • RelativeStrengthIndex – Basis-Momentum-Indikator.
  • MovingAverage – erste Glättung des RSI (schnelle Linie).
  • MovingAverage – zweite Glättung der schnellen Linie (langsame Linie).

Handelsregeln

  1. RSI aus Kerzenschlusskursen berechnen.
  2. RSI mit dem ersten gleitenden Durchschnitt glätten, um die schnelle Linie zu erhalten.
  3. Schnelle Linie mit dem zweiten gleitenden Durchschnitt glätten, um die langsame Linie zu erhalten.
  4. Long-Position eingehen, wenn die schnelle Linie unter die langsame Linie kreuzt. Vorher bestehende Short-Position schließen.
  5. Short-Position eingehen, wenn die schnelle Linie über die langsame Linie kreuzt. Vorher bestehende Long-Position schließen.
  6. Stop-Loss- und Take-Profit-Schutz anwenden, wenn ihre Parameter größer als null sind.

Parameter

Name Beschreibung
CandleType Für Berechnungen verwendete Kerzenserie.
RsiPeriod RSI-Berechnungsperiode.
FirstMaType Art des gleitenden Durchschnitts für die schnelle Linie.
FirstMaLength Periode des schnellen gleitenden Durchschnitts.
SecondMaType Art des gleitenden Durchschnitts für die langsame Linie.
SecondMaLength Periode des langsamen gleitenden Durchschnitts.
StopLossPercent Stop-Loss in Prozent des Einstiegspreises.
TakeProfitPercent Take-Profit in Prozent des Einstiegspreises.
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;
	}
}