This morning – while working on the F4 framework with Morgan – I was reminded of a project that I had stopped working on about a month ago. This project – that had become very frustrating – was the development of an order wrapper library for the Metatrader 4 platform in order to allow for the virtual taking of multiple orders while remaining in an NFA compliant environment Essentially what I want to achieve with this is to create a library in which positions can be taken in any way that is required while keeping only one net position opened per symbol. This way you can trade your systems in whatever way you wish while not violating the limitations imposed by NFA brokers. On today’s post I want to discuss why I found this problem so challenging and what solutions I will be trying soon in order to circumvent the issues found.
My original idea to create an NFA compliant virtual order manager for MT4 centered on the use of an external database which I used to store order information. This seemed like the best idea since you want to have a place where you can have organized virtual order information in a way that is easily accessible and which allows you to carry out modifications without any problems. The idea was quite simple, you wrap all the relevant MT4 order sending and querying commands – like OrderSend(), OrderSelect(), etc – so that they get redirected through a library that performs the appropriate task. When a system opens a position the library creates an entry in the database and then the library opens/closes a given lot size on the requested symbol to match the required net positioning approach. So if a strategy opens up a long for 0.01 and another opens up a short for 0.02 lots you will have two virtual orders in the database – a long for 0.01 and a short for 0.02 – but in reality you will only be short 0.01 lots on the EUR/USD. Perfect (?).
Well, not quite. While I was programming this implementation it soon became apparent that things are not as easy as they appear because you have two asynchronous places were trades are being taken. There is a virtual order-book where trades are “taken” and there is the real trading scenario where trades are really taken. The problem is that these two should match all the time, otherwise you will run into some very nasty problems. For example I realized that you should always open virtual orders first because if for some reason the net positioning part of the wrapper fails to open up the order (a crash in the middle for example) it will be obvious that the trade wasn’t opened properly because the open position will not match the sum of the lot size of the opened virtual positions. You can then open whatever is needed to correct the positioning — although obviously the price you get will not be the same you intended. The same applies to exits.
However my problems with the database engine started to become bad when I started to put the implementation to the test. When two or three instances were running there were database access problems because sometimes they all wanted to write to the DB at the same time. There were also problems related with the synchronization of trades when two opposing trades were opened at the same time because sometimes the synchronization reacted in the middle of the process and several modifications to the net positioning were done. For example if a long for 0.05 lots and a short for 0.05 lots were opened at the same time, the synchronization engine sometimes opened a long and then closed it, incurring in an unnecessary trading cost. Additionally it soon became clear that the cost of a DLL cost and DB access would be problematic when doing significant amounts of testing since the code added significant overhead to the computational cost of running the system implementations. The delay between virtual order recording and the synchronization of the net positioning also meant significant slippage, something which was simply not acceptable.
I was ready to give up the idea of an MT4 order wrapper but we then started to discuss with Morgan the fact that we will need a wrapper anyway when we build the front-end for MT5. Since MT5 uses a natural net positioning approach, it is obvious that if you want to trade several systems you will need someway to manage your open orders. Then I realized that if this is something many people will need, someone had probably already done it. After some simple google searching I then found the MQL5 Virtual Order Manager, which implements the exact same concept I wanted to implement on MT4 (but for MT5). The approach taken here is quite different and the results are quite good, this wrapper – as far as I could see – is able to do everything I wanted to do without the need for an external DB or DLL calls. The great thing is that – with this implementation already available – the building of the MT5 front-end will most likely be a breeze.
The approach used by this coder was actually quite simple, using global variables inside the platform to store order information. Although this is something I dreaded – because global variable use seems somehow disorganized when you can use a DB – the approach actually faces much less problems than my far more sophisticated (and yet worthless) approach using an external DLL and a proper DB implementation. Although the approach in MT5 is much simpler by nature – because net positioning naturally shifts from long to short – I can see the idea implemented in MT4 without any major problems. This will be much faster than using a DLL and will allow for much less problems dealing with synchronization issues.
My next try for the implementation of the MT4 NFA compliant order wrapper will now be much simpler, trying to keep things to the MT4 platform in order to wrap all order functions in the smoothest possible way. I guess you guys will have a ton of suggestions for the implementation of the Order wrapper, because many of you are excellent coders :o) Feel free to leave a comment with your opinion and any advice you think will be useful in the coding of this library. Hopefully within the next few weeks I will have a shiny implementation to show to the community :o)
If you would like to learn more about trading systems and how you too can learn to code and analyse your own trading strategies please consider joining Asirikuy.com, a website filled with educational videos, trading systems, development and a sound, honest and transparent approach towards automated trading in general . I hope you enjoyed this article ! :o)