node.jsmql4metatrader4mql5

How to post from MetaTrader Terminal 5 MQL 5 a request to my nodejs server, which is running locally on my MT5 host?


I'm trying to get FX rates in my nodejs server and socke.io emit them to the client, while running MetaTrader Terminal 5 or 4.

So I guess I have to use MQL4/5. I know how the handle the request in my nodejs server. What I dont know is where to write the MQL4 code, what to config in my MetaTrader Terminal.

Lets say I want to send EUR/USD bid rate to my nodejs server everytime it gets changed. How do I achieve that, using MT4/5 and MQL4/5?

My nodejs code:

app.post('/fxroute', (req, res) => {
   console.log(req);
   let fxRates = req.body // dont know if the payload will be in body
   socket.emit('fxRates', fxRates);
});

MQL5 script:

#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart(){
     string  headers;
     char    data[],
             result[];
     string             str = "data=value";        // POST-data, variables to send    
     StringToCharArray( str,                data );
     string          b = CharArrayToString( data );
     Print( "Test:", b ); // just a test of data, if good ... OK, data was setup correctly.

     WebRequest( "POST",
                 "http://localhost:3000/fxroute",
                 NULL,
                 NULL,
                 3000,
                 data,
                 ArraySize( data ),
                 result,
                 headers
                 );
     Print( CharArrayToString( result ) );   // see the results
                                             // it returns
                                             // "Results:" No posted data.
  }

When I compile and run, I see that it was executed in MT Experts tab, but on my nodejs server, console logs nothing.


Solution

  • Plan of work:

    1. Enable MT4/5 to use {http:|https:} transport-class to selected targets

    2. Create MT4/5 code to execute some kind of {http:|https:} based service

    3. Implement end-to-end logic to be wrapped + hidden inside the dumb http-protocol exchanges


    1) Terminal permissions:

    Using Terminal->Tools->Options enable [x] "Allow a WebRequest URL" to use a localhost {http:|https:} URL of your choice, matching the nodejs-server setup, in the list enter image description here

    2) WebRequest() code inside event-loop

    Given your intentions, create an MQL4 script, using either a built-in IDE F4 or using an external editor of your choice and save the produced .mq4 script file in ~an_MT4_Terminal_Home_Directory/MQL4/Scripts directory

    The event-loop is principally your design job:

    int start() {
        while !isStopped() {                            // ACK LOOP
               if ( RefreshRates() ) {                  // NEW QUOTE has arrived
                    ...                                 // JOB PROCESS Bid
                    int aHttpRetCODE = WebRequest(...); // SIG-> NodeJS Server
                    ...                                 // JOB PROCESS Response ( if a bi-directional service )
               }
               else {
                    Sleep(...);                         // NOP on NACK, Terminal has nothing to do
               }
        }
    }
    

    For further details, may like to check my other posts on WebRequest() use-cases and warnings about it's principal limitations.

    3) end-to-end logic

    Here comes the creme-ala-creme of your design.


    Is there any other way?

    Yes, there is. That would be the one of my choice - using ZeroMQ or nanomsg on both sides ( MT4/5 Terminal & NodeJS ), thus being able to fully enjoy the freedom of a full-scale distributed systems design ( check the principal aMiniRESPONDER()-prototype example structure for [SIG,MSG] jobs in fully distributed systems ) .