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.

Tuesday, November 17, 2009

Active MQL5 sites

Just thought I'd put in another plug for Metatrader 5, which is the most active Metatrader 5 blog, hosted by miranon. The latest post Indicator Update for MT5 is up to his usual standard, and he has kindly pointed to some of my work on this blog as well.

*** update 20/11/2009 *** http://www.mql5.com/ is now active!

The Russian forum  is bound to be as active as it was in mql4 and is quite readable with Google translate.

That's about it - if you google MQL5 or Metatrader 5 or MT5 you'll find lots of other sites such as Forex-TSD but although there are some interesting discussions, there's not much original MQL5 code.  If anyone reading this has another site they'd like to highlight, feel free to post a comment.

Monday, November 9, 2009

Auto-generated documentation for MQL5 code

I submitted this blog entry to Metaquotes when I wrote it, and I'm pleased to announce that they have now published it on the MQL5.com site.
The article includes a very useful MQL5 source code help file which I intend to update from time to time when new code is released.

Here's the introduction:

Most Java coders will be familiar with the auto-generated documentation that can be created with JavaDocs. The idea is to add comments into the code in a semi-structured way that can then be extracted into an easy to navigate help file.

The C++ world also has a number of documentation auto-generators, with Microsoft's SandCastle and Doxygen being two leaders. I decided to see how well Doxygen could document MQL5, which is in essence a customised subset of C++. To me this is an important step in the maturity of MQL5, because the complexity of the language is easily capable of fostering some quite large class libraries.

The experiment worked very well and I believe the help documentation that Doxygen produces from MQL5 code will add a great deal of value.

Thursday, November 5, 2009

NewsTrader

I was looking through my blog posts (yeah, I know, I'm about the only one who does) and I noticed that back a while I had promised a longer description of my NewsTrader.  V2.3 has been forward testing for quite a while now (since 4th September), and has made the princely sum of $82.  This was on a ridiculously small (for a news trader) lot size of 0.1, so I've just bumped it up to a more reasonable 1.0.  With a stoploss of 10 pips this gives a nominal risk of $100 per trade, although I have seen absolutely massive slippage on news in the 30 pip range.  Many traders really get upset by things like that, but with my IT background I've no real problem with it.  Big news events would stress the forex trading network in such a fundamental way that it's not surprising that serious overloads occur.

Anyway, NewsTrader V2_3 reads the news events from the weekly XML list kindly supplied by ForexFactory, and issues a very tight straddle just before each medium or high impact event.  What's a straddle?  It's a BuyStop / SellStop pair that is able to profit from a clean move in either direction.  If you look at the forward test, you'll see a number of indications of how this trader works:-
  • Large numbers of deleted stop orders.  This is when the price didn't immediately react to the news at all.
  • Stopped out orders at -10 pips.  This is when a spike in one direction preceeds the real direction of the move.
  • Closed orders in loss.  I have a timestop which from memory closes the orders after about 2 minutes (yes these trades are short!)
  • And, wonder of wonders, a number of profitable orders which hit the TP of 10 pips

NewsTrader V2_4
When I first wrote NewsTrader I was trading from my laptop in Australia.  I've made other comments about the ping times between Australia and the US: suffice to say the theory of News Trading is greatly undermined by network latency across the Pacific.  With the use of my virtual host in New York, I've now reinstated my original code which worked so badly in practice, which was a very tight client-side trailing stoploss.  I'm running 4 pips at the moment.  You wouldn't believe how long it takes for a MetaTrader 4 terminal in Australia to deliver tick updates and do something trivial like close an order.

So that's it: NewsTrader V2_4 is NewsTrader V2_3 with a trailing stoploss of 4 pips and a takeprofit of 30 pips (which will not normally be hit).  It remains to be seen how much better it will work on the faster network.  Forward tested here.

When I get a moment I might add auto screendumps of the news events as they occur.