在 GitHub 上查看

Frank Ud 对冲网格策略

概述

Frank Ud Hedging Grid Strategy 是将 MetaTrader 专家顾问 “Frank Ud” 迁移到 StockSharp 高级 API 的版本。策略同时在同一标的上持有多头和空头篮子,并在价格朝不利方向移动时按马丁格尔原则加仓。全部信号都基于最优买卖价(Level 1)更新处理,因此适用于低延迟执行或逐笔回测。

交易逻辑

  1. 初始对冲:当没有持仓时,策略立即以相同手数买入和卖出各一笔市场单,并为它们设置以点数表示的止损和止盈。
  2. 止盈/止损管理:只要多头和空头篮子同时存在,就持续监控它们的保护水平,触发后立即平掉对应篮子。
  3. 单边管理:当只剩多头或空头时,策略会:
    • 计算该篮子的加权平均建仓价;
    • 将共同止盈调整为平均价加/减设定距离;
    • 移除止损(与原始 EA 相同,此后仅依赖止盈保护)。
  4. 马丁加仓:如果价格反向移动超过设定步长,策略将当前乘数翻倍并追加新的市场单。AdjustVolume 方法复刻了 MQL5 的 LotCheck,确保下单量符合交易品种的最小/最大值以及数量步长。
  5. 循环重启:当所有头寸都被平掉后,乘数重置为 1,随后开始新的对冲循环。

参数

  • TakeProfitPips:篮子平均价与公共止盈之间的距离(默认 12 点)。
  • StopLossPips:仅用于首笔对冲订单的保护性止损(默认 12 点)。
  • StepPips:触发下一次马丁加仓所需的反向波动距离(默认 16 点)。
  • AutoLot:为 true 时使用 LotSize 参数,否则采用品种允许的最小交易量。
  • LotSize:在启用 AutoLot 时参与马丁乘数计算的基础手数。

实现细节

  • 策略使用高级 Strategy API:通过 Level 1 订阅驱动逻辑,并使用 BuyMarket/SellMarket 快捷方法下单。
  • 内部保存每笔加仓的价格和数量,从而完全复制原 EA 的加权管理方式。
  • _multiplier 字段对应 MQL 版本中的 Coefficient,每次加仓后翻倍,在所有仓位结束时恢复为 1
  • AdjustVolume 根据交易品种的数量限制调整下单量,以保证订单合法。
  • 策略假设账户支持对冲模式,因为它会同时持有多头和空头篮子。

文件

  • CS/FrankUdStrategy.cs:包含详细英文注释的策略实现。
  • README.md:英文说明。
  • README_ru.md:俄文说明。
  • README_zh.md:中文说明(本文件)。
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>
/// Hedging grid strategy converted from the Frank Ud MetaTrader expert.
/// Opens a position and adds martingale entries when price moves against the trade.
/// Closes all on take profit from average price.
/// </summary>
public class FrankUdStrategy : Strategy
{
	private readonly StrategyParam<decimal> _takeProfit;
	private readonly StrategyParam<decimal> _stopLoss;
	private readonly StrategyParam<decimal> _stepDistance;
	private readonly StrategyParam<int> _maxEntries;
	private readonly StrategyParam<DataType> _candleType;

	private int _lastSignal;

	public decimal TakeProfit { get => _takeProfit.Value; set => _takeProfit.Value = value; }
	public decimal StopLoss { get => _stopLoss.Value; set => _stopLoss.Value = value; }
	public decimal StepDistance { get => _stepDistance.Value; set => _stepDistance.Value = value; }
	public int MaxEntries { get => _maxEntries.Value; set => _maxEntries.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public FrankUdStrategy()
	{
		_takeProfit = Param(nameof(TakeProfit), 5000m)
			.SetGreaterThanZero()
			.SetDisplay("Take Profit", "Take profit distance from avg price", "Risk");

		_stopLoss = Param(nameof(StopLoss), 5000m)
			.SetGreaterThanZero()
			.SetDisplay("Stop Loss", "Stop loss distance from avg price", "Risk");

		_stepDistance = Param(nameof(StepDistance), 300m)
			.SetGreaterThanZero()
			.SetDisplay("Step Distance", "Price distance for adding martingale entries", "Grid");

		_maxEntries = Param(nameof(MaxEntries), 1)
			.SetGreaterThanZero()
			.SetDisplay("Max Entries", "Maximum martingale entries", "Grid");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Candle type for calculations", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_lastSignal = 0;
	}

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

		SubscribeCandles(CandleType)
			.Bind(ProcessCandle)
			.Start();

		StartProtection(
			new Unit(TakeProfit, UnitTypes.Absolute),
			new Unit(StopLoss, UnitTypes.Absolute));
	}

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

		var bodySize = (candle.ClosePrice - candle.OpenPrice).Abs();
		if (bodySize < StepDistance)
			return;

		var direction = candle.ClosePrice > candle.OpenPrice ? 1 : -1;
		if (direction == _lastSignal)
			return;

		if (direction > 0 && Position <= 0)
		{
			BuyMarket();
			_lastSignal = 1;
		}
		else if (direction < 0 && Position >= 0)
		{
			SellMarket();
			_lastSignal = -1;
		}
	}
}