简体   繁体   English

如何使用 ZeroMQ 从 Python 到 Metatrader 4 建立连接

[英]How can I get a connection from Python to Metatrader 4 using ZeroMQ

I want an Expert Advisor to open an Trade triggerd by a Telegram-Message.我想要一个智能交易系统来打开一个由电报消息触发的交易。

I succesfully set up an Hello-World application using MQ4 as Server and Python/Telegram-bot as Client.我成功地使用 MQ4 作为服务器和 Python/Telegram-bot 作为客户端设置了一个 Hello-World 应用程序。 When the Telegram-Bot recieves a Message, he will send a request to MQ4 and gets a simple response without executing a trade.当 Telegram-Bot 收到消息时,他会向 MQ4 发送请求并获得简单的响应,而无需执行交易。

Running Code below.下面运行代码。

#   Hello World client in Python
#   Connects REQ socket to tcp://localhost:5555

import zmq
context = zmq.Context()

#  Socket to talk to server
print("Connecting to trading server…")
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
print("Connecting to trading server succeed")


#################################################################################
# Use your own values from my.telegram.org
api_id = ######
api_hash = '#####'
bot_token = '#####'
#################################################################################

from telethon import TelegramClient, events

client = TelegramClient('anon', api_id, api_hash)

@client.on(events.NewMessage)
async def my_event_handler(event):


    if "Ascending" in event.raw_text:

        if "AUDUSD" in event.raw_text:
            await event.reply("AUDUSD sell")

            #  Do 1 request, waiting for a response
            for request in range(1):
                print("Telegram: AUDUSD sell execution requested %s …" % request)
                socket.send(b"AUDUSD Sell execute")
                #Send 2 variables (Ordertype // Symbol)

                #  Get the reply. -> Not neccesary for final application
                #  Apülication just needs to send 2 Varianles to MQ4 and trigger the open_order()
                message = socket.recv()
                print("Received reply %s [ %s ]" % (request, message))



client.start()
client.run_until_disconnected()

// Hello World server in MQ4

#include <Zmq/Zmq.mqh>

//+------------------------------------------------------------------+
 void OnTick()
  {
   Context context("helloworld");
   Socket socket(context,ZMQ_REP);

   socket.bind("tcp://*:5555");

   while(!IsStopped())
     {
      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: AUDUSD Sell execute");

      Sleep(1000);

      ZmqMsg reply("Trade was executed");
      // Send reply back to client
      socket.send(reply);
      Print("Feedback: Trade was executed");
     }
  }
//+------------------------------------------------------------------+

Now I want to send 2 variables from Python to MQ4.现在我想将 2 个变量从 Python 发送到 MQ4。 1. Ordertype: buy/sell 2. Symbol: EURUSD, AUDUSD,... 1. 订单类型:买入/卖出 2. 交易品种:EURUSD, AUDUSD,...

Send "Sell" if message contains "Ascending" - Send "Buy" if message contains "Descending"如果消息包含“升序”,则发送“卖出” - 如果消息包含“降序”,则发送“买入”

Send "AUDUSD" if message contains "AUDUSD",...如果消息包含“AUDUSD”,则发送“AUDUSD”,...

To do so I found a Library from Darwinex and want to combine it (interpretation of message, sending value as an array) with my already functioning telegram-bot.为此,我从 Darwinex 找到了一个库,并希望将它(消息的解释,将值作为数组发送)与我已经运行的电报机器人相结合。


For testing I wanted to try the example-code from Darwinex by itself.为了测试,我想单独尝试来自 Darwinex 的示例代码。

I found the Code v2.0.1:我找到了代码 v2.0.1:

Python: https://github.com/darwinex/DarwinexLabs/blob/master/tools/dwx_zeromq_connector/v2.0.1/Python/DWX_ZeroMQ_Connector_v2_0_1_RC8.py Python: https : //github.com/darwinex/DarwinexLabs/blob/master/tools/dwx_zeromq_connector/v2.0.1/Python/DWX_ZeroMQ_Connector_v2_0_1_RC8.py

MQ4: (Note: This Library code may replace the whole MQ4 code above in final app.) https://github.com/darwinex/DarwinexLabs/blob/master/tools/dwx_zeromq_connector/v2.0.1/MQL4/DWX_ZeroMQ_Server_v2.0.1_RC8.mq4 MQ4:(注意:此库代码可能会替换最终应用程序中上面的整个 MQ4 代码。) https://github.com/darwinex/DarwinexLabs/blob/master/tools/dwx_zeromq_connector/v2.0.1/MQL4/DWX_ZeroMQ_Server_v2.0.1_RC8 .mq4

When I copy the Code without changing I get an error in Python:当我在不更改的情况下复制代码时,在 Python 中出现错误:

NameError: name '_zmq' is not defined NameError: 名称 '_zmq' 未定义

After running: _zmq._DWX_ZeroMQ_Connector() - in the Kernel of Spyder.运行后: _zmq._DWX_ZeroMQ_Connector() - 在 Spyder 的内核中。

What can I do to fix that error?我该怎么做才能修复该错误?


In the final state I want to run the Python-Code and the Expert Advisor on the same Windows Server 2012 R2.在最终状态下,我想在同一个 Windows Server 2012 R2 上运行 Python 代码和智能交易系统。

Is it enough if I run the .py file in the powershell from the server or should I host the file with the Webside?如果我从服务器在 powershell 中运行 .py 文件是否足够,或者我应该使用 Webside 托管该文件?

I expect to get the whole system/examplecode running on my VPS or Webside-Host-Server and get an testing environment for further coding action, but currenty I cant get the Library Code in Python to run properly.我希望让整个系统/示例代码在我的 VPS 或 Webside-Host-Server 上运行,并获得一个测试环境以进行进一步的编码操作,但目前我无法让 Python 中的库代码正常运行。

Also the MT4 ceeps crashing with the current code - but should be fixed if I combine my application with the Library-Codeexample.此外,MT4 ceeps 会因当前代码而崩溃 - 但如果我将我的应用程序与 Library-Codeexample 结合使用,应该会得到修复。

(running everything on my local PC with WIN 10). (使用 WIN 10 在我的本地 PC 上运行所有内容)。

Q : I think it is a connection-problem between MT4 and Python.我认为这是 MT4 和 Python 之间的连接问题。

Without a fully reproducible MCVE-code this is undecideable.如果没有完全可重现的 MCVE 代码,这是无法确定的。

Having used a ZeroMQ-based bidirectional signalling/messaging between a QuantFX in python and trading ecosystem MetaTrader 4 Terminal implemented in MQL4 , there is a positive experience of using this architecture.在 python 中的 QuantFX 和在MQL4实现的交易生态系统 MetaTrader 4 终端之间使用了基于 ZeroMQ 的双向信号/消息传递,使用这种架构有积极的经验。

Details decide.细节决定。


The Best Next Step :最好的下一步:

Start with a plain PUSH/PULL archetype python- PUSH -es, MQL4 -script- PULL -s, preferably using tcp:// transport-class ( win platforms need not be ready to use an even simpler, protocol-less, ipc:// transport-class.从简单的推PUSH/PULL原型 python- PUSH es、 MQL4 script- PULL s 开始,最好使用tcp:// transport-class(win 平台不需要准备好使用更简单的、无协议的ipc://运输类。

Once you have posack'd this trivial step, move forwards.一旦你完成了这个微不足道的步骤,继续前进。

Q : How do I need to setup my Server to get a connection betwen those two - since it should be the same as on my local PC?我需要如何设置我的服务器才能在这两者之间建立连接 - 因为它应该与我本地 PC 上的连接相同?

It is normal to use ZeroMQ on the same localhost during prototyping, so you may test and debug the integration.在原型设计期间在同一localhost上使用 ZeroMQ 是正常的,因此您可以测试和调试集成。 For details on ZeroMQ, feel free to read all details in other posts .有关 ZeroMQ 的详细信息,请随时阅读其他帖子中的所有详细信息

Q : Is it enough if I run the .py file in the powershell from the server or should I host the file with the Webside I already have and use that as "Python-Server"?如果我从服务器在 powershell 中运行 .py 文件就足够了,还是应该使用我已经拥有的 Webside 托管该文件并将其用作“Python-Server”?

Yes, in case the .py file was designed that way.是的,以防 .py 文件是这样设计的。 No code, no advice.没有代码,没有建议。 That simple.就那么简单。


Possible issues :可能的问题:

Versions - ZeroMQ, since 2.11.x till the recent 4.3.+, has made a lot of changes Installation DLL-details matter.版本 - ZeroMQ,从 2.11.x 到最近的 4.3.+,已经做了很多改变安装 DLL 细节很重要。

MQL4 has similarly gone through many changes ( string ceased to be a string and become struct to name a biggest impacting one ), so start with simple scenarios and integrate the target architecture in steps / phases with due testing whether the completed phases work as expected. MQL4 同样经历了许多变化( string不再是一个字符串, struct成为struct以命名影响最大的一个),所以从简单的场景开始,并在步骤/阶段中集成目标架构,并适当测试完成的阶段是否按预期工作。

to fix that problem you need this:要解决该问题,您需要这样做:

from DWX_ZeroMQ_Connector_v2_0_1_RC8 import DWX_ZeroMQ_Connector
_zmq = DWX_ZeroMQ_Connector()

(adjust your version of the connector as appropriate). (根据需要调整您的连接器版本)。 should fix that problem.应该解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM