Browse Source

Merge branch 'updation/partial_closing_feature' of MQL-Development/bl_telegram_to_mt4 into master

Wajeeh Saqib 4 months ago
parent
commit
3ac6fdaeb3
2 changed files with 120 additions and 10 deletions
  1. BIN
      blTelegramToMT4.ex4
  2. 120 10
      blTelegramToMT4.mq4

BIN
blTelegramToMT4.ex4


+ 120 - 10
blTelegramToMT4.mq4

@@ -15,9 +15,11 @@
15 15
 struct msgDetails
16 16
   {
17 17
    int               msgid;
18
+   ulong             tickt;
18 19
                      msgDetails()
19 20
      {
20 21
       msgid       = -1;
22
+      tickt       = -1;
21 23
      }
22 24
   };
23 25
 
@@ -29,6 +31,7 @@ input       string               symbolMatch           = "GOLD:XAUUSD,BitCoin:BT
29 31
 input       string               suffix                = "";                                                 // Account Suffix
30 32
 input       string               prefix                = "";                                                 // Account Prefix
31 33
 input       double               lotSize               = 0.1;                                                // Lot Size
34
+input       double               partialClose          = 20;                                                 // Partial Closing %
32 35
 
33 36
 //+------------------------------------------------------------------+
34 37
 //| Expert initialization function                                   |
@@ -269,6 +272,7 @@ void execute_functionality_on_new_message(int i)
269 272
      {
270 273
 
271 274
       string isReplyValue = getJsonStringValue(jsonString, "is_reply");
275
+      StringReplace(isReplyValue," ","");
272 276
 
273 277
       if(isReplyValue == "True")
274 278
         {
@@ -280,6 +284,74 @@ void execute_functionality_on_new_message(int i)
280 284
          Print(" ================ Replied Message found of message_id ================ ",reply_to_msg_id);
281 285
          Print(" ================ Message: ================ ",message);
282 286
 
287
+         int ticket = getTicket(reply_to_msg_id);
288
+
289
+         if(ticket != -1)
290
+           {
291
+            if(StringFind(message,"set") != -1 && StringFind(message,"stop") != -1 && StringFind(message,"loss") != -1 && StringFind(message,"entry") != -1)
292
+              {
293
+
294
+               if(OrderSelect(ticket,SELECT_BY_TICKET))
295
+                 {
296
+
297
+                  bool result = OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrNONE);
298
+
299
+                  if(result)
300
+                    {
301
+                     Print("Order Sl Modify to Open Price ", ticket);
302
+                    }
303
+                  else
304
+                    {
305
+                     Print("Error in Modify order SL : ", GetLastError()," Setting SL at: ",OrderOpenPrice()," price: ",OrderClosePrice());
306
+                    }
307
+                 }
308
+              }
309
+
310
+            if(StringFind(message,"take") != -1 && StringFind(message,"some") != -1 && StringFind(message,"partial") != -1 && StringFind(message,"profit") != -1)
311
+              {
312
+
313
+               if(OrderSelect(ticket,SELECT_BY_TICKET))
314
+                 {
315
+
316
+                  double lot = NormalizeDouble(OrderLots()*(partialClose/100),2);
317
+
318
+                  bool result = OrderClose(ticket,lot,OrderClosePrice(),10, clrNONE);
319
+
320
+                  if(result)
321
+                    {
322
+                     Print("Partially closed ", lot, " lots from order ", ticket);
323
+
324
+
325
+
326
+                     for(int j = OrdersTotal()-1; j>=0; j--)
327
+                       {
328
+                        if(OrderSelect(j, SELECT_BY_POS))
329
+                          {
330
+                           if(OrderComment() != "")
331
+                             {
332
+                              string ticketToBeReplace = OrderComment();
333
+                              StringReplace(ticketToBeReplace,"from #","");
334
+
335
+                              if(int(ticketToBeReplace) == ticket)
336
+                                {
337
+                                 findAndreplaceTicketFromStructure(OrderTicket(),reply_to_msg_id);
338
+                                }
339
+                             }
340
+                          }
341
+                       }
342
+
343
+
344
+                    }
345
+                  else
346
+                    {
347
+                     Print("Error closing partial order: ", GetLastError()," lot = ",lot," OrderLots() = ",OrderLots());
348
+                    }
349
+                 }
350
+              }
351
+
352
+           }
353
+
354
+
283 355
         }
284 356
 
285 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 442
    string lineOne[]; // = result[0];
336 443
    int lineIndex = 0;
@@ -423,12 +530,12 @@ void message(string &result[], string message, int message_id)
423 530
 
424 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 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 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 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 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 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 595
    for(int i=0; i < MaxOrders; i++)
487 596
      {
488 597
       if(od[i].msgid == -1)
489 598
         {
490 599
          od[i].msgid = message_id;
600
+         od[i].tickt = ticket;
491 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 603
          break;
494 604
         }
495 605
      }