AhtashamShahzad3 месяцев назад: 4
Родитель
Сommit
3e67848e39
2 измененных файлов с 120 добавлено и 10 удалено
  1. BIN
      blTelegramToMT4.ex4
  2. 120 10
      blTelegramToMT4.mq4

BIN
blTelegramToMT4.ex4


+ 120 - 10
blTelegramToMT4.mq4

@@ -15,9 +15,11 @@
15
 struct msgDetails
15
 struct msgDetails
16
   {
16
   {
17
    int               msgid;
17
    int               msgid;
18
+   ulong             tickt;
18
                      msgDetails()
19
                      msgDetails()
19
      {
20
      {
20
       msgid       = -1;
21
       msgid       = -1;
22
+      tickt       = -1;
21
      }
23
      }
22
   };
24
   };
23
 
25
 
@@ -29,6 +31,7 @@ input       string               symbolMatch           = "GOLD:XAUUSD,BitCoin:BT
29
 input       string               suffix                = "";                                                 // Account Suffix
31
 input       string               suffix                = "";                                                 // Account Suffix
30
 input       string               prefix                = "";                                                 // Account Prefix
32
 input       string               prefix                = "";                                                 // Account Prefix
31
 input       double               lotSize               = 0.1;                                                // Lot Size
33
 input       double               lotSize               = 0.1;                                                // Lot Size
34
+input       double               partialClose          = 20;                                                 // Partial Closing %
32
 
35
 
33
 //+------------------------------------------------------------------+
36
 //+------------------------------------------------------------------+
34
 //| Expert initialization function                                   |
37
 //| Expert initialization function                                   |
@@ -269,6 +272,7 @@ void execute_functionality_on_new_message(int i)
269
      {
272
      {
270
 
273
 
271
       string isReplyValue = getJsonStringValue(jsonString, "is_reply");
274
       string isReplyValue = getJsonStringValue(jsonString, "is_reply");
275
+      StringReplace(isReplyValue," ","");
272
 
276
 
273
       if(isReplyValue == "True")
277
       if(isReplyValue == "True")
274
         {
278
         {
@@ -280,6 +284,74 @@ void execute_functionality_on_new_message(int i)
280
          Print(" ================ Replied Message found of message_id ================ ",reply_to_msg_id);
284
          Print(" ================ Replied Message found of message_id ================ ",reply_to_msg_id);
281
          Print(" ================ Message: ================ ",message);
285
          Print(" ================ Message: ================ ",message);
282
 
286
 
287
+         int ticket = getTicket(reply_to_msg_id);
288
+
289
+         if(ticket != -1)
290
+           {
291
+
292
+            if(StringFind(message,"take") != -1 && StringFind(message,"some") != -1 && StringFind(message,"partial") != -1 && StringFind(message,"profit") != -1)
293
+              {
294
+
295
+               if(OrderSelect(ticket,SELECT_BY_TICKET))
296
+                 {
297
+
298
+                  double lot = NormalizeDouble(OrderLots()*(partialClose/100),2);
299
+
300
+                  bool result = OrderClose(ticket,lot,OrderOpenPrice(),10, clrNONE);
301
+
302
+                  if(result)
303
+                    {
304
+                     Print("Partially closed ", lot, " lots from order ", ticket);
305
+
306
+
307
+
308
+                     for(int j = OrdersTotal()-1; j>=0; j--)
309
+                       {
310
+                        if(OrderSelect(j, SELECT_BY_POS))
311
+                          {
312
+                           if(OrderComment() != "")
313
+                             {
314
+                              string ticketToBeReplace = OrderComment();
315
+                              StringReplace(ticketToBeReplace,"from #","");
316
+
317
+                              if(int(ticketToBeReplace) == ticket)
318
+                                {
319
+                                 findAndreplaceTicketFromStructure(OrderTicket(),reply_to_msg_id);
320
+                                }
321
+                             }
322
+                          }
323
+                       }
324
+
325
+
326
+                    }
327
+                  else
328
+                    {
329
+                     Print("Error closing partial order: ", GetLastError()," lot = ",lot," OrderLots() = ",OrderLots());
330
+                    }
331
+                 }
332
+              }
333
+
334
+            if(StringFind(message,"set") != -1 && StringFind(message,"stop") != -1 && StringFind(message,"loss") != -1 && StringFind(message,"entry") != -1)
335
+              {
336
+
337
+               if(OrderSelect(ticket,SELECT_BY_TICKET))
338
+                 {
339
+
340
+                  bool result = OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrNONE);
341
+
342
+                  if(result)
343
+                    {
344
+                     Print("Order Sl Modify to Open Price ", ticket);
345
+                    }
346
+                  else
347
+                    {
348
+                     Print("Error in Modify order SL : ", GetLastError());
349
+                    }
350
+                 }
351
+              }
352
+           }
353
+
354
+
283
         }
355
         }
284
 
356
 
285
       else
357
       else
@@ -320,17 +392,52 @@ void execute_functionality_on_new_message(int i)
320
                  }
392
                  }
321
               }
393
               }
322
 
394
 
323
-            addtoMessageStructure(group_message_id,message);
395
+            int ticket = -1;
396
+
397
+            message(result,message,group_message_id,ticket);
398
+
399
+            addtoMessageStructure(group_message_id,message,ticket);
324
 
400
 
325
-            message(result,message,group_message_id);
326
            }
401
            }
327
         }
402
         }
328
      }
403
      }
329
   }
404
   }
405
+
406
+//+------------------------------------------------------------------+
407
+//|                                                                  |
408
+//+------------------------------------------------------------------+
409
+void findAndreplaceTicketFromStructure(int ticketToBeReplace,int reply_to_msg_id)
410
+  {
411
+   for(int i=0; i < MaxOrders; i++)
412
+     {
413
+      if(od[i].msgid == reply_to_msg_id)
414
+        {
415
+
416
+         od[i].tickt = ticketToBeReplace;
417
+
418
+         Print("codeOrder is partially closed so update Ticket ",ticketToBeReplace);
419
+         break;
420
+        }
421
+     }
422
+  }
423
+//+------------------------------------------------------------------+
424
+//|                                                                  |
425
+//+------------------------------------------------------------------+
426
+int getTicket(int reply_to_msg_id)
427
+  {
428
+   for(int i=0; i < MaxOrders; i++)
429
+     {
430
+      if(od[i].msgid == reply_to_msg_id)
431
+        {
432
+         return int(od[i].tickt);
433
+        }
434
+     }
435
+   return -1;
436
+  }
330
 //+------------------------------------------------------------------+
437
 //+------------------------------------------------------------------+
331
 //|                                                                  |
438
 //|                                                                  |
332
 //+------------------------------------------------------------------+
439
 //+------------------------------------------------------------------+
333
-void message(string &result[], string message, int message_id)
440
+void message(string &result[], string message, int message_id,int & tickt)
334
   {
441
   {
335
    string lineOne[]; // = result[0];
442
    string lineOne[]; // = result[0];
336
    int lineIndex = 0;
443
    int lineIndex = 0;
@@ -423,12 +530,12 @@ void message(string &result[], string message, int message_id)
423
 
530
 
424
          if(direction == buy)
531
          if(direction == buy)
425
            {
532
            {
426
-            placeBuyTrade(symbol, tp, sl, message_id, lotSize);
533
+            tickt = placeBuyTrade(symbol, tp, sl, message_id, lotSize);
427
            }
534
            }
428
 
535
 
429
          if(direction == sell)
536
          if(direction == sell)
430
            {
537
            {
431
-            placeSellTrade(symbol, tp, sl, message_id, lotSize);
538
+            tickt = placeSellTrade(symbol, tp, sl, message_id, lotSize);
432
            }
539
            }
433
         }
540
         }
434
      }
541
      }
@@ -436,7 +543,7 @@ void message(string &result[], string message, int message_id)
436
 //+------------------------------------------------------------------+
543
 //+------------------------------------------------------------------+
437
 //|                                                                  |
544
 //|                                                                  |
438
 //+------------------------------------------------------------------+
545
 //+------------------------------------------------------------------+
439
-void placeBuyTrade(string symbol,double tp,double sl, int messageId, double lot_size)
546
+int placeBuyTrade(string symbol,double tp,double sl, int messageId, double lot_size)
440
   {
547
   {
441
 
548
 
442
    double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
549
    double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
@@ -455,11 +562,13 @@ void placeBuyTrade(string symbol,double tp,double sl, int messageId, double lot_
455
      {
562
      {
456
       Print(" Buy Order Is Placed Sucessfully ");
563
       Print(" Buy Order Is Placed Sucessfully ");
457
      }
564
      }
565
+
566
+   return ticket;
458
   }
567
   }
459
 //+------------------------------------------------------------------+
568
 //+------------------------------------------------------------------+
460
 //|                                                                  |
569
 //|                                                                  |
461
 //+------------------------------------------------------------------+
570
 //+------------------------------------------------------------------+
462
-void placeSellTrade(string symbol,double tp,double sl, int messageId, double lot_size)
571
+int placeSellTrade(string symbol,double tp,double sl, int messageId, double lot_size)
463
   {
572
   {
464
 
573
 
465
    double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
574
    double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
@@ -476,20 +585,21 @@ void placeSellTrade(string symbol,double tp,double sl, int messageId, double lot
476
      {
585
      {
477
       Print(" Sell Order Is Placed Sucessfully ");
586
       Print(" Sell Order Is Placed Sucessfully ");
478
      }
587
      }
479
-
588
+   return ticket;
480
   }
589
   }
481
 //+------------------------------------------------------------------+
590
 //+------------------------------------------------------------------+
482
 //|                                                                  |
591
 //|                                                                  |
483
 //+------------------------------------------------------------------+
592
 //+------------------------------------------------------------------+
484
-void addtoMessageStructure(int message_id,string message)
593
+void addtoMessageStructure(int message_id,string message,ulong ticket)
485
   {
594
   {
486
    for(int i=0; i < MaxOrders; i++)
595
    for(int i=0; i < MaxOrders; i++)
487
      {
596
      {
488
       if(od[i].msgid == -1)
597
       if(od[i].msgid == -1)
489
         {
598
         {
490
          od[i].msgid = message_id;
599
          od[i].msgid = message_id;
600
+         od[i].tickt = ticket;
491
          StringToLower(message);
601
          StringToLower(message);
492
-         Print(" Message ID ",message_id,"  of Message = ",message," is added To Structure :: ");
602
+         Print(" Message ID ",message_id,"  of Message = ",message," Having Ticket ",ticket," is added To Structure :: ");
493
          break;
603
          break;
494
         }
604
         }
495
      }
605
      }