Auf GitHub ansehen

OCO-Orderausführungs-Strategie

Diese Strategie repliziert ein "One Cancels the Other"-Order-Ticket, das ursprünglich für MetaTrader geschrieben wurde. Sie ermöglicht dem Trader, bis zu vier unabhängige Preis-Trigger zu definieren:

  • Buy Limit Price
  • Sell Limit Price
  • Buy Stop Price
  • Sell Stop Price

Die Strategie abonniert Level1-Daten, um kontinuierlich das beste Angebot und die beste Nachfrage zu überwachen. Wenn ein Trigger-Preis erreicht wird, sendet sie eine Marktorder in der entsprechenden Richtung. Nach Ausführung einer Order werden Stop-Loss- und Take-Profit-Schutzmaßnahmen mit Abständen in Pips angewendet. Diese Abstände werden automatisch basierend auf dem PriceStep des Instruments in absolute Preise umgewandelt.

Wenn der OCO-Modus aktiviert ist, deaktiviert das Auslösen eines Triggers automatisch alle anderen Trigger und implementiert damit das klassische "einer-annulliert-den-anderen"-Verhalten. Wenn der OCO-Modus deaktiviert ist, bleiben andere Trigger aktiv und können zusätzliche Positionen eröffnen, wenn der Preis weiter steigt oder fällt.

Details

  • Einstiegskriterien:
    • Long, wenn Ask <= BuyLimitPrice (Buy-Limit-Trigger).
    • Long, wenn Ask >= BuyStopPrice (Buy-Stop-Trigger).
    • Short, wenn Bid >= SellLimitPrice (Sell-Limit-Trigger).
    • Short, wenn Bid <= SellStopPrice (Sell-Stop-Trigger).
  • Long/Short: Beide.
  • Ausstiegskriterien:
    • Positionen werden automatisch durch vordefinierte Stop-Loss- oder Take-Profit-Niveaus geschlossen.
  • Stops: Ja, Stop-Loss und Take-Profit in Pips.
  • Standardwerte:
    • StopLossPips = 300.
    • TakeProfitPips = 300.
    • OCO Mode = aktiviert.
  • Filter:
    • Kategorie: Orderausführung.
    • Richtung: Beide.
    • Indikatoren: Keine.
    • Stops: Ja.
    • Komplexität: Einfach.
    • Zeitrahmen: Tick-basiert.
    • Saisonalität: Nein.
    • Neuronale Netze: Nein.
    • Divergenz: Nein.
    • Risikolevel: Mittel.
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>
/// OCO-style breakout strategy. Calculates dynamic buy-stop and sell-stop levels
/// from recent high/low and enters on breakout, with StdDev-based stop-loss and take-profit.
/// </summary>
public class OcoOrderStrategy : Strategy
{
	private readonly StrategyParam<int> _lookbackPeriod;
	private readonly StrategyParam<decimal> _stdMultiplier;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _recentHigh;
	private decimal _recentLow;
	private decimal _entryPrice;
	private int _barCount;

	public int LookbackPeriod { get => _lookbackPeriod.Value; set => _lookbackPeriod.Value = value; }
	public decimal StdMultiplier { get => _stdMultiplier.Value; set => _stdMultiplier.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public OcoOrderStrategy()
	{
		_lookbackPeriod = Param(nameof(LookbackPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("Lookback", "Bars for high/low calculation", "General");

		_stdMultiplier = Param(nameof(StdMultiplier), 1.5m)
			.SetDisplay("StdDev Multiplier", "Multiplier for SL/TP distance", "Risk");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_recentHigh = 0;
		_recentLow = decimal.MaxValue;
		_entryPrice = 0;
		_barCount = 0;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var stdDev = new StandardDeviation { Length = LookbackPeriod };

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

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

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

		_barCount++;

		// Track rolling high/low
		if (_barCount <= LookbackPeriod)
		{
			if (candle.HighPrice > _recentHigh)
				_recentHigh = candle.HighPrice;
			if (candle.LowPrice < _recentLow)
				_recentLow = candle.LowPrice;
			return;
		}

		if (stdValue <= 0)
			return;

		var close = candle.ClosePrice;
		var slDistance = stdValue * StdMultiplier;

		// Exit logic
		if (Position > 0)
		{
			if (close <= _entryPrice - slDistance || close >= _entryPrice + slDistance * 2)
			{
				SellMarket();
				_entryPrice = 0;
			}
		}
		else if (Position < 0)
		{
			if (close >= _entryPrice + slDistance || close <= _entryPrice - slDistance * 2)
			{
				BuyMarket();
				_entryPrice = 0;
			}
		}

		// Entry: breakout above recent high or below recent low
		if (Position == 0)
		{
			if (close > _recentHigh)
			{
				BuyMarket();
				_entryPrice = close;
			}
			else if (close < _recentLow)
			{
				SellMarket();
				_entryPrice = close;
			}
		}

		// Update high/low
		if (candle.HighPrice > _recentHigh)
			_recentHigh = candle.HighPrice;
		if (candle.LowPrice < _recentLow)
			_recentLow = candle.LowPrice;
	}
}