Auf GitHub ansehen

ColorMETRO XRSX-Strategie

Diese Strategie ist eine StockSharp-Implementierung, inspiriert vom originalen MQL5 Expert Advisor "Exp_ColorMETRO_XRSX". Sie verwendet zwei geglättete gleitende Durchschnitte, um Trendwechsel zu erkennen. Eine Long-Position wird eröffnet, wenn der schnelle Durchschnitt den langsamen von unten kreuzt, während eine Short-Position eröffnet wird, wenn der schnelle Durchschnitt den langsamen von oben kreuzt.

Parameter

  • Fast Period – Periode des schnellen gleitenden Durchschnitts.
  • Slow Period – Periode des langsamen gleitenden Durchschnitts.
  • Candle Type – Zeitrahmen der für Berechnungen verwendeten Kerzen.

Funktionsweise

  1. Die Strategie abonniert die ausgewählte Kerzenserie.
  2. Zwei Sma-Indikatoren mit unterschiedlichen Perioden werden auf den Schlusskurs berechnet.
  3. Wenn der schnelle SMA den langsamen SMA von unten kreuzt, wird eine offene Short-Position geschlossen und eine Long-Position eröffnet.
  4. Wenn der schnelle SMA den langsamen SMA von oben kreuzt, wird eine offene Long-Position geschlossen und eine Short-Position eröffnet.
  5. Die vorherigen Werte der Durchschnitte werden gespeichert, um Kreuzungen nur einmal zu erkennen.

Hinweise

  • Die Strategie verwendet die High-Level-API mit Bind für die Indikatorverarbeitung.
  • StartProtection ist aktiviert, um Schutzmechanismen zu verwalten.
  • Diese Implementierung ist eine vereinfachte Übersetzung und verwendet nicht den originalen benutzerdefinierten Indikator.
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;
	}
}