Auf GitHub ansehen

Trendfortsetzungs-Strategie

Diese Strategie identifiziert die Fortsetzung des vorherrschenden Trends mithilfe eines Paares exponentieller gleitender Durchschnitte auf Preisdaten. Eine Long-Position wird eröffnet, wenn der schnelle EMA den langsamen EMA von unten nach oben kreuzt, was eine Aufwärtsfortsetzung signalisiert. Eine Short-Position wird eröffnet, wenn der schnelle EMA den langsamen EMA von oben nach unten kreuzt.

Parameter

  • Fast EMA Length – Periode für den schnellen EMA (Standard: 20).
  • Candle Type – Zeitrahmen der Kerzen (Standard: 4 Stunden).
  • Stop Loss – Schutz-Stop-Loss angewendet über StartProtection (Standard: 1000).
  • Take Profit – Gewinnziel angewendet über StartProtection (Standard: 2000).

Funktionsweise

  1. Beim Start abonniert die Strategie die ausgewählte Kerzenserie und erstellt zwei EMA-Indikatoren.
  2. Jede abgeschlossene Kerze wird verarbeitet, um Kreuzungen zwischen dem schnellen und langsamen EMA zu erkennen.
  3. Eine Kreuzung von unten nach oben eröffnet eine Long-Position und schließt jede Short-Position. Die entgegengesetzte Kreuzung eröffnet eine Short-Position und schließt jede Long-Position.
  4. Das Risikomanagement wird über die integrierten Stop-Loss- und Take-Profit-Parameter gehandhabt.

Dieses Beispiel ist eine vereinfachte Konvertierung des ursprünglichen MQL-Experten Exp_TrendContinuation.

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>
/// Trend continuation strategy based on fast and slow EMA cross.
/// Opens long when fast EMA crosses above slow EMA, short on opposite.
/// </summary>
public class TrendContinuationStrategy : Strategy
{
	private readonly StrategyParam<int> _length;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevFast;
	private decimal? _prevSlow;

	public int Length
	{
		get => _length.Value;
		set => _length.Value = value;
	}

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

	public TrendContinuationStrategy()
	{
		_length = Param(nameof(Length), 20)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA Length", "Period for the fast EMA", "Indicators");

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

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

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

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

		_prevFast = _prevSlow = null;

		var fast = new ExponentialMovingAverage { Length = Length };
		var slow = new ExponentialMovingAverage { Length = Length * 2 };

		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.HasValue && _prevSlow.HasValue)
		{
			if (_prevFast < _prevSlow && fast >= slow && Position <= 0)
				BuyMarket();

			if (_prevFast > _prevSlow && fast <= slow && Position >= 0)
				SellMarket();
		}

		_prevFast = fast;
		_prevSlow = slow;
	}
}