# MQL4 ZMQ设置 **Published by:** [linhaidu.eth](https://paragraph.com/@linhaidu-eth/) **Published on:** 2023-11-18 **URL:** https://paragraph.com/@linhaidu-eth/mql4-zmq ## Content mql4的zmq使用下面这个github https://github.com/dingmaotu/mql-zmq 将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 ## Publication Information - [linhaidu.eth](https://paragraph.com/@linhaidu-eth/): Publication homepage - [All Posts](https://paragraph.com/@linhaidu-eth/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@linhaidu-eth): Subscribe to updates