我无法使用普通 zeroMQ Push/Pull 模式 grab/print 拉取实例上的数据
I can't grab/print data on the pull instance using plain zeroMQ Push/Pull pattern
我很难抓取和打印已成功推送到网络的数据。我已经建立了一个简单的推拉架构,其中 Metatrader 4 充当 producer
和 Python 后端充当 consumer
.
当谈到抓取和打印数据时,我就是做不到。
这是我的推送实例,工作正常:
Metatrader 4 zeroMQ 推送实例:
#include <Zmq/Zmq.mqh>
// EA plot settings
extern string PROJECT_NAME = "Dashex.Feeder";
extern string ZEROMQ_PROTOCOL = "tcp";
extern string HOSTNAME = "localhost";
extern int PUSH_PORT = 32225;
extern string t0 = "--- Feeder Parameters ---";
input string DID = "insert your DID here";
extern string t1 = "--- ZeroMQ Configuration ---";
extern bool Publish_MarketData = false;
// ZeroMQ environment //
// CREATE ZeroMQ Context
Context context(PROJECT_NAME);
// CREATE ZMQ_PUSH SOCKET
Socket pushSocket(context, ZMQ_PUSH);
string Publish_Symbols[7] = {
"EURUSD","GBPUSD","USDJPY","USDCAD","AUDUSD","NZDUSD","USDCHF"
};
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
EventSetTimer(1); // Set Millisecond Timer to get client socket input
context.setBlocky(false);
// Send responses to PULL_PORT that client is listening on.
Print("[PUSH] Connecting MT4 Server to Socket on Port " + IntegerToString(PUSH_PORT) + "..");
pushSocket.connect(StringFormat("%s://%s:%d", ZEROMQ_PROTOCOL, HOSTNAME, PUSH_PORT));
pushSocket.setSendHighWaterMark(1);
pushSocket.setLinger(0);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
Print("[PUSH] Disconnecting MT4 Server from Socket on Port " + IntegerToString(PUSH_PORT) + "..");
pushSocket.disconnect(StringFormat("%s://%s:%d", ZEROMQ_PROTOCOL, HOSTNAME, PUSH_PORT));
// Shutdown ZeroMQ Context
context.shutdown();
context.destroy(0);
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTimer()
{
/*
Use this OnTimer() function to send market data to consumer.
*/
if(!IsStopped() && Publish_MarketData == true)
{
for(int s = 0; s < ArraySize(Publish_Symbols); s++)
{
string _tick = GetBidAsk(Publish_Symbols[s]);
Print("Sending " + Publish_Symbols[s] + " " + _tick + " to PUSH Socket");
ZmqMsg reply(StringFormat("%s %s", Publish_Symbols[s], _tick));
pushSocket.send(reply, true);
}
}
}
//+------------------------------------------------------------------+
string GetBidAsk(string symbol) {
MqlTick last_tick;
if(SymbolInfoTick(symbol,last_tick))
{
return(StringFormat("%f;%f", last_tick.bid, last_tick.ask));
}
// Default
return "";
}
如何将数据推送到线路(字符串数组,上面代码的片段):
string _tick = GetBidAsk(Publish_Symbols[s]);
Print("Sending " + Publish_Symbols[s] + " " + _tick + " to PUSH Socket");
ZmqMsg reply(StringFormat("%s %s", Publish_Symbols[s], _tick));
pushSocket.send(reply, true);
Python 没有 grab/print 任何数据的侧拉客户端:
import zmq
import time
from time import sleep
context = zmq.Context()
zmq_socket = context.socket(zmq.PULL)
zmq_socket.bind("tcp://*:32220")
time.sleep(1)
while True:
def pull_MT4():
try:
msg = zmq_socket.recv_string()
print(msg)
return msg
except zmq.error.Again:
print("\nResource timeout.. please try again.")
sleep(0.000001)
return None
没有打印数据的控制台输出:-(
在 while True
中,您只能一次又一次地定义函数 pull_MT4
但您永远不会执行该函数 - 您永远不会使用 pull_MT4()
到 运行 它。
你应该直接在循环中使用这段代码(没有定义函数也没有 return
)
while True:
try:
msg = zmq_socket.recv_string()
print(msg)
except zmq.error.Again:
print("\nResource timeout.. please try again.")
sleep(0.000001)
或者你应该在循环之前定义函数并在循环中执行它
def pull_MT4(): # define function
try:
msg = zmq_socket.recv_string()
print(msg)
return msg
except zmq.error.Again:
print("\nResource timeout.. please try again.")
sleep(0.000001)
return None
while True:
pull_MT4() # execute function
我很难抓取和打印已成功推送到网络的数据。我已经建立了一个简单的推拉架构,其中 Metatrader 4 充当 producer
和 Python 后端充当 consumer
.
当谈到抓取和打印数据时,我就是做不到。
这是我的推送实例,工作正常:
Metatrader 4 zeroMQ 推送实例:
#include <Zmq/Zmq.mqh>
// EA plot settings
extern string PROJECT_NAME = "Dashex.Feeder";
extern string ZEROMQ_PROTOCOL = "tcp";
extern string HOSTNAME = "localhost";
extern int PUSH_PORT = 32225;
extern string t0 = "--- Feeder Parameters ---";
input string DID = "insert your DID here";
extern string t1 = "--- ZeroMQ Configuration ---";
extern bool Publish_MarketData = false;
// ZeroMQ environment //
// CREATE ZeroMQ Context
Context context(PROJECT_NAME);
// CREATE ZMQ_PUSH SOCKET
Socket pushSocket(context, ZMQ_PUSH);
string Publish_Symbols[7] = {
"EURUSD","GBPUSD","USDJPY","USDCAD","AUDUSD","NZDUSD","USDCHF"
};
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
EventSetTimer(1); // Set Millisecond Timer to get client socket input
context.setBlocky(false);
// Send responses to PULL_PORT that client is listening on.
Print("[PUSH] Connecting MT4 Server to Socket on Port " + IntegerToString(PUSH_PORT) + "..");
pushSocket.connect(StringFormat("%s://%s:%d", ZEROMQ_PROTOCOL, HOSTNAME, PUSH_PORT));
pushSocket.setSendHighWaterMark(1);
pushSocket.setLinger(0);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
Print("[PUSH] Disconnecting MT4 Server from Socket on Port " + IntegerToString(PUSH_PORT) + "..");
pushSocket.disconnect(StringFormat("%s://%s:%d", ZEROMQ_PROTOCOL, HOSTNAME, PUSH_PORT));
// Shutdown ZeroMQ Context
context.shutdown();
context.destroy(0);
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTimer()
{
/*
Use this OnTimer() function to send market data to consumer.
*/
if(!IsStopped() && Publish_MarketData == true)
{
for(int s = 0; s < ArraySize(Publish_Symbols); s++)
{
string _tick = GetBidAsk(Publish_Symbols[s]);
Print("Sending " + Publish_Symbols[s] + " " + _tick + " to PUSH Socket");
ZmqMsg reply(StringFormat("%s %s", Publish_Symbols[s], _tick));
pushSocket.send(reply, true);
}
}
}
//+------------------------------------------------------------------+
string GetBidAsk(string symbol) {
MqlTick last_tick;
if(SymbolInfoTick(symbol,last_tick))
{
return(StringFormat("%f;%f", last_tick.bid, last_tick.ask));
}
// Default
return "";
}
如何将数据推送到线路(字符串数组,上面代码的片段):
string _tick = GetBidAsk(Publish_Symbols[s]);
Print("Sending " + Publish_Symbols[s] + " " + _tick + " to PUSH Socket");
ZmqMsg reply(StringFormat("%s %s", Publish_Symbols[s], _tick));
pushSocket.send(reply, true);
Python 没有 grab/print 任何数据的侧拉客户端:
import zmq
import time
from time import sleep
context = zmq.Context()
zmq_socket = context.socket(zmq.PULL)
zmq_socket.bind("tcp://*:32220")
time.sleep(1)
while True:
def pull_MT4():
try:
msg = zmq_socket.recv_string()
print(msg)
return msg
except zmq.error.Again:
print("\nResource timeout.. please try again.")
sleep(0.000001)
return None
没有打印数据的控制台输出:-(
在 while True
中,您只能一次又一次地定义函数 pull_MT4
但您永远不会执行该函数 - 您永远不会使用 pull_MT4()
到 运行 它。
你应该直接在循环中使用这段代码(没有定义函数也没有 return
)
while True:
try:
msg = zmq_socket.recv_string()
print(msg)
except zmq.error.Again:
print("\nResource timeout.. please try again.")
sleep(0.000001)
或者你应该在循环之前定义函数并在循环中执行它
def pull_MT4(): # define function
try:
msg = zmq_socket.recv_string()
print(msg)
return msg
except zmq.error.Again:
print("\nResource timeout.. please try again.")
sleep(0.000001)
return None
while True:
pull_MT4() # execute function