Brak opisu

blTelegramToMT4.mq4 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. //+------------------------------------------------------------------+
  2. //| blTelegramToMT4.mq4 |
  3. //| Copyright 2025, MQL Development |
  4. //| https://www.mqldevelopment.com/ |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2025, MQL Development"
  7. #property link "https://www.mqldevelopment.com/"
  8. #property version "1.00"
  9. #property strict
  10. #define buy "buy"
  11. #define sell "sell"
  12. #define MaxOrders 10000
  13. struct msgDetails
  14. {
  15. int msgid;
  16. msgDetails()
  17. {
  18. msgid = -1;
  19. }
  20. };
  21. msgDetails od[MaxOrders];
  22. struct new_trade_store
  23. {
  24. ulong ticket; // Ticket
  25. int order_type; // OrderType
  26. double lot; // Lot
  27. double stop_loss; // StopLoss
  28. double take_profit; // TakeProfit
  29. double price; // Price
  30. string symbol; // Symbol
  31. int chatId; // ChatId
  32. string trade_type; // Trade Type (Trade Type: Tp1, Tp2, Tp3.... Tpn)
  33. new_trade_store()
  34. {
  35. ticket = -1;
  36. chatId=-1;
  37. trade_type = "";
  38. }
  39. };
  40. new_trade_store newTradeStore[MaxOrders];
  41. input string Settings = " ------------- General Settings ------------- "; //_
  42. input int magic_no = 333; // Magic no
  43. input string symbolMatch = "GOLD:XAUUSD,BitCoin:BTCUSD"; // Symbol Mapping (Telegram:MT5)
  44. input string suffix = ""; // Account Suffix
  45. input string prefix = ""; // Account Prefix
  46. input double lotSize = 0.1; // Lot Size
  47. //+------------------------------------------------------------------+
  48. //| Expert initialization function |
  49. //+------------------------------------------------------------------+
  50. // Global Variables
  51. string url1 = "http://127.0.0.1";
  52. string header = "Content-Type: application/json\r\nAccept: application/json\r\n";
  53. int getme_result;
  54. string symbolChart[];
  55. string symbolSnd[];
  56. string symbolsListUserInput[];
  57. uchar sym1[];
  58. uchar sym2[];
  59. datetime ea_start_time = 0;
  60. int OnInit()
  61. {
  62. //--- create timer
  63. ea_start_time = TimeCurrent();
  64. ushort u_sep = StringGetCharacter(",",0);
  65. StringSplit(symbolMatch,u_sep,symbolsListUserInput);
  66. ArrayResize(symbolChart,0);
  67. ArrayResize(symbolSnd,0);
  68. for(int i = 0; i < ArraySize(symbolsListUserInput); i++)
  69. {
  70. string str = symbolsListUserInput[i];
  71. int index = StringFind(str,":");
  72. int index2 = StringLen(str);
  73. ArrayResize(sym1,0);
  74. ArrayResize(sym2,0);
  75. for(int j = 0; j < index; j++)
  76. {
  77. ArrayResize(sym1,ArraySize(sym1)+1);
  78. sym1[j] = uchar(str[j]);
  79. }
  80. int k = 0;
  81. for(int j = index + 1 ; j < index2; j++)
  82. {
  83. ArrayResize(sym2,ArraySize(sym2)+1);
  84. sym2[k] = uchar(str[j]);
  85. k++;
  86. }
  87. ArrayResize(symbolChart,ArraySize(symbolChart)+1);
  88. ArrayResize(symbolSnd,ArraySize(symbolSnd)+1);
  89. symbolChart[i] = CharArrayToString(sym1);
  90. symbolSnd[i] = CharArrayToString(sym2);
  91. }
  92. string SplitArray[];
  93. string jsonString = GET_function(url1+"/get-all-messages",header);
  94. StringReplace(jsonString,"},", "*");
  95. StringSplit(jsonString,'*',SplitArray);
  96. for(int i = 0; i < ArraySize(SplitArray); i++)
  97. {
  98. seperatingDataOnint(i,SplitArray[i]);
  99. }
  100. EventSetTimer(1);
  101. //---
  102. return(INIT_SUCCEEDED);
  103. }
  104. //+------------------------------------------------------------------+
  105. //| Expert deinitialization function |
  106. //+------------------------------------------------------------------+
  107. void OnDeinit(const int reason)
  108. {
  109. //--- destroy timer
  110. EventKillTimer();
  111. }
  112. //+------------------------------------------------------------------+
  113. //| Expert tick function |
  114. //+------------------------------------------------------------------+
  115. void OnTick()
  116. {
  117. //---
  118. }
  119. //+------------------------------------------------------------------+
  120. //| Timer function |
  121. //+------------------------------------------------------------------+
  122. void OnTimer()
  123. {
  124. //---
  125. string SplitArray[];
  126. string jsonString = GET_function(url1+"/get-all-messages",header);
  127. StringReplace(jsonString,"},", "*");
  128. StringSplit(jsonString,'*',SplitArray);
  129. for(int i = 0; i < ArraySize(SplitArray); i++)
  130. {
  131. seperatingData(i,SplitArray[i]);
  132. }
  133. }
  134. //+------------------------------------------------------------------+
  135. //| |
  136. //+------------------------------------------------------------------+
  137. string GET_function(string url,string headers)
  138. {
  139. string result_string = NULL;
  140. int timeout = 10; // Set the timeout value in seconds
  141. char result[],data[];
  142. string resultHeaders;
  143. int res = WebRequest("GET",url,headers,timeout,data,result,resultHeaders);
  144. if(res == 200)
  145. {
  146. result_string = CharArrayToString(result);
  147. }
  148. else
  149. {
  150. Print("content GETT: ", result_string," Error: ",GetLastError(), " res: ",res);
  151. }
  152. return result_string;
  153. }
  154. //+------------------------------------------------------------------+
  155. //| |
  156. //+------------------------------------------------------------------+
  157. void seperatingDataOnint(int index, string data)
  158. {
  159. string message = getJsonStringValue(data,"message",4,"\"");
  160. int message_id = (int)getJsonStringValue(data,"message_id",4,"\"");
  161. if(checkExistingTrade(message_id) == false)
  162. {
  163. Print(" ================ OnInit New Trade Message Found ================");
  164. StringReplace(message,"~","#");
  165. StringToLower(message);
  166. Print("Message is ", message);
  167. for(int i=0; i < MaxOrders; i++)
  168. {
  169. if(od[i].msgid == -1)
  170. {
  171. od[i].msgid = message_id;
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. //+------------------------------------------------------------------+
  178. //| |
  179. //+------------------------------------------------------------------+
  180. bool checkExistingTrade(int message_id)
  181. {
  182. for(int i=0; i < MaxOrders; i++)
  183. {
  184. if(od[i].msgid == message_id)
  185. {
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. //+------------------------------------------------------------------+
  192. //| |
  193. //+------------------------------------------------------------------+
  194. string getJsonStringValue(string json,string key,int addUp,string endSign)
  195. {
  196. int indexStart = StringFind(json,key)+StringLen(key)+addUp;
  197. int indexEnd = StringFind(json,endSign,indexStart);
  198. return StringSubstr(json,indexStart,indexEnd-indexStart);
  199. }
  200. //+------------------------------------------------------------------+
  201. //| |
  202. //+------------------------------------------------------------------+
  203. void seperatingData(int index, string data)
  204. {
  205. // Print(" Data: ", data);
  206. string message = getJsonStringValue(data,"message",4,"\"");
  207. int message_id = (int)getJsonStringValue(data,"message_id",4,"\"");
  208. string isReplyValue = getJsonStringValue(data,"is_reply",4,"\"");
  209. int reply_to_msg_id = (int)getJsonStringValue(data,"reply_to_msg_id",4,"\"");
  210. if(isReplyValue == "False")
  211. {
  212. if(checkExistingTrade(message_id) == false)
  213. {
  214. Print(" --------------- New Trade Message Found ----------------- ", " Message Id: ", message_id);
  215. StringReplace(message,"~","#");
  216. StringReplace(message,"##", "#");
  217. StringToLower(message);
  218. message = removeExtraSpaces(message);
  219. Print("Message is ",message);
  220. addtoMessageStructure(message_id,message);
  221. string result[];
  222. string tempResult[];
  223. int indexTemp = 0;
  224. StringSplit(message,'#',tempResult);
  225. for(int i=0; i<ArraySize(tempResult); i++)
  226. {
  227. //result[i] = StringTrimLeft(result[i]);
  228. //result[i] = StringTrimRight(result[i]);
  229. //Print("Temp Result : ", tempResult[i], " index is: ", i);
  230. if(HasAlphanumeric(tempResult[i]))
  231. {
  232. //Print(" contains alphanumeric characters.");
  233. ArrayResize(result,ArraySize(result)+1);
  234. result[indexTemp] = tempResult[i];
  235. indexTemp++;
  236. }
  237. else
  238. {
  239. //Print(" does not contain alphanumeric characters.");
  240. //ArrayResize(indexToDelete,ArraySize(indexToDelete)+1);
  241. //indexToDelete[indexTemp] = i;
  242. //indexTemp++;
  243. }
  244. }
  245. }
  246. }
  247. if(isReplyValue == "True")
  248. {
  249. if(checkExistingTrade(message_id) == false)
  250. {
  251. Print(" --------------- New Replied Message Found ----------------- ", " Message Id: ", message_id, " Reply Message Id: ", reply_to_msg_id);
  252. StringToLower(message);
  253. Print("Message is ",message);
  254. addtoMessageStructure(message_id,message);
  255. }
  256. }
  257. }
  258. //+------------------------------------------------------------------+
  259. //| |
  260. //+------------------------------------------------------------------+
  261. void addtoMessageStructure(int message_id,string message)
  262. {
  263. for(int i=0; i < MaxOrders; i++)
  264. {
  265. if(od[i].msgid == -1)
  266. {
  267. od[i].msgid = message_id;
  268. break;
  269. }
  270. }
  271. }
  272. //+------------------------------------------------------------------+
  273. //| |
  274. //+------------------------------------------------------------------+
  275. string removeExtraSpaces(string str)
  276. {
  277. string result = "";
  278. int len = StringLen(str);
  279. bool lastWasSpace = false;
  280. for(int i = 0; i < len; i++)
  281. {
  282. string currentChar = StringSubstr(str, i, 1);
  283. if(currentChar == " ")
  284. {
  285. // Skip adding this space if the last character was also a space
  286. if(lastWasSpace)
  287. continue;
  288. lastWasSpace = true;
  289. }
  290. else
  291. {
  292. lastWasSpace = false;
  293. }
  294. result += currentChar;
  295. }
  296. return result;
  297. }
  298. //+------------------------------------------------------------------+
  299. //| |
  300. //+------------------------------------------------------------------+
  301. bool HasAlphanumeric(string str)
  302. {
  303. //Print("String Length: ", StringLen(str));
  304. for(int i = 0; i <= StringLen(str); i++)
  305. {
  306. //Print("Here ", StringLen(str));
  307. if(IsAlphanumeric((char)str[i]))
  308. {
  309. return true;
  310. break;
  311. }
  312. }
  313. return false;
  314. }
  315. //+------------------------------------------------------------------+
  316. //| |
  317. //+------------------------------------------------------------------+
  318. bool IsAlphanumeric(char c)
  319. {
  320. if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
  321. return true;
  322. return false;
  323. }
  324. //+------------------------------------------------------------------+
  325. //| |
  326. //+------------------------------------------------------------------+
  327. //+------------------------------------------------------------------+