Auf GitHub ansehen

Exp Super Trend-Strategie

Strategie konvertiert aus dem MQL-Skript Exp_Super_Trend.mq5 (ID 14269). Sie folgt der Richtung des SuperTrend-Indikators und kehrt Positionen um, sobald der Trend wechselt. Die Implementierung verwendet die High-Level StockSharp API und den integrierten SuperTrend-Indikator.

Der Indikator berechnet eine dynamische Support- oder Widerstandslinie basierend auf ATR. Wenn der Preis über dieser Linie bleibt, gilt der Trend als bullisch, andernfalls als bärisch. Die Strategie eröffnet während bullischer Phasen eine Long-Position und wechselt in bärischen Phasen zur Short-Position. Jeder Indikatorwechsel bewirkt eine sofortige Positionsumkehr.

Dieser Ansatz funktioniert am besten in Trendmärkten, wo nach einem Ausbruch große Bewegungen folgen. Er eignet sich auch als Lehrvorlage, die zeigt, wie man einen Indikator mit BindEx verbindet und Marktaufträge auf abgeschlossenen Kerzen ausführt.

Details

  • Einstiegskriterien:
    • Long: SuperTrend signalisiert einen Aufwärtstrend.
    • Short: SuperTrend signalisiert einen Abwärtstrend.
  • Long/Short: Beide.
  • Ausstiegskriterien: Gegensignal von SuperTrend (Position wird umgekehrt).
  • Stops: Kein expliziter Stop-Loss; die Indikatorlinie wirkt als Trailing Stop.
  • Standardwerte:
    • AtrPeriod = 10
    • Multiplier = 3m
    • CandleType = TimeSpan.FromHours(1).TimeFrame()
  • Filter:
    • Kategorie: Trendfolge
    • Richtung: Beide
    • Indikatoren: SuperTrend
    • Stops: Indikatorbasiert
    • Komplexität: Grundlegend
    • Zeitrahmen: Mittel (standardmäßig 1 Stunde)
    • Saisonalität: Keine
    • Neuronale Netze: Nein
    • Divergenz: Nein
    • Risikolevel: Mittel
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>
/// SuperTrend expert strategy.
/// Opens positions following the SuperTrend direction.
/// </summary>
public class ExpSuperTrendStrategy : Strategy
{
	private readonly StrategyParam<int> _atrPeriod;
	private readonly StrategyParam<decimal> _multiplier;
	private readonly StrategyParam<DataType> _candleType;

	private SuperTrend _superTrend;

	/// <summary>
	/// ATR period for SuperTrend.
	/// </summary>
	public int AtrPeriod
	{
		get => _atrPeriod.Value;
		set => _atrPeriod.Value = value;
	}

	/// <summary>
	/// ATR multiplier for SuperTrend.
	/// </summary>
	public decimal Multiplier
	{
		get => _multiplier.Value;
		set => _multiplier.Value = value;
	}

	/// <summary>
	/// Candle type.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of <see cref="ExpSuperTrendStrategy"/>.
	/// </summary>
	public ExpSuperTrendStrategy()
	{
		_atrPeriod = Param(nameof(AtrPeriod), 10)
			.SetDisplay("ATR Period", "ATR period for SuperTrend", "SuperTrend")
			.SetGreaterThanZero()
			
			.SetOptimize(5, 20, 1);

		_multiplier = Param(nameof(Multiplier), 3m)
			.SetDisplay("Multiplier", "ATR multiplier for SuperTrend", "SuperTrend")
			.SetGreaterThanZero()
			
			.SetOptimize(1m, 5m, 0.5m);

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe for indicator calculation", "General");
	}

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

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

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

		_superTrend = new SuperTrend
		{
			Length = AtrPeriod,
			Multiplier = Multiplier
		};

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(_superTrend, ProcessCandle)
			.Start();
	}

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

		if (stValue is not SuperTrendIndicatorValue st)
			return;

		var isUpTrend = st.IsUpTrend;

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