Friday, November 20, 2009

Writing a Virtual Order Manager to enable hedging in MetaTrader 5

Since I wrote about a Virtual Order Manager in August, the MetaTrader 5 public beta commenced, and it has become clear that arguably the biggest change in the transition from MetaTrader 4 to MetaTrader 5 is the management of open trades as positions. At any one time there can be one position only open for each currency pair, and the size of this position adjusts up and down each time orders are processed by the broker. Apart from anything else, this aligns with the FIFO rules recently introduced in the US which effectively outlaw hedging.

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--)
   {
      OrderSelect(i,SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() == magic
         && (OrderType() == OP_BUY || OrderType() == OP_SELL)
         && OrderSymbol() == Symbol())
      {
         OrderCount++;
      }
   }
   return(OrderCount);
}

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:

Behaviour
  • 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.
Implementation

  • 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.
Challenges
Some 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.
Release
So when will version 1.0 be released? I would hope within two weeks.

10 comments:

  1. Yes Paul...I'm sure if MT5 is embraced by any Brokers or traders (I don't think many like it very much)...I will have to implement something similar in my personal EA's. The main impossible challenge is entering the initial Buy and Sell as they cancel each other out. The better possibiloity os to have two accounts one for buy another for sell...Talking Global. Bill

    ReplyDelete
  2. Great idea to have two accounts for buy and sell, Bill. Even that approach, ie running the same EA long only on one account, and short only on another account, would need some sort of global money management and position sizing module. It would end up being a lot simpler to code than the VOM I'm putting together, but doesn't discourage me from completing it.

    I personally like MQL5 a great deal. Assuming people get over the hedging issue, I think it will end up being strongly embraced by the fx community for lots of reasons - OO coding, rich indicator environment & speed to name a few. The 2010 trading championships promise to be an eye-opener to what can be done - I'll certainly be entering.

    ReplyDelete
  3. Yes the bad news is we'd need twice the money to fund two accounts... The good news is some Brokers are paying up to 6 percent interest on unused balances... This same Broker allows Hedgeing LOL...Not US NFA Brokers...when will the NFA get it?? After all the accounts have closed??

    ReplyDelete
  4. http://paulsfxrandomwalk.blogspot.com.au/2010/05/at-last-vom-article-has-been-published.html

    ReplyDelete
  5. I appreciate from your this post thanks for sharing with us. its seems looking so good.
    MetaTrader MT5

    ReplyDelete
  6. I appreciate with your this post thanks for sharing with. its seems looking so good.
    CFD

    ReplyDelete
  7. Are you monetizing your premium file uploads?
    Did you know AdWorkMedia will pay you an average of $0.50 per file download?

    ReplyDelete
  8. Forex trading is alot more easier when its done by a forex robot and EA Builder allows you to create your own free expert advisor.

    ReplyDelete