Python Socket send from an MT5 mql5 script to Python (using socket server) and to another MT5 platform as a trade copier. This can be used to modify your strategy and risk settings across to another platform, limit and filter trades based on your trading plan and more.
What the full video below to learn more about using python sockets to send trade alerts to yourself over the internet.
MQL5 Code
//MQL5 CODE
//+——————————————————————+
//| Socket.mq5 |
//| GlobalFinanceTrading |
//| www.globalfinancetrading.com|
//+——————————————————————+
#property copyright “GlobalFinanceTrading”
#property link “https:// www.globalfinancetrading.com”
#property version “1.00”
int My_Socket_Handle = INVALID_HANDLE;
#include <Math\Stat\Math.mqh>
#include<Trade\Trade.mqh>
CTrade trade;
double BidGER = 0.00;
double AskGER = 0.00;
double Balance = 0.00;
double Equity = 0.00;
int GERStop = 40;
input int GERTarget = 30;
string GER40 = “GER40”;
void OnStart()
{
My_Socket_Handle = Socket_Connect();
BidGER =NormalizeDouble(SymbolInfoDouble(GER40, SYMBOL_BID), _Digits);
AskGER =NormalizeDouble(SymbolInfoDouble(GER40, SYMBOL_ASK), _Digits);
string Message= “mt.TRADE_ACTION_DEAL”+”,”+Symbol()+”,”+DoubleToString(0.10)+”,”+”0″+”,”+DoubleToString(AskGER,_Digits)+”,”+DoubleToString(BidGER-GERStop)+”,”+DoubleToString(BidGER+GERTarget)+”,PythonBuySender”+”,”+”mt.ORDER_FILLING_IOC”;
Socket_Send(My_Socket_Handle,Message);
/* {
trade.Buy(0.10, GER40, AskGER, BidGER-GERStop, BidGER+GERTarget, “GER40 Buy Order Placed by Script”);
}*/
Socket_Close(My_Socket_Handle)
}
int Socket_Connect()
{
int h_socket = SocketCreate(SOCKET_DEFAULT);
if(h_socket != INVALID_HANDLE)
{
if(SocketConnect(h_socket,”YOUR IP”,YOUR PORT,2000))
{
Print(“Connected to Socket Server”);
}//if(SocketConnect
else
{
Print(“Fail connected to Socket Server. error code : “, GetLastError());
}
}//if(socket != INVALID_HANDLE)
else
{
Print(“Fail SocketCreate error code : “, GetLastError());
}
return h_socket;
}
void Socket_Close(int socket_handle)
{
if(socket_handle != INVALID_HANDLE)
{
SocketClose(socket_handle);
My_Socket_Handle = INVALID_HANDLE;
Print(“Socket Closed”);
}
}
int Socket_Send(int socket_handle,string str_data)
{
if(socket_handle == INVALID_HANDLE) return 0;
uchar bytes[];
int byte_size = StringToCharArray(str_data,bytes)-1;
Print(“runningsomething”);
return SocketSend(socket_handle,bytes,byte_size);
}
Python Code
#Python Side Code
import socket
import MetaTrader5 as mt
import requests
from typing import Final
from typing import List
from datetime import datetime
import time as t
class socketserver:
def __init__(self, address = ‘YOUR IP ADDRESS’, port = YOUR PORT):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.address = address
self.port = port
self.sock.bind((self.address, self.port))
self.cummdata = ”
def waitforconnection(self):
self.sock.listen(1)
self.conn, self.addr = self.sock.accept()
print(‘connected to’, self.addr)
return 1
def recvmsg(self):
self.cummdata = ”
while True:
data = self.conn.recv(10000)
self.cummdata+=data.decode(“utf-8”)
if not data:
break
self.conn.send(bytes(self.cummdata,”utf-8″))
return self.cummdata
def __del__(self):
print(‘socket close’)
self.sock.close()
#####################################################
serv = socketserver(‘YOUR IP ADDRESS’, PORT)
serv.waitforconnection()
if True:
msg = serv.recvmsg()
print(‘received data : ‘,msg);
listmaker = str(msg)
tradeindex_list = list(listmaker.split(‘,’))
login = ‘YOUR MT5 LOGIN’
password = ‘YOUR MT5 PASSWORD’
server = ‘YOUR MT5 SERVER’
path = “C:\\Program Files\\BROKER\\terminal64.exe” #YOUR MT5 File Path but use backslash
timeout = 100
portable = False
if not mt.initialize(path):
print(“First Erorr info “, mt.last_error())
if not mt.login(login, server, password):
print(“second error info “, mt.last_error())
account_info=mt.account_info()
print(account_info)
print(tradeindex_list[0])
def create_order(tradeindex_list):
request = {
“action”: mt.TRADE_ACTION_DEAL,
“symbol” : tradeindex_list[1],
“volume” : float(0.10),
“price” : mt.symbol_info_tick(‘GER40’).ask,
“type” : int(tradeindex_list[3]),
“sl” : float(tradeindex_list[5]),
“tp” : float(tradeindex_list[6]),
“deviation” : 10,
“magic” : 5555,
“comment” : tradeindex_list[7],
“type_time” : mt.ORDER_TIME_GTC,
“type_filling”: mt.ORDER_FILLING_IOC,
}
makeithappen = mt.order_send(request)
print(“Printing request value “, request)
create_order(tradeindex_list)