在 GitHub 上查看

OCO 订单执行策略

该策略重现了最初为 MetaTrader 编写的“一触即撤”(OCO)订单功能。交易者可以预先设定四个独立的触发价格:

  • Buy Limit Price:买入限价触发价
  • Sell Limit Price:卖出限价触发价
  • Buy Stop Price:买入止损触发价
  • Sell Stop Price:卖出止损触发价

策略订阅 Level1 数据以持续监控买一价和卖一价。当市场价格触及某个触发价时,策略会立即以市价单方式入场。下单后,系统根据设置的点数距离自动添加止损与止盈,这些距离会根据品种的 PriceStep 转换为绝对价格。

如果启用 OCO 模式,任一触发价被触发后,其余所有触发价都会被自动清除,实现典型的“一个成交其余取消”行为。若关闭该模式,其他触发价仍然有效,价格继续移动时可能产生新的头寸。

细节

  • 入场条件
    • Ask <= BuyLimitPrice 时做多(买入限价触发)。
    • Ask >= BuyStopPrice 时做多(买入止损触发)。
    • Bid >= SellLimitPrice 时做空(卖出限价触发)。
    • Bid <= SellStopPrice 时做空(卖出止损触发)。
  • 多空方向:支持多、空两侧。
  • 离场条件
    • 头寸通过预设的止损或止盈自动平仓。
  • 止损:支持,使用点数形式设置止损和止盈。
  • 默认值
    • StopLossPips = 300。
    • TakeProfitPips = 300。
    • OCO Mode = 启用。
  • 过滤条件
    • 分类:订单执行。
    • 方向:双向。
    • 指标:无。
    • 止损:有。
    • 复杂度:简单。
    • 时间框架:逐笔。
    • 季节性:无。
    • 神经网络:无。
    • 背离:无。
    • 风险等级:中等。
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;
	}
}