Full Code of ns2250225/py-mt4 for AI

master 60b6b98f8092 cached
4 files
4.4 KB
1.5k tokens
9 symbols
1 requests
Download .txt
Repository: ns2250225/py-mt4
Branch: master
Commit: 60b6b98f8092
Files: 4
Total size: 4.4 KB

Directory structure:
gitextract_c9l5gy0g/

├── README.md
├── TA_Lib-0.4.17-cp35-cp35m-win_amd64.whl
├── pythonicMT4.py
└── 自动化EA脚本.py

================================================
FILE CONTENTS
================================================

================================================
FILE: README.md
================================================
# py-mt4
用Python来写MT4的自动化交易脚本 🎉

# 原理
- 使用MQL4原生库调用ZERO-MQ作为消息服务端
- 使用其它语言,如Python作为客户端调用接口

# 机器环境
- win10
- python3.5

# 安装教程
- 安装C++运行环境[需要C++的运行环境,The DLLs require that you have the latest Visual C++ runtime (2015)]:https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads
- 克隆代码下来
- 关闭杀毒软件!!!
- 解压MT4配置文件.rar
- 把Include的东西放到MT4的Include文件夹下
- 把Library/X86/的东西放到MT4的Library文件夹下
- 把ZeroMQ_MT4_EA_Template_Edited.mq4放到MT4的Experts文件夹下
- pip install pyzmq
- pip install TA_Lib-0.4.17-cp35-cp35m-win_amd64.whl  (版本可以在这里下载:https://www.lfd.uci.edu/~gohlke/pythonlibs/)

# 启动EA脚本
- 打开MT4,在左下方把ZeroMQ_MT4_EA_Template_Edited.mq4托入想交易的图形中, 并允许EA添加外部DLL和自动交易
- 右上角会有个哭脸,说明没启动
- 点击【自动交易】,哭脸变笑脸则启动成功
- 修改和运行自动化EA脚本.py进行测试

# 效果
![](./1.png)
![](./2.png)


================================================
FILE: pythonicMT4.py
================================================
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 30 20:15:31 2018
@author: Ars
"""
import zmq
import numpy as np
class zmq_python():
    
    def __init__(self):
        # Create ZMQ Context
        self.context = zmq.Context()

        # Create REQ Socket
        self.reqSocket = self.context.socket(zmq.REQ)
        self.reqSocket.connect("tcp://localhost:5555")

        # Create PULL Socket
        self.pullSocket = self.context.socket(zmq.PULL)
        self.pullSocket.connect("tcp://localhost:5556")
    
    def remote_send(self, socket, data):
    
        try:
            socket.send_string(data)
            msg_send = socket.recv_string()
            print (msg_send)

        except zmq.Again as e:
            print ("Waiting for PUSH from MetaTrader 4..")
            
    def remote_pull(self, socket):
    
        try:
            msg_pull = socket.recv(flags=zmq.NOBLOCK)
            return msg_pull

        except zmq.Again as e:
            print ("Waiting for PUSH from MetaTrader 4..")
            
    
    def get_data(self, symbol, timeframe, start_bar, end_bar):
        '''
        only start_bar and end_bar as int
        '''
        self.data = "DATA|"+ symbol+"|"+"PERIOD_"+timeframe+"|"+str(start_bar)+"|"+str(end_bar+1)
        self.remote_send(self.reqSocket, self.data)
        prices= self.remote_pull(self.pullSocket)
        prices_str= str(prices)
        price_lst= prices_str.split(sep='|')[1:-1]
        price_lst= [float(i) for i in price_lst]
        price_lst= price_lst[::-1]
        price_arr= np.array(price_lst)
        return price_arr
    
    def buy_order(self, symbol, stop_loss, take_profit):
        self.buy= "TRADE|OPEN|0|"+ str(symbol)+"|"+str(stop_loss)+"|"+str(take_profit)
        self.remote_send(self.reqSocket, self.buy)
        reply= self.remote_pull(self.pullSocket)
        return reply
    
    def sell_order(self, symbol, stop_loss, take_profit):
        self.buy= "TRADE|OPEN|1|"+ str(symbol)+"|"+str(stop_loss)+"|"+str(take_profit)
        self.remote_send(self.reqSocket, self.buy)
        reply= self.remote_pull(self.pullSocket)
        return reply
    
    def close_buy_order(self):
        self.close_buy= "TRADE|CLOSE|0"
        self.remote_send(self.reqSocket, self.close_buy)
        reply= self.remote_pull(self.pullSocket)
        return reply
    
    def close_sell_order(self):
        self.close_sell= "TRADE|CLOSE|1"
        self.remote_send(self.reqSocket, self.close_sell)
        reply= self.remote_pull(self.pullSocket)
        return reply

================================================
FILE: 自动化EA脚本.py
================================================
import pythonicMT4 
import talib
from time import sleep

symbol= 'EURUSD'
timeframe= 'H1'
start= 0
end= 2000
period= 96
stopLoss= 500
takeProfit= 1000
order= ''

trade= pythonicMT4.zmq_python()

while True:
    try:
        prices= trade.get_data(symbol= symbol, timeframe= 'H1', start_bar=start, end_bar=end)
        SMA= talib.SMA(prices, timeperiod=period)
        print ("Current price: {} \nSMA: {}".format(prices[-1], SMA[-1]))
        
        if order != 'Buy' and order != 'Sell':
            if (prices[-1] > prices [-2]) and (prices[-1]<SMA[-1]):
                order= 'Buy'
                trade.buy_order(symbol= symbol, stop_loss= stopLoss, take_profit= takeProfit)
                
            else:
                if (prices[-1] < prices[-2]) and (prices[-1] > SMA[-1]):
                    order= 'Sell'
                    trade.sell_order(symbol= symbol, stop_loss= stopLoss, take_profit= takeProfit)
                
        if order== 'Buy' and prices[-1]>SMA[-1]:
            order= ''
            trade.close_buy_order()
        
        else:
            if order== 'Sell' and prices[-1]<SMA[-1]:
                order= ''
                trade.close_sell_order()
    except:
        continue
    
    sleep(100)
Download .txt
gitextract_c9l5gy0g/

├── README.md
├── TA_Lib-0.4.17-cp35-cp35m-win_amd64.whl
├── pythonicMT4.py
└── 自动化EA脚本.py
Download .txt
SYMBOL INDEX (9 symbols across 1 files)

FILE: pythonicMT4.py
  class zmq_python (line 8) | class zmq_python():
    method __init__ (line 10) | def __init__(self):
    method remote_send (line 22) | def remote_send(self, socket, data):
    method remote_pull (line 32) | def remote_pull(self, socket):
    method get_data (line 42) | def get_data(self, symbol, timeframe, start_bar, end_bar):
    method buy_order (line 56) | def buy_order(self, symbol, stop_loss, take_profit):
    method sell_order (line 62) | def sell_order(self, symbol, stop_loss, take_profit):
    method close_buy_order (line 68) | def close_buy_order(self):
    method close_sell_order (line 74) | def close_sell_order(self):
Condensed preview — 4 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (5K chars).
[
  {
    "path": "README.md",
    "chars": 775,
    "preview": "# py-mt4\n用Python来写MT4的自动化交易脚本 🎉\n\n# 原理\n- 使用MQL4原生库调用ZERO-MQ作为消息服务端\n- 使用其它语言,如Python作为客户端调用接口\n\n# 机器环境\n- win10\n- python3.5\n"
  },
  {
    "path": "pythonicMT4.py",
    "chars": 2537,
    "preview": "# -*- coding: utf-8 -*-\n\"\"\"\nCreated on Mon Apr 30 20:15:31 2018\n@author: Ars\n\"\"\"\nimport zmq\nimport numpy as np\nclass zmq"
  },
  {
    "path": "自动化EA脚本.py",
    "chars": 1238,
    "preview": "import pythonicMT4 \nimport talib\nfrom time import sleep\n\nsymbol= 'EURUSD'\ntimeframe= 'H1'\nstart= 0\nend= 2000\nperiod= 96\n"
  }
]

// ... and 1 more files (download for full content)

About this extraction

This page contains the full source code of the ns2250225/py-mt4 GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 4 files (4.4 KB), approximately 1.5k tokens, and a symbol index with 9 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!