AhtashamShahzad3 9 месяцев назад
Родитель
С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 @@
 struct msgDetails
 struct msgDetails
   {
   {
    int               msgid;
    int               msgid;
+   ulong             tickt;
                      msgDetails()
                      msgDetails()
      {
      {
       msgid       = -1;
       msgid       = -1;
+      tickt       = -1;
      }
      }
   };
   };
 
 
@@ -29,6 +31,7 @@ input       string               symbolMatch           = "GOLD:XAUUSD,BitCoin:BT
 input       string               suffix                = "";                                                 // Account Suffix
 input       string               suffix                = "";                                                 // Account Suffix
 input       string               prefix                = "";                                                 // Account Prefix
 input       string               prefix                = "";                                                 // Account Prefix
 input       double               lotSize               = 0.1;                                                // Lot Size
 input       double               lotSize               = 0.1;                                                // Lot Size
+input       double               partialClose          = 20;                                                 // Partial Closing %
 
 
 //+------------------------------------------------------------------+
 //+------------------------------------------------------------------+
 //| Expert initialization function                                   |
 //| Expert initialization function                                   |
@@ -269,6 +272,7 @@ void execute_functionality_on_new_message(int i)
      {
      {
 
 
       string isReplyValue = getJsonStringValue(jsonString, "is_reply");
       string isReplyValue = getJsonStringValue(jsonString, "is_reply");
+      StringReplace(isReplyValue," ","");
 
 
       if(isReplyValue == "True")
       if(isReplyValue == "True")
         {
         {
@@ -280,6 +284,74 @@ void execute_functionality_on_new_message(int i)
          Print(" ================ Replied Message found of message_id ================ ",reply_to_msg_id);
          Print(" ================ Replied Message found of message_id ================ ",reply_to_msg_id);
          Print(" ================ Message: ================ ",message);
          Print(" ================ Message: ================ ",message);
 
 
+         int ticket = getTicket(reply_to_msg_id);
+
+         if(ticket != -1)
+           {
+
+            if(StringFind(message,"take") != -1 && StringFind(message,"some") != -1 && StringFind(message,"partial") != -1 && StringFind(message,"profit") != -1)
+              {
+
+               if(OrderSelect(ticket,SELECT_BY_TICKET))
+                 {
+
+                  double lot = NormalizeDouble(OrderLots()*(partialClose/100),2);
+
+                  bool result = OrderClose(ticket,lot,OrderOpenPrice(),10, clrNONE);
+
+                  if(result)
+                    {
+                     Print("Partially closed ", lot, " lots from order ", ticket);
+
+
+
+                     for(int j = OrdersTotal()-1; j>=0; j--)
+                       {
+                        if(OrderSelect(j, SELECT_BY_POS))
+                          {
+                           if(OrderComment() != "")
+                             {
+                              string ticketToBeReplace = OrderComment();
+                              StringReplace(ticketToBeReplace,"from #","");
+
+                              if(int(ticketToBeReplace) == ticket)
+                                {
+                                 findAndreplaceTicketFromStructure(OrderTicket(),reply_to_msg_id);
+                                }
+                             }
+                          }
+                       }
+
+
+                    }
+                  else
+                    {
+                     Print("Error closing partial order: ", GetLastError()," lot = ",lot," OrderLots() = ",OrderLots());
+                    }
+                 }
+              }
+
+            if(StringFind(message,"set") != -1 && StringFind(message,"stop") != -1 && StringFind(message,"loss") != -1 && StringFind(message,"entry") != -1)
+              {
+
+               if(OrderSelect(ticket,SELECT_BY_TICKET))
+                 {
+
+                  bool result = OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrNONE);
+
+                  if(result)
+                    {
+                     Print("Order Sl Modify to Open Price ", ticket);
+                    }
+                  else
+                    {
+                     Print("Error in Modify order SL : ", GetLastError());
+                    }
+                 }
+              }
+           }
+
+
         }
         }
 
 
       else
       else
@@ -320,17 +392,52 @@ void execute_functionality_on_new_message(int i)
                  }
                  }
               }
               }
 
 
-            addtoMessageStructure(group_message_id,message);
+            int ticket = -1;
+
+            message(result,message,group_message_id,ticket);
+
+            addtoMessageStructure(group_message_id,message,ticket);
 
 
-            message(result,message,group_message_id);
            }
            }
         }
         }
      }
      }
   }
   }
+
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void findAndreplaceTicketFromStructure(int ticketToBeReplace,int reply_to_msg_id)
+  {
+   for(int i=0; i < MaxOrders; i++)
+     {
+      if(od[i].msgid == reply_to_msg_id)
+        {
+
+         od[i].tickt = ticketToBeReplace;
+
+         Print("codeOrder is partially closed so update Ticket ",ticketToBeReplace);
+         break;
+        }
+     }
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+int getTicket(int reply_to_msg_id)
+  {
+   for(int i=0; i < MaxOrders; i++)
+     {
+      if(od[i].msgid == reply_to_msg_id)
+        {
+         return int(od[i].tickt);
+        }
+     }
+   return -1;
+  }
 //+------------------------------------------------------------------+
 //+------------------------------------------------------------------+
 //|                                                                  |
 //|                                                                  |
 //+------------------------------------------------------------------+
 //+------------------------------------------------------------------+
-void message(string &result[], string message, int message_id)
+void message(string &result[], string message, int message_id,int & tickt)
   {
   {
    string lineOne[]; // = result[0];
    string lineOne[]; // = result[0];
    int lineIndex = 0;
    int lineIndex = 0;
@@ -423,12 +530,12 @@ void message(string &result[], string message, int message_id)
 
 
          if(direction == buy)
          if(direction == buy)
            {
            {
-            placeBuyTrade(symbol, tp, sl, message_id, lotSize);
+            tickt = placeBuyTrade(symbol, tp, sl, message_id, lotSize);
            }
            }
 
 
          if(direction == sell)
          if(direction == sell)
            {
            {
-            placeSellTrade(symbol, tp, sl, message_id, lotSize);
+            tickt = placeSellTrade(symbol, tp, sl, message_id, lotSize);
            }
            }
         }
         }
      }
      }
@@ -436,7 +543,7 @@ void message(string &result[], string message, int message_id)
 //+------------------------------------------------------------------+
 //+------------------------------------------------------------------+
 //|                                                                  |
 //|                                                                  |
 //+------------------------------------------------------------------+
 //+------------------------------------------------------------------+
-void placeBuyTrade(string symbol,double tp,double sl, int messageId, double lot_size)
+int placeBuyTrade(string symbol,double tp,double sl, int messageId, double lot_size)
   {
   {
 
 
    double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
    double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
@@ -455,11 +562,13 @@ void placeBuyTrade(string symbol,double tp,double sl, int messageId, double lot_
      {
      {
       Print(" Buy Order Is Placed Sucessfully ");
       Print(" Buy Order Is Placed Sucessfully ");
      }
      }
+
+   return ticket;
   }
   }
 //+------------------------------------------------------------------+
 //+------------------------------------------------------------------+
 //|                                                                  |
 //|                                                                  |
 //+------------------------------------------------------------------+
 //+------------------------------------------------------------------+
-void placeSellTrade(string symbol,double tp,double sl, int messageId, double lot_size)
+int placeSellTrade(string symbol,double tp,double sl, int messageId, double lot_size)
   {
   {
 
 
    double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
    double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
@@ -476,20 +585,21 @@ void placeSellTrade(string symbol,double tp,double sl, int messageId, double lot
      {
      {
       Print(" Sell Order Is Placed Sucessfully ");
       Print(" Sell Order Is Placed Sucessfully ");
      }
      }
-
+   return ticket;
   }
   }
 //+------------------------------------------------------------------+
 //+------------------------------------------------------------------+
 //|                                                                  |
 //|                                                                  |
 //+------------------------------------------------------------------+
 //+------------------------------------------------------------------+
-void addtoMessageStructure(int message_id,string message)
+void addtoMessageStructure(int message_id,string message,ulong ticket)
   {
   {
    for(int i=0; i < MaxOrders; i++)
    for(int i=0; i < MaxOrders; i++)
      {
      {
       if(od[i].msgid == -1)
       if(od[i].msgid == -1)
         {
         {
          od[i].msgid = message_id;
          od[i].msgid = message_id;
+         od[i].tickt = ticket;
          StringToLower(message);
          StringToLower(message);
-         Print(" Message ID ",message_id,"  of Message = ",message," is added To Structure :: ");
+         Print(" Message ID ",message_id,"  of Message = ",message," Having Ticket ",ticket," is added To Structure :: ");
          break;
          break;
         }
         }
      }
      }