GitHub で見る

Frank Ud Hedging Grid Strategy

Overview

The Frank Ud Hedging Grid Strategy is a direct port of the MetaTrader expert advisor "Frank Ud" into the StockSharp high-level API. The bot keeps simultaneous long and short baskets on the same instrument and then performs martingale-style averaging whenever price drifts against the active basket. All signal handling is performed on top of best bid/ask (Level 1) updates, making the strategy suitable for low-latency execution or tick-by-tick backtesting.

Trading Logic

  1. Initial hedge – when no positions are open the strategy immediately opens both a buy and a sell market order with the same volume. Each order receives a stop-loss and take-profit expressed in pips.
  2. Stop/take management – as long as both baskets exist, their protective levels are respected. Whenever price hits a protective level the matching basket is closed.
  3. Single-sided management – when only buy or only sell positions remain the strategy:
    • Calculates the volume-weighted average entry price of the active basket.
    • Re-assigns the common take-profit to the average price ± configured distance.
    • Removes the stop-loss (the original EA relies purely on the take-profit from this point).
  4. Martingale step – if price moves against the active basket by more than the configured step, the strategy doubles the multiplier and opens a new market order. The helper method AdjustVolume keeps each order aligned with the instrument’s volume step, minimum, and maximum volume.
  5. Cycle reset – once all baskets are closed the multiplier resets to 1 and a new hedged cycle begins.

Parameters

  • TakeProfitPips – distance between the basket average price and the collective take-profit target (default 12 pips).
  • StopLossPips – protective stop distance used only for the very first hedge orders (default 12 pips).
  • StepPips – adverse movement required before adding the next martingale order (default 16 pips).
  • AutoLot – when true the strategy uses LotSize; otherwise it trades with the instrument minimum volume.
  • LotSize – custom base lot size used together with the martingale multiplier when AutoLot is enabled.

Implementation Notes

  • The conversion uses the high-level Strategy API: Level 1 subscriptions drive the logic, and order placement relies on BuyMarket/SellMarket helpers.
  • Position tracking is internal: the strategy stores the entry price and volume of each basket order so that it can reproduce the original MetaTrader averaging rules.
  • The multiplier (_multiplier) mirrors the EA’s Coefficient variable and doubles after each additional order. Once every trade is closed the multiplier resets to 1.
  • AdjustVolume emulates the MQL5 LotCheck function by clamping requested volumes to the allowed trading step and contract limits.
  • The strategy expects a hedging-enabled account, because it keeps long and short baskets simultaneously just like the source EA.

Files

  • CS/FrankUdStrategy.cs – main strategy implementation with English inline comments explaining each block.
  • README.md – this document.
  • README_ru.md – Russian translation.
  • README_zh.md – Simplified Chinese translation.
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;
		}
	}
}