832 lines
21 KiB
MQL5
832 lines
21 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| ProjectName |
|
|
//| Copyright 2020, CompanyName |
|
|
//| http://www.companyname.net |
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
|
#property copyright "Pulkit Chadha"
|
|
#property link "https://www.mql5.com"
|
|
#property strict
|
|
|
|
#define SOCKET_LIBRARY_USE_EVENTS
|
|
#include "SocketSend.mqh"
|
|
|
|
|
|
string Hostname = "127.0.0.1";
|
|
//ushort ServerPort = (ushort)"30001";
|
|
|
|
ClientSocket * glbClientSocket = NULL;
|
|
input group ""
|
|
input group "|---------- Fyers Bridge Settings ----------|"
|
|
input string Strategy_Tag = "TRAILINGSTOPLOSS";
|
|
string sym = Symbol();
|
|
string ordertype = "L";
|
|
string ins = NULL;
|
|
|
|
input string Port ="30001";
|
|
extern int Quantity=1;
|
|
|
|
ushort u_sep;
|
|
string sep =",";
|
|
string result45[];
|
|
int L1=1;
|
|
int Exposure =0;
|
|
int K=0;
|
|
double bprice, cprice ;
|
|
|
|
enum stringOptionsordertype
|
|
{
|
|
MARKET=1,
|
|
LIMIT=2,
|
|
SLL=3,
|
|
SLM=4,
|
|
};
|
|
input stringOptionsordertype Ordertype = MARKET;
|
|
enum stringOptionsInstrument
|
|
{
|
|
EQ = 1,
|
|
FUTSTK = 2,
|
|
FUTIDX = 3,
|
|
OPTSTK = 4,
|
|
OPTIDX = 5,
|
|
FUTCUR = 6,
|
|
FUTCOM = 7,
|
|
};
|
|
input stringOptionsInstrument Instrument = FUTIDX;
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void LongEntry(int qt, double price)
|
|
{
|
|
|
|
string ordertypeLongEntry ;
|
|
if(Ordertype==MARKET)
|
|
{
|
|
ordertypeLongEntry="MARKET";
|
|
}
|
|
else
|
|
if(Ordertype==LIMIT)
|
|
{
|
|
ordertypeLongEntry="LIMIT";
|
|
}
|
|
|
|
if(Instrument==1)
|
|
{
|
|
ins = "EQ";
|
|
}
|
|
else
|
|
if(Instrument==2)
|
|
{
|
|
ins = "FUTSTK";
|
|
}
|
|
else
|
|
if(Instrument==3)
|
|
{
|
|
ins = "FUTIDX";
|
|
}
|
|
else
|
|
if(Instrument==4)
|
|
{
|
|
ins = "OPTSTK";
|
|
}
|
|
else
|
|
if(Instrument==5)
|
|
{
|
|
ins = "OPTIDX";
|
|
}
|
|
else
|
|
if(Instrument==6)
|
|
{
|
|
ins = "FUTCUR";
|
|
}
|
|
else
|
|
if(Instrument==7)
|
|
{
|
|
ins = "FUTCOM";
|
|
}
|
|
u_sep=StringGetCharacter(sep,0);
|
|
int j=StringSplit(Port,u_sep,result45);
|
|
|
|
for(int i=0; i<j; i++)
|
|
{
|
|
if(!glbClientSocket)
|
|
{
|
|
glbClientSocket = new ClientSocket(Hostname, (ushort)result45[i]);
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
Print("Client connection succeeded");
|
|
}
|
|
else
|
|
{
|
|
Print("Client connection failed");
|
|
}
|
|
}
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
string strMsg = IntegerToString(L1)+",LE,"+ Symbol()+"|||,"
|
|
+ordertypeLongEntry+",,"+DoubleToString(price,2)+","
|
|
+IntegerToString(qt)+","+ins+","+Strategy_Tag+ ",30001";
|
|
glbClientSocket.Send(strMsg);
|
|
}
|
|
else
|
|
{
|
|
}
|
|
if(!glbClientSocket.IsSocketConnected())
|
|
{
|
|
// Destroy the server socket. A new connection
|
|
// will be attempted on the next tick
|
|
Print("Client disconnected. Will retry.");
|
|
delete glbClientSocket;
|
|
glbClientSocket = NULL;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//s.Send(yo);
|
|
|
|
|
|
L1++;
|
|
if(K==1)
|
|
{
|
|
K--;
|
|
}
|
|
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void ShortEntry(int qt, double price)
|
|
{
|
|
string ordertypeShortEntry ;
|
|
if(Ordertype==MARKET)
|
|
{
|
|
ordertypeShortEntry="MARKET";
|
|
}
|
|
else
|
|
if(Ordertype==LIMIT)
|
|
{
|
|
ordertypeShortEntry="LIMIT";
|
|
}
|
|
|
|
if(Instrument==1)
|
|
{
|
|
ins = "EQ";
|
|
}
|
|
else
|
|
if(Instrument==2)
|
|
{
|
|
ins = "FUTSTK";
|
|
}
|
|
else
|
|
if(Instrument==3)
|
|
{
|
|
ins = "FUTIDX";
|
|
}
|
|
else
|
|
if(Instrument==4)
|
|
{
|
|
ins = "OPTSTK";
|
|
}
|
|
else
|
|
if(Instrument==5)
|
|
{
|
|
ins = "OPTIDX";
|
|
}
|
|
else
|
|
if(Instrument==6)
|
|
{
|
|
ins = "FUTCUR";
|
|
}
|
|
else
|
|
if(Instrument==7)
|
|
{
|
|
ins = "FUTCOM";
|
|
}
|
|
|
|
u_sep=StringGetCharacter(sep,0);
|
|
int j=StringSplit(Port,u_sep,result45);
|
|
|
|
for(int i=0; i<j; i++)
|
|
{
|
|
if(!glbClientSocket)
|
|
{
|
|
glbClientSocket = new ClientSocket(Hostname, (ushort)result45[i]);
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
Print("Client connection succeeded");
|
|
}
|
|
else
|
|
{
|
|
Print("Client connection failed");
|
|
}
|
|
}
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
string strMsg = IntegerToString(L1)+",SE,"+ Symbol()+"|||,"
|
|
+ordertypeShortEntry+",,"+DoubleToString(price,2)+","
|
|
+IntegerToString(qt)+","+ins+","+Strategy_Tag+ ",30001";
|
|
glbClientSocket.Send(strMsg);
|
|
}
|
|
else
|
|
{
|
|
}
|
|
if(!glbClientSocket.IsSocketConnected())
|
|
{
|
|
// Destroy the server socket. A new connection
|
|
// will be attempted on the next tick
|
|
Print("Client disconnected. Will retry.");
|
|
delete glbClientSocket;
|
|
glbClientSocket = NULL;
|
|
}
|
|
}
|
|
|
|
|
|
L1++;
|
|
if(K==1)
|
|
{
|
|
K--;
|
|
}
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void LongExit(int qt, double price)
|
|
{
|
|
string ordertypeLongExit ;
|
|
if(Ordertype==MARKET)
|
|
{
|
|
ordertypeLongExit="MARKET";
|
|
}
|
|
else
|
|
if(Ordertype==LIMIT)
|
|
{
|
|
ordertypeLongExit="LIMIT";
|
|
}
|
|
|
|
if(Instrument==1)
|
|
{
|
|
ins = "EQ";
|
|
}
|
|
else
|
|
if(Instrument==2)
|
|
{
|
|
ins = "FUTSTK";
|
|
}
|
|
else
|
|
if(Instrument==3)
|
|
{
|
|
ins = "FUTIDX";
|
|
}
|
|
else
|
|
if(Instrument==4)
|
|
{
|
|
ins = "OPTSTK";
|
|
}
|
|
else
|
|
if(Instrument==5)
|
|
{
|
|
ins = "OPTIDX";
|
|
}
|
|
else
|
|
if(Instrument==6)
|
|
{
|
|
ins = "FUTCUR";
|
|
}
|
|
else
|
|
if(Instrument==7)
|
|
{
|
|
ins = "FUTCOM";
|
|
}
|
|
u_sep=StringGetCharacter(sep,0);
|
|
int j=StringSplit(Port,u_sep,result45);
|
|
|
|
for(int i=0; i<j; i++)
|
|
{
|
|
PrintFormat("result[%d]=%s",i,result45[i]);
|
|
if(!glbClientSocket)
|
|
{
|
|
glbClientSocket = new ClientSocket(Hostname, (ushort)result45[i]);
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
Print("Client connection succeeded");
|
|
}
|
|
else
|
|
{
|
|
Print("Client connection failed");
|
|
}
|
|
}
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
string strMsg = IntegerToString((L1))+",LX,"+ Symbol()+"|||,"
|
|
+ordertypeLongExit+",,"+DoubleToString(price,2)+","
|
|
+IntegerToString(qt)+","+ins+","+Strategy_Tag+",30001";
|
|
glbClientSocket.Send(strMsg);
|
|
}
|
|
else
|
|
{
|
|
}
|
|
if(!glbClientSocket.IsSocketConnected())
|
|
{
|
|
// Destroy the server socket. A new connection
|
|
// will be attempted on the next tick
|
|
Print("Client disconnected. Will retry.");
|
|
delete glbClientSocket;
|
|
glbClientSocket = NULL;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
L1++;
|
|
if(K==0)
|
|
{
|
|
K++;
|
|
}
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void ShortExit(int qt, double price)
|
|
{
|
|
string ordertypeShortExit ;
|
|
if(Ordertype==MARKET)
|
|
{
|
|
ordertypeShortExit="MARKET";
|
|
}
|
|
else
|
|
if(Ordertype==LIMIT)
|
|
{
|
|
ordertypeShortExit="LIMIT";
|
|
}
|
|
|
|
if(Instrument==1)
|
|
{
|
|
ins = "EQ";
|
|
}
|
|
else
|
|
if(Instrument==2)
|
|
{
|
|
ins = "FUTSTK";
|
|
}
|
|
else
|
|
if(Instrument==3)
|
|
{
|
|
ins = "FUTIDX";
|
|
}
|
|
else
|
|
if(Instrument==4)
|
|
{
|
|
ins = "OPTSTK";
|
|
}
|
|
else
|
|
if(Instrument==5)
|
|
{
|
|
ins = "OPTIDX";
|
|
}
|
|
else
|
|
if(Instrument==6)
|
|
{
|
|
ins = "FUTCUR";
|
|
}
|
|
else
|
|
if(Instrument==7)
|
|
{
|
|
ins = "FUTCOM";
|
|
}
|
|
u_sep=StringGetCharacter(sep,0);
|
|
int j=StringSplit(Port,u_sep,result45);
|
|
|
|
for(int i=0; i<j; i++)
|
|
{
|
|
PrintFormat("result[%d]=%s",i,result45[i]);
|
|
if(!glbClientSocket)
|
|
{
|
|
glbClientSocket = new ClientSocket(Hostname, (ushort)result45[i]);
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
Print("Client connection succeeded");
|
|
}
|
|
else
|
|
{
|
|
Print("Client connection failed");
|
|
}
|
|
}
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
string strMsg = IntegerToString((L1))+",SX,"+ Symbol()+"|||,"
|
|
+ordertypeShortExit+",,"+DoubleToString(price,2)+","
|
|
+IntegerToString(qt)+","+ins+","+Strategy_Tag+",30001";
|
|
glbClientSocket.Send(strMsg);
|
|
}
|
|
else
|
|
{
|
|
}
|
|
if(!glbClientSocket.IsSocketConnected())
|
|
{
|
|
// Destroy the server socket. A new connection
|
|
// will be attempted on the next tick
|
|
Print("Client disconnected. Will retry.");
|
|
delete glbClientSocket;
|
|
glbClientSocket = NULL;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
L1++;
|
|
if(K==0)
|
|
{
|
|
K++;
|
|
}
|
|
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void slbuy(int qt,double Trigerprice)
|
|
{
|
|
if(Instrument==1)
|
|
{
|
|
ins = "EQ";
|
|
}
|
|
else
|
|
if(Instrument==2)
|
|
{
|
|
ins = "FUTSTK";
|
|
}
|
|
else
|
|
if(Instrument==3)
|
|
{
|
|
ins = "FUTIDX";
|
|
}
|
|
else
|
|
if(Instrument==4)
|
|
{
|
|
ins = "OPTSTK";
|
|
}
|
|
else
|
|
if(Instrument==5)
|
|
{
|
|
ins = "OPTIDX";
|
|
}
|
|
else
|
|
if(Instrument==6)
|
|
{
|
|
ins = "FUTCUR";
|
|
}
|
|
else
|
|
if(Instrument==7)
|
|
{
|
|
ins = "FUTCOM";
|
|
}
|
|
u_sep=StringGetCharacter(sep,0);
|
|
int j=StringSplit(Port,u_sep,result45);
|
|
string diff = (string)(iClose(Symbol(),PERIOD_CURRENT,0)- Trigerprice);
|
|
for(int i=0; i<j; i++)
|
|
{
|
|
if(!glbClientSocket)
|
|
{
|
|
glbClientSocket = new ClientSocket(Hostname, (ushort)result45[i]);
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
Print("Client connection succeeded");
|
|
}
|
|
else
|
|
{
|
|
Print("Client connection failed");
|
|
}
|
|
}
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
string strMsg = IntegerToString((L1))+",LE,"+ Symbol()+"|||,SLM,"
|
|
+(string)Trigerprice+"|"+diff+","
|
|
+DoubleToString(iClose(Symbol(),PERIOD_CURRENT,0),2)+","
|
|
+IntegerToString(qt)+","+ins+","+Strategy_Tag+",30001";
|
|
glbClientSocket.Send(strMsg);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
L1++;
|
|
if(K==0)
|
|
{
|
|
K++;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void slsell(int qt,double Trigerprice)
|
|
{
|
|
if(Instrument==1)
|
|
{
|
|
ins = "EQ";
|
|
}
|
|
else
|
|
if(Instrument==2)
|
|
{
|
|
ins = "FUTSTK";
|
|
}
|
|
else
|
|
if(Instrument==3)
|
|
{
|
|
ins = "FUTIDX";
|
|
}
|
|
else
|
|
if(Instrument==4)
|
|
{
|
|
ins = "OPTSTK";
|
|
}
|
|
else
|
|
if(Instrument==5)
|
|
{
|
|
ins = "OPTIDX";
|
|
}
|
|
else
|
|
if(Instrument==6)
|
|
{
|
|
ins = "FUTCUR";
|
|
}
|
|
else
|
|
if(Instrument==7)
|
|
{
|
|
ins = "FUTCOM";
|
|
}
|
|
u_sep=StringGetCharacter(sep,0);
|
|
int j=StringSplit(Port,u_sep,result45);
|
|
string diff = (string)(iClose(Symbol(),PERIOD_CURRENT,0) - Trigerprice);
|
|
for(int i=0; i<j; i++)
|
|
{
|
|
|
|
if(!glbClientSocket)
|
|
{
|
|
glbClientSocket = new ClientSocket(Hostname, (ushort)result45[i]);
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
Print("Client connection succeeded");
|
|
}
|
|
else
|
|
{
|
|
Print("Client connection failed");
|
|
}
|
|
}
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
string strMsg = IntegerToString((L1))+",SE,"+ Symbol()+"|||,SLM,"
|
|
+(string)Trigerprice+"|"+diff+","+DoubleToString(iClose(Symbol(),PERIOD_CURRENT,0),2)
|
|
+","+IntegerToString(qt)+","+ins+","+Strategy_Tag+",30001";
|
|
glbClientSocket.Send(strMsg);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
L1++;
|
|
if(K==0)
|
|
{
|
|
K++;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void bracketorderbuy(int qt,double price, double trigerprice,double difference,double squareoff)
|
|
{
|
|
string ordertypeBracketorderbuy ;
|
|
if(Ordertype==MARKET)
|
|
{
|
|
ordertypeBracketorderbuy="MARKET";
|
|
}
|
|
if(Ordertype==LIMIT)
|
|
{
|
|
ordertypeBracketorderbuy="LIMIT";
|
|
}
|
|
if(Ordertype==SLL)
|
|
{
|
|
ordertypeBracketorderbuy="SLL";
|
|
}
|
|
if(Ordertype==SLM)
|
|
{
|
|
ordertypeBracketorderbuy="SL-M";
|
|
}
|
|
|
|
if(Instrument==1)
|
|
{
|
|
ins = "EQ";
|
|
}
|
|
else
|
|
if(Instrument==2)
|
|
{
|
|
ins = "FUTSTK";
|
|
}
|
|
else
|
|
if(Instrument==3)
|
|
{
|
|
ins = "FUTIDX";
|
|
}
|
|
else
|
|
if(Instrument==4)
|
|
{
|
|
ins = "OPTSTK";
|
|
}
|
|
else
|
|
if(Instrument==5)
|
|
{
|
|
ins = "OPTIDX";
|
|
}
|
|
else
|
|
if(Instrument==6)
|
|
{
|
|
ins = "FUTCUR";
|
|
}
|
|
else
|
|
if(Instrument==7)
|
|
{
|
|
ins = "FUTCOM";
|
|
}
|
|
u_sep=StringGetCharacter(sep,0);
|
|
int j=StringSplit(Port,u_sep,result45);
|
|
|
|
for(int i=0; i<j; i++)
|
|
{
|
|
if(!glbClientSocket)
|
|
{
|
|
glbClientSocket = new ClientSocket(Hostname, (ushort)result45[i]);
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
Print("Client connection succeeded");
|
|
}
|
|
else
|
|
{
|
|
Print("Client connection failed");
|
|
}
|
|
}
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
string strMsg = IntegerToString((L1))+",LE,"
|
|
+(string)sym+"|||,"
|
|
+(string)ordertypeBracketorderbuy+","
|
|
+(string)trigerprice+"|"
|
|
+(string)difference+"|"
|
|
+(string)squareoff+","
|
|
+(string)price+","
|
|
+(string)qt+","
|
|
+ins+","
|
|
+Strategy_Tag
|
|
+ ",30001";
|
|
glbClientSocket.Send(strMsg);
|
|
}
|
|
else
|
|
{
|
|
}
|
|
if(!glbClientSocket.IsSocketConnected())
|
|
{
|
|
// Destroy the server socket. A new connection
|
|
// will be attempted on the next tick
|
|
Print("Client disconnected. Will retry.");
|
|
delete glbClientSocket;
|
|
glbClientSocket = NULL;
|
|
}
|
|
}
|
|
|
|
|
|
L1++;
|
|
if(K==0)
|
|
{
|
|
K++;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void bracketordersell(int qt,double price, double trigerprice,double difference,double squareoff)
|
|
{
|
|
string ordertypeBracketordersell ;
|
|
if(Ordertype==MARKET)
|
|
{
|
|
ordertypeBracketordersell="MARKET";
|
|
}
|
|
if(Ordertype==LIMIT)
|
|
{
|
|
ordertypeBracketordersell="LIMIT";
|
|
}
|
|
if(Ordertype==SLL)
|
|
{
|
|
ordertypeBracketordersell="SLL";
|
|
}
|
|
if(Ordertype==SLM)
|
|
{
|
|
ordertypeBracketordersell="SL-M";
|
|
}
|
|
|
|
if(Instrument==1)
|
|
{
|
|
ins = "EQ";
|
|
}
|
|
else
|
|
if(Instrument==2)
|
|
{
|
|
ins = "FUTSTK";
|
|
}
|
|
else
|
|
if(Instrument==3)
|
|
{
|
|
ins = "FUTIDX";
|
|
}
|
|
else
|
|
if(Instrument==4)
|
|
{
|
|
ins = "OPTSTK";
|
|
}
|
|
else
|
|
if(Instrument==5)
|
|
{
|
|
ins = "OPTIDX";
|
|
}
|
|
else
|
|
if(Instrument==6)
|
|
{
|
|
ins = "FUTCUR";
|
|
}
|
|
else
|
|
if(Instrument==7)
|
|
{
|
|
ins = "FUTCOM";
|
|
}
|
|
u_sep=StringGetCharacter(sep,0);
|
|
int j=StringSplit(Port,u_sep,result45);
|
|
|
|
for(int i=0; i<j; i++)
|
|
{
|
|
|
|
if(!glbClientSocket)
|
|
{
|
|
glbClientSocket = new ClientSocket(Hostname,(ushort)(result45[i]));
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
Print("Client connection succeeded");
|
|
}
|
|
else
|
|
{
|
|
Print("Client connection failed");
|
|
}
|
|
}
|
|
if(glbClientSocket.IsSocketConnected())
|
|
{
|
|
string strMsg =IntegerToString((L1))
|
|
+",SE,"
|
|
+sym+"|||,"
|
|
+ordertypeBracketordersell+","
|
|
+(string)trigerprice+"|"
|
|
+(string)difference+"|"
|
|
+(string)squareoff+","
|
|
+(string)price+","
|
|
+(string)qt+","
|
|
+ins+","
|
|
+Strategy_Tag
|
|
+ ",3000";
|
|
glbClientSocket.Send(strMsg);
|
|
}
|
|
else
|
|
{
|
|
}
|
|
if(!glbClientSocket.IsSocketConnected())
|
|
{
|
|
// Destroy the server socket. A new connection
|
|
// will be attempted on the next tick
|
|
Print("Client disconnected. Will retry.");
|
|
delete glbClientSocket;
|
|
glbClientSocket = NULL;
|
|
}
|
|
}
|
|
|
|
|
|
L1++;
|
|
if(K==0)
|
|
{
|
|
K++;
|
|
}
|
|
|
|
|
|
}
|
|
//+------------------------------------------------------------------+
|