Apache License 2.0

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

分析:dingmaotu/mql-zmq

项目背景与痛点

在金融市场,交易策略的执行速度和稳定性至关重要。MQL语言(MetaQuotes Language)则是MetaTrader交易平台上用于编写自定义交易自动化脚本的主要语言,广泛应用于外汇、股票及加密货币市场。ZMQ(ZeroMQ)是一个高性能的异步消息库,提供了诸如发布/订阅、请求/回复等高级模式,以实现进程间的通信。

然而,MetaTrader平台本身并不支持ZMQ,其封闭的架构以及对第三方库的支持有限,使开发者难以利用ZMQ的高效通信能力。dingmaotu/mql-zmq 库正是基于这一痛点而开发,为MetaTrader 4和5平台提供了ZMQ绑定,使开发者能够在MQL语言中调用ZMQ,从而实现高效的跨进程和跨平台通信。

核心技术揭秘

MQL-ZMQ 的实现涉及多个层次的设计和优化:

  1. ZMQ绑定:通过封装ZMQ接口,提供易于使用且与MQL语言兼容的接口。
  2. 线程安全:考虑到MetaTrader环境的多线程特性,MQL-ZMQ在实现中采用了同步和异步模式,避免了潜在的线程竞争问题。
  3. 内存管理:优化了ZMQ套接字的资源管理方式,确保在不同环境中的稳定性。

MQL-ZMQ 技术架构

下面的Mermaid图展示了MQL-ZMQ主要的技术架构和数据流:

graph TD A[用户请求ZMQ连接] --> B("创建ZMQ上下文") B --> C("配置ZMQ套接字") C --> D{"连接请求"} D -->|"成功"| E[执行业务逻辑] E --> F[发送消息] D -->|"失败"| G[返回错误] F --> H("接收消息") H --> I[消息处理] I --> J[业务逻辑返回]

功能亮点与差异

MQL-ZMQ相对于现有竞品的突出之处在于:

  • 全面支持MetaTrader 4和5:同时提供32位和64位支持,满足不同平台的需求。
  • 深度优化MQL环境中的网络通信:经过针对性的性能调优,使得在网络I/O方面表现卓越。
  • 灵活的接口设计:提供丰富的API接口,适用于多种业务场景。

应用场景与落地建议

在实际应用中,开发者应注意:

  • 网络稳定性:确保ZMQ连接的高可用性,尤其是在并发量大的情况下。
  • 资源管理:合理分配并释放ZMQ套接字资源,避免资源泄露。
  • 性能监控:实施性能监控,及时发现并解决网络瓶颈等问题。

MQL-ZMQ适合应用于需要高效跨平台通信的金融策略自动化系统,特别是在高频交易和多线程环境中效果显著。

综合评价

优势方面,MQL-ZMQ有效地弥合了MetaTrader平台与高性能通信库ZMQ之间的技术鸿沟,其优化的网络性能和灵活的接口设计显著增强了交易策略执行的效率与稳定性。与此同时,它也存在一些不足,诸如性能监控工具的缺失以及文档仍需进一步完善。

总体而言,MQL-ZMQ是一个值得推荐的项目,特别适用于需要强大实时网络通信能力的MetaTrader自动化策略开发者社区。

关于

GitHub 原始描述(英文)

ZMQ binding for the MQL language (both 32bit MT4 and 64bit MT5)

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
1465
价值分
531
活跃状态
活跃
暂无主题

语言占比

MQL4
MQL5

README

mql-zmq

ZMQ binding for the MQL language (both 32bit MT4 and 64bit MT5)

Introduction

This is a complete binding of the ZeroMQ library for the MQL4/5 language provided by MetaTrader4/5.

Traders with programming abilities have always wanted a messaging solution like ZeroMQ, simple and powerful, far better than the PIPE trick as suggested by the official articles. However, bindings for MQL were either outdated or not complete (mostly toy projects and only basic features are implemented). This binding is based on latest 4.2 version of the library, and provides all functionalities as specified in the API documentation.

This binding tries to remain compatible between MQL4/5. Users of both versions can use this binding, with a single set of headers. MQL4 and MQL5 are basically the same in that they are merged in recent versions. The difference is in the runtime environment (MetaTrader5 is 64bit by default, while MetaTrader4 is 32bit). The trading system is also different, but it is no concern of this binding.

Files and Installation

This binding contains three sets of files:

  1. The binding itself is in the Include/Zmq directory. Note that there is a Mql directory in Include, which is part of the mql4-lib. Previous Common.mqh and GlobalHandle.mqh are actually from this library. At release 1.4, this becomes a direct reference, with mql4-lib content copied here verbatim. It is recommended you install the full mql4-lib, as it contains a lot other features. But for those who want to use mql-zmq alone, it is OK to deploy only the small subset included here.

  2. The testing scripts and zmq guide examples are in Scripts directory. The script files are mq4 by default, but you can change the extension to mq5 to use them in MetaTrader5.

  3. Precompiled DLLs of both 64bit (Library/MT5) and 32bit (Library/MT4) ZeroMQ (4.2.0) and libsodium (1.0.11) are provided. Copy the corresponding DLLs to the Library folder of your MetaTrader terminal. If you are using MT5 32bit, use the 32bit version from Library/MT4. The DLLs require that you have the latest Visual C++ runtime (2015).

    Note that if you are using MT5 32bit, you need to comment out the __X64__ macro definition at the top of the Include/Mql/Lang/Native.mqh. I assume MT5 is 64 bit, since their is no way to detect 32 bit by native macros, and to define pointer related values a macro like this is required.

    Note that these DLLs are compiled from official sources, without any modification. You can compile your own if you don’t trust these binaries. The libsodium.dll is copied from the official binary release. If you want to support security mechanisms other than curve, or you want to use transports like OpenPGM, you need to compile your own DLL.

    Note for WINE users, if the default binaries do not work for you, you can try the binaries in the Library/VC2010 directory. The new binaries are a little newer (libzmq 4.2.2 and libsodium 1.0.36). They are compiled with Visual C++ 2010 Express SP1 (using the Windows SDK 7.1), and supposed to be more compatible to WINE than the VS2015 version. They depend on VC2010 runtime (msvcr100.dll and msvcp100.dll). I have actually tested the old and the new DLLs on WINE 2.0.3 (Debian Jessie PlayOnLinux 32bit with MetaTrader4 build 1090) and they both work. So it is not guarenteed but it is nice to have an alternative. The new libzmq.dll only runs on vista or newer windows because I turned on the using poll option. This improves performance a little bit. Since MetaTrader4 officially no longer supports Windows XP, I assume this would not be a problem.

About string encoding

MQL strings are Win32 UNICODE strings (basically 2-byte UTF-16). In this binding all strings are converted to utf-8 strings before sending to the dll layer. The ZmqMsg supports a constructor from MQL strings, the default is NOT null-terminated.

Notes on context creation

In the official guide:

You should create and use exactly one context in your process. Technically, the context is the container for all sockets in a single process, and acts as the transport for inproc sockets, which are the fastest way to connect threads in one process. If at runtime a process has two contexts, these are like separate ZeroMQ instances.

In MetaTrader, every Script and Expert Advsior has its own thread, but they all share a process, that is the Terminal. So it is advised to use a single global context on all your MQL programs. The shared parameter of Context is used for sychronization of context creation and destruction. It is better named globally, and in a manner not easily recognized by humans, for example: __3kewducdxhkd__

Usage

You can find a simple test script in Scripts/Test, and you can find examples of the official guide in Scripts/ZeroMQGuideExamples. I intend to translate all examples to this binding, but now only the hello world example is provided. I will gradually add those examples. Of course forking this binding if you are interested and welcome to send pull requests.

Here is a sample from HelloWorldServer.mq4:

#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);
     }
  }

TODO

  1. Write more tests.
  2. Add more examples from the official ZMQ guide.
  3. More documentation
  4. High level API for common patterns

Changes

  • 2017-10-28: Released 1.5: Important: API change for Socket.send; Remove PollItem duplicate API (#11); Fix compiler warning (#10) and compile failure (#12); Add RTReq example from ZMQ Guide Chapter 3.
  • 2017-08-18: Released 1.4: Fix ZmqMsg setData bug; Change License to Apache 2.0; Inlcude mql4-lib dependencies directly.
  • 2017-07-18: Released 1.3: Refactored poll support; Add Chapter 2 examples from the official ZMQ guide.
  • 2017-06-08: Released 1.2: Fix GlobalHandle bug; Add rebuild method to ZmqMsg; Complete all examples in ZMQ Guide Chapter 1.
  • 2017-05-26: Released 1.1: add the ability to share a ZMQ context globally in a terminal
  • 2016-12-27: Released 1.0.

Donation

This binding is created in spare time and the author answers questions, solves issues, and intends to maintain the code so that bugs are fixed and the binding is kept up to date with official releases. If you think that the binding is useful to you or your organization, please consider donate to the author for his effort to maintain this binding. Thanks!

paypal

alipay

评论

暂无评论