我想要一个 EA 交易来开启由 Telegram 消息触发的交易。
我成功设置了一个 Hello-World 应用程序,使用 MQ4 作为服务器,Python/Telegram-bot 作为客户端。 当 Telegram-Bot 收到消息时,他将向 MQ4 发送请求并获得简单的响应,而不执行交易。
运行下面的代码。
# 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");
}
}
//+------------------------------------------------------------------+
<小时/>
现在我想将 2 个变量从 Python 发送到 MQ4。 1.订单类型:买入/卖出 2. 交易品种:EURUSD, AUDUSD,...
如果消息包含“升序”,则发送“卖出”- 如果消息包含“降序”,则发送“购买”
如果消息包含“AUDUSD”,则发送“AUDUSD”,...
为此,我从 Darwinex 找到了一个库,并希望将其(消息解释、以数组形式发送值)与我已经运行的 telegram-bot 结合起来。
<小时/>为了进行测试,我想单独尝试 Darwinex 的示例代码。
我找到了代码 v2.0.1:
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
当我复制代码而不进行更改时,我在 Python 中收到错误:
名称错误:名称“_zmq”未定义
运行后:_zmq._DWX_ZeroMQ_Connector() - 在Spyder的内核中。
我该如何修复该错误?
<小时/>在最终状态下,我希望在同一个 Windows Server 2012 R2 上运行 Python 代码和 Expert Advisor。
如果我从服务器的 powershell 中运行 .py 文件就足够了,还是应该通过 Web 端托管该文件?
我希望让整个系统/示例代码在我的 VPS 或 Webside-Host-Server 上运行,并获得一个用于进一步编码操作的测试环境,但目前我无法让 Python 中的库代码正常运行。
此外,MT4 ceeps 使用当前代码会崩溃 - 但如果我将我的应用程序与 Library-Codeexample 结合起来,应该可以修复。
(在我的本地电脑上使用 WIN 10 运行所有内容)。
最佳答案
Q : I think it is a connection-problem between MT4 and Python.
如果没有完全可重现的 MCVE 代码,这是无法确定的。
在 python 中的 QuantFX 和使用 MQL4
实现的交易生态系统 MetaTrader 4 终端之间使用基于 ZeroMQ 的双向信号/消息传递后,使用此功能获得了积极的体验架构。
细节决定。
<小时/>最好的下一步:
从简单的 PUSH/PULL
原型(prototype)开始 python-PUSH
-es, MQL4
-script-PULL
-s,最好使用 tcp://
传输类(win 平台不需要准备好使用甚至更简单,无协议(protocol),ipc://
传输类。
完成这个简单的步骤后,继续前进。
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?
在原型(prototype)设计过程中,在同一个localhost
上使用 ZeroMQ 是正常的,因此您可以测试和调试集成。有关ZeroMQ的详细信息,请随时联系read all details in other posts .
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"?
是的,如果 .py 文件是这样设计的。没有代码,没有建议。就这么简单。
<小时/>可能的问题:
版本 - ZeroMQ,从 2.11.x 到最近的 4.3.+,做了很多更改 安装 DLL - 细节很重要。
MQL4 同样经历了许多变化(string
不再是字符串,而是变为 struct
来命名影响最大的变化),因此从简单的场景开始,并集成分步骤/阶段的目标架构,并适当测试已完成的阶段是否按预期工作。
关于python - 如何使用 ZeroMQ 从 Python 建立到 Metatrader 4 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57732595/