Ver en GitHub

Constituents EA Strategy

This strategy ports the Constituents EA from MQL/22595 into the StockSharp high-level API. It recreates the original logic of placing two pending orders around the most recent range at a specific hour while keeping the workflow compatible with StockSharp order handling and risk protection helpers.

How the Strategy Works

  1. Scheduled activation – at the end of each candle the strategy checks whether the next bar will start at StartHour. Only at that moment are fresh pending orders considered, which mirrors the MetaTrader code that reacted to the birth of the bar whose open time matches the configured hour.
  2. Range detection – the highest high and lowest low among the previous SearchDepth completed candles are tracked with Highest/Lowest indicators. These two prices define the breakout/mean-reversion levels used for order placement.
  3. Price distance filters – current best bid/ask quotes are streamed from the order book feed. Orders are placed only if the distance between the quote and the candidate price is greater than or equal to MinOrderDistancePips (converted to absolute price using PointValue). This reimplements the original freeze-level validation and prevents invalid pending orders.
  4. Order style selectionPendingOrderMode chooses between limit orders (buy limit at the low, sell limit at the high) or stop orders (buy stop above the high, sell stop below the low). Both orders are submitted simultaneously, just like in the MetaTrader script.
  5. Risk protection – the built-in StartProtection helper attaches stop-loss and take-profit levels expressed in absolute price steps (StopLossPips/TakeProfitPips). Minimum-distance checks against MinStopDistancePips replicate the MT5 requirement that protective orders must respect the symbol stop level.
  6. Order management – if one pending order fills, the opposite order is cancelled immediately. During the bar interval the strategy never places additional orders as long as active ones exist, matching the source EA behaviour.

Parameters

Parameter Description
StartHour Hour (0-23) when the new pair of pending orders is created.
SearchDepth Number of previous completed candles used to compute the high/low range.
PendingOrderMode Limit replicates the mean-reversion variant, Stop places breakout orders.
StopLossPips Stop-loss distance measured in pips (converted with PointValue). Set to 0 to disable.
TakeProfitPips Take-profit distance in pips. Set to 0 to disable.
PointValue Pip value in price units. Set to 0 to auto-detect from Security.PriceStep/MinStep.
MinOrderDistancePips Minimal allowed distance between current bid/ask and the pending price, modelling freeze-level checks.
MinStopDistancePips Minimal allowed stop/take distance, mirroring StopsLevel checks.
CandleType Timeframe used for the range calculation and scheduling logic.

Strategy.Volume controls the order size; keep it positive so that BuyLimit, SellLimit, BuyStop, and SellStop can submit orders.

Usage

  1. Attach the strategy to a security and set CandleType to the timeframe you want to trade.
  2. Configure StartHour and SearchDepth exactly as in the MT5 inputs. Adjust the Min*Pips thresholds if the broker enforces minimum distances between orders and the market price.
  3. Calibrate PointValue when auto-detection from the security metadata is not possible (for example, on synthetic instruments).
  4. Set StopLossPips and TakeProfitPips to match the original EA. The protection module will automatically attach stops and targets once an order fills.
  5. Provide a positive Volume and start the strategy. It will subscribe to candles and order book data, place both pending orders at the scheduled bar, and cancel the opposite order whenever one trade is executed.

Differences from the Original EA

  • The MetaTrader MoneyFixedMargin risk mode (percentage-based sizing) is not ported. StockSharp users should configure Strategy.Volume directly or wrap the strategy with an external position sizing module.
  • Freeze-level and stop-level checks are expressed through the configurable MinOrderDistancePips and MinStopDistancePips parameters because the equivalent exchange metadata is not always available via StockSharp.
  • Order placement occurs when the prior candle closes and the upcoming bar starts at StartHour. This is functionally identical to the MT5 implementation that triggered on the birth of the new bar.
  • All comments inside the source have been translated into English, while the external documentation is available in three languages for convenience.

Tune the distances and trading hour to match the instrument you plan to trade. On markets with wide spreads you may need larger MinOrderDistancePips or pip values to avoid immediate rejection by the broker.

using System;
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>
/// Constituents breakout strategy converted from the original MetaTrader expert advisor.
/// Detects the recent high/low range from N candles and enters with market orders
/// when price breaks above the high (buy) or below the low (sell).
/// Uses stop-loss, take-profit, and trailing stop for risk management.
/// </summary>
public class ConstituentsEAStrategy : Strategy
{
	private readonly StrategyParam<int> _searchDepth;
	private readonly StrategyParam<decimal> _stopLossPips;
	private readonly StrategyParam<decimal> _takeProfitPips;
	private readonly StrategyParam<DataType> _candleType;

	private Highest _highest = null!;
	private Lowest _lowest = null!;

	private decimal _pipSize;
	private decimal _entryPrice;
	private decimal? _stopPrice;
	private decimal? _takePrice;
	private decimal _prevHigh;
	private decimal _prevLow;
	private bool _exitRequested;

	/// <summary>
	/// Number of completed candles used to determine the recent range.
	/// </summary>
	public int SearchDepth
	{
		get => _searchDepth.Value;
		set => _searchDepth.Value = value;
	}

	/// <summary>
	/// Stop loss distance expressed in pips.
	/// </summary>
	public decimal StopLossPips
	{
		get => _stopLossPips.Value;
		set => _stopLossPips.Value = value;
	}

	/// <summary>
	/// Take profit distance expressed in pips.
	/// </summary>
	public decimal TakeProfitPips
	{
		get => _takeProfitPips.Value;
		set => _takeProfitPips.Value = value;
	}

	/// <summary>
	/// Working candle type.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the <see cref="ConstituentsEaStrategy"/> class.
	/// </summary>
	public ConstituentsEAStrategy()
	{
		_searchDepth = Param(nameof(SearchDepth), 3)
			.SetGreaterThanZero()
			.SetDisplay("Search Depth", "Number of completed candles used to find extremes", "Setup");

		_stopLossPips = Param(nameof(StopLossPips), 50m)
			.SetNotNegative()
			.SetDisplay("Stop Loss (pips)", "Stop loss distance expressed in pips", "Risk");

		_takeProfitPips = Param(nameof(TakeProfitPips), 100m)
			.SetNotNegative()
			.SetDisplay("Take Profit (pips)", "Take profit distance expressed in pips", "Risk");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Working timeframe used to evaluate highs/lows", "General");
	}

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

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

		_highest = null!;
		_lowest = null!;
		_pipSize = 0m;
		_entryPrice = 0m;
		_stopPrice = null;
		_takePrice = null;
		_prevHigh = 0m;
		_prevLow = 0m;
		_exitRequested = false;
	}

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

		_pipSize = CalculatePipSize();

		_highest = new Highest { Length = SearchDepth };
		_lowest = new Lowest { Length = SearchDepth };

		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;

		// Process indicators
		var highValue = _highest.Process(new DecimalIndicatorValue(_highest, candle.HighPrice, candle.OpenTime) { IsFinal = true });
		var lowValue = _lowest.Process(new DecimalIndicatorValue(_lowest, candle.LowPrice, candle.OpenTime) { IsFinal = true });

		if (!_highest.IsFormed || !_lowest.IsFormed)
			return;

		var currentHigh = highValue.ToDecimal();
		var currentLow = lowValue.ToDecimal();

		// Manage existing position
		if (Position != 0)
		{
			ManagePosition(candle);

			// Update range for next trade
			_prevHigh = currentHigh;
			_prevLow = currentLow;
			return;
		}

		// Check for breakout signals using previous range
		if (_prevHigh > 0m && _prevLow > 0m)
		{
			// Breakout above the recent high -> buy
			if (candle.ClosePrice > _prevHigh)
			{
				_entryPrice = candle.ClosePrice;
				_exitRequested = false;

				if (StopLossPips > 0m)
					_stopPrice = _entryPrice - StopLossPips * _pipSize;
				else
					_stopPrice = null;

				if (TakeProfitPips > 0m)
					_takePrice = _entryPrice + TakeProfitPips * _pipSize;
				else
					_takePrice = null;

				BuyMarket();
			}
			// Breakout below the recent low -> sell
			else if (candle.ClosePrice < _prevLow)
			{
				_entryPrice = candle.ClosePrice;
				_exitRequested = false;

				if (StopLossPips > 0m)
					_stopPrice = _entryPrice + StopLossPips * _pipSize;
				else
					_stopPrice = null;

				if (TakeProfitPips > 0m)
					_takePrice = _entryPrice - TakeProfitPips * _pipSize;
				else
					_takePrice = null;

				SellMarket();
			}
		}

		// Update range for next candle
		_prevHigh = currentHigh;
		_prevLow = currentLow;
	}

	private void ManagePosition(ICandleMessage candle)
	{
		if (_exitRequested)
			return;

		if (Position > 0)
		{
			// Check take profit
			if (_takePrice.HasValue && candle.HighPrice >= _takePrice.Value)
			{
				_exitRequested = true;
				SellMarket();
				return;
			}

			// Check stop loss
			if (_stopPrice.HasValue && candle.LowPrice <= _stopPrice.Value)
			{
				_exitRequested = true;
				SellMarket();
				return;
			}
		}
		else if (Position < 0)
		{
			// Check take profit
			if (_takePrice.HasValue && candle.LowPrice <= _takePrice.Value)
			{
				_exitRequested = true;
				BuyMarket();
				return;
			}

			// Check stop loss
			if (_stopPrice.HasValue && candle.HighPrice >= _stopPrice.Value)
			{
				_exitRequested = true;
				BuyMarket();
				return;
			}
		}
	}

	private decimal CalculatePipSize()
	{
		var step = Security?.PriceStep ?? 0m;
		if (step <= 0m)
			return 0.01m;

		return step;
	}
}