Auf GitHub ansehen

Brandy v1.2-Strategie (C#)

Überblick

Die Brandy v1.2-Strategie ist eine direkte Konvertierung des MetaTrader 4 Expert Advisors „Brandy_v1_2.mq4“ in das StockSharp High-Level-Strategie-Framework. Das System wertet ein Paar verschobener einfacher gleitender Durchschnitte (SMAs) aus, die auf der Grundlage des Schlusskurses der konfigurierten Kerzenserie berechnet werden. Neue Positionen werden nur eröffnet, wenn sowohl der langfristige als auch der kurzfristige SMA ein synchronisiertes Momentum in die gleiche Richtung aufweisen, während bestehende Geschäfte mithilfe von Steigungsumkehrungen, festen Stop-Loss-Levels und einem optionalen Trailing-Stop-Modul verwaltet werden.

Das ursprüngliche MQL-Skript wurde genau einmal pro abgeschlossenem Balken ausgeführt. Dieser Port verarbeitet fertige StockSharp-Kerzen auf die gleiche Weise und stellt so sicher, dass alle Handelsentscheidungen auf geschlossenen Daten basieren, ohne sich auf teilweise geformte Balken zu verlassen.

Handelslogik

  1. Indikatorvorbereitung
    • Es werden zwei SMAs berechnet: eine längere Basislinie (LongPeriod) und eine kürzere Bestätigungslinie (ShortPeriod).
    • Auf jeden Durchschnitt wird zweimal zugegriffen: auf den Wert des vorherigen Balkens (Verschiebung = 1) und einen anderen um jeweils LongShift/ShortShift Balken verschobenen Wert. Dadurch werden die im ursprünglichen EA vorhandenen iMA(..., shift)-Aufrufe reproduziert.
  2. Eintrittsregeln
    • Kaufen, wenn der Wert beider SMAs auf dem vorherigen Balken größer ist als ihre verschobenen Gegenstücke (beide Steigungen zeigen nach oben) und keine Position offen ist.
    • Verkaufen, wenn der Wert beider SMAs auf dem vorherigen Balken niedriger ist als der ihrer verschobenen Gegenstücke (beide Steigungen zeigen nach unten) und keine Position offen ist.
    • Es kann immer nur eine Position aktiv sein, was die k == 0-Prüfung in der MQL-Quelle widerspiegelt.
  3. Ausgangsregeln
    • Steigungsumkehr: Eine offene Long-Position wird liquidiert, wenn die Long-Position SMA nach unten geht (longPrev < longShifted), während eine Short-Position gedeckt wird, wenn die Long-Position SMA nach oben geht (longPrev > longShifted).
    • Fester Stop-Loss: Beim Einstieg speichert die Strategie einen anfänglichen Stop-Level, der um StopLossPoints × PriceStep vom Einstiegspreis abweicht. Der Stop wird anhand des Hoch-/Tief-Bereichs der Kerze überprüft, was annähernd dem Tick-Level-Management des ursprünglichen Beraters entspricht.
    • Trailing Stop: Bei TrailingStopPoints ≥ 100 repliziert die Strategie die Trailing-Logik (Parameter ts). Sobald der variable Gewinn die Trailing-Distanz überschreitet, wird der Stop auf currentPrice ± trailingDistance gezogen, sofern das neue Niveau näher am Preis liegt als der bestehende Stop. Dieses Verhalten entspricht den OrderModify-Aufrufen im MQL-Experten.

Parameter

Parameter Standard Beschreibung
LongPeriod 70 Länge des primären SMA (p1 in MQL). Muss > 0 sein.
LongShift 5 Auf den langen SMA-Vergleich (s1) wird eine Rückwärtsverschiebung angewendet. Kann Null sein.
ShortPeriod 20 Länge der Bestätigung SMA (p2). Muss > 0 sein.
ShortShift 5 Rückwärtsverschiebung für das kurze SMA (s2). Kann Null sein.
StopLossPoints 50 Feste Stoppdistanz in Preisschritten (sl). Auf 0 setzen, um den harten Stopp zu deaktivieren.
TrailingStopPoints 150 Nachlaufdistanz in Preisschritten (ts). Trailing wird nur aktiviert, wenn der Wert ≥ 100 ist, was den ursprünglichen Schwellenwert widerspiegelt.
Volume 0,1 Für Einträge verwendetes Bestellvolumen (lots).
CandleType 15-minütiger Zeitrahmen Von der Strategie verarbeitete Kerzenserie (vom Benutzer konfigurierbar).

Preisschrittabhängigkeit

Beide Stoppparameter wirken in Instrumentenpunkten. Die Hilfsmethode wandelt sie über Security.PriceStep in absolute Preisdeltas um. Wenn die Datenquelle PriceStep nicht bereitstellt, greift die Strategie auf 0.0001 zurück, sodass die Logik weiterhin funktioniert, wenn auch mit einer ungefähren Konvertierung. Überprüfen Sie vor der Live-Nutzung immer die Symbolmetadaten in StockSharp.

Risikomanagement

  • Hard Stop: intern gespeichert und anhand jeder fertigen Kerze validiert. Wenn der Preis den Stop überschreitet, schließt der entsprechende SellMarket/BuyMarket-Anruf die gesamte Position.
  • Trailing Stop: folgt den genauen Bedingungen des ursprünglichen EA und verschiebt den Stop nur, wenn der aktuelle Gewinn die Trailing-Distanz überschreitet und der bestehende Stop immer noch weiter als diese Distanz entfernt ist.
  • Einzelne Position: Der Algorithmus bildet niemals eine Pyramide; Es gibt entweder eine einzelne Long-Position, eine einzelne Short-Position oder es ist flach.

Implementierungshinweise

  • Der Status (Einstiegspreis, Stop-Level, SMA-Historien) wird am OnReseted() automatisch zurückgesetzt, um saubere Backtests und Neustarts zu gewährleisten.
  • Indikatorverläufe werden in kurzen Rollpuffern gespeichert, um die iMA(..., shift)-Offsets ohne Aufruf von GetValue() zu reproduzieren.
  • Alle Inline-Kommentare bleiben gemäß den Repository-Richtlinien auf Englisch.
  • Es wird kein Python-Gegenstück bereitgestellt. Nur die C#-High-Level-Implementierung wird wie angefordert in CS/BrandyV12Strategy.cs bereitgestellt.

Nutzung

  1. Platzieren Sie die Strategie in einer StockSharp-Lösung, wählen Sie das gewünschte Instrument aus und stellen Sie sicher, dass die Kerzendaten mit dem durch CandleType angegebenen Zeitrahmen übereinstimmen.
  2. Konfigurieren Sie die Parameter in der Benutzeroberfläche oder per Code. Die Standardwerte replizieren die ursprünglichen MT4-Werte.
  3. Starten Sie die Strategie. Es abonniert die Kerzenserie, zeichnet beide SMAs auf dem Chart und verwaltet Trades automatisch.

Haftungsausschluss: Dieser Port ist für Bildungs- und Testzwecke gedacht. Überprüfen Sie immer das Verhalten bei historischen und Papierhandelssitzungen, bevor Sie es auf Live-Märkten einsetzen.

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>
/// Trend-following strategy using displaced simple moving averages.
/// </summary>
public class BrandyV12Strategy : Strategy
{
	private readonly StrategyParam<int> _longPeriod;
	private readonly StrategyParam<int> _longShift;
	private readonly StrategyParam<int> _shortPeriod;
	private readonly StrategyParam<int> _shortShift;
	private readonly StrategyParam<decimal> _stopLossPoints;
	private readonly StrategyParam<decimal> _trailingStopPoints;
	private readonly StrategyParam<DataType> _candleType;

	private SimpleMovingAverage _longSma;
	private SimpleMovingAverage _shortSma;
	private readonly List<decimal> _longHistory = new();
	private readonly List<decimal> _shortHistory = new();
	private decimal? _entryPrice;
	private decimal? _stopPrice;

	/// <summary>
	/// Initializes a new instance of <see cref="BrandyV12Strategy"/>.
	/// </summary>
	public BrandyV12Strategy()
	{
		_longPeriod = Param(nameof(LongPeriod), 70)
			.SetGreaterThanZero()
			.SetDisplay("Long SMA Period", "Period for the longer moving average.", "Indicators")
			;

		_longShift = Param(nameof(LongShift), 5)
			.SetNotNegative()
			.SetDisplay("Long SMA Shift", "Backward shift applied to the longer SMA.", "Indicators")
			;

		_shortPeriod = Param(nameof(ShortPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("Short SMA Period", "Period for the shorter moving average.", "Indicators")
			;

		_shortShift = Param(nameof(ShortShift), 5)
			.SetNotNegative()
			.SetDisplay("Short SMA Shift", "Backward shift applied to the shorter SMA.", "Indicators")
			;

		_stopLossPoints = Param(nameof(StopLossPoints), 50m)
			.SetNotNegative()
			.SetDisplay("Stop Loss (points)", "Initial stop-loss distance expressed in price steps.", "Risk")
			;

		_trailingStopPoints = Param(nameof(TrailingStopPoints), 150m)
			.SetNotNegative()
			.SetDisplay("Trailing Stop (points)", "Trailing stop distance in price steps. Activates when >= 100.", "Risk")
			;

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(2).TimeFrame())
			.SetDisplay("Candle Type", "Candle series processed by the strategy.", "General");
	}

	/// <summary>
	/// Period for the longer simple moving average.
	/// </summary>
	public int LongPeriod
	{
		get => _longPeriod.Value;
		set => _longPeriod.Value = value;
	}

	/// <summary>
	/// Backward shift used when evaluating the longer SMA.
	/// </summary>
	public int LongShift
	{
		get => _longShift.Value;
		set => _longShift.Value = value;
	}

	/// <summary>
	/// Period for the shorter simple moving average.
	/// </summary>
	public int ShortPeriod
	{
		get => _shortPeriod.Value;
		set => _shortPeriod.Value = value;
	}

	/// <summary>
	/// Backward shift used when evaluating the shorter SMA.
	/// </summary>
	public int ShortShift
	{
		get => _shortShift.Value;
		set => _shortShift.Value = value;
	}

	/// <summary>
	/// Stop-loss distance in points (price steps).
	/// </summary>
	public decimal StopLossPoints
	{
		get => _stopLossPoints.Value;
		set => _stopLossPoints.Value = value;
	}

	/// <summary>
	/// Trailing stop distance in points (price steps).
	/// Trailing activates only when the configured value is at least 100.
	/// </summary>
	public decimal TrailingStopPoints
	{
		get => _trailingStopPoints.Value;
		set => _trailingStopPoints.Value = value;
	}

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_longSma = null;
		_shortSma = null;
		_longHistory.Clear();
		_shortHistory.Clear();
		_entryPrice = null;
		_stopPrice = null;
	}

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

		_longSma = new SMA { Length = LongPeriod };
		_shortSma = new SMA { Length = ShortPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_longSma, _shortSma, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, _longSma);
			DrawIndicator(area, _shortSma);
			DrawOwnTrades(area);
		}
	}

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

		if (_longSma?.IsFormed != true || _shortSma?.IsFormed != true)
			return;

		var longCapacity = Math.Max(LongShift, 1) + 2;
		var shortCapacity = Math.Max(ShortShift, 1) + 2;
		UpdateHistory(_longHistory, longValue, longCapacity);
		UpdateHistory(_shortHistory, shortValue, shortCapacity);

		if (!TryGetShiftedValue(_longHistory, 1, out var longPrev) ||
			!TryGetShiftedValue(_longHistory, LongShift, out var longShifted) ||
			!TryGetShiftedValue(_shortHistory, 1, out var shortPrev) ||
			!TryGetShiftedValue(_shortHistory, ShortShift, out var shortShifted))
		{
			return;
		}

		if (ManageExistingPosition(candle, longPrev, longShifted))
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (Position == 0)
		{
			var bullish = longPrev > longShifted && shortPrev > shortShifted;
			var bearish = longPrev < longShifted && shortPrev < shortShifted;

			if (bullish)
			{
				EnterLong(candle);
			}
			else if (bearish)
			{
				EnterShort(candle);
			}
		}
	}

	private bool ManageExistingPosition(ICandleMessage candle, decimal longPrev, decimal longShifted)
	{
		if (Position > 0)
		{
			if (longPrev < longShifted)
			{
				SellMarket(Position);
				ResetPositionState();
				return true;
			}

			if (UpdateLongStops(candle))
			{
				SellMarket(Position);
				ResetPositionState();
				return true;
			}
		}
		else if (Position < 0)
		{
			if (longPrev > longShifted)
			{
				BuyMarket(Math.Abs(Position));
				ResetPositionState();
				return true;
			}

			if (UpdateShortStops(candle))
			{
				BuyMarket(Math.Abs(Position));
				ResetPositionState();
				return true;
			}
		}

		return false;
	}

	private void EnterLong(ICandleMessage candle)
	{
		var volume = Volume;
		if (volume <= 0m)
			return;

		BuyMarket(volume);

		var step = GetPoint();
		var price = candle.ClosePrice;
		_entryPrice = price;

		_stopPrice = StopLossPoints > 0m ? price - StopLossPoints * step : null;
	}

	private void EnterShort(ICandleMessage candle)
	{
		var volume = Volume;
		if (volume <= 0m)
			return;

		SellMarket(volume);

		var step = GetPoint();
		var price = candle.ClosePrice;
		_entryPrice = price;

		_stopPrice = StopLossPoints > 0m ? price + StopLossPoints * step : null;
	}

	private bool UpdateLongStops(ICandleMessage candle)
	{
		if (_entryPrice is not decimal entry)
			return false;

		var step = GetPoint();
		if (step <= 0m)
			return false;

		if (_stopPrice is null && StopLossPoints > 0m)
		{
			_stopPrice = entry - StopLossPoints * step;
		}

		if (TrailingStopPoints >= 100m)
		{
			var trailingDistance = TrailingStopPoints * step;
			if (trailingDistance > 0m)
			{
				var currentPrice = candle.ClosePrice;
				if (currentPrice - entry > trailingDistance)
				{
					var newStop = currentPrice - trailingDistance;
					if (_stopPrice is not decimal existing || currentPrice - existing > trailingDistance)
					{
						_stopPrice = newStop;
					}
				}
			}
		}

		if (_stopPrice is not decimal stop)
			return false;

		return candle.LowPrice <= stop;
	}

	private bool UpdateShortStops(ICandleMessage candle)
	{
		if (_entryPrice is not decimal entry)
			return false;

		var step = GetPoint();
		if (step <= 0m)
			return false;

		if (_stopPrice is null && StopLossPoints > 0m)
		{
			_stopPrice = entry + StopLossPoints * step;
		}

		if (TrailingStopPoints >= 100m)
		{
			var trailingDistance = TrailingStopPoints * step;
			if (trailingDistance > 0m)
			{
				var currentPrice = candle.ClosePrice;
				if (entry - currentPrice > trailingDistance)
				{
					var newStop = currentPrice + trailingDistance;
					if (_stopPrice is not decimal existing || existing - currentPrice > trailingDistance)
					{
						_stopPrice = newStop;
					}
				}
			}
		}

		if (_stopPrice is not decimal stop)
			return false;

		return candle.HighPrice >= stop;
	}

	private void ResetPositionState()
	{
		_entryPrice = null;
		_stopPrice = null;
	}

	private static void UpdateHistory(List<decimal> history, decimal value, int capacity)
	{
		history.Add(value);
		if (history.Count > capacity)
		{
			history.RemoveAt(0);
		}
	}

	private static bool TryGetShiftedValue(List<decimal> history, int shift, out decimal value)
	{
		value = 0m;

		if (shift < 0)
			return false;

		var index = history.Count - 1 - shift;
		if (index < 0 || index >= history.Count)
			return false;

		value = history[index];
		return true;
	}

	private decimal GetPoint()
	{
		var step = Security?.PriceStep;
		if (step is decimal priceStep && priceStep > 0m)
			return priceStep;

		return 0.0001m;
	}
}