-
Notifications
You must be signed in to change notification settings - Fork 1
/
socket.mq5
159 lines (150 loc) · 6.73 KB
/
socket.mq5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//+------------------------------------------------------------------+
//| socket.mq5 |
//| avoitenko |
//| https://login.mql5.com/en/users/avoitenko |
//+------------------------------------------------------------------+
#property copyright "avoitenko"
#property link "https://login.mql5.com/en/users/avoitenko"
#property version "1.00"
//+------------------------------------------------------------------+
//| Defines |
//+------------------------------------------------------------------+
#define ERROR_SUCCESS 0
#define SOCKET_STATUS_CONNECTED 1
#define SOCKET_STATUS_DISCONNECTED 2
//+------------------------------------------------------------------+
//| SOCKET_CLIENT |
//+------------------------------------------------------------------+
struct SOCKET_CLIENT
{
uchar status;
ushort sequence;
uint sock;
};
//+------------------------------------------------------------------+
//| ENUM_DATA_TYPE |
//+------------------------------------------------------------------+
enum ENUM_DATA_TYPE
{
DATA_STRING,//String
DATA_STRUCT //Struct
};
//+------------------------------------------------------------------+
//| Inport DLL |
//+------------------------------------------------------------------+
#import "socket_mql5_x86.dll"
uint SocketOpen(SOCKET_CLIENT &socket,const string host,const ushort port);
void SocketClose(SOCKET_CLIENT &socket);
uint SocketWriteStruct(SOCKET_CLIENT &socket,const string symbol,const MqlTick &tick);
uint SocketWriteString(SOCKET_CLIENT &socket,const string str);
string SocketErrorString(int error_code);
#import "socket_mql5_x64.dll"
uint SocketOpen(SOCKET_CLIENT &socket,const string host,const ushort port);
void SocketClose(SOCKET_CLIENT &socket);
uint SocketWriteStruct(SOCKET_CLIENT &socket,const string symbol,const MqlTick &tick);
uint SocketWriteString(SOCKET_CLIENT &socket,const string str);
string SocketErrorString(int error_code);
#import
//+------------------------------------------------------------------+
//| SocketOpen |
//+------------------------------------------------------------------+
uint SocketOpen(SOCKET_CLIENT &socket,const string host,const ushort port)
{
if(_IsX64)return(socket_mql5_x64::SocketOpen(socket, host, port));
return(socket_mql5_x86::SocketOpen(socket, host, port));
}
//+------------------------------------------------------------------+
//| SocketClose |
//+------------------------------------------------------------------+
void SocketClose(SOCKET_CLIENT &socket)
{
if(_IsX64)socket_mql5_x64::SocketClose(socket);
else socket_mql5_x86::SocketClose(socket);
}
//+------------------------------------------------------------------+
//| SocketWriteData |
//+------------------------------------------------------------------+
uint SocketWriteStruct(SOCKET_CLIENT &socket,const string symbol,const MqlTick &tick)
{
if(_IsX64)return(socket_mql5_x64::SocketWriteStruct(socket,symbol,tick));
return(socket_mql5_x86::SocketWriteStruct(socket,symbol,tick));
}
//+------------------------------------------------------------------+
//| SocketWriteString |
//+------------------------------------------------------------------+
uint SocketWriteString(SOCKET_CLIENT &socket,const string str)
{
if(_IsX64)return(socket_mql5_x64::SocketWriteString(socket,str));
return(socket_mql5_x86::SocketWriteString(socket,str));
}
//+------------------------------------------------------------------+
//| SysErrorMessage |
//+------------------------------------------------------------------+
string SocketErrorString(const int error_code)
{
if(_IsX64)return(socket_mql5_x64::SocketErrorString(error_code));
return(socket_mql5_x86::SocketErrorString(error_code));
}
//+------------------------------------------------------------------+
//| Input variables |
//+------------------------------------------------------------------+
input string InpHost="localhost"; // Host
input ushort InpPort=777; // Port
input ENUM_DATA_TYPE InpType=DATA_STRING; // Data Type
//--- global variables
SOCKET_CLIENT client;
MqlTick last_tick;
//+------------------------------------------------------------------+
//| OnInit |
//+------------------------------------------------------------------+
int OnInit()
{
SocketOpen(client,InpHost,InpPort);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| OnDeinit |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
SocketClose(client);
}
//+------------------------------------------------------------------+
//| OnTick |
//+------------------------------------------------------------------+
void OnTick()
{
if(!SymbolInfoTick(_Symbol,last_tick))return;
string str_msg=StringFormat("symbol: %s dt: %s bid: %s ask: %s",_Symbol,TimeToString(last_tick.time,TIME_DATE|TIME_SECONDS),
DoubleToString(last_tick.bid,_Digits),DoubleToString(last_tick.ask,_Digits));
switch(InpType)
{
case DATA_STRING: //write string
{
string str_out=StringFormat("%s %s %s %s",_Symbol,TimeToString(last_tick.time,TIME_DATE|TIME_SECONDS),
DoubleToString(last_tick.bid,_Digits),DoubleToString(last_tick.ask,_Digits));
uint err=SocketWriteString(client,str_out);
if(err!=ERROR_SUCCESS)
{
Print(SocketErrorString(err));
SocketOpen(client,InpHost,InpPort);
}
else
Print(str_msg);
}
break;
case DATA_STRUCT: //write struct
{
uint err=SocketWriteStruct(client,_Symbol,last_tick);
if(err!=ERROR_SUCCESS)
{
Print(SocketErrorString(err));
SocketOpen(client,InpHost,InpPort);
}
else
Print(str_msg);
}
break;
}// end switch
}
//+------------------------------------------------------------------+