在 GitHub 上查看

EES 对冲器(高级版)

概述

该策略复刻了 MetaTrader 上的经典 "EES Hedger" 智能交易顾问。当外部策略、手工交易者或其它自动化系统在同一账户上开仓时,策略会立即按照设定的手数建立方向相反的对冲仓位,并通过止损、止盈、保本和移动止损规则保护该对冲仓位,从而锁定敞口并在行情有利时保留收益。

策略本身不负责生成入场信号,它只监听账户成交,识别需要对冲的交易并管理对冲仓位,直到保护性挂单触发或仓位被手动处理。

交易逻辑

  1. 监控外部成交:通过连接器实时接收账户成交。若设置了 OriginalOrderComment,只有备注匹配的成交才会被识别为原始仓位;若留空则对所有成交生效。策略会记录自身订单的事务编号以免重复处理。
  2. 下达对冲订单:识别到合格成交后,策略立即以市价下单建立相反方向的对冲仓位,手数由 HedgeVolume 指定。可选的 HedgerOrderComment 便于事后在报表中区分对冲订单。
  3. 风险控制:对冲成交后,策略会根据参数距离挂出止损和止盈。达到保本条件后,止损移动到开仓价上方(或下方)一个点。若启用移动止损,则在行情继续向有利方向发展时进一步推进止损。
  4. 状态清理:当仓位回到零(例如手动平仓)时,策略撤销所有保护性挂单并重置内部标志,为下一笔外部成交做好准备。

参数

参数 说明
HedgeVolume 建立对冲仓位的手数。
StopLossPips 距离开仓价的止损点数。
TakeProfitPips 距离开仓价的止盈点数。
TrailingStopPips 移动止损与当前价格保持的距离,设为 0 可关闭移动止损。
TrailingActivationPips 移动止损开始工作的最小盈利点数。
BreakEvenPips 止损移动到开仓价所需的盈利点数。
OriginalOrderComment 过滤外部成交的备注,留空表示对所有成交生效。
HedgerOrderComment 策略创建的对冲单及保护单所使用的备注。

使用建议

  • 确认策略与原始交易者使用相同的账户或投资组合,这样连接器才能看到外部仓位并执行对冲。
  • 如果通过 MetaTrader 桥接复制交易,请确保原始单的备注能够传递到 StockSharp 端,以便过滤功能生效。
  • 点值根据品种的报价步长自动推导,适用于五位数外汇报价等情况。
  • 保本和移动止损只会将止损朝盈利方向移动,不会拉远到亏损区域。一旦达到保本条件,止损不会再回到亏损位置。
  • 策略不会管理原始仓位的退出或加仓,相关操作仍需由主策略负责。

操作流程

  1. 设置参数,重点检查备注过滤和对冲手数是否符合需求。
  2. 启动策略并确认已经连接到券商。未收到外部成交前策略保持待机状态。
  3. 出现符合条件的成交后,观察对冲单的生成以及保护性挂单在盘口中的位置。
  4. 监控保本与移动止损行为,确认点数设置与经纪商合约规范一致。
  5. 当不再需要对冲时停止策略,停止过程中会自动撤销所有未成交的保护性挂单。

限制

  • 策略要求能够接收到账户成交流,无法对冲完全不可见的交易。
  • 不同经纪商的手数步长限制不同,请确认 HedgeVolume 与品种的合约规格兼容。
  • 对冲采用市价单,在行情快速波动时可能出现一定滑点,可适当放宽止损距离以留出缓冲。
using System;
using System.Linq;
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>
/// Adapted from the MetaTrader "EES Hedger" expert advisor.
/// Uses EMA crossover signals with break-even and trailing stop risk management.
/// </summary>
public class EesHedgerAdvancedStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _stopLossPips;
	private readonly StrategyParam<int> _takeProfitPips;
	private readonly StrategyParam<DataType> _candleType;

	/// <summary>
	/// Fast EMA period.
	/// </summary>
	public int FastPeriod
	{
		get => _fastPeriod.Value;
		set => _fastPeriod.Value = value;
	}

	/// <summary>
	/// Slow EMA period.
	/// </summary>
	public int SlowPeriod
	{
		get => _slowPeriod.Value;
		set => _slowPeriod.Value = value;
	}

	/// <summary>
	/// Stop-loss distance in price steps.
	/// </summary>
	public int StopLossPips
	{
		get => _stopLossPips.Value;
		set => _stopLossPips.Value = value;
	}

	/// <summary>
	/// Take-profit distance in price steps.
	/// </summary>
	public int TakeProfitPips
	{
		get => _takeProfitPips.Value;
		set => _takeProfitPips.Value = value;
	}

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

	/// <summary>
	/// Initializes default parameters.
	/// </summary>
	public EesHedgerAdvancedStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");

		_slowPeriod = Param(nameof(SlowPeriod), 30)
			.SetGreaterThanZero()
			.SetDisplay("Slow EMA", "Slow EMA period", "Indicators");

		_stopLossPips = Param(nameof(StopLossPips), 500)
			.SetDisplay("Stop Loss", "Stop-loss distance", "Risk Management");

		_takeProfitPips = Param(nameof(TakeProfitPips), 500)
			.SetDisplay("Take Profit", "Take-profit distance", "Risk Management");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Candles used for calculations", "Market Data");
	}

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

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		var fastEma = new EMA { Length = FastPeriod };
		var slowEma = new EMA { Length = SlowPeriod };

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

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

		// Use StartProtection for SL/TP
		var tp = TakeProfitPips > 0 ? new Unit(TakeProfitPips, UnitTypes.Absolute) : null;
		var sl = StopLossPips > 0 ? new Unit(StopLossPips, UnitTypes.Absolute) : null;
		StartProtection(tp, sl);

		base.OnStarted2(time);
	}

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevFast = fastValue;
			_prevSlow = slowValue;
			_hasPrev = true;
			return;
		}

		if (!_hasPrev)
		{
			_prevFast = fastValue;
			_prevSlow = slowValue;
			_hasPrev = true;
			return;
		}

		// EMA crossover detection
		var crossedUp = _prevFast <= _prevSlow && fastValue > slowValue;
		var crossedDown = _prevFast >= _prevSlow && fastValue < slowValue;

		if (crossedUp)
		{
			// Close short if any, then go long
			if (Position < 0)
				BuyMarket(Math.Abs(Position));
			if (Position <= 0)
				BuyMarket(Volume);
		}
		else if (crossedDown)
		{
			// Close long if any, then go short
			if (Position > 0)
				SellMarket(Position);
			if (Position >= 0)
				SellMarket(Volume);
		}

		_prevFast = fastValue;
		_prevSlow = slowValue;
	}
}