Connect node.js to your MT4 Expert Advisor for a two-way realtime communication

You may think about the smartest, fastest and most reliable way to connect your node.js application with your Metatrader 4 instance. As I already went through the whole process of testing, approving and productive usage I will tell you the best way to connect MT4 to your node.js application.

As time is money I will start with the result without wasting your and my time telling you about the ways that did not work.

My personal advice: Use the built in HTTP requests in Metratrader. No addons, no libs. Simple, plain HTTP requests.

Now some of you might say “I want my node.js application to trigger actions in MT4  and not MT4 to trigger actions in node.js!”

My answer: Be patient, give me one more second. Assuming that you want your node.js application to trigger decisions in a random, unpredictable moment, polling your node.js application from MT4 every e.g. 1000ms is not the key (as you can not act within those 1000ms but you have to wait for the next request. For those who say now: “Ok, let’s poll every 1ms” let me give you an advice: Don’t do it). For realtime communication there is – if you use the built-in MT4 HTTP requests towards node.js – only one solution: node.js parks the incoming HTTP request and answers it exactly then, when your node.js application wants to trigger an action in MT4.

It’s no high-sophisticated philiosophy, but sometimes you have to go a long way to see the beauty of simplicity.

This leads to some problems: The thread within the MT4 chart where your EA is waiting for the answer from your node.js application is blocked and can not be used for sending ticks to your node.js application. Simple solution for this problem: Use one chart for sending the ticks from MT4 to node.js and another chart for waiting for the responses from node.js which will tell your MT4 Expert Advisor whether to buy or to sell.

Dont’t think the story stops here. When you will be happy that this system works great – you will suddenly face another problem if your node.js application wants to send several decisions per second: Your MT4 chart that is processing your last response will not be fast enough to process your last response and immediately open a new HTTP request towards your node.js application: There will not be any open HTTP request to be answered in your node.js application in a short timeframe after answering the last request.

This will lead to the following solution: You need 3 to 4 charts that will open waiting HTTP requests towards your node.js application which will be queued in your node.js application and served first in first out.

If you have trouble implementing this solution, or if you just do not have the time to do it yourself, do not hesitate to contact us.

In my next post I will describe how I built a node.js based FIX Protocol client for a direct FIX API 4.2 / 4.4 connection to LMAX.

Kind regards