To work in this position-centric environment, each Expert Advisor written in MQL5 needs additional programming to record which trades it has open, because it cannot find this out in a simple way from the broker. The most startling example of this would be when two EAs running against the same pair, say a scalper and a trend-follower, issue orders in opposite directions. In MT4, the result would be a long and short order with zero margin used. In MT5, no position would be open at all.
An example of the programming challenge is in the following function. Most people would use something similar in their MT4 EAs to ensure that only one order is open at a time. A similar function would not work as intended in MT5.
int OpenOrders(int magic)
int OrderCount = 0;
for (int i = OpenOrdersTotal()-1 ; i >= 0 ; i--)
if (OrderMagicNumber() == magic
&& (OrderType() == OP_BUY || OrderType() == OP_SELL)
&& OrderSymbol() == Symbol())
So ... here comes a Virtual Order Manager
To work with this new order management environment I am writing a utility which I call a Virtual Order Manager, or VOM, which maintains a local record at the MetaTrader terminal of the orders issued by each EA.
Here are the attributes of the VOM:
- Real orders at the broker reflect total position for that pair, but the VOM also maintains a virtual set of orders at the MT5 terminal. As an example one EA could be virtual long EURUSD 0.2 lots and another EA could be virtual short EURUSD 0.3 lots, with the real position showing in the MT5 open orders list as short 0.1 lots.
- Virtual stoplosses, takeprofits, magic numbers and comments are supported. Virtual stoplosses and takeprofits can be modified
- Real broker stoplosses are maintained at a configurable distance away from the virtual stoplosses as disaster protection against PC or internet connection failures.
- Virtual entry prices, stoplosses and takeprofits can optionally be displayed as horizontal lines on the chart.
- [for eventual development] A virtual_statement.htm can be produced similar to the normal statement.htm which lists open orders and closed order history.
- The VOM functionality is added with an #include
in each EA.
- I am modelling the API for the VOM on MT4 trading functions. These are familiar, proven functions which will make migration from MT4 EAs easier. So instead of writing OrderSend(….), one writes VirtualOrderManager.OrderSend(…..). I have simplified some of the functions so they are not necessarily a plug-in replacement, but I may also implement the original full versions to make MT4 migration even easier.
- The list of open orders is maintained in a file for all VOM-enabled EAs to access. A table listing all open orders will be viewable using a OpenVirtualOrderViewer.mq5 indicator.
ChallengesSome of the more challenging parts of writing the VOM are listed here
- The VOM must be able to track when a pending order activates. I am using the OnTrade() event in MT5 for this purpose.
- Since multiple EAs read and write to the one file listing virtual open orders, I have to use a global variable as a file locking mechanism.
- The value of the disaster protection stoploss is not easy to determine because it is obtained from a number of virtual orders.
So when will version 1.0 be released? I would hope within two weeks.