Kaynağa Gözat

Ticket: 5083 Complete

Huzaifa-MQLDev 10 ay önce
ebeveyn
işleme
1afd78ac03
2 değiştirilmiş dosya ile 346 ekleme ve 13 silme
  1. BIN
      blTelegramToMT4.ex4
  2. 346 13
      blTelegramToMT4.mq4

BIN
blTelegramToMT4.ex4


+ 346 - 13
blTelegramToMT4.mq4

@@ -67,6 +67,7 @@ string symbolsListUserInput[];
 uchar  sym1[];
 uchar  sym2[];
 datetime ea_start_time = 0;
+int last_message_id = INT_MIN;
 int OnInit()
   {
 //--- create timer
@@ -111,15 +112,16 @@ int OnInit()
 
      }
 
-   string SplitArray[];
-
-   string jsonString = GET_function(url1+"/get-all-messages",header);
+   string jsonString = GET_function(url1 + "/get-latest-message-id", header);
    StringReplace(jsonString,"},", "*");
-   StringSplit(jsonString,'*',SplitArray);
+   last_message_id = (int)getJsonStringValue(jsonString, "id") != 0 ? (int)getJsonStringValue(jsonString, "id") : INT_MIN;
+
+   string message = getJsonStringValue(jsonString, "message");
 
-   for(int i = 0; i < ArraySize(SplitArray); i++)
+   if(last_message_id != INT_MIN)
      {
-      seperatingDataOnint(i,SplitArray[i]);
+      Print(" latest_message_id = ",last_message_id);
+      Print(" result found against get-latest-message-id = ",message);
      }
 
    EventSetTimer(1);
@@ -150,15 +152,43 @@ void OnTick()
 void OnTimer()
   {
 //---
-   string SplitArray[];
+   if(last_message_id == INT_MIN)
+     {
+
+      string jsonString = GET_function(url1 + "/get-latest-message-id", header);
+      StringReplace(jsonString,"},", "*");
+      last_message_id = (int)getJsonStringValue(jsonString, "id") != 0 ? (int)getJsonStringValue(jsonString, "id") : INT_MIN;
 
-   string jsonString = GET_function(url1+"/get-all-messages",header);
-   StringReplace(jsonString,"},", "*");
-   StringSplit(jsonString,'*',SplitArray);
 
-   for(int i = 0; i < ArraySize(SplitArray); i++)
+      if(last_message_id != INT_MIN)
+        {
+
+         string message = getJsonStringValue(jsonString, "message");
+
+         if(last_message_id != INT_MIN)
+           {
+            Print(" latest_message_id = ",last_message_id);
+            Print(" result found against get-latest-message-id = ",message);
+           }
+         execute_functionality_on_new_message(last_message_id);
+
+        }
+
+     }
+   else
      {
-      seperatingData(i,SplitArray[i]);
+      string jsonString = GET_function(url1 + "/get-latest-message-id", header);
+      StringReplace(jsonString,"},", "*");
+      int latest_message_id = (int)getJsonStringValue(jsonString, "id") != 0 ? (int)getJsonStringValue(jsonString, "id") : INT_MIN;
+
+      if(last_message_id != latest_message_id)
+        {
+         for(int i = latest_message_id; i > last_message_id; i--)
+           {
+            execute_functionality_on_new_message(i);
+           }
+         last_message_id = latest_message_id;
+        }
      }
   }
 //+------------------------------------------------------------------+
@@ -237,6 +267,41 @@ string getJsonStringValue(string json,string key,int addUp,string endSign)
 //+------------------------------------------------------------------+
 //|                                                                  |
 //+------------------------------------------------------------------+
+string getJsonStringValue(string json, string key)
+  {
+   int start = StringFind(json, "\""+key+"\"");
+   if(start == -1)
+      return "";
+
+// Find colon after key
+   int colon = StringFind(json, ":", start);
+   if(colon == -1)
+      return "";
+
+// Find next comma or closing brace
+   int endComma = StringFind(json, ",", colon);
+   int endBrace = StringFind(json, "}", colon);
+   int end = (endComma != -1 && (endComma < endBrace || endBrace == -1)) ? endComma : endBrace;
+   if(end == -1)
+      end = StringLen(json);
+
+   string value = trim(StringSubstr(json, colon+1, end-colon-1));
+// remove quotes if exist
+   StringReplace(value, "\"", "");
+   return value;
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+string trim(string text)
+  {
+   StringTrimLeft(text);
+   StringTrimRight(text);
+   return text;
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
 void seperatingData(int index, string data)
   {
 // Print(" Data: ", data);
@@ -285,7 +350,7 @@ void seperatingData(int index, string data)
               }
            }
 
-
+         message(result,message,message_id);
 
         }
      }
@@ -304,6 +369,235 @@ void seperatingData(int index, string data)
 //+------------------------------------------------------------------+
 //|                                                                  |
 //+------------------------------------------------------------------+
+void execute_functionality_on_new_message(int i)
+  {
+
+   string url = url1 + "/get-message/" + IntegerToString(i);
+   string jsonString = GET_function(url, header);
+   StringReplace(jsonString,"},", "*");
+
+   string message = getJsonStringValue(jsonString,"message",4,"\"");
+
+   int group_message_id = (int)getJsonStringValue(jsonString, "message_id");
+
+   StringToLower(message);
+   StringReplace(message,"~","#");
+
+   if(checkExistingTrade(group_message_id) == false)
+     {
+
+      string isReplyValue = getJsonStringValue(jsonString, "is_reply");
+
+      if(isReplyValue == "True")
+        {
+
+         int reply_to_msg_id = (int)getJsonStringValue(jsonString, "reply_to_msg_id");
+         message = getJsonStringValue(jsonString, "message");
+         StringToLower(message);
+
+         Print(" ================ Replied Message found of message_id ================ ",reply_to_msg_id);
+         Print(" ================ Message: ================ ",message);
+
+        }
+
+      else
+        {
+         if(checkExistingTrade(group_message_id) == false)
+           {
+            Print(" --------------- New Trade Message Found ----------------- ", " Message Id: ", group_message_id);
+            StringReplace(message,"~","#");
+            StringReplace(message,"##", "#");
+            StringToLower(message);
+            message = removeExtraSpaces(message);
+            Print("Message is ",message);
+
+            string result[];
+            string tempResult[];
+            int indexTemp = 0;
+
+            StringSplit(message,'#',tempResult);
+
+            for(int j=0; j<ArraySize(tempResult); j++)
+              {
+               //result[i] = StringTrimLeft(result[i]);
+               //result[i] = StringTrimRight(result[i]);
+               //Print("Temp Result : ", tempResult[i], " index is: ", i);
+               if(HasAlphanumeric(tempResult[j]))
+                 {
+                  //Print(" contains alphanumeric characters.");
+                  ArrayResize(result,ArraySize(result)+1);
+                  result[indexTemp] = tempResult[j];
+                  indexTemp++;
+                 }
+               else
+                 {
+                  //Print(" does not contain alphanumeric characters.");
+                  //ArrayResize(indexToDelete,ArraySize(indexToDelete)+1);
+                  //indexToDelete[indexTemp] = i;
+                  //indexTemp++;
+                 }
+              }
+
+            addtoMessageStructure(group_message_id,message);
+
+            message(result,message,group_message_id);
+           }
+        }
+     }
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void message(string &result[], string message, int message_id)
+  {
+   string lineOne[]; // = result[0];
+   int lineIndex = 0;
+   string direction = "";
+   int direction_index = -1;
+   string symbol = "";
+
+   if(ArraySize(result) >= 1)
+     {
+      StringSplit(result[0], ' ', lineOne);
+      if(((StringFind(result[0], "buy", 0) != -1) || (StringFind(result[0], "sell", 0) != -1)))
+        {
+
+         for(int i=0; i<ArraySize(lineOne); i++)
+           {
+            if(HasAlphanumeric(lineOne[i]))
+              {
+               ArrayResize(lineOne,ArraySize(lineOne)+1);
+               lineOne[lineIndex] = lineOne[i];
+               Print("Direction and Symbol: ", lineOne[lineIndex], " index is: ", lineIndex);
+
+               if(lineOne[lineIndex] == buy || lineOne[lineIndex] == sell)
+                 {
+                  direction = lineOne[lineIndex];
+                  direction_index = lineIndex;
+                  //Print(" Direction is: ", direction, " Direction Index: ", direction_index);
+                 }
+               lineIndex++;
+              }
+           }
+         if(ArraySize(lineOne) >= 2)
+           {
+            if(direction_index == 0)
+              {
+               symbol = lineOne[1];
+               StringToUpper(symbol);
+
+               //Print(" This is Message format One (1). Where Direction is: ", direction, " Symbol: ", symbol);
+              }
+            else
+               if(direction_index > 0)
+                 {
+                  symbol = lineOne[0];
+                  StringToUpper(symbol);
+
+                  //Print(" This is Message format One (1). Where Direction is: ", direction, " Symbol: ", symbol);
+                 }
+           }
+         symbol = symbolMapping(symbol);
+
+         double sl = 0;
+         double tpStore[]; // = result[0];
+         int tpIndex = 0;
+         for(int i=0 ; i < ArraySize(result); i++)
+           {
+            // result[i] = StringTrimLeft(result[i]);
+            // result[i] = StringTrimRight(result[i]);
+            // Print("Result : ", result[i], " index is: ", i);
+            if((StringFind(result[i], "sl", 0) != -1))
+              {
+               string tempSl[];
+               result[i] = trimString(result[i]);
+               //Print(" Sl String: ", result[i]);
+               StringSplit(result[i], ' ', tempSl);
+               if(ArraySize(tempSl) >= 2)
+                  sl = (double) tempSl[1];
+               Print("Sl : ", sl);
+              }
+            if((StringFind(result[i], "tp", 0) != -1))
+              {
+               // Print("Tp : ", result[i], " index is: ", i);
+               string tempTp[];
+               result[i] = trimString(result[i]);
+               StringSplit(result[i], ' ', tempTp);
+               //double tp = (double) tempTp[1];
+
+               ArrayResize(tpStore,ArraySize(tpStore)+1);
+               if(ArraySize(tempTp) >= 2)
+                  tpStore[tpIndex] = (double) tempTp[1];
+               Print("Tp : ", tpStore[tpIndex]);
+               tpIndex++;
+              }
+           }
+         Print("Side:", direction, " Symbol: ", symbol, " Tp Array Size: ", ArraySize(tpStore), " Message Id: ", message_id);
+         for(int i = 0 ; i < ArraySize(tpStore) ; i++)
+           {
+            Print("Side:", direction, " Symbol: ", symbol, " Tp: ", tpStore[i], " Sl: ", sl, " Message Id: ", message_id);
+            if(direction == buy)
+              {
+               placeBuyTrade(symbol, tpStore[i], sl, message_id, lotSize);
+              }
+
+            if(direction == sell)
+              {
+               placeSellTrade(symbol, tpStore[i], sl, message_id, lotSize);
+              }
+           }
+
+        }
+     }
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void placeBuyTrade(string symbol,double tp,double sl, int messageId, double lot_size)
+  {
+
+   double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
+   double bid = SymbolInfoDouble(symbol,SYMBOL_BID);
+   double buySl  = sl;
+   double buyTp  = tp;
+
+
+   int ticket = OrderSend(symbol, OP_BUY, lot_size, ask, 3, buySl, buyTp, "Buy Trade Placed.", magic_no, 0, clrBlue);
+   Print("Buy order Print: Stop Loss: ", buySl, " Take profit: ", buyTp);
+   if(ticket < 0)
+     {
+      Print("Buy Order Failed ", GetLastError());
+     }
+   else
+     {
+      Print(" Buy Order Is Placed Sucessfully ");
+     }
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void placeSellTrade(string symbol,double tp,double sl, int messageId, double lot_size)
+  {
+
+   double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
+   double bid = SymbolInfoDouble(symbol,SYMBOL_BID);
+   double sellSl = sl;
+   double sellTp = tp;
+
+   int ticket = OrderSend(symbol, OP_SELL, lot_size, bid, 3, sellSl, sellTp, "Sell Trade Placed.", magic_no, 0, clrRed);
+   if(ticket < 0)
+     {
+      Print("Sell Order Failed ", GetLastError());
+     }
+   else
+     {
+      Print(" Sell Order Is Placed Sucessfully ");
+     }
+
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
 void addtoMessageStructure(int message_id,string message)
   {
    for(int i=0; i < MaxOrders; i++)
@@ -311,10 +605,31 @@ void addtoMessageStructure(int message_id,string message)
       if(od[i].msgid == -1)
         {
          od[i].msgid = message_id;
+         StringToLower(message);
+         Print(" Message ID ",message_id,"  of Message = ",message," is added To Structure :: ");
          break;
         }
      }
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+string trimString(string inputt)
+  {
+// Remove spaces from the left and right sides
+   int start = 0;
+   int end = StringLen(inputt) - 1;
 
+// Find the first non-space character
+   while(start <= end && StringGetCharacter(inputt, start) == ' ')
+      start++;
+
+// Find the last non-space character
+   while(end >= start && StringGetCharacter(inputt, end) == ' ')
+      end--;
+
+// Extract the substring without leading or trailing spaces
+   return StringSubstr(inputt, start, end - start + 1);
   }
 //+------------------------------------------------------------------+
 //|                                                                  |
@@ -376,4 +691,22 @@ bool IsAlphanumeric(char c)
 //+------------------------------------------------------------------+
 //|                                                                  |
 //+------------------------------------------------------------------+
+string symbolMapping(string symbol)
+  {
+   for(int k = 0; k < ArraySize(symbolChart); k++)
+     {
+      StringToUpper(symbolChart[k]);
+      if(symbol == symbolChart[k])
+        {
+         symbol = symbolSnd[k];
+        }
+     }
+   symbol = prefix + symbol + suffix;
+   return symbol;
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+//+------------------------------------------------------------------+
+
 //+------------------------------------------------------------------+