Search code examples
mql5metatrader5

Testing WebRequest on MT5


I have an MQL5 object that sends a web request to a webserver:

class OrderSendMaster {
private:
   string   m_addr;     // The endpoint of the server we'll send trade requests to
   
public:
   
   OrderSendMaster(const string addr) : m_addr(addr) {};
   
   int Disperse(const MqlTradeRequest &request);
};

int OrderSendMaster::Disperse(const MqlTradeRequest &request) {

   // First, convert the trade request to JSON and write that to our buffer
   char req[];
   int len = ConvertToJSON(request, req);
   
   // Next, attempt to send the JSON data to our web server; if this fails then return an error
   char result[];
   string headers;
   int res = WebRequest("POST", m_addr, "", "", 1000, req, len, result, headers);
   if (res == -1) {
      res = GetLastError();
   }
   
   PrintFormat("Response Code: %d, Headers: %s, Response: %s", res, headers, CharArrayToString(result));

   // Finally, return 0 to indicate that all the operations succeeded if we got a 200 response code
   // Otherwise, return the response code we received
   return res == 200 ? 0 : res;
}

The issue I'm having is that, when testing the Expert I added it to, the function always returns an error code of 4014. I have followed the documentation and added the URL to Options > Expert Advisors > Allow Web Requests and this code is not used by anything except the EA I'm testing. Judging by what I've seen on the MQL5 forums here, here and here; the issue appears to be a problem with expert testing but I haven't seen any confirmation of that. Does anyone know why this issue is occurring and any workarounds or fixes for it?


Solution

  • As per the documentation WebRequest() cannot be executed in the Strategy Tester which is written in both MQL4 and MQL5 documentation but still working in MQL4. If you tested your EA in chart and still didn't work, try using a hostname instead of loopback address or the localhost as this solution worked for me.