The Asirikuy Strategy Tester: Live Trading Through a Direct MT4 API

When I started the creation of the Asirikuy Strategy Tester one of my main goals was to create a full trading terminal that would provide us with both the ability to simulate and the ability to execute strategies outside of the Metatrader 4 platform. The reason for this is mainly that if we’re going to take the time to code strategies in a format which is compatible with the tester it would seem like a double amount of work if additional mql4 code would need to be done and maintained in order to execute the trading strategy on a live account.  In order for strategy evaluation to achieve all its goals it inevitably needs to be coupled with the possibility of executing the exact same code used for simulations on a live/demo account. On today’s post I will talk about the implementation of a direct MT4 API on the program and what the big advantages of having this possibility actually are.

As you know the MT4 platform communicates with the MT4 server using a proprietary API, allowing you to place automated trades with your broker exclusively through this platform. However through the years the interest of many of us in building a way to execute strategies on MT4 without the limits impose by the MT4 platform has grown and several people have tried (while some have succeeded) to implement a useful API for the MT4 platform. Since I didn’t have the time (or network protocol reverse engineering skills) to build one API for Asirikuy myself, I decided to look online and buy an API that best suited our needs for independent execution. The final choices I had were a .Net API (source not included) and a Delphi 7 API (source included and not available to the public) so I finally chose the second one since it was much more compatible with the Asirikuy Tester implementation (which is based on FreePascal and coded in Lazaurs — inherently compatible with the API’s Delphi DLL).

Once I had the API it was time to port it to Delphi 2010 (a long process that took me almost a week mainly because of Delphi 2010/7 differences regarding unicode strings) and build the appropriate mechanism to execute strategies built inside the tester on MT4 servers. This is when I started the creation of a multi-threading module for the tester which would create separate threads for each “instance” and a user interface which would allow you to select the strategy you wanted to trade, time frames, currency pairs to get data from, input MT4 login credentials, etc. After a lot of work I finished the first implementation of the Asirikuy Tester live trading module which allows a user to create N instances and execute them across X brokers with the possibility to load as many instances as needed on any combination of accounts. So far the execution of live strategies on demo servers has been “smooth” no “crashes” and error logging has been very complete and within what we would expect for a program which will do live execution. Trades have been opened/closed, historical data has been gathered without problems and therefore the main aspects of the tester (at least to discover other bugs) are already implemented.

Sadly this API solution  isn’t as complete as I would have wanted it to be since we’re limited to windows due to some characteristics of the DLL API. Not only does it make extensive use of Windows API functions (for process handling) but it also uses some libraries and other aspects of the MT4 code which make it inherently incompatible with any platform different than Windows (the API loads a small “terminal clone program” through which it does trading, this is by no means a “full blown MT4 terminal” but a very small terminal program that runs on the background for each instance). So sadly the strategy tester will NOT be able to work under platforms different than Windows (with this API at least) and therefore strategy execution will be limited to windows machines. The use of these MT4 libraries and the small background terminal seem to be caused by the need of the developers to find the most reliable and compatible solution regarding both connectivity and strategy execution. I spoke to one of the developers who mentioned that this allowed them to have a much better control of connection status and order error handling while having a very transparent set of functions for the user.

As far as I have been able to see – and although I needed to do a lot of tidying and modifications to migrate the setup to Delphi 2010 – the API does a great job at providing us with a transparent way to communicate with an MT4 server without the inherent limitations of the MT4 platform. Using the API’s simple functions you can connect to as many MT4 servers as you want and using simple functions analogous to MT4 such as OrderSend you can easily place orders and get any error messages generated either by the API or by the order placement within the MT4 server. The API logs every error and returns it to you with both a code and a readable message so you’re able to easily see everything which is going on and you can easily be aware of things such as TradeContextbusy or No connection errors when using the API.

The Asirikuy strategy tester builds its strategy execution mechanism for MT4 servers around this API and provides some additional “native” mechanisms which are not present in MT4 through this wrapping. For example the Asirikuy Strategy Tester will automatically log any error messages on trade opening, modify or closing (something not done by MT4) and will automatically retry any failed orderSend, Modify or Close commands without the need of the user to attempt them again. There are a couple of errors on which the tester will not retry order execution (such as when an Orderclose gives an “invalid ticket” message) as some errors simply imply that the given command cannot be executed no matter how “hard” you try to get them through. Obviously the execution of orders within the Asirikuy strategy tester is still limited by what the MT4 server “can do” so you cannot effectively execute two orders on the same account at the same time so orders will go through in a sequential manner as they do so in any MT4 server (even across separate terminals). Note however that one of the big advantages of our implementation  is asynchronous execution, which means that you can execute an automated strategy whenever you want and not just on every “tick”. 

With this API in place (and the Asirikuy Tester implementing it) it also made sense to implement a small manual live trading tab so that users could execute some manual commands on an account if this became necessary. The Asirikuy Strategy Tester therefore has some options which allow a user to manually open, close or modify positions so that any intervention needed to run tests or to correct some problems might be done. This mechanism allowed me to easily test the ability of the systems to respond to order closes and how they handled their order counting and reporting mechanism. Using this feature you can also do “one click” trading and you can also do things such as closing all your positions with a single click.

Obviously I think that the current implementation of the Asirikuy Strategy Tester is NOT ready for true live trading (still a ton of testing, feature implementation and debugging needs to take place) but currently the tester has the ability to do something very important : execute basic strategies and commands on an MT4 server, doing this in a multi-threaded , multi-core enabled way and therefore allowing the execution of strategies on many different accounts through a single trading interface. The tester also allows the user to execute basic manual trading commands and also has basic reporting commands such as the ability to query open trades, trade history or symbol history with the click of a button.

The Asirikuy tester’s first official release will be made this weekend to the Asirikuy community and after this there will most likely be months of testing before we actually have the first terminal which we consider reliable enough for true live trading with real money. Right now it’s a lot of fun to implement and test all these features and hopefully going forward this tool will be a great addition to the Asirikuy community. If you would like to learn more about our trading community and learn about trading through education and understanding please consider joining, 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.

6 Responses to “The Asirikuy Strategy Tester: Live Trading Through a Direct MT4 API”

  1. Franco says:

    Excellent Daniel thanks for all your hard work. Let’s hope brokers will support MT4 for at least the next three years.

    One question, how will Myfxbook work with the MT4 API implementation?

    • admin says:

      Hi Franco,

      Thank you for your comment :o) Well brokers are bound to support MT4 at least for the next 3-5 years (hey, some still support MT3) mainly because a lot of people are using MT4 and since MT5 is totally different migration is bound to be slow. The good thing is that we can always implement new API structures so trading through FIX, JForex, MT5, etc should not be a problem in the future (of course, we would need to get an API for MT5). Regarding MyFXBook, they have a direct account updating procedure so it doesn’t matter how we execute strategies the information from accounts is obtained directly through the MT4 server by the MyFXBook website. I hope this answers your questions,

      Best Regards,


  2. F DCG says:

    Hi Daniel,

    Good show indeed, but .. I think you can see the question coming ..;-)
    Would the more expensive API have had the same drawback re. the multiplatform issue (or rather the non-multiplatform-ness)?

    If so i can understand the choice. If not would it not be worth the additional investment? My offer still stands off course ;-)

    Nonetheless, (y)


    • admin says:

      Hi Fermin,

      Thank you for your post :o) The more expensive API is also only Windows compatible due to similar issues with the additional problem of not making the source code available and being inherently incompatible with FreePascal due to its .Net nature. Since the Delphi choice had the source available, was an exclusive sale (not available publicly) and was much cheaper, I decided to go ahead with it instead of the other one. Thanks again for posting!

      Best Regards,


  3. ahmet says:

    Dear Daniel,

    Thanks for sharing and seems that there is a progress.
    Is it possible for you to share the results you reached for name of the 2 APIs? ( .NET and Delphi)

  4. Kieran says:

    Hi Daniel,

    Very interesting article, what is the name of the API’s that you looked at? You mentioned a .NET and a Delphi one. I have looked around but have been unable to find any Direct API’s anywhere.



Leave a Reply

internal_server_error <![CDATA[WordPress &rsaquo; Error]]> 500