DWX ZeroMQ Connector

repository·master·Indexed 18 days ago

https://github.com/darwinex/dwx-zeromq-connector

A high-performance bridge between Python 3 and MetaTrader 4 using ZeroMQ. It enables the development of trading strategies in external languages like Python and C++ while utilizing MT4 for market data and execution. Note: As of February 15, 2022, this project is archived and no longer actively maintained; users are encouraged to migrate to DWX Connect.

Tokens
2.1K
Snippets
5
Records
9
Agent score
13%

What's inside dwx-zeromq-connector

  1. Important usage constraints for DWX ZeroMQ Connector

    master

    To ensure stability and avoid errors, adhere to the following constraints:

    • No Notebooks: Do NOT run this code in Jupyter or IPython Notebooks. The project should be run as a standalone process (e.g., via a Python console or a batch process).
    • Standalone Execution: The project is designed to run as a standalone bridge.
    • Self-Sufficiency: The project provides the bridge, but users are responsible for their own Python and MQL implementation logic. The provided code is intended for educational purposes and should be modified to fit specific requirements.
  2. Subscribe to real-time market data

    master

    You can subscribe to real-time BID/ASK price updates for specific symbols. The process involves two steps:

    1. Calling _DWX_MTX_SUBSCRIBE_MARKETDATA_ to register the interest in a symbol.
    2. Calling _DWX_MTX_SEND_TRACKPRICES_REQUEST_ to instruct MetaTrader 4 to begin publishing that data.

    Once subscribed, the streaming data is stored in the _zmq._Market_Data_DB dictionary, where keys are timestamps and values are tuples of (bid, ask) prices.

    # subscribe to data:
    _zmq._DWX_MTX_SUBSCRIBE_MARKETDATA_('EURUSD')
    # tell MT4 to publish data:
    _zmq._DWX_MTX_SEND_TRACKPRICES_REQUEST_(['EURUSD'])
    
    # Access the streamed data
    print(_zmq._Market_Data_DB)
    
    # To stop receiving updates:
    _zmq._DWX_MTX_UNSUBSCRIBE_MARKETDATA('EURUSD')
  3. Configure the DWX ZeroMQ Server in MetaTrader 4

    master

    After installation, follow these steps to initialize the bridge:

    1. Restart MetaTrader 4 to load the new DLLs and include files.
    2. Attach the EA: Open a chart (e.g., EUR/USD) and drag and drop the DWX_ZeroMQ_Server_v2.0.1_RC8 Expert Advisor onto it.
    3. Configure Inputs: Open the EA's Inputs tab to customize settings.

    Note on Market Data: The Publish_MarketData variable has been removed. You do not need to manually modify the Publish_Symbols array. Symbols are automatically added to the publishing stream when the _DWX_MTX_SEND_TRACKPRICES_REQUEST_() function is called from your Python client.

  4. Install the DWX ZeroMQ Connector

    master

    The DWX ZeroMQ Connector acts as a high-performance bridge between Python 3 and MetaTrader 4 using ZeroMQ.

    Prerequisites

    • OS: Windows 10 (not tested on emulated environments like WINE or VMWare).
    • Python: Minimum v3.6.
    • System Libraries: Latest Visual C++ runtime (2015) libraries must be installed. Failure to install these will result in "Resource Timeout" errors because the DLLs (mql-zmq, libzmq, libsodium) require them.
    • Python Dependencies:
      • libzmq (min v4.2.5)
      • pyzmq (min v17.1.2)
      • libsodium
      • mql4-lib
      • mql-zmq

    You can install Python dependencies via pip:

    pip install -r ./v2.0.1/python/api/requirements.txt

    Setup Steps

    1. MQL Include Files: Download mql-zmq-master.zip. Copy the contents of mql-zmq-master/Include/Mql and mql-zmq-master/Include/Zmq into your MetaTrader installation's MQL4/Include directory.
    2. DLLs: Copy libsodium.dll and libzmq.dll from mql-zmq-master/Library/MT4 to your MetaTrader installation's MQL4/Libraries directory.
    3. Expert Advisor (Server): Download DWX_ZeroMQ_Server_vX.Y.Z_RCx.mq4 and place it in your MetaTrader MQL4/Experts directory.
    4. Python Client: Download v2.0.1 / python / api / DWX_ZeroMQ_Connector_v2_0_1_RC8.py to use in your Python environment.
  5. Manage open trades: Get, Close, and Partial Close

    master

    The connector provides several methods to manage active positions in MetaTrader:

    • Get all trades: Use _DWX_MTX_GET_ALL_OPEN_TRADES_() to retrieve a dictionary of all currently open trades.
    • Close a specific trade: Use _DWX_MTX_CLOSE_TRADE_BY_TICKET_(_ticket) to close a position by its unique ticket ID.
    • Partially close a trade: Use _DWX_MTX_CLOSE_PARTIAL_BY_TICKET_(_ticket, _lots) to close a specific amount of lots. Note: Partially closing a trade will result in a new ticket ID being generated for the remaining position; you must call _DWX_MTX_GET_ALL_OPEN_TRADES_() again to retrieve the updated ticket.
    • Close by Magic Number: Use _DWX_MTX_CLOSE_TRADES_BY_MAGIC_(_magic) to close all trades associated with a specific magic number.
    • Close all trades: Use _DWX_MTX_CLOSE_ALL_TRADES_() to close every open position.
    # Get all trades
    trades = _zmq._DWX_MTX_GET_ALL_OPEN_TRADES_()
    
    # Close a specific trade by ticket
    _zmq._DWX_MTX_CLOSE_TRADE_BY_TICKET_(85051856)
    
    # Partially close 0.01 lots of a trade
    _zmq._DWX_MTX_CLOSE_PARTIAL_BY_TICKET_(85051741, 0.01)
    
    # Close all trades with a specific magic number
    _zmq._DWX_MTX_CLOSE_TRADES_BY_MAGIC_(123456)
    
    # Close every open trade
    _zmq._DWX_MTX_CLOSE_ALL_TRADES_()
  6. Migrate from DWX ZeroMQ Connector to DWX Connect

    master

    As of 15 Feb 2022, the DWX ZeroMQ Connector is being archived and is no longer actively maintained. Users are encouraged to migrate to DWX Connect.

    DWX Connect advantages:

    • Removes the ZeroMQ dependency.
    • Provides native support for both MetaTrader 4 and MetaTrader 5.
    • Requires no 3rd-party dependencies.
  7. Construct and send a new trade to MetaTrader

    master

    To execute a trade, first generate a default order dictionary using _generate_default_order_dict(). You can then modify specific keys such as _lots, _SL (Stop Loss), _TP (Take Profit), and _comment before sending it to MetaTrader using _DWX_MTX_NEW_TRADE_(_order=...).

    # 1. Create the order template
    _my_trade = _zmq._generate_default_order_dict()
    
    # 2. Customize the trade parameters
    _my_trade['_lots'] = 0.05
    _my_trade['_SL'] = 250
    _my_trade['_TP'] = 750
    _my_trade['_comment'] = 'nerds_rox0r'
    
    # 3. Send the trade to MetaTrader
    response = _zmq._DWX_MTX_NEW_TRADE_(_order=_my_trade)
    print(response)
  8. Reference: DWX_ZeroMQ_Connector public methods

    master

    The following methods are available on the DWX_ZeroMQ_Connector instance for interacting with MetaTrader 4:

    DWX_MTX_NEW_TRADE_(self, _order=None)
    DWX_MTX_MODIFY_TRADE_BY_TICKET_(self, _ticket, _SL, _TP)
    DWX_MTX_CLOSE_TRADE_BY_TICKET_(self, _ticket)
    DWX_MTX_CLOSE_PARTIAL_BY_TICKET_(self, _ticket, _lots)
    DWX_MTX_CLOSE_TRADES_BY_MAGIC_(self, _magic)
    DWX_MTX_CLOSE_ALL_TRADES_(self)
    DWX_MTX_GET_ALL_OPEN_TRADES_(self)
    generate_default_order_dict(self)
    generate_default_data_dict(self)
    DWX_MTX_SEND_MARKETDATA_REQUEST_(self, _symbol, _timeframe, _start, _end)
    DWX_MTX_SEND_COMMAND_(self, _action, _type, _symbol, _price, _SL, _TP, _comment, _lots, _magic, _ticket)
    DWX_MTX_SUBSCRIBE_MARKETDATA_(self, _symbol, _string_delimiter=';')
    DWX_MTX_UNSUBSCRIBE_MARKETDATA_(self, _symbol)
    DWX_MTX_UNSUBSCRIBE_ALL_MARKETDATA_REQUESTS_(self)