MQL4 HTTP integration

MQL4 WebRequest setup, code, and runtime limits

WebRequest sends a synchronous HTTP request from an Expert Advisor or script. Add the destination URL to MT4 Options under Expert Advisors, set a finite timeout, check the returned HTTP status or minus one, and parse the response byte array according to its encoding.

HttpGetExample.mq4.mq4
#property strict
#property script_show_inputs

input string Url = "https://example.com";
input int TimeoutMs = 5000;

void OnStart()
  {
   char request_data[];
   char response[];
   string response_headers;

   ResetLastError();
   int status = WebRequest("GET", Url, "", TimeoutMs,
                           request_data, response, response_headers);

   if(status == -1)
     {
      Print("WebRequest failed. Error ", GetLastError(),
            ". Check the terminal URL allowlist.");
      return;
     }

   Print("HTTP status: ", status,
         ", response bytes: ", ArraySize(response));
  }
  • URLs require terminal allowlisting
  • The call is synchronous
  • Indicators cannot call WebRequest
  • Strategy Tester does not execute it

Terminal permission is part of the setup

MT4 requires destination URLs in the allowed list under Tools, Options, Expert Advisors. A valid source file can still return minus one at runtime when the URL is not allowed, TLS setup fails, DNS is unavailable, or the connection times out.

WebRequest automatically chooses the standard port from the protocol. Use HTTPS for sensitive data and avoid putting secrets in URLs or source files.

ResultMeaningNext check
-1MQL4 or transport failureGetLastError, allowlist, network, timeout
200 to 299HTTP success classExpected body and content type
400 to 499Client or authorization responseRequest, credentials, rate limit
500 to 599Server responseRetry policy and service status

Synchronous work must stay off sensitive paths

WebRequest blocks the calling MQL4 program until completion or timeout. It is allowed for EAs and scripts but not custom indicators, because indicators share execution threads across charts for a symbol.

The function is unavailable in the Strategy Tester. Separate signal logic from the transport boundary so tests can use deterministic inputs instead of silently skipping a live dependency.

  • Use a finite timeout and bounded retry schedule
  • Validate HTTP status before parsing the body
  • Check content type, encoding, length, and required fields
  • Never place a network call inside an indicator calculation loop

WebRequest is HTTP request-response, not a WebSocket client

The native API sends an HTTP request and returns one response. It does not expose a persistent WebSocket connection or asynchronous callback stream.

A bridge or DLL can expand connectivity, but that adds installation, security, architecture, and operational dependencies beyond a standalone single-file MQL4 program.

Review the complete example

The script performs a bounded GET request, distinguishes transport failure from an HTTP status, and reports the raw response length. The URL must be allowlisted in the terminal before execution.

The example makes a real network request when run. Do not embed secrets, trust unvalidated response data, or make a remote service a hidden prerequisite for trade safety.

HttpGetExample.mq4.mq4
#property strict
#property script_show_inputs

input string Url = "https://example.com";
input int TimeoutMs = 5000;

void OnStart()
  {
   char request_data[];
   char response[];
   string response_headers;

   ResetLastError();
   int status = WebRequest("GET", Url, "", TimeoutMs,
                           request_data, response, response_headers);

   if(status == -1)
     {
      Print("WebRequest failed. Error ", GetLastError(),
            ". Check the terminal URL allowlist.");
      return;
     }

   Print("HTTP status: ", status,
         ", response bytes: ", ArraySize(response));
  }
Where Pineify fits

Move from the rule to editable MQL4 source

Pineify can generate or revise one self-contained .mq4 file with MQL4-specific reference context. Its independent static checker returns MetaEditor diagnostics so you can repair source issues before testing in your own MT4 environment.

Open MQL4 Coding Agent

Static diagnostics do not prove runtime behavior, backtest results, broker compatibility, or live-trading safety.

Pineify MQL4 Coding Agent with MQL4 selected and an editable MetaTrader 4 code artifact

A practical workflow

  1. 1

    Allowlist the endpoint

    Add the HTTPS origin in the MT4 Expert Advisors options.

  2. 2

    Build the request

    Set method, headers, body encoding, and a finite timeout.

  3. 3

    Separate failure layers

    Handle minus one through GetLastError and HTTP errors through the returned status.

  4. 4

    Validate the response

    Check size, content type, encoding, schema, and stale-data policy before use.

Frequently asked questions

Why does MQL4 WebRequest return error 4014 or minus one?

A runtime restriction, missing URL allowlist entry, network failure, TLS issue, or timeout can cause failure. Read GetLastError immediately.

Can MQL4 indicators call WebRequest?

No. The official API prohibits WebRequest from custom indicators and reports error 4060 for that context.

Does WebRequest work in the Strategy Tester?

No. The official documentation states that WebRequest cannot be executed in the Strategy Tester.

Is WebRequest asynchronous?

No. It is synchronous and blocks the calling EA or script until completion or timeout.

Can native MQL4 WebRequest open a WebSocket?

No. It provides HTTP request-response calls, not a persistent native WebSocket connection.

Last verified against the linked official references on . Compile and test the final source in the MetaTrader 4 build, broker, symbol, and account environment you plan to use.

Continue with one reviewable MQL4 file

Define the requirement, inspect the generated source, repair diagnostics, and test the behavior in your own MT4 environment.

Open Coding Agent