Ver no GitHub

Balance Drawdown In MT4 Strategy

This strategy ports the original MetaTrader 4 expert advisor BalanceDrawdownInMT4 to the StockSharp high-level API. The EA immediately opens a single long position and continuously measures the account drawdown relative to the peak balance reached since the session started.

Trading Logic

  1. When the strategy starts it calls StartProtection to arm managed stop-loss and take-profit levels that mimic the MQL inputs expressed in price points.
  2. On the first finished candle (default timeframe: 1 minute) the strategy verifies whether a position is open. If no exposure exists it submits a market buy order using the configured Volume.
  3. After every finished candle it updates the drawdown metric:
    • The strategy tracks the maximum achieved balance as StartBalance + realized PnL.
    • The current equity equals StartBalance + realized PnL + unrealized PnL, where unrealized PnL is derived from the latest candle close price, the average entry price, and the instrument's PriceStep/StepPrice.
    • Drawdown is the percentage decline from the stored peak balance to the current equity. The value is logged with an informational message on every update.

The algorithm never opens additional positions or reverses. Once the initial position is established it remains active until stopped out, the take-profit fires, or the user manually intervenes.

Parameters

Parameter Default Description
StartBalance 1000 Baseline balance used when calculating peak equity and drawdown percentage.
Volume 0.01 Net volume (in instrument units) of the initial market buy order.
StopLossPoints 300 Distance from the entry price to the protective stop, measured in price points. A value of 0 disables the stop.
TakeProfitPoints 400 Distance from the entry price to the protective target, measured in price points. A value of 0 disables the target.
CandleType 1m time frame Time frame that drives periodic drawdown updates and the initial entry check.

Implementation Notes

  • The drawdown counter uses the strategy's realized PnL (PnL) combined with the unrealized PnL estimated from price differences, matching the running balance logic found in the MT4 version.
  • If PriceStep or StepPrice is unavailable for the security, the unrealized PnL calculation safely returns zero, preventing divide-by-zero errors.
  • Volume is validated to ensure a positive value before the initial trade; otherwise a warning is logged and the strategy stays flat.
  • DrawdownPercent exposes the latest drawdown reading so that other modules (dashboards, risk controllers) can pull the value programmatically.

Usage Tips

  • Set StartBalance to the real account balance (or the balance at the beginning of the trading session) to obtain meaningful drawdown statistics.
  • Keep the default 1-minute candles for timely updates, or choose a faster synthetic candle type if you need near-tick precision.
  • Because this strategy intentionally holds a single long position, pair it with manual risk controls or external automation if you need to re-enter after a stop or target is hit.
  • Always test on a simulator to confirm that the broker supplies PriceStep and StepPrice so the unrealized PnL conversion matches expectations.
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>
/// Replicates the BalanceDrawdownInMT4 expert advisor: opens a single long position and tracks drawdown from the peak balance.
/// </summary>
public class BalanceDrawdownInMt4Strategy : Strategy
{
	private readonly StrategyParam<decimal> _startBalance;
	private readonly StrategyParam<decimal> _stopLossPoints;
	private readonly StrategyParam<decimal> _takeProfitPoints;
	private readonly StrategyParam<int> _entryCooldownDays;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _maxBalance;
	private decimal _lastDrawdown;
	private decimal _lastPrice;
	private DateTime _lastEntryDate;

	/// <summary>
	/// Balance used as the baseline for drawdown calculations.
	/// </summary>
	public decimal StartBalance
	{
		get => _startBalance.Value;
		set => _startBalance.Value = value;
	}


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

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

	/// <summary>
	/// Minimum number of days between new entries.
	/// </summary>
	public int EntryCooldownDays
	{
		get => _entryCooldownDays.Value;
		set => _entryCooldownDays.Value = value;
	}

	/// <summary>
	/// Timeframe used to trigger periodic drawdown updates.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Current drawdown percentage relative to the peak balance.
	/// </summary>
	public decimal DrawdownPercent => _lastDrawdown;

	/// <summary>
	/// Initializes a new instance of the strategy.
	/// </summary>
	public BalanceDrawdownInMt4Strategy()
	{
		_startBalance = Param(nameof(StartBalance), 1000m)
			.SetDisplay("Start Balance", "Initial balance for drawdown measurement.", "Risk")
			;


		_stopLossPoints = Param(nameof(StopLossPoints), 300m)
			.SetDisplay("Stop-Loss (points)", "Distance from entry price to the protective stop.", "Risk")
			;

		_takeProfitPoints = Param(nameof(TakeProfitPoints), 400m)
			.SetDisplay("Take-Profit (points)", "Distance from entry price to the profit target.", "Risk")
			;

		_entryCooldownDays = Param(nameof(EntryCooldownDays), 5)
			.SetGreaterThanZero()
			.SetDisplay("Entry Cooldown Days", "Minimum number of days between new long entries.", "Risk");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe that drives drawdown monitoring.", "General");
	}

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

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

		_maxBalance = 0m;
		_lastDrawdown = 0m;
		_lastPrice = 0m;
		_lastEntryDate = default;
	}

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

		StartProtection(
			stopLoss: new Unit(StopLossPoints * GetPriceStep(), UnitTypes.Absolute),
			takeProfit: new Unit(TakeProfitPoints * GetPriceStep(), UnitTypes.Absolute));

		_maxBalance = StartBalance;

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

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

		_lastPrice = candle.ClosePrice;

		EnsurePosition(candle.CloseTime);
		UpdateDrawdown();
	}

	private void EnsurePosition(DateTime candleDate)
	{
		if (Position != 0m)
			return;

		if (_lastEntryDate != default && (candleDate.Date - _lastEntryDate.Date).TotalDays < EntryCooldownDays)
			return;

		if (Volume <= 0m)
		{
			LogWarning("Volume parameter must be positive to open the initial trade.");
			return;
		}

		BuyMarket(Volume);
		_lastEntryDate = candleDate.Date;
	}

	private void UpdateDrawdown()
	{
		var balanceWithoutFloating = StartBalance + PnL;
		if (balanceWithoutFloating > _maxBalance)
			_maxBalance = balanceWithoutFloating;

		if (_maxBalance <= 0m)
		{
			_lastDrawdown = 0m;
			return;
		}

		var unrealized = CalculateUnrealizedPnL(_lastPrice);
		var currentBalance = balanceWithoutFloating + unrealized;

		var drawdown = (_maxBalance - currentBalance) / _maxBalance * 100m;
		_lastDrawdown = drawdown > 0m ? drawdown : 0m;

		LogInfo($"Current drawdown: {_lastDrawdown:F2}%.");
	}

	private decimal CalculateUnrealizedPnL(decimal price)
	{
		if (Position == 0m)
			return 0m;

		var step = Security?.PriceStep ?? 0m;
		var stepPrice = GetSecurityValue<decimal?>(Level1Fields.StepPrice) ?? 0m;
		if (step <= 0m || stepPrice <= 0m)
			return 0m;

		var priceDiff = price - _lastPrice;
		var points = priceDiff / step;

		return points * stepPrice * Position;
	}

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