mql-zmq Documentation

repository·master·Indexed 20 days ago

https://github.com/dingmaotu/mql-zmq

A ZeroMQ binding for MetaTrader (MT4 and MT5) that provides a C++ interface for ZMQ sockets. It includes support for UTF-8 string conversion, shared context management across MQL programs, and precompiled DLLs for 32-bit and 64-bit architectures.

Tokens
942
Snippets
1
Records
4
Agent score
22%

What's inside mql-zmq

  1. Best practices for ZMQ Context creation in MetaTrader

    master

    In ZeroMQ, a Context is the container for all sockets in a process. Since all Scripts and Expert Advisors in MetaTrader share the same terminal process, you should use a single global context across all your MQL programs to avoid having separate, isolated ZeroMQ instances.

    To achieve this, use the shared parameter of the Context constructor with a unique, hard-to-guess string to synchronize context creation and destruction across different MQL programs.

    Example shared name: __3kewducdxhkd__

  2. How string encoding works in mql-zmq

    master

    MQL strings are Win32 UNICODE (2-byte UTF-16). This binding automatically converts all MQL strings to UTF-8 before passing them to the DLL layer.

    When using the ZmqMsg constructor with MQL strings, note that the resulting message is not null-terminated by default.

  3. Install mql-zmq in MetaTrader

    master

    To use this binding, you must deploy three sets of files to your MetaTrader terminal directory:

    1. Include Files: Copy the contents of the Include/Zmq directory to your MetaTrader Include folder.
    2. DLLs: Copy the precompiled DLLs to your MetaTrader Library folder:
      • For MT5 (64-bit): Use the DLLs in Library/MT5.
      • For MT4 (32-bit): Use the DLLs in Library/MT4.
      • For MT5 (32-bit): Use the 32-bit DLLs from Library/MT4.
    3. Requirements: The DLLs require the latest Visual C++ runtime (2015).

    Special Configuration for MT5 32-bit: If you are using a 32-bit version of MetaTrader 5, you must comment out the __X64__ macro definition at the top of Include/Mql/Lang/Native.mqh to ensure correct pointer handling.

  4. Implement a Hello World Server in MQL

    master

    This example demonstrates how to bind a ZMQ_REP (Reply) socket to a TCP address, receive a request, and send a reply.

    Warning: Using socket.recv() in a script will block the MetaTrader script thread. If you attempt to terminate the script while it is blocked, MetaTrader may hang or crash.

    #include <Zmq/Zmq.mqh>
    //+------------------------------------------------------------------+
    //| Hello World server in MQL                                        |
    //| Binds REP socket to tcp://*:5555                                |
    //| Expects "Hello" from client, replies with "World"              |
    //+------------------------------------------------------------------+
    void OnStart()
      {
       Context context("helloworld");
       Socket socket(context,ZMQ_REP);
    
       socket.bind("tcp://*:5555");
    
       while(true)
         {
          ZmqMsg request;
    
          // Wait for next request from client
    
          // MetaTrader note: this will block the script thread
          // and if you try to terminate this script, MetaTrader
          // will hang (and crash if you force closing it)
          socket.recv(request);
          Print("Receive Hello");
    
          Sleep(1000);
    
          ZmqMsg reply("World");
          // Send reply back to client
          socket.send(reply);
         }
      }