在 GitHub 上查看

双向挂单策略

概述

该策略复刻了 MetaTrader 中的“Open Two Pending Orders”专家顾问,会同时在当前买卖价附近挂出买入止损单和卖出止损单。策略只针对单一标的,使用 StockSharp 的高层 API 订阅盘口、管理挂单并执行风险控制。一旦其中一张止损单被触发,另一张挂单会被取消,持仓随后通过固定止损、固定止盈以及可选的追踪止损进行管理。

交易逻辑

  1. 订阅盘口数据,实时记录最优买价和最优卖价。
  2. 当没有持仓或激活的入场挂单时,计算交易数量并放置两张止损单:
    • 买入止损价 = 卖价 + EntryOffsetPoints × PriceStep
    • 卖出止损价 = 买价 − EntryOffsetPoints × PriceStep
  3. 某张止损单成交后:
    • 取消另一张挂单;
    • 记录成交价作为新的入场价;
    • 根据成交价及参数计算初始止损、止盈价格。
  4. 持仓期间持续监控盘口:
    • 多头:当买价触及止损或止盈水平时市价平仓;
    • 空头:当卖价触及止损或止盈水平时市价平仓;
    • 当价格按照追踪距离有利波动后,启动追踪止损并随价格移动止损水平。
  5. 持仓归零后重置内部状态,并重新挂出下一组双向止损单。

策略在保护水平被触及时使用市价单离场,从而在不依赖低层订单修改接口的情况下保持与原始 MQL 逻辑一致。

资金管理

策略支持两种头寸控制模式:

  • 固定仓位:按 FixedVolume 参数设定的数量下单。
  • 资金管理:启用后,依据组合净值、RiskPercent 百分比以及止损距离计算下单数量,并按照标的的最小变动和数量步长进行取整和限制。

参数说明

参数 说明
UseMoneyManagement 是否启用风险百分比仓位管理,默认 true
RiskPercent 启用资金管理时,每笔交易风险占组合净值的百分比,默认 2
FixedVolume 未启用资金管理时的固定下单量,默认 1
StopLossPoints 入场价到止损位的价格步数,默认 100
TakeProfitPoints 入场价到止盈位的价格步数,默认 300
TrailingStopPoints 追踪止损的价格步数,设为 0 则关闭追踪,默认 50
EntryOffsetPoints 入场挂单相对于买卖价的偏移步数,默认 50
SlippagePoints 额外保留的滑点步数,目前仅用于提示,默认 5

注意事项

  • 策略依赖盘口深度数据,请确保所选标的提供 order book 信息。
  • 止损与止盈通过盘口触发后立即以市价单执行,与原始 MQL EA 的行为保持一致。
  • 追踪止损仅在价格朝有利方向移动超过设定距离后才会启用。
  • 代码遵循项目规范:使用制表符缩进、英文注释及 StockSharp 高层 API。
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>
/// Strategy that simulates placing both buy stop and sell stop orders around the current price.
/// It uses candle-based breakout detection and manages the resulting position
/// with fixed stop loss, take profit and optional trailing stop levels.
/// </summary>
public class OpenTwoPendingOrdersStrategy : Strategy
{
	private readonly StrategyParam<decimal> _stopLossPoints;
	private readonly StrategyParam<decimal> _takeProfitPoints;
	private readonly StrategyParam<decimal> _trailingStopPoints;
	private readonly StrategyParam<decimal> _entryOffsetPoints;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _pendingBuyPrice;
	private decimal? _pendingSellPrice;
	private decimal? _entryPrice;
	private decimal? _stopLevel;
	private decimal? _takeLevel;
	private decimal _highestSinceEntry;
	private decimal _lowestSinceEntry;
	private int _cooldown;

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

	/// <summary>
	/// Take profit distance expressed in price steps.
	/// </summary>
	public decimal TakeProfitPoints
	{
		get => _takeProfitPoints.Value;
		set => _takeProfitPoints.Value = value;
	}

	/// <summary>
	/// Trailing stop distance expressed in price steps.
	/// </summary>
	public decimal TrailingStopPoints
	{
		get => _trailingStopPoints.Value;
		set => _trailingStopPoints.Value = value;
	}

	/// <summary>
	/// Distance in price steps used to place the pending entries away from the current price.
	/// </summary>
	public decimal EntryOffsetPoints
	{
		get => _entryOffsetPoints.Value;
		set => _entryOffsetPoints.Value = value;
	}

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

	/// <summary>
	/// Initializes a new instance of <see cref="OpenTwoPendingOrdersStrategy"/>.
	/// </summary>
	public OpenTwoPendingOrdersStrategy()
	{
		_stopLossPoints = Param(nameof(StopLossPoints), 5000m)
			.SetDisplay("Stop Loss (steps)", "Stop loss distance in price steps", "Risk")
			.SetOptimize(20m, 300m, 20m);

		_takeProfitPoints = Param(nameof(TakeProfitPoints), 8000m)
			.SetDisplay("Take Profit (steps)", "Take profit distance in price steps", "Risk")
			.SetOptimize(50m, 600m, 50m);

		_trailingStopPoints = Param(nameof(TrailingStopPoints), 3000m)
			.SetDisplay("Trailing Stop (steps)", "Trailing stop distance in price steps", "Risk")
			.SetOptimize(10m, 200m, 10m);

		_entryOffsetPoints = Param(nameof(EntryOffsetPoints), 1000m)
			.SetDisplay("Entry Offset (steps)", "Offset from close for pending entries", "Execution")
			.SetOptimize(10m, 150m, 10m);

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

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

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

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

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

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

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

		if (_cooldown > 0)
		{
			_cooldown--;
			return;
		}

		var step = GetStep();

		// Manage existing position
		if (Position != 0 && _entryPrice.HasValue)
		{
			ManagePosition(candle, step);

			// If position was closed, reset and set up new pending entries
			if (Position == 0)
			{
				ResetState();
				_cooldown = 20;
			}
			return;
		}

		// Check pending entries
		if (_pendingBuyPrice.HasValue && _pendingSellPrice.HasValue)
		{
			var buyLevel = _pendingBuyPrice.Value;
			var sellLevel = _pendingSellPrice.Value;

			// Buy stop triggered: price went up to pending buy level
			if (candle.HighPrice >= buyLevel)
			{
				_pendingBuyPrice = null;
				_pendingSellPrice = null;
				BuyMarket();
				InitializePositionLevels(true, buyLevel, step);
				return;
			}

			// Sell stop triggered: price went down to pending sell level
			if (candle.LowPrice <= sellLevel)
			{
				_pendingBuyPrice = null;
				_pendingSellPrice = null;
				SellMarket();
				InitializePositionLevels(false, sellLevel, step);
				return;
			}
		}
		else
		{
			// No pending entries, set up new ones
			SetupPendingEntries(candle.ClosePrice, step);
		}
	}

	private void SetupPendingEntries(decimal currentPrice, decimal step)
	{
		var offset = EntryOffsetPoints * step;
		_pendingBuyPrice = currentPrice + offset;
		_pendingSellPrice = currentPrice - offset;
	}

	private void InitializePositionLevels(bool isLong, decimal entryPrice, decimal step)
	{
		_entryPrice = entryPrice;
		_highestSinceEntry = entryPrice;
		_lowestSinceEntry = entryPrice;

		_stopLevel = StopLossPoints > 0m
			? entryPrice + (isLong ? -StopLossPoints : StopLossPoints) * step
			: null;

		_takeLevel = TakeProfitPoints > 0m
			? entryPrice + (isLong ? TakeProfitPoints : -TakeProfitPoints) * step
			: null;
	}

	private void ManagePosition(ICandleMessage candle, decimal step)
	{
		if (Position > 0)
		{
			_highestSinceEntry = Math.Max(_highestSinceEntry, candle.HighPrice);

			if (_stopLevel.HasValue && candle.LowPrice <= _stopLevel.Value)
			{
				SellMarket();
				return;
			}

			if (_takeLevel.HasValue && candle.HighPrice >= _takeLevel.Value)
			{
				SellMarket();
				return;
			}

			UpdateTrailingStop(true, step);
		}
		else if (Position < 0)
		{
			_lowestSinceEntry = Math.Min(_lowestSinceEntry, candle.LowPrice);

			if (_stopLevel.HasValue && candle.HighPrice >= _stopLevel.Value)
			{
				BuyMarket();
				return;
			}

			if (_takeLevel.HasValue && candle.LowPrice <= _takeLevel.Value)
			{
				BuyMarket();
				return;
			}

			UpdateTrailingStop(false, step);
		}
	}

	private void UpdateTrailingStop(bool isLong, decimal step)
	{
		if (TrailingStopPoints <= 0m || _entryPrice == null)
			return;

		var trailingDistance = TrailingStopPoints * step;
		if (trailingDistance <= 0m)
			return;

		if (isLong)
		{
			if (_highestSinceEntry - _entryPrice.Value >= trailingDistance)
			{
				var desiredStop = _highestSinceEntry - trailingDistance;
				if (_stopLevel == null || desiredStop > _stopLevel.Value)
					_stopLevel = desiredStop;
			}
		}
		else
		{
			if (_entryPrice.Value - _lowestSinceEntry >= trailingDistance)
			{
				var desiredStop = _lowestSinceEntry + trailingDistance;
				if (_stopLevel == null || desiredStop < _stopLevel.Value)
					_stopLevel = desiredStop;
			}
		}
	}

	private void ResetState()
	{
		_pendingBuyPrice = null;
		_pendingSellPrice = null;
		_entryPrice = null;
		_stopLevel = null;
		_takeLevel = null;
		_highestSinceEntry = 0m;
		_lowestSinceEntry = 0m;
		_cooldown = 0;
	}

	private decimal GetStep()
	{
		var step = Security?.PriceStep ?? 0m;
		return step > 0m ? step : 0.01m;
	}
}