Przeglądaj źródła

Ticket: 5083 Message Fetching

Huzaifa-MQLDev 4 miesięcy temu
rodzic
commit
1d047b3dce
2 zmienionych plików z 379 dodań i 0 usunięć
  1. BIN
      blTelegramToMT4.ex4
  2. 379 0
      blTelegramToMT4.mq4

BIN
blTelegramToMT4.ex4


+ 379 - 0
blTelegramToMT4.mq4

@@ -0,0 +1,379 @@
1
+//+------------------------------------------------------------------+
2
+//|                                              blTelegramToMT4.mq4 |
3
+//|                                  Copyright 2025, MQL Development |
4
+//|                                  https://www.mqldevelopment.com/ |
5
+//+------------------------------------------------------------------+
6
+#property copyright "Copyright 2025, MQL Development"
7
+#property link      "https://www.mqldevelopment.com/"
8
+#property version   "1.00"
9
+#property strict
10
+
11
+#define buy "buy"
12
+#define sell "sell"
13
+#define MaxOrders 10000
14
+
15
+struct msgDetails
16
+  {
17
+   int               msgid;
18
+                     msgDetails()
19
+     {
20
+      msgid       = -1;
21
+     }
22
+  };
23
+
24
+msgDetails od[MaxOrders];
25
+
26
+struct new_trade_store
27
+  {
28
+   ulong             ticket;         // Ticket
29
+   int               order_type;     // OrderType
30
+   double            lot;            // Lot
31
+   double            stop_loss;      // StopLoss
32
+   double            take_profit;    // TakeProfit
33
+   double            price;          // Price
34
+   string            symbol;         // Symbol
35
+   int               chatId;         // ChatId
36
+   string            trade_type;     // Trade Type (Trade Type: Tp1, Tp2, Tp3.... Tpn)
37
+
38
+                     new_trade_store()
39
+     {
40
+      ticket = -1;
41
+      chatId=-1;
42
+      trade_type = "";
43
+     }
44
+
45
+  };
46
+new_trade_store newTradeStore[MaxOrders];
47
+
48
+input       string               Settings              = " ------------- General Settings ------------- ";   //_
49
+input       int                  magic_no              = 333;                                                // Magic no
50
+input       string               symbolMatch           = "GOLD:XAUUSD,BitCoin:BTCUSD";                       // Symbol Mapping (Telegram:MT5)
51
+input       string               suffix                = "";                                                 // Account Suffix
52
+input       string               prefix                = "";                                                 // Account Prefix
53
+input       double               lotSize               = 0.1;                                                // Lot Size
54
+
55
+//+------------------------------------------------------------------+
56
+//| Expert initialization function                                   |
57
+//+------------------------------------------------------------------+
58
+// Global Variables
59
+string      url1      = "http://127.0.0.1";
60
+string      header    = "Content-Type: application/json\r\nAccept: application/json\r\n";
61
+
62
+int getme_result;
63
+
64
+string symbolChart[];
65
+string symbolSnd[];
66
+string symbolsListUserInput[];
67
+uchar  sym1[];
68
+uchar  sym2[];
69
+datetime ea_start_time = 0;
70
+int OnInit()
71
+  {
72
+//--- create timer
73
+   ea_start_time = TimeCurrent();
74
+
75
+   ushort u_sep = StringGetCharacter(",",0);
76
+   StringSplit(symbolMatch,u_sep,symbolsListUserInput);
77
+   ArrayResize(symbolChart,0);
78
+   ArrayResize(symbolSnd,0);
79
+
80
+   for(int i = 0; i < ArraySize(symbolsListUserInput); i++)
81
+     {
82
+
83
+      string str = symbolsListUserInput[i];
84
+
85
+      int index  = StringFind(str,":");
86
+      int index2 = StringLen(str);
87
+
88
+      ArrayResize(sym1,0);
89
+      ArrayResize(sym2,0);
90
+
91
+      for(int j = 0; j < index; j++)
92
+        {
93
+         ArrayResize(sym1,ArraySize(sym1)+1);
94
+         sym1[j] = uchar(str[j]);
95
+        }
96
+
97
+      int k = 0;
98
+
99
+      for(int j = index + 1 ; j < index2; j++)
100
+        {
101
+         ArrayResize(sym2,ArraySize(sym2)+1);
102
+         sym2[k] = uchar(str[j]);
103
+         k++;
104
+        }
105
+
106
+      ArrayResize(symbolChart,ArraySize(symbolChart)+1);
107
+      ArrayResize(symbolSnd,ArraySize(symbolSnd)+1);
108
+
109
+      symbolChart[i] = CharArrayToString(sym1);
110
+      symbolSnd[i]   = CharArrayToString(sym2);
111
+
112
+     }
113
+
114
+   string SplitArray[];
115
+
116
+   string jsonString = GET_function(url1+"/get-all-messages",header);
117
+   StringReplace(jsonString,"},", "*");
118
+   StringSplit(jsonString,'*',SplitArray);
119
+
120
+   for(int i = 0; i < ArraySize(SplitArray); i++)
121
+     {
122
+      seperatingDataOnint(i,SplitArray[i]);
123
+     }
124
+
125
+   EventSetTimer(1);
126
+
127
+//---
128
+   return(INIT_SUCCEEDED);
129
+  }
130
+//+------------------------------------------------------------------+
131
+//| Expert deinitialization function                                 |
132
+//+------------------------------------------------------------------+
133
+void OnDeinit(const int reason)
134
+  {
135
+//--- destroy timer
136
+   EventKillTimer();
137
+
138
+  }
139
+//+------------------------------------------------------------------+
140
+//| Expert tick function                                             |
141
+//+------------------------------------------------------------------+
142
+void OnTick()
143
+  {
144
+//---
145
+
146
+  }
147
+//+------------------------------------------------------------------+
148
+//| Timer function                                                   |
149
+//+------------------------------------------------------------------+
150
+void OnTimer()
151
+  {
152
+//---
153
+   string SplitArray[];
154
+
155
+   string jsonString = GET_function(url1+"/get-all-messages",header);
156
+   StringReplace(jsonString,"},", "*");
157
+   StringSplit(jsonString,'*',SplitArray);
158
+
159
+   for(int i = 0; i < ArraySize(SplitArray); i++)
160
+     {
161
+      seperatingData(i,SplitArray[i]);
162
+     }
163
+  }
164
+//+------------------------------------------------------------------+
165
+//|                                                                  |
166
+//+------------------------------------------------------------------+
167
+string  GET_function(string url,string headers)
168
+  {
169
+
170
+   string result_string = NULL;
171
+   int timeout          = 10;  // Set the timeout value in seconds
172
+
173
+   char result[],data[];
174
+   string resultHeaders;
175
+   int res = WebRequest("GET",url,headers,timeout,data,result,resultHeaders);
176
+
177
+   if(res == 200)
178
+     {
179
+      result_string = CharArrayToString(result);
180
+     }
181
+   else
182
+     {
183
+      Print("content GETT: ", result_string," Error: ",GetLastError(), " res: ",res);
184
+     }
185
+
186
+   return result_string;
187
+  }
188
+//+------------------------------------------------------------------+
189
+//|                                                                  |
190
+//+------------------------------------------------------------------+
191
+void seperatingDataOnint(int index, string data)
192
+  {
193
+
194
+   string message = getJsonStringValue(data,"message",4,"\"");
195
+   int message_id = (int)getJsonStringValue(data,"message_id",4,"\"");
196
+
197
+   if(checkExistingTrade(message_id) == false)
198
+     {
199
+      Print(" ================ OnInit New Trade Message Found ================");
200
+      StringReplace(message,"~","#");
201
+      StringToLower(message);
202
+      Print("Message is ", message);
203
+      for(int i=0; i < MaxOrders; i++)
204
+        {
205
+         if(od[i].msgid == -1)
206
+           {
207
+            od[i].msgid = message_id;
208
+            break;
209
+           }
210
+        }
211
+     }
212
+  }
213
+//+------------------------------------------------------------------+
214
+//|                                                                  |
215
+//+------------------------------------------------------------------+
216
+bool checkExistingTrade(int message_id)
217
+  {
218
+   for(int i=0; i < MaxOrders; i++)
219
+     {
220
+      if(od[i].msgid == message_id)
221
+        {
222
+         return true;
223
+        }
224
+     }
225
+   return false;
226
+  }
227
+
228
+//+------------------------------------------------------------------+
229
+//|                                                                  |
230
+//+------------------------------------------------------------------+
231
+string getJsonStringValue(string json,string key,int addUp,string endSign)
232
+  {
233
+   int indexStart = StringFind(json,key)+StringLen(key)+addUp;
234
+   int indexEnd   = StringFind(json,endSign,indexStart);
235
+   return StringSubstr(json,indexStart,indexEnd-indexStart);
236
+  }
237
+//+------------------------------------------------------------------+
238
+//|                                                                  |
239
+//+------------------------------------------------------------------+
240
+void seperatingData(int index, string data)
241
+  {
242
+// Print(" Data: ", data);
243
+   string message = getJsonStringValue(data,"message",4,"\"");
244
+   int message_id = (int)getJsonStringValue(data,"message_id",4,"\"");
245
+
246
+   string isReplyValue = getJsonStringValue(data,"is_reply",4,"\"");
247
+   int reply_to_msg_id = (int)getJsonStringValue(data,"reply_to_msg_id",4,"\"");
248
+
249
+   if(isReplyValue == "False")
250
+     {
251
+      if(checkExistingTrade(message_id) == false)
252
+        {
253
+         Print(" --------------- New Trade Message Found ----------------- ", " Message Id: ", message_id);
254
+         StringReplace(message,"~","#");
255
+         StringReplace(message,"##", "#");
256
+         StringToLower(message);
257
+         message = removeExtraSpaces(message);
258
+         Print("Message is ",message);
259
+         addtoMessageStructure(message_id,message);
260
+
261
+         string result[];
262
+         string tempResult[];
263
+         int indexTemp = 0;
264
+
265
+         StringSplit(message,'#',tempResult);
266
+
267
+         for(int i=0; i<ArraySize(tempResult); i++)
268
+           {
269
+            //result[i] = StringTrimLeft(result[i]);
270
+            //result[i] = StringTrimRight(result[i]);
271
+            //Print("Temp Result : ", tempResult[i], " index is: ", i);
272
+            if(HasAlphanumeric(tempResult[i]))
273
+              {
274
+               //Print(" contains alphanumeric characters.");
275
+               ArrayResize(result,ArraySize(result)+1);
276
+               result[indexTemp] = tempResult[i];
277
+               indexTemp++;
278
+              }
279
+            else
280
+              {
281
+               //Print(" does not contain alphanumeric characters.");
282
+               //ArrayResize(indexToDelete,ArraySize(indexToDelete)+1);
283
+               //indexToDelete[indexTemp] = i;
284
+               //indexTemp++;
285
+              }
286
+           }
287
+
288
+
289
+
290
+        }
291
+     }
292
+
293
+   if(isReplyValue == "True")
294
+     {
295
+      if(checkExistingTrade(message_id) == false)
296
+        {
297
+         Print(" --------------- New Replied Message Found ----------------- ", " Message Id: ", message_id, " Reply Message Id: ", reply_to_msg_id);
298
+         StringToLower(message);
299
+         Print("Message is ",message);
300
+         addtoMessageStructure(message_id,message);
301
+        }
302
+     }
303
+  }
304
+//+------------------------------------------------------------------+
305
+//|                                                                  |
306
+//+------------------------------------------------------------------+
307
+void addtoMessageStructure(int message_id,string message)
308
+  {
309
+   for(int i=0; i < MaxOrders; i++)
310
+     {
311
+      if(od[i].msgid == -1)
312
+        {
313
+         od[i].msgid = message_id;
314
+         break;
315
+        }
316
+     }
317
+
318
+  }
319
+//+------------------------------------------------------------------+
320
+//|                                                                  |
321
+//+------------------------------------------------------------------+
322
+string removeExtraSpaces(string str)
323
+  {
324
+   string result = "";
325
+   int len = StringLen(str);
326
+   bool lastWasSpace = false;
327
+
328
+   for(int i = 0; i < len; i++)
329
+     {
330
+      string currentChar = StringSubstr(str, i, 1);
331
+
332
+      if(currentChar == " ")
333
+        {
334
+         // Skip adding this space if the last character was also a space
335
+         if(lastWasSpace)
336
+            continue;
337
+
338
+         lastWasSpace = true;
339
+        }
340
+      else
341
+        {
342
+         lastWasSpace = false;
343
+        }
344
+
345
+      result += currentChar;
346
+     }
347
+
348
+   return result;
349
+  }
350
+//+------------------------------------------------------------------+
351
+//|                                                                  |
352
+//+------------------------------------------------------------------+
353
+bool HasAlphanumeric(string str)
354
+  {
355
+//Print("String Length: ", StringLen(str));
356
+   for(int i = 0; i <= StringLen(str); i++)
357
+     {
358
+      //Print("Here ", StringLen(str));
359
+      if(IsAlphanumeric((char)str[i]))
360
+        {
361
+         return true;
362
+         break;
363
+        }
364
+     }
365
+   return false;
366
+  }
367
+//+------------------------------------------------------------------+
368
+//|                                                                  |
369
+//+------------------------------------------------------------------+
370
+bool IsAlphanumeric(char c)
371
+  {
372
+   if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
373
+      return true;
374
+   return false;
375
+  }
376
+//+------------------------------------------------------------------+
377
+//|                                                                  |
378
+//+------------------------------------------------------------------+
379
+//+------------------------------------------------------------------+