Auf GitHub ansehen

ColorMETRO-Strategie

Diese Strategie handelt auf Basis des ColorMETRO-Indikators, der schnelle und langsame Stufenlinien um den RSI aufbaut. Eine Long-Position wird eröffnet, wenn die schnelle Linie die langsame Linie von unten kreuzt. Eine Short-Position wird eröffnet, wenn die schnelle Linie die langsame Linie von oben kreuzt. Entgegengesetzte Positionen werden bei denselben Signalen geschlossen.

Parameter

  • Candle Type – Kerzentyp für Berechnungen.
  • RSI Period – Periode für die RSI-Berechnung.
  • Fast Step – Schrittgröße für die schnelle Linie.
  • Slow Step – Schrittgröße für die langsame Linie.
  • Stop Loss – Abstand in Punkten für den Stop-Loss-Schutz.
  • Take Profit – Abstand in Punkten für den Take-Profit-Schutz.
  • Allow Buy – Erlaubnis zum Öffnen von Long-Positionen.
  • Allow Sell – Erlaubnis zum Öffnen von Short-Positionen.
  • Close Long – Erlaubnis zum Schließen von Long-Positionen.
  • Close Short – Erlaubnis zum Schließen von Short-Positionen.

Die Strategie verwendet StartProtection zur Verwaltung von Stop-Loss- und Take-Profit-Niveaus.

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>
/// ColorMETRO strategy using RSI crossover with 50 level.
/// Buys when RSI crosses above 50, sells when below 50.
/// </summary>
public class ColorMetroStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevRsi;

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

	public ColorMetroStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "RSI calculation period", "Indicator");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevRsi = null;
	}

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

		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };

		SubscribeCandles(CandleType)
			.Bind(rsi, ProcessCandle)
			.Start();
	}

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

		if (_prevRsi is null)
		{
			_prevRsi = rsiValue;
			return;
		}

		var crossUp = _prevRsi < 50m && rsiValue > 50m;
		var crossDown = _prevRsi > 50m && rsiValue < 50m;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevRsi = rsiValue;
	}
}