ARD Order Management Command Strategy
Strategy overview
The ARD Order Management strategy ports the MetaTrader 4 expert advisor ARD_ORDER_MANAGEMENT_.mq4 to StockSharp's high-level strategy framework. The original script exposed a set of manual commands—buy, sell, close, and modify—that could be triggered from external scripts or UI buttons. Every command recalculated the trade volume from available free margin, opened or reversed market positions, and attached protective stop-loss and take-profit levels at fixed point distances.
The StockSharp version keeps the same interaction model. You drive the behaviour through the Command parameter; once a non-None value is set the strategy performs the requested action on the next Level 1 update and automatically resets the command back to None. Protective orders are recreated with every new entry or modify request so that the stop-loss and take-profit always reflect the current parameter values.
Command lifecycle
- Command dispatch – when
Commandis set toBuyorSell, the strategy stores the request and immediately callsClosePosition()to flatten any open exposure. Active protective orders are cancelled before the new trade is considered, mirroring the MQL loop that closed all tickets viaOrderClose. - Volume calculation – the volume is recomputed for every command. It uses
Portfolio.CurrentValue(fallback toPortfolio.BeginValue) divided byLotSizeDivisorand scaled by1/1000, exactly asAccountFreeMargin()/lotsize/1000was used in MetaTrader. The result is rounded toLotDecimalsand constrained byMinimumVolume. - Waiting for a flat position – if a position was open when the command arrived, the new entry is deferred until
Positiondrops to zero. The strategy checks this condition on every Level 1 tick to avoid racing the asynchronous execution pipeline. - Market execution – once flat, the strategy sends
BuyMarketorSellMarket. The last known best bid/ask prices are stored so that protective orders are derived from realistic execution prices. - Protection placement – stop-loss and take-profit levels are materialised as separate stop and limit orders. For long trades the stop sits at
bid − StopLossPoints * PriceStepand the target atask + TakeProfitPoints * PriceStep. Short trades invert those calculations. Modify commands reuse the same routine but withModifyStopLossPointsandModifyTakeProfitPoints. - Close command – setting
CommandtoClosecancels all protective orders and callsClosePosition(). If the strategy is already flat the command simply logs the fact and does nothing else.
Money management
- Margin driven volume – the code inspects the current portfolio value so the volume shrinks or grows with the available capital. If the divisor parameter accidentally drops to zero the algorithm falls back to the configured
MinimumVolumeand emits a warning. - Rounding –
LotDecimalsdefines how many decimal places are retained after rounding. The implementation usesMath.RoundwithMidpointRounding.AwayFromZeroso that positive and negative adjustments behave like MetaTrader'sNormalizeDouble. - Minimum lot – after rounding, the size is clamped with
MinimumVolume, reproducing the original behaviour where values belowlotmaxwere promoted to0.1.
Stop-loss and take-profit handling
- Protective orders are always recreated from scratch. Existing stop or take orders are cancelled before new ones are submitted.
- The strategy checks
Security.PriceStepbefore calculating absolute prices. If the step is missing or non-positive, protection orders are skipped and a warning is logged. - Modify commands (
Command = Modify) rebuild protection using the dedicated modify distances without changing the current position size.
Data and execution requirements
- Subscribes to Level 1 data via
SubscribeLevel1()to mirror the MetaTrader quote updates (Bid/Ask). - Does not require candles or indicators; all logic runs on tick/quote updates.
- Uses high-level helpers (
BuyMarket,SellMarket,BuyStop,SellStop,BuyLimit,SellLimit,CancelOrder) to stay within the StockSharp recommended API layer.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
SlippageSteps |
int | 4 | Permitted slippage expressed in price steps. Stored for compatibility; StockSharp market orders execute immediately and do not consume this value. |
LotDecimals |
int | 1 | Number of decimal places retained after rounding the calculated volume. |
StopLossPoints |
decimal | 50 | Distance (in price points) from entry to the initial stop-loss. |
TakeProfitPoints |
decimal | 100 | Distance (in price points) from entry to the initial take-profit. |
LotSizeDivisor |
decimal | 5 | Divides the portfolio value before scaling to lots (freeMargin / divisor / 1000). |
ModifyStopLossPoints |
decimal | 20 | Stop-loss distance applied when Command = Modify. |
ModifyTakeProfitPoints |
decimal | 100 | Take-profit distance applied when Command = Modify. |
MinimumVolume |
decimal | 0.1 | Lower bound for the final volume after rounding. |
OrderComment |
string | "Placing Order" |
Comment inserted into every order for easier auditing. |
Command |
ArdOrderCommand |
None |
Manual command to execute. Automatically reset to None once processed. |
Usage notes
- Set
Commandthrough the UI or programmatically. The command is processed only once per change; to repeat an action set it back toNoneand then to the desired value again. - Because stop-loss and take-profit are placed as independent orders, brokers/exchanges must support native stop/limit orders for the same security. If they do not, consider replacing them with synthetic exits in code.
- Slippage is kept as a parameter for documentation parity with the MT4 version. StockSharp's high-level market helpers do not expose an explicit slippage parameter, so the value is only informational.
- The strategy logs every important action (
LogInfo/LogWarn) to assist with audit trails during discretionary execution.
Differences compared with the original MQL expert advisor
- MetaTrader attached stops and targets directly to the market ticket. StockSharp issues separate stop and limit orders instead.
- The port uses the asynchronous event model of StockSharp. When reversing a position the entry waits until the previous position reports as closed, preventing order overlap.
- Portfolio information replaces
AccountFreeMargin. Ensure that the portfolio adapter populatesCurrentValueor configureBeginValuebefore starting the strategy. - Error handling relies on StockSharp logging rather than repeated
OrderSendretries because order submission exceptions are surfaced by the framework itself.
Testing tips
- Run the strategy in simulation with Level 1 data to confirm that protection orders appear at the expected distances.
- Experiment with different
LotSizeDivisorandLotDecimalsvalues to match the broker's contract specifications before using the strategy in live environments.