Loading…

Mock Trade Server

Testing an expert's order-handling logic normally means either risking a demo account or waiting for the Strategy Tester to walk through history at whatever speed the terminal feels like. Neither is a fast loop for checking "does my retry logic actually retry" or "does my partial-close code leave the right ticket open". This header is a broker in a box: a "Mock" function set matching the shape of the real trading API on each platform, backed by an in-memory order journal instead of a server connection. On MQL4 the shape mirrors OrderSend / OrderSelect / OrderClose / OrderModify directly - same argument order, same SELECT_BY_POS / SELECT_BY_TICKET / MODE_TRADES / MODE_HISTORY constants. On MQL5 there is no such order pool to mirror, so the simulator instead mirrors the real position/history shape: MockPositionsTotal() and MockPositionGetTicket(index) walk the open pool by position exactly like PositionsTotal()/PositionGetTicket(index) do, and MockPositionSelectByTicket(ticket) reads a specific one into context exactly like the real PositionSelectByTicket(). A caller who tests against this mock is exercising the same collect-tickets-then-act pattern the real API needs, not a shortcut that would not port back out. Three behaviours are modelled deliberately on both platforms, because a naive stub gets all three wrong: Requotes. A fill is rejected (ERR_PRICE_CHANGED on MQL4, TRADE_RETCODE_REQUOTE on MQL5) when the requested price has drifted from the current quote by more than the caller's own slippage tolerance - exactly the dealing-desk behaviour that catches EAs which assume their price argument is decorative. Partial fills. MockSetMaxFillLots caps how much volume a single send can absorb. A request above the cap is filled up to it, not rejected outright, the same way thin liquidity behaves. The returned ticket carries the filled size - read it back after selecting the ticket, the same way a real fill report works on either platform. Injected errors. MockQueueError pushes a one-shot error code (an MQL4 ERR_* value, or an MQL5 TRADE_RETCODE_* value) that the next trade call returns instead of acting, with no state change. Error-handling paths - retry loops, retryable-error branches, logging - become testable on demand instead of waiting for a bad connection to happen naturally. Partial close is modelled the way a live broker actually does it on both platforms: the closed slice becomes its own history record with its own ticket and realised profit, while the original ticket shrinks to whatever volume remains open. Floating profit on an open position is marked to the current quote each time it is read, via the same MockSetPrice call a test uses to move the market. Contract-size handling is deliberately the same naive figure on both platforms: profit = price-diff times volume times contract size, nothing else. This is a mock's fake profit model, stated plainly here rather than dressed up as anything SymbolInfoDouble-accurate. Both headers are self-contained. No dependencies, no DLLs, no classes on MQL4; a single lightweight struct-backed journal on MQL5 - either way a test script calls it directly with no setup beyond MockReset(). Notes and limitations - Market/position actions only. There is no pending-order pool on either platform - the simulator answers "does my fill and close logic work", not "does my pending-order state machine work". - No swap or commission. Profit is price difference times volume times contract size, nothing else. Add those in the caller if the test needs them. - Slippage is a uniform random draw between zero and the caller's own tolerance, always adverse - it exercises worst-case handling, it is not a statistically accurate slippage model. - Global state. All functions read and write module-level arrays, so a single terminal process shares one simulator. Call MockReset() at the start of every test to avoid one case leaking into the next. - Ticket numbers are assigned sequentially from 1 and never reused, including by the closed-slice ticket a partial close creates - this mirrors how real ticket numbers behave, monotonic and never recycled. - MQL5 only: no netting-merge simulation - see the differences section above.

Script · MT5 · v1.0 · Free

About Mock Trade Server

Mock Trade Server is an expert advisor (trading robot) for MetaTrader 5. It has 1 setting, such as Symbol. MQL source code included.

Files in the download: MockTradeServer.mq5.mqh, MockTradeServer.txt, MockTradeServerDemo.ex5, MockTradeServerDemo.mq5.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
SymbolEURUSDSymbol name used throughout the demo

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria