An NFA Compliant MT4 Virtual Order Manager, Finally!

If you read the post I wrote a few days ago (post before this one on the blog) you might already know my problematic story with the building of an NFA compliant virtual order manager for the Metatrader 4 platform. There were many problems which were – at first – difficult to circumvent but with the help of Maxim’s and Fd’s suggestions (see their comments on the last post) I was able to build a (hopefully) reliable order wrapper for the MT4 platform. The wrapper has now been tested on a demo and a live account and seems to be behaving quite ideally, even when large numbers of instances (5-10) are running on the platforms. On today’s post I want to write about this project, how I solved the problems posted before and what particular benefits this new wrapper implementation will give Asirikuy members.

As it was mentioned on my last post, the idea of the NFA compliant wrapper was to build a library that replaces all built-in MT4 order related functions in a way that allows the creation of virtual order manager which manages trades from a net-positioning perspective. What you want is to be able to have trading that complies perfectly with NFA rules (no hedging and FIFO) while retaining the ability to trade any set of trades in a virtual manner. Since any hedging approach is equivalent to a net-position on a given instrument, there should be no problem in doing this from a theoretical point of view. However, in practice many problems became apparent,  particularly problems related with the nature of the implementation and the speed in which trades got executed. As I mentioned before my first approach was to build a database engine to manage the virtual trades, a terrible (and snail slow) idea.

Perhaps the most important hint to solving this problem was given by Fd, although I was quite sceptical when I first read his comment. He proposed me to create a transaction log based on a text file which I could then use to populate a virtual order manager, with a single thread being in charge of syncing the virtual and real positioning of the account. In this approach orders are never assigned directly to an order manager but they are written to a text file which is later read by functions relevant to order management (such as OrderSelect, OrderProfit, etc). I took some important elements from this idea and I made some modifications to make the implementation faster from a practical point of view.

The first thing I did was to build a virtual order log that contains a file with open positions, each time a system enters a new position a line with all relevant order information is added to this file. Additionally I created another file which I called a “virtual history log” where information from closed positions is placed. If you decide to close a position the information is removed from the virtual order log and placed inside the virtual history log. This way the order log dealing with currently open positions will always remain small while the virtual history log can grow as much as needed. Since looping of the history log is much less frequent, this speeds up the performance of the order manager significantly. This is also important since behaviour relevant to live trading (closing and modifying positions) usually uses the virtual order log and not the history log.

Once I had these two logs established I then created a wrapper for all the order query functions. The OrderSelect function I created builds a table from the virtual order log or virtual history logs (as requested) and selects a position, populating many global variables (global in the library not the program) which carry out information to populate functions such as OrderMagicNumber, OrderOpenPrice, etc. The selection process is very efficient and takes just as long as the regular OrderSelect function on the MT4 platform. Orders are reliably selected and information from both virtual open and historical positions can be obtained without any problems. Up until this was the “easy part” because most of these functions require simply read-only access.

I then created two functions that carry out important writing to the log files. The first one is a function that does the syncing of positioning, this is the function responsible for opening live positions to match the net positioning of the virtual trades. In order to be able to call this function from all systems – keeping only one active thread doing syncing – while avoiding the use of any additional expert or indicator, I created a mutex which ensures that only one thread accesses the sync positioning function. In the same manner I also created a function that checks whether the virtual SL/TP values have been reached and exits virtual positions accordingly. Both of these functions use a mutex and both can only be accessed by a single call of the library at a time. Since syncing addresses all symbols and does bidirectional synchronization (meaning that we synchronize virtual against real and then real against virtual (always seeking to match virtual)) there is no problem with the speed of syncing as it is always called every number of seconds (this is something the user can set, tested to 10 seconds).

In the end – thanks to the suggestions made – I created a (hopefully good) NFA compliant virtual order manager for the MT4 platform. The wrapper keeps an account’s real and net positioning values synced, allowing the use of non-NFA compliant trading setups on NFA compliant brokers. I have tested the wrapper implementation on the F4 framework and will seek to release a testing version to the Asirikuy community soon. The wrapper survives all sorts of stress tests, including the opening of additional positions that would skew its positioning values and the use of many concurring system instances. The setup is able to go back to normal (appropriately close or open new orders) to match its intended virtual positioning values. Slippage with this implementation is also very small although there might obviously be some long term small cost incurred due to the usage of the order manager (due to the slightly additional execution time).

If you would like to learn more about system building and how you to can build algorithmic strategies based on sound principles  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)

Print Friendly, PDF & Email
You can leave a response, or trackback from your own site.

One Response to “An NFA Compliant MT4 Virtual Order Manager, Finally!”

  1. Fd says:

    Hi Daniel,

    sounds quite good! Glad to hear my comments have been helpful. :)

    Best regards,
    Fd

Leave a Reply

Subscribe to RSS Feed Follow me on Twitter!
Show Buttons
Hide Buttons