MQL5, Python, Javascript integration. Two computers.

 

Hi all.

It is my understanding (please correct me if I'm wrong) that I can follow the instructions here to build the equivalent of an EA in python instead of MQL5, calling the python functions on that documentation page to handle all the broker interaction, and all the other logic and calculations and whatever else can be in my Python code.

Assuming I've understood that correctly, I want to extend that in the following way.

  1. Set up all the python stuff as per that documentation page, on a Windows PC (of course, because it won't run on a Mac or anywhere else) -- this is computer #1.
  2. Build a platform-independent (ie. PC, Mac, Linux) node/javascript app that can run on any other computer -- computer #2.
  3. Computer #2 needs to somehow call the python functions on computer #1 when the functionality they provide is needed (getting tick, order, deal, position, etc. info; sending orders; etc.), and do everything else an EA and indicators would do (all the math, decision making, etc), in the javascript code on computer #2.

So, two questions:

  • Question 1. I'm 99% certain this is possible.  I think it means setting up a web/API server on computer #1 (with node/python/both?) and then the JS app on computer #2 is a simple web client (browser, command line node app, etc.) that sends instructions to the computer #1 web/API server, just like any other kind of web based client-server app. However, that's just my best guess, so is that an ok approach?  1a. If it is, I don't know where to start, what to look up, how to even research it, let alone do it, or 1b. if that approach is significantly flawed, then what is the best/correct approach?
  • Question 2. I could be reading it wrong, but that documentation page seems to be focussed on using those MT5 python functions to collect data for external analysis. The suggestion there might be that these functions are not intended for interactive live algo trading. Perhaps they're too slow for the high paced information exchange and manipulation required for live trading...?  Or... is the idea of using these python functions for live algo trading perfectly reasonable?

Has anyone done any of this kind of thing?  Can anyone advise if it's reasonable and how I should approach this?

Thanks!

Documentation on MQL5: Integration / MetaTrader for Python
Documentation on MQL5: Integration / MetaTrader for Python
  • www.mql5.com
MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Your approach seems reasonable.

You will need to set up a web server in python as you said. It will interact as your backend.

You will need to design the whole API structure for interacting with your web client and build a complete REST to support your functionality.

All in all it is some overhead and therefore the system will not be as fast as native mql code. But it should give enough performance for most types of strategies.

Having such a long pipe between your decision logic and the execution on the brokers server will introduce lots of possible error sources.

There are projects on GitHub to support your going around the python backend. Look for melt json API.


 
Dominik Christian Egert #:
Your approach seems reasonable.

You will need to set up a web server in python as you said. It will interact as your backend.

You will need to design the whole API structure for interacting with your web client and build a complete REST to support your functionality.

All in all it is some overhead and therefore the system will not be as fast as native mql code. But it should give enough performance for most types of strategies.

Having such a long pipe between your decision logic and the execution on the brokers server will introduce lots of possible error sources.

There are projects on GitHub to support your going around the python backend. Look for melt json API.



Hi Dominik,

Thanks for your response.  I apologize I didn't reply sooner. Soon after posting this question something came up and I had to pause. I'm back now.  

Regarding Performance: 

> Having such a long pipe between your decision logic and the execution on the brokers server will introduce lots of possible error sources.

In any setup, the biggest bottleneck is between the MT5 computer and the broker (ie. connected over the internet/WAN), isn't it?  So if the client running the decision logic etc. has another computer on the same LAN that it's going through before getting to the broker, the communication between the two LAN computers is still going to be tens or hundreds of times faster than the connection to the broker... is that extra step really going to make any significant difference (compared with a more typical single PC setup)?

(Also, another option occurred to me, although I think it's lengthy enough that it warrants another post, so I'll ask that separately).

Thanks!

 
No problem. Life happens while we are having other plans...

I was not actually referring to the latency between the computers, but to the amount of code involved. Because of having so much code which has to work throughout the whole "chain" if connection, you introduce lots of sources for errors and inconsistencies.

That's what I was referring to.
 
Dominik Christian Egert #:
No problem. Life happens while we are having other plans...

I was not actually referring to the latency between the computers, but to the amount of code involved. Because of having so much code which has to work throughout the whole "chain" if connection, you introduce lots of sources for errors and inconsistencies.

That's what I was referring to.

And this time somehow I just missed that you replied again altogether, until now. Not sure how that happened, as I was checking this page for a couple of weeks after my previous reply. I must have had it cached or something?  Weird. Apologies again for (this time a much longer) delay!

Needless to say, thanks again for your explanations.  I've asked this and similar questions in multiple places and received an intriguing array of answers, but the most viable one has repeatedly come down to the same suggestion -- python based REST API server to call the mt5... functions and return the responses to whatever calls them.

With the help of someone who's written a few other python based REST API's using Flask, I've built one now and it seems to be working really well. It actually turned out very simple, not needing much design, just a simple, single endpoint that receives a POST request with a small json data object, for example: `{functionName: "login", args: {login: 1234567, password "asdfasdf", server: "SomeBrokerInc - Demo"} }`. And then uses `fn = getattr(mt5, functionName)` and `result = fn(**args)` to get the result, which it then returns. I'm still testing and refining it but so far so good.

On performance: Admittedly, I splashed out on the fastest M1 Max Mac currently available, and a high end near new core i9 PC, for some other work projects but running this on those is certainly helping with performance of course. As you said, gives "enough performance", at least so far.

So... Good outcome I think.  Thanks again.  Much appreciated!

Documentation on MQL5: Integration / MetaTrader for Python / login
Documentation on MQL5: Integration / MetaTrader for Python / login
  • www.mql5.com
login - MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5