Huzaifa-MQLDev 4 місяців тому
батько
коміт
1afd78ac03
2 змінених файлів з 346 додано та 13 видалено
  1. BIN
      blTelegramToMT4.ex4
  2. 346 13
      blTelegramToMT4.mq4

BIN
blTelegramToMT4.ex4


+ 346 - 13
blTelegramToMT4.mq4

@@ -67,6 +67,7 @@ string symbolsListUserInput[];
67 67
 uchar  sym1[];
68 68
 uchar  sym2[];
69 69
 datetime ea_start_time = 0;
70
+int last_message_id = INT_MIN;
70 71
 int OnInit()
71 72
   {
72 73
 //--- create timer
@@ -111,15 +112,16 @@ int OnInit()
111 112
 
112 113
      }
113 114
 
114
-   string SplitArray[];
115
-
116
-   string jsonString = GET_function(url1+"/get-all-messages",header);
115
+   string jsonString = GET_function(url1 + "/get-latest-message-id", header);
117 116
    StringReplace(jsonString,"},", "*");
118
-   StringSplit(jsonString,'*',SplitArray);
117
+   last_message_id = (int)getJsonStringValue(jsonString, "id") != 0 ? (int)getJsonStringValue(jsonString, "id") : INT_MIN;
118
+
119
+   string message = getJsonStringValue(jsonString, "message");
119 120
 
120
-   for(int i = 0; i < ArraySize(SplitArray); i++)
121
+   if(last_message_id != INT_MIN)
121 122
      {
122
-      seperatingDataOnint(i,SplitArray[i]);
123
+      Print(" latest_message_id = ",last_message_id);
124
+      Print(" result found against get-latest-message-id = ",message);
123 125
      }
124 126
 
125 127
    EventSetTimer(1);
@@ -150,15 +152,43 @@ void OnTick()
150 152
 void OnTimer()
151 153
   {
152 154
 //---
153
-   string SplitArray[];
155
+   if(last_message_id == INT_MIN)
156
+     {
157
+
158
+      string jsonString = GET_function(url1 + "/get-latest-message-id", header);
159
+      StringReplace(jsonString,"},", "*");
160
+      last_message_id = (int)getJsonStringValue(jsonString, "id") != 0 ? (int)getJsonStringValue(jsonString, "id") : INT_MIN;
154 161
 
155
-   string jsonString = GET_function(url1+"/get-all-messages",header);
156
-   StringReplace(jsonString,"},", "*");
157
-   StringSplit(jsonString,'*',SplitArray);
158 162
 
159
-   for(int i = 0; i < ArraySize(SplitArray); i++)
163
+      if(last_message_id != INT_MIN)
164
+        {
165
+
166
+         string message = getJsonStringValue(jsonString, "message");
167
+
168
+         if(last_message_id != INT_MIN)
169
+           {
170
+            Print(" latest_message_id = ",last_message_id);
171
+            Print(" result found against get-latest-message-id = ",message);
172
+           }
173
+         execute_functionality_on_new_message(last_message_id);
174
+
175
+        }
176
+
177
+     }
178
+   else
160 179
      {
161
-      seperatingData(i,SplitArray[i]);
180
+      string jsonString = GET_function(url1 + "/get-latest-message-id", header);
181
+      StringReplace(jsonString,"},", "*");
182
+      int latest_message_id = (int)getJsonStringValue(jsonString, "id") != 0 ? (int)getJsonStringValue(jsonString, "id") : INT_MIN;
183
+
184
+      if(last_message_id != latest_message_id)
185
+        {
186
+         for(int i = latest_message_id; i > last_message_id; i--)
187
+           {
188
+            execute_functionality_on_new_message(i);
189
+           }
190
+         last_message_id = latest_message_id;
191
+        }
162 192
      }
163 193
   }
164 194
 //+------------------------------------------------------------------+
@@ -237,6 +267,41 @@ string getJsonStringValue(string json,string key,int addUp,string endSign)
237 267
 //+------------------------------------------------------------------+
238 268
 //|                                                                  |
239 269
 //+------------------------------------------------------------------+
270
+string getJsonStringValue(string json, string key)
271
+  {
272
+   int start = StringFind(json, "\""+key+"\"");
273
+   if(start == -1)
274
+      return "";
275
+
276
+// Find colon after key
277
+   int colon = StringFind(json, ":", start);
278
+   if(colon == -1)
279
+      return "";
280
+
281
+// Find next comma or closing brace
282
+   int endComma = StringFind(json, ",", colon);
283
+   int endBrace = StringFind(json, "}", colon);
284
+   int end = (endComma != -1 && (endComma < endBrace || endBrace == -1)) ? endComma : endBrace;
285
+   if(end == -1)
286
+      end = StringLen(json);
287
+
288
+   string value = trim(StringSubstr(json, colon+1, end-colon-1));
289
+// remove quotes if exist
290
+   StringReplace(value, "\"", "");
291
+   return value;
292
+  }
293
+//+------------------------------------------------------------------+
294
+//|                                                                  |
295
+//+------------------------------------------------------------------+
296
+string trim(string text)
297
+  {
298
+   StringTrimLeft(text);
299
+   StringTrimRight(text);
300
+   return text;
301
+  }
302
+//+------------------------------------------------------------------+
303
+//|                                                                  |
304
+//+------------------------------------------------------------------+
240 305
 void seperatingData(int index, string data)
241 306
   {
242 307
 // Print(" Data: ", data);
@@ -285,7 +350,7 @@ void seperatingData(int index, string data)
285 350
               }
286 351
            }
287 352
 
288
-
353
+         message(result,message,message_id);
289 354
 
290 355
         }
291 356
      }
@@ -304,6 +369,235 @@ void seperatingData(int index, string data)
304 369
 //+------------------------------------------------------------------+
305 370
 //|                                                                  |
306 371
 //+------------------------------------------------------------------+
372
+void execute_functionality_on_new_message(int i)
373
+  {
374
+
375
+   string url = url1 + "/get-message/" + IntegerToString(i);
376
+   string jsonString = GET_function(url, header);
377
+   StringReplace(jsonString,"},", "*");
378
+
379
+   string message = getJsonStringValue(jsonString,"message",4,"\"");
380
+
381
+   int group_message_id = (int)getJsonStringValue(jsonString, "message_id");
382
+
383
+   StringToLower(message);
384
+   StringReplace(message,"~","#");
385
+
386
+   if(checkExistingTrade(group_message_id) == false)
387
+     {
388
+
389
+      string isReplyValue = getJsonStringValue(jsonString, "is_reply");
390
+
391
+      if(isReplyValue == "True")
392
+        {
393
+
394
+         int reply_to_msg_id = (int)getJsonStringValue(jsonString, "reply_to_msg_id");
395
+         message = getJsonStringValue(jsonString, "message");
396
+         StringToLower(message);
397
+
398
+         Print(" ================ Replied Message found of message_id ================ ",reply_to_msg_id);
399
+         Print(" ================ Message: ================ ",message);
400
+
401
+        }
402
+
403
+      else
404
+        {
405
+         if(checkExistingTrade(group_message_id) == false)
406
+           {
407
+            Print(" --------------- New Trade Message Found ----------------- ", " Message Id: ", group_message_id);
408
+            StringReplace(message,"~","#");
409
+            StringReplace(message,"##", "#");
410
+            StringToLower(message);
411
+            message = removeExtraSpaces(message);
412
+            Print("Message is ",message);
413
+
414
+            string result[];
415
+            string tempResult[];
416
+            int indexTemp = 0;
417
+
418
+            StringSplit(message,'#',tempResult);
419
+
420
+            for(int j=0; j<ArraySize(tempResult); j++)
421
+              {
422
+               //result[i] = StringTrimLeft(result[i]);
423
+               //result[i] = StringTrimRight(result[i]);
424
+               //Print("Temp Result : ", tempResult[i], " index is: ", i);
425
+               if(HasAlphanumeric(tempResult[j]))
426
+                 {
427
+                  //Print(" contains alphanumeric characters.");
428
+                  ArrayResize(result,ArraySize(result)+1);
429
+                  result[indexTemp] = tempResult[j];
430
+                  indexTemp++;
431
+                 }
432
+               else
433
+                 {
434
+                  //Print(" does not contain alphanumeric characters.");
435
+                  //ArrayResize(indexToDelete,ArraySize(indexToDelete)+1);
436
+                  //indexToDelete[indexTemp] = i;
437
+                  //indexTemp++;
438
+                 }
439
+              }
440
+
441
+            addtoMessageStructure(group_message_id,message);
442
+
443
+            message(result,message,group_message_id);
444
+           }
445
+        }
446
+     }
447
+  }
448
+//+------------------------------------------------------------------+
449
+//|                                                                  |
450
+//+------------------------------------------------------------------+
451
+void message(string &result[], string message, int message_id)
452
+  {
453
+   string lineOne[]; // = result[0];
454
+   int lineIndex = 0;
455
+   string direction = "";
456
+   int direction_index = -1;
457
+   string symbol = "";
458
+
459
+   if(ArraySize(result) >= 1)
460
+     {
461
+      StringSplit(result[0], ' ', lineOne);
462
+      if(((StringFind(result[0], "buy", 0) != -1) || (StringFind(result[0], "sell", 0) != -1)))
463
+        {
464
+
465
+         for(int i=0; i<ArraySize(lineOne); i++)
466
+           {
467
+            if(HasAlphanumeric(lineOne[i]))
468
+              {
469
+               ArrayResize(lineOne,ArraySize(lineOne)+1);
470
+               lineOne[lineIndex] = lineOne[i];
471
+               Print("Direction and Symbol: ", lineOne[lineIndex], " index is: ", lineIndex);
472
+
473
+               if(lineOne[lineIndex] == buy || lineOne[lineIndex] == sell)
474
+                 {
475
+                  direction = lineOne[lineIndex];
476
+                  direction_index = lineIndex;
477
+                  //Print(" Direction is: ", direction, " Direction Index: ", direction_index);
478
+                 }
479
+               lineIndex++;
480
+              }
481
+           }
482
+         if(ArraySize(lineOne) >= 2)
483
+           {
484
+            if(direction_index == 0)
485
+              {
486
+               symbol = lineOne[1];
487
+               StringToUpper(symbol);
488
+
489
+               //Print(" This is Message format One (1). Where Direction is: ", direction, " Symbol: ", symbol);
490
+              }
491
+            else
492
+               if(direction_index > 0)
493
+                 {
494
+                  symbol = lineOne[0];
495
+                  StringToUpper(symbol);
496
+
497
+                  //Print(" This is Message format One (1). Where Direction is: ", direction, " Symbol: ", symbol);
498
+                 }
499
+           }
500
+         symbol = symbolMapping(symbol);
501
+
502
+         double sl = 0;
503
+         double tpStore[]; // = result[0];
504
+         int tpIndex = 0;
505
+         for(int i=0 ; i < ArraySize(result); i++)
506
+           {
507
+            // result[i] = StringTrimLeft(result[i]);
508
+            // result[i] = StringTrimRight(result[i]);
509
+            // Print("Result : ", result[i], " index is: ", i);
510
+            if((StringFind(result[i], "sl", 0) != -1))
511
+              {
512
+               string tempSl[];
513
+               result[i] = trimString(result[i]);
514
+               //Print(" Sl String: ", result[i]);
515
+               StringSplit(result[i], ' ', tempSl);
516
+               if(ArraySize(tempSl) >= 2)
517
+                  sl = (double) tempSl[1];
518
+               Print("Sl : ", sl);
519
+              }
520
+            if((StringFind(result[i], "tp", 0) != -1))
521
+              {
522
+               // Print("Tp : ", result[i], " index is: ", i);
523
+               string tempTp[];
524
+               result[i] = trimString(result[i]);
525
+               StringSplit(result[i], ' ', tempTp);
526
+               //double tp = (double) tempTp[1];
527
+
528
+               ArrayResize(tpStore,ArraySize(tpStore)+1);
529
+               if(ArraySize(tempTp) >= 2)
530
+                  tpStore[tpIndex] = (double) tempTp[1];
531
+               Print("Tp : ", tpStore[tpIndex]);
532
+               tpIndex++;
533
+              }
534
+           }
535
+         Print("Side:", direction, " Symbol: ", symbol, " Tp Array Size: ", ArraySize(tpStore), " Message Id: ", message_id);
536
+         for(int i = 0 ; i < ArraySize(tpStore) ; i++)
537
+           {
538
+            Print("Side:", direction, " Symbol: ", symbol, " Tp: ", tpStore[i], " Sl: ", sl, " Message Id: ", message_id);
539
+            if(direction == buy)
540
+              {
541
+               placeBuyTrade(symbol, tpStore[i], sl, message_id, lotSize);
542
+              }
543
+
544
+            if(direction == sell)
545
+              {
546
+               placeSellTrade(symbol, tpStore[i], sl, message_id, lotSize);
547
+              }
548
+           }
549
+
550
+        }
551
+     }
552
+  }
553
+//+------------------------------------------------------------------+
554
+//|                                                                  |
555
+//+------------------------------------------------------------------+
556
+void placeBuyTrade(string symbol,double tp,double sl, int messageId, double lot_size)
557
+  {
558
+
559
+   double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
560
+   double bid = SymbolInfoDouble(symbol,SYMBOL_BID);
561
+   double buySl  = sl;
562
+   double buyTp  = tp;
563
+
564
+
565
+   int ticket = OrderSend(symbol, OP_BUY, lot_size, ask, 3, buySl, buyTp, "Buy Trade Placed.", magic_no, 0, clrBlue);
566
+   Print("Buy order Print: Stop Loss: ", buySl, " Take profit: ", buyTp);
567
+   if(ticket < 0)
568
+     {
569
+      Print("Buy Order Failed ", GetLastError());
570
+     }
571
+   else
572
+     {
573
+      Print(" Buy Order Is Placed Sucessfully ");
574
+     }
575
+  }
576
+//+------------------------------------------------------------------+
577
+//|                                                                  |
578
+//+------------------------------------------------------------------+
579
+void placeSellTrade(string symbol,double tp,double sl, int messageId, double lot_size)
580
+  {
581
+
582
+   double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
583
+   double bid = SymbolInfoDouble(symbol,SYMBOL_BID);
584
+   double sellSl = sl;
585
+   double sellTp = tp;
586
+
587
+   int ticket = OrderSend(symbol, OP_SELL, lot_size, bid, 3, sellSl, sellTp, "Sell Trade Placed.", magic_no, 0, clrRed);
588
+   if(ticket < 0)
589
+     {
590
+      Print("Sell Order Failed ", GetLastError());
591
+     }
592
+   else
593
+     {
594
+      Print(" Sell Order Is Placed Sucessfully ");
595
+     }
596
+
597
+  }
598
+//+------------------------------------------------------------------+
599
+//|                                                                  |
600
+//+------------------------------------------------------------------+
307 601
 void addtoMessageStructure(int message_id,string message)
308 602
   {
309 603
    for(int i=0; i < MaxOrders; i++)
@@ -311,10 +605,31 @@ void addtoMessageStructure(int message_id,string message)
311 605
       if(od[i].msgid == -1)
312 606
         {
313 607
          od[i].msgid = message_id;
608
+         StringToLower(message);
609
+         Print(" Message ID ",message_id,"  of Message = ",message," is added To Structure :: ");
314 610
          break;
315 611
         }
316 612
      }
613
+  }
614
+//+------------------------------------------------------------------+
615
+//|                                                                  |
616
+//+------------------------------------------------------------------+
617
+string trimString(string inputt)
618
+  {
619
+// Remove spaces from the left and right sides
620
+   int start = 0;
621
+   int end = StringLen(inputt) - 1;
317 622
 
623
+// Find the first non-space character
624
+   while(start <= end && StringGetCharacter(inputt, start) == ' ')
625
+      start++;
626
+
627
+// Find the last non-space character
628
+   while(end >= start && StringGetCharacter(inputt, end) == ' ')
629
+      end--;
630
+
631
+// Extract the substring without leading or trailing spaces
632
+   return StringSubstr(inputt, start, end - start + 1);
318 633
   }
319 634
 //+------------------------------------------------------------------+
320 635
 //|                                                                  |
@@ -376,4 +691,22 @@ bool IsAlphanumeric(char c)
376 691
 //+------------------------------------------------------------------+
377 692
 //|                                                                  |
378 693
 //+------------------------------------------------------------------+
694
+string symbolMapping(string symbol)
695
+  {
696
+   for(int k = 0; k < ArraySize(symbolChart); k++)
697
+     {
698
+      StringToUpper(symbolChart[k]);
699
+      if(symbol == symbolChart[k])
700
+        {
701
+         symbol = symbolSnd[k];
702
+        }
703
+     }
704
+   symbol = prefix + symbol + suffix;
705
+   return symbol;
706
+  }
707
+//+------------------------------------------------------------------+
708
+//|                                                                  |
709
+//+------------------------------------------------------------------+
710
+//+------------------------------------------------------------------+
711
+
379 712
 //+------------------------------------------------------------------+