MQL4 ZMQ设置
November 18th, 2023

mql4的zmq使用下面这个github

将include下面的内容拷贝到mt4的include目录

将library下面的内容拷贝到mt4的library目录

然后再mt4里面还要设置一下允许DLL导入,工具→选项void OnStart()

{

  
   Context context;
   Socket publisher(context,ZMQ_PUB);
   publisher.bind("tcp://*:5558");
   Sleep(2000);
//---
   for (int i=0;i<100;i++)
   {
   Print("i=",i);
   double a = Ask;
   double b = Bid;
   double c = MarketInfo(PAIR,MODE_ASK);
   double d = MarketInfo(PAIR,MODE_BID);
   Print(PAIR," 卖一价=",c);
   Print(PAIR," 买一价=",d);
   //Print(StringFormat("%s 卖一价= %d",PAIR,c));
   ZmqMsg message(StringFormat("%f,%f",c,d));
   //ZmqMsg message(DoubleToStr(c));
   publisher.send(message);
   Sleep(500);
   }
   
   publisher.unbind("tcp://*:5558");
   
 }

Python设置

安装pyzmq

def print_hi():
    host = "127.0.0.1"
    port = "5555"

    context = zmq.Context()

    #  Socket to talk to server
    print("Connecting to hello world server…")
    socket = context.socket(zmq.SUB)
    socket.setsockopt(zmq.LINGER, 0)
    socket.setsockopt(zmq.IMMEDIATE, 1)
    socket.connect("tcp://localhost:5558")
    # Subscribes to all topics
    socket.subscribe("")
    try:
        while True:
            #receives a string format message
            data = socket.recv_string()
            y = data.split(",")
            c = float(y[0])
            d = float(y[1])
            print(c, d, c-d)
    except :
        print("EXC'd")
    finally:
        socket.unbind("tcp://localhost:5558")
        socket.close()
        context.term()

一定要注意:先确认端口是否被其他进程占用

netstat -na

如果强制退出mql4程序,会导致端口无法正常释放。此时需要关闭mt4,或者杀掉进程。

先查找进程:

netstat -nao|findstr 5558

查到进程号,然后kill掉

taskkill /t /f /im xxxx

Subscribe to linhaidu.eth
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from linhaidu.eth

Skeleton

Skeleton

Skeleton