Nenhuma descrição

blTelegramToMT4.mq4 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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 "2.0"
  9. #property strict
  10. #define buy "buy"
  11. #define sell "sell"
  12. #define MaxOrders 10000
  13. struct msgDetails
  14. {
  15. int msgid;
  16. ulong tickt;
  17. msgDetails()
  18. {
  19. msgid = -1;
  20. tickt = -1;
  21. }
  22. };
  23. msgDetails od[MaxOrders];
  24. input string Settings = " ------------- General Settings ------------- "; //_
  25. input int magic_no = 333; // Magic no
  26. input string symbolMatch = "GOLD:XAUUSD,BitCoin:BTCUSD"; // Symbol Mapping (Telegram:MT4)
  27. input string suffix = ""; // Account Suffix
  28. input string prefix = ""; // Account Prefix
  29. input double lotSize = 0.1; // Lot Size
  30. input double partialClose = 20; // Partial Closing %
  31. //+------------------------------------------------------------------+
  32. //| Expert initialization function |
  33. //+------------------------------------------------------------------+
  34. // Global Variables
  35. string url1 = "http://127.0.0.1"; // "http://myapp.local";
  36. string header = "Content-Type: application/json\r\nAccept: application/json\r\n";
  37. string symbolChart[];
  38. string symbolSnd[];
  39. string symbolsListUserInput[];
  40. uchar sym1[];
  41. uchar sym2[];
  42. int last_message_id = INT_MIN;
  43. int OnInit()
  44. {
  45. //--- create timer
  46. ushort u_sep = StringGetCharacter(",",0);
  47. StringSplit(symbolMatch,u_sep,symbolsListUserInput);
  48. ArrayResize(symbolChart,0);
  49. ArrayResize(symbolSnd,0);
  50. for(int i = 0; i < ArraySize(symbolsListUserInput); i++)
  51. {
  52. string str = symbolsListUserInput[i];
  53. int index = StringFind(str,":");
  54. int index2 = StringLen(str);
  55. ArrayResize(sym1,0);
  56. ArrayResize(sym2,0);
  57. for(int j = 0; j < index; j++)
  58. {
  59. ArrayResize(sym1,ArraySize(sym1)+1);
  60. sym1[j] = uchar(str[j]);
  61. }
  62. int k = 0;
  63. for(int j = index + 1 ; j < index2; j++)
  64. {
  65. ArrayResize(sym2,ArraySize(sym2)+1);
  66. sym2[k] = uchar(str[j]);
  67. k++;
  68. }
  69. ArrayResize(symbolChart,ArraySize(symbolChart)+1);
  70. ArrayResize(symbolSnd,ArraySize(symbolSnd)+1);
  71. symbolChart[i] = CharArrayToString(sym1);
  72. symbolSnd[i] = CharArrayToString(sym2);
  73. }
  74. string jsonString = GET_function(url1 + "/get-latest-message-id", header);
  75. StringReplace(jsonString,"},", "*");
  76. last_message_id = (int)getJsonStringValue(jsonString, "id") != 0 ? (int)getJsonStringValue(jsonString, "id") : INT_MIN;
  77. string message = getJsonStringValue(jsonString, "message");
  78. if(last_message_id != INT_MIN)
  79. {
  80. Print(" latest_message_id = ",last_message_id);
  81. Print(" result found against get-latest-message-id = ",message);
  82. }
  83. EventSetTimer(1);
  84. //---
  85. return(INIT_SUCCEEDED);
  86. }
  87. //+------------------------------------------------------------------+
  88. //| Expert deinitialization function |
  89. //+------------------------------------------------------------------+
  90. void OnDeinit(const int reason)
  91. {
  92. //--- destroy timer
  93. EventKillTimer();
  94. }
  95. //+------------------------------------------------------------------+
  96. //| Expert tick function |
  97. //+------------------------------------------------------------------+
  98. void OnTick()
  99. {
  100. //---
  101. }
  102. //+------------------------------------------------------------------+
  103. //| Timer function |
  104. //+------------------------------------------------------------------+
  105. void OnTimer()
  106. {
  107. //---
  108. if(last_message_id == INT_MIN)
  109. {
  110. string jsonString = GET_function(url1 + "/get-latest-message-id", header);
  111. StringReplace(jsonString,"},", "*");
  112. last_message_id = (int)getJsonStringValue(jsonString, "id") != 0 ? (int)getJsonStringValue(jsonString, "id") : INT_MIN;
  113. if(last_message_id != INT_MIN)
  114. {
  115. string message = getJsonStringValue(jsonString, "message");
  116. if(last_message_id != INT_MIN)
  117. {
  118. Print(" latest_message_id = ",last_message_id);
  119. Print(" result found against get-latest-message-id = ",message);
  120. }
  121. execute_functionality_on_new_message(last_message_id);
  122. }
  123. }
  124. else
  125. {
  126. string jsonString = GET_function(url1 + "/get-latest-message-id", header);
  127. StringReplace(jsonString,"},", "*");
  128. int latest_message_id = (int)getJsonStringValue(jsonString, "id") != 0 ? (int)getJsonStringValue(jsonString, "id") : INT_MIN;
  129. if(last_message_id != latest_message_id)
  130. {
  131. for(int i = latest_message_id; i > last_message_id; i--)
  132. {
  133. execute_functionality_on_new_message(i);
  134. }
  135. last_message_id = latest_message_id;
  136. }
  137. }
  138. }
  139. //+------------------------------------------------------------------+
  140. //| |
  141. //+------------------------------------------------------------------+
  142. string GET_function(string url,string headers)
  143. {
  144. string result_string = NULL;
  145. int timeout = 10; // Set the timeout value in seconds
  146. char result[],data[];
  147. string resultHeaders;
  148. int res = WebRequest("GET",url,headers,timeout,data,result,resultHeaders);
  149. if(res == 200)
  150. {
  151. result_string = CharArrayToString(result);
  152. }
  153. else
  154. {
  155. Print("content GETT: ", result_string," Error: ",GetLastError(), " res: ",res);
  156. }
  157. return result_string;
  158. }
  159. //+------------------------------------------------------------------+
  160. //| |
  161. //+------------------------------------------------------------------+
  162. bool checkExistingTrade(int message_id)
  163. {
  164. for(int i=0; i < MaxOrders; i++)
  165. {
  166. if(od[i].msgid == message_id)
  167. {
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. //+------------------------------------------------------------------+
  174. //| |
  175. //+------------------------------------------------------------------+
  176. string getJsonStringValue(string json,string key,int addUp,string endSign)
  177. {
  178. int indexStart = StringFind(json,key)+StringLen(key)+addUp;
  179. int indexEnd = StringFind(json,endSign,indexStart);
  180. return StringSubstr(json,indexStart,indexEnd-indexStart);
  181. }
  182. //+------------------------------------------------------------------+
  183. //| |
  184. //+------------------------------------------------------------------+
  185. string getJsonStringValue(string json, string key)
  186. {
  187. int start = StringFind(json, "\""+key+"\"");
  188. if(start == -1)
  189. return "";
  190. // Find colon after key
  191. int colon = StringFind(json, ":", start);
  192. if(colon == -1)
  193. return "";
  194. // Find next comma or closing brace
  195. int endComma = StringFind(json, ",", colon);
  196. int endBrace = StringFind(json, "}", colon);
  197. int end = (endComma != -1 && (endComma < endBrace || endBrace == -1)) ? endComma : endBrace;
  198. if(end == -1)
  199. end = StringLen(json);
  200. string value = trim(StringSubstr(json, colon+1, end-colon-1));
  201. // remove quotes if exist
  202. StringReplace(value, "\"", "");
  203. return value;
  204. }
  205. //+------------------------------------------------------------------+
  206. //| |
  207. //+------------------------------------------------------------------+
  208. string trim(string text)
  209. {
  210. StringTrimLeft(text);
  211. StringTrimRight(text);
  212. return text;
  213. }
  214. //+------------------------------------------------------------------+
  215. //| |
  216. //+------------------------------------------------------------------+
  217. void execute_functionality_on_new_message(int i)
  218. {
  219. string url = url1 + "/get-message/" + IntegerToString(i);
  220. string jsonString = GET_function(url, header);
  221. StringReplace(jsonString,"},", "*");
  222. string message = getJsonStringValue(jsonString,"message",4,"\"");
  223. int group_message_id = (int)getJsonStringValue(jsonString, "message_id");
  224. StringToLower(message);
  225. StringReplace(message,"~","#");
  226. if(checkExistingTrade(group_message_id) == false)
  227. {
  228. string isReplyValue = getJsonStringValue(jsonString, "is_reply");
  229. StringReplace(isReplyValue," ","");
  230. if(isReplyValue == "True")
  231. {
  232. int reply_to_msg_id = (int)getJsonStringValue(jsonString, "reply_to_msg_id");
  233. message = getJsonStringValue(jsonString, "message");
  234. StringToLower(message);
  235. Print(" ================ Replied Message found of message_id ================ ",reply_to_msg_id);
  236. Print(" ================ Message: ================ ",message);
  237. int ticket = getTicket(reply_to_msg_id);
  238. if(ticket != -1)
  239. {
  240. if(StringFind(message,"set") != -1 && StringFind(message,"stop") != -1 && StringFind(message,"loss") != -1 && StringFind(message,"entry") != -1)
  241. {
  242. if(OrderSelect(ticket,SELECT_BY_TICKET))
  243. {
  244. bool result = OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrNONE);
  245. if(result)
  246. {
  247. Print("Order Sl Modify to Open Price ", ticket);
  248. }
  249. else
  250. {
  251. Print("Error in Modify order SL : ", GetLastError()," Setting SL at: ",OrderOpenPrice()," price: ",OrderClosePrice());
  252. }
  253. }
  254. }
  255. if(StringFind(message,"take") != -1 && StringFind(message,"some") != -1 && StringFind(message,"partial") != -1 && StringFind(message,"profit") != -1)
  256. {
  257. if(OrderSelect(ticket,SELECT_BY_TICKET))
  258. {
  259. double lot = NormalizeDouble(OrderLots()*(partialClose/100),2);
  260. bool result = OrderClose(ticket,lot,OrderClosePrice(),10, clrNONE);
  261. if(result)
  262. {
  263. Print("Partially closed ", lot, " lots from order ", ticket);
  264. for(int j = OrdersTotal()-1; j>=0; j--)
  265. {
  266. if(OrderSelect(j, SELECT_BY_POS))
  267. {
  268. if(OrderComment() != "")
  269. {
  270. string ticketToBeReplace = OrderComment();
  271. StringReplace(ticketToBeReplace,"from #","");
  272. if(int(ticketToBeReplace) == ticket)
  273. {
  274. findAndreplaceTicketFromStructure(OrderTicket(),reply_to_msg_id);
  275. }
  276. }
  277. }
  278. }
  279. }
  280. else
  281. {
  282. Print("Error closing partial order: ", GetLastError()," lot = ",lot," OrderLots() = ",OrderLots());
  283. }
  284. }
  285. }
  286. }
  287. }
  288. else
  289. {
  290. if(checkExistingTrade(group_message_id) == false)
  291. {
  292. Print(" --------------- New Trade Message Found ----------------- ", " Message Id: ", group_message_id);
  293. StringReplace(message,"~","#");
  294. StringReplace(message,"##", "#");
  295. StringToLower(message);
  296. message = removeExtraSpaces(message);
  297. Print("Message is ",message);
  298. string result[];
  299. string tempResult[];
  300. int indexTemp = 0;
  301. StringSplit(message,'#',tempResult);
  302. for(int j=0; j<ArraySize(tempResult); j++)
  303. {
  304. //result[i] = StringTrimLeft(result[i]);
  305. //result[i] = StringTrimRight(result[i]);
  306. //Print("Temp Result : ", tempResult[i], " index is: ", i);
  307. if(HasAlphanumeric(tempResult[j]))
  308. {
  309. //Print(" contains alphanumeric characters.");
  310. ArrayResize(result,ArraySize(result)+1);
  311. result[indexTemp] = tempResult[j];
  312. indexTemp++;
  313. }
  314. else
  315. {
  316. //Print(" does not contain alphanumeric characters.");
  317. //ArrayResize(indexToDelete,ArraySize(indexToDelete)+1);
  318. //indexToDelete[indexTemp] = i;
  319. //indexTemp++;
  320. }
  321. }
  322. int ticket = -1;
  323. message(result,message,group_message_id,ticket);
  324. pendingMessage(result,message,group_message_id,ticket);
  325. addtoMessageStructure(group_message_id,message,ticket);
  326. }
  327. }
  328. }
  329. }
  330. //+------------------------------------------------------------------+
  331. //| |
  332. //+------------------------------------------------------------------+
  333. void findAndreplaceTicketFromStructure(int ticketToBeReplace,int reply_to_msg_id)
  334. {
  335. for(int i=0; i < MaxOrders; i++)
  336. {
  337. if(od[i].msgid == reply_to_msg_id)
  338. {
  339. od[i].tickt = ticketToBeReplace;
  340. Print("codeOrder is partially closed so update Ticket ",ticketToBeReplace);
  341. break;
  342. }
  343. }
  344. }
  345. //+------------------------------------------------------------------+
  346. //| |
  347. //+------------------------------------------------------------------+
  348. int getTicket(int reply_to_msg_id)
  349. {
  350. for(int i=0; i < MaxOrders; i++)
  351. {
  352. if(od[i].msgid == reply_to_msg_id)
  353. {
  354. return int(od[i].tickt);
  355. }
  356. }
  357. return -1;
  358. }
  359. //+------------------------------------------------------------------+
  360. //| |
  361. //+------------------------------------------------------------------+
  362. void message(string &result[], string message, int message_id,int & tickt)
  363. {
  364. string lineOne[]; // = result[0];
  365. int lineIndex = 0;
  366. string direction = "";
  367. int direction_index = -1;
  368. string symbol = "";
  369. if(ArraySize(result) >= 1)
  370. {
  371. StringSplit(result[0], ' ', lineOne);
  372. if(StringFind(result[0], "now", 0) != -1)
  373. if(((StringFind(result[0], "buy", 0) != -1) || (StringFind(result[0], "sell", 0) != -1)))
  374. {
  375. Print(" ------------------------------- Active Message ---------------------------------- ");
  376. for(int i=0; i<ArraySize(lineOne); i++)
  377. {
  378. if(HasAlphanumeric(lineOne[i]))
  379. {
  380. ArrayResize(lineOne,ArraySize(lineOne)+1);
  381. lineOne[lineIndex] = lineOne[i];
  382. Print("Direction and Symbol: ", lineOne[lineIndex], " index is: ", lineIndex);
  383. if(lineOne[lineIndex] == buy || lineOne[lineIndex] == sell)
  384. {
  385. direction = lineOne[lineIndex];
  386. direction_index = lineIndex;
  387. //Print(" Direction is: ", direction, " Direction Index: ", direction_index);
  388. }
  389. lineIndex++;
  390. }
  391. }
  392. if(ArraySize(lineOne) >= 2)
  393. {
  394. if(direction_index == 0)
  395. {
  396. symbol = lineOne[1];
  397. StringToUpper(symbol);
  398. //Print(" This is Message format One (1). Where Direction is: ", direction, " Symbol: ", symbol);
  399. }
  400. else
  401. if(direction_index > 0)
  402. {
  403. symbol = lineOne[0];
  404. StringToUpper(symbol);
  405. //Print(" This is Message format One (1). Where Direction is: ", direction, " Symbol: ", symbol);
  406. }
  407. }
  408. symbol = symbolMapping(symbol);
  409. double sl = 0;
  410. double tp = 0; // = result[0];
  411. for(int i=0 ; i < ArraySize(result); i++)
  412. {
  413. // result[i] = StringTrimLeft(result[i]);
  414. // result[i] = StringTrimRight(result[i]);
  415. // Print("Result : ", result[i], " index is: ", i);
  416. if((StringFind(result[i], "sl", 0) != -1))
  417. {
  418. string tempSl[];
  419. StringReplace(result[i],":", " ");
  420. result[i] = trimString(result[i]);
  421. result[i] = spaceRemove(result[i]);
  422. //Print(" Sl String: ", result[i]);
  423. StringSplit(result[i], ' ', tempSl);
  424. if(ArraySize(tempSl) >= 2)
  425. sl = (double) tempSl[ArraySize(tempSl) - 1];
  426. Print("Sl : ", sl);
  427. }
  428. if((StringFind(result[i], "tp", 0) != -1))
  429. {
  430. Print("Tp : ", result[i], " index is: ", i);
  431. string tempTp[];
  432. StringReplace(result[i],":", " ");
  433. result[i] = trimString(result[i]);
  434. result[i] = spaceRemove(result[i]);
  435. Print("Tp After String Replace : ", result[i], " index is: ", i);
  436. StringSplit(result[i], ' ', tempTp);
  437. //double tp = (double) tempTp[1];
  438. for(int j=0 ; j < ArraySize(tempTp); j++)
  439. {
  440. Print(" Data is: ", tempTp[j]);
  441. }
  442. if(ArraySize(tempTp) >= 2)
  443. tp = (double) tempTp[ArraySize(tempTp) - 1];
  444. Print("Tp : ", tp);
  445. }
  446. }
  447. Print("Side:", direction, " Symbol: ", symbol, " Tp: ", tp, " Sl: ", sl, " Message Id: ", message_id);
  448. if(direction == buy)
  449. {
  450. tickt = placeBuyTrade(symbol, OP_BUY, SymbolInfoDouble(symbol, SYMBOL_ASK), tp, sl, message_id, lotSize);
  451. }
  452. if(direction == sell)
  453. {
  454. tickt = placeSellTrade(symbol, OP_SELL, SymbolInfoDouble(symbol, SYMBOL_BID), tp, sl, message_id, lotSize);
  455. }
  456. }
  457. }
  458. }
  459. //+------------------------------------------------------------------+
  460. //| |
  461. //+------------------------------------------------------------------+
  462. void pendingMessage(string &result[], string message, int message_id,int & tickt)
  463. {
  464. string lineOne[]; // = result[0];
  465. int lineIndex = 0;
  466. string direction = "";
  467. int direction_index = -1;
  468. string symbol = "";
  469. if(ArraySize(result) >= 1)
  470. {
  471. StringSplit(result[0], ' ', lineOne);
  472. if(StringFind(result[0], "limit", 0) != -1)
  473. if(((StringFind(result[0], "buy", 0) != -1) || (StringFind(result[0], "sell", 0) != -1)))
  474. {
  475. Print(" ------------------------------- Pending Message ---------------------------------- ");
  476. for(int i=0; i<ArraySize(lineOne); i++)
  477. {
  478. if(HasAlphanumeric(lineOne[i]))
  479. {
  480. ArrayResize(lineOne,ArraySize(lineOne)+1);
  481. lineOne[lineIndex] = lineOne[i];
  482. Print("Direction and Symbol: ", lineOne[lineIndex], " index is: ", lineIndex);
  483. if(lineOne[lineIndex] == buy || lineOne[lineIndex] == sell)
  484. {
  485. direction = lineOne[lineIndex];
  486. direction_index = lineIndex;
  487. //Print(" Direction is: ", direction, " Direction Index: ", direction_index);
  488. }
  489. lineIndex++;
  490. }
  491. }
  492. if(ArraySize(lineOne) >= 2)
  493. {
  494. if(direction_index == 0)
  495. {
  496. symbol = lineOne[1];
  497. StringToUpper(symbol);
  498. //Print(" This is Message format One (1). Where Direction is: ", direction, " Symbol: ", symbol);
  499. }
  500. else
  501. if(direction_index > 0)
  502. {
  503. symbol = lineOne[0];
  504. StringToUpper(symbol);
  505. //Print(" This is Message format One (1). Where Direction is: ", direction, " Symbol: ", symbol);
  506. }
  507. }
  508. symbol = symbolMapping(symbol);
  509. double price = 0;
  510. double sl = 0;
  511. double tp = 0; // = result[0];
  512. for(int i=0 ; i < ArraySize(result); i++)
  513. {
  514. // result[i] = StringTrimLeft(result[i]);
  515. // result[i] = StringTrimRight(result[i]);
  516. // Print("Result : ", result[i], " index is: ", i);
  517. if((StringFind(result[i], "open", 0) != -1))
  518. {
  519. string tempOpen[];
  520. StringReplace(result[i],":", " ");
  521. result[i] = trimString(result[i]);
  522. result[i] = spaceRemove(result[i]);
  523. //Print(" Sl String: ", result[i]);
  524. StringSplit(result[i], ' ', tempOpen);
  525. if(ArraySize(tempOpen) >= 2)
  526. price = (double) tempOpen[ArraySize(tempOpen) - 1];
  527. Print("Open : ", price);
  528. }
  529. if((StringFind(result[i], "sl", 0) != -1))
  530. {
  531. string tempSl[];
  532. StringReplace(result[i],":", " ");
  533. result[i] = trimString(result[i]);
  534. result[i] = spaceRemove(result[i]);
  535. //Print(" Sl String: ", result[i]);
  536. StringSplit(result[i], ' ', tempSl);
  537. if(ArraySize(tempSl) >= 2)
  538. sl = (double) tempSl[ArraySize(tempSl) - 1];
  539. Print("Sl : ", sl);
  540. }
  541. if((StringFind(result[i], "tp", 0) != -1))
  542. {
  543. Print("Tp : ", result[i], " index is: ", i);
  544. string tempTp[];
  545. StringReplace(result[i],":", " ");
  546. result[i] = trimString(result[i]);
  547. result[i] = spaceRemove(result[i]);
  548. Print("Tp After String Replace : ", result[i], " index is: ", i);
  549. StringSplit(result[i], ' ', tempTp);
  550. //double tp = (double) tempTp[1];
  551. for(int j=0 ; j < ArraySize(tempTp); j++)
  552. {
  553. Print(" Data is: ", tempTp[j]);
  554. }
  555. if(ArraySize(tempTp) >= 2)
  556. tp = (double) tempTp[ArraySize(tempTp) - 1];
  557. Print("Tp : ", tp);
  558. }
  559. }
  560. ENUM_ORDER_TYPE pending_side = 0;
  561. if(direction == buy)
  562. {
  563. if(price > SymbolInfoDouble(symbol, SYMBOL_ASK))
  564. {
  565. pending_side = OP_BUYSTOP;
  566. }
  567. else
  568. {
  569. pending_side = OP_BUYLIMIT;
  570. }
  571. }
  572. if(direction == sell)
  573. {
  574. if(price < SymbolInfoDouble(symbol, SYMBOL_BID))
  575. {
  576. pending_side = OP_SELLSTOP;
  577. }
  578. else
  579. {
  580. pending_side = OP_SELLLIMIT;
  581. }
  582. }
  583. Print("Side:", direction, " Type: ", pending_side, " Symbol: ", symbol, " Tp: ", tp, " Sl: ", sl, " Message Id: ", message_id);
  584. if(direction == buy)
  585. {
  586. tickt = placeBuyTrade(symbol, pending_side, price, tp, sl, message_id, lotSize);
  587. }
  588. if(direction == sell)
  589. {
  590. tickt = placeSellTrade(symbol, pending_side, price, tp, sl, message_id, lotSize);
  591. }
  592. }
  593. }
  594. }
  595. //+------------------------------------------------------------------+
  596. //| |
  597. //+------------------------------------------------------------------+
  598. int placeBuyTrade(string symbol, ENUM_ORDER_TYPE orderType, double priceIs,double tp,double sl, int messageId, double lot_size)
  599. {
  600. double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
  601. double bid = SymbolInfoDouble(symbol,SYMBOL_BID);
  602. double buySl = sl;
  603. double buyTp = tp;
  604. int ticket = OrderSend(symbol, orderType, lot_size, priceIs, 3, buySl, buyTp, "Buy Trade Placed.", magic_no, 0, clrBlue);
  605. Print("Buy order Print: Stop Loss: ", buySl, " Take profit: ", buyTp);
  606. if(ticket < 0)
  607. {
  608. Print("Buy Order Failed ", GetLastError());
  609. }
  610. else
  611. {
  612. Print(" Buy Order Is Placed Sucessfully ");
  613. }
  614. return ticket;
  615. }
  616. //+------------------------------------------------------------------+
  617. //| |
  618. //+------------------------------------------------------------------+
  619. int placeSellTrade(string symbol, ENUM_ORDER_TYPE orderType, double priceIs,double tp,double sl, int messageId, double lot_size)
  620. {
  621. double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
  622. double bid = SymbolInfoDouble(symbol,SYMBOL_BID);
  623. double sellSl = sl;
  624. double sellTp = tp;
  625. int ticket = OrderSend(symbol, orderType, lot_size, priceIs, 3, sellSl, sellTp, "Sell Trade Placed.", magic_no, 0, clrRed);
  626. if(ticket < 0)
  627. {
  628. Print("Sell Order Failed ", GetLastError());
  629. }
  630. else
  631. {
  632. Print(" Sell Order Is Placed Sucessfully ");
  633. }
  634. return ticket;
  635. }
  636. //+------------------------------------------------------------------+
  637. //| |
  638. //+------------------------------------------------------------------+
  639. void addtoMessageStructure(int message_id,string message,ulong ticket)
  640. {
  641. for(int i=0; i < MaxOrders; i++)
  642. {
  643. if(od[i].msgid == -1)
  644. {
  645. od[i].msgid = message_id;
  646. od[i].tickt = ticket;
  647. StringToLower(message);
  648. Print(" Message ID ",message_id," of Message = ",message," Having Ticket ",ticket," is added To Structure :: ");
  649. break;
  650. }
  651. }
  652. }
  653. //+------------------------------------------------------------------+
  654. //| |
  655. //+------------------------------------------------------------------+
  656. string trimString(string inputt)
  657. {
  658. // Remove spaces from the left and right sides
  659. int startt = 0;
  660. int end = StringLen(inputt) - 1;
  661. // Find the first non-space character
  662. while(startt <= end && StringGetCharacter(inputt, startt) == ' ')
  663. startt++;
  664. // Find the last non-space character
  665. while(end >= startt && StringGetCharacter(inputt, end) == ' ')
  666. end--;
  667. // Extract the substring without leading or trailing spaces
  668. return StringSubstr(inputt, startt, end - startt + 1);
  669. }
  670. //+------------------------------------------------------------------+
  671. //| |
  672. //+------------------------------------------------------------------+
  673. string spaceRemove(string inputt)
  674. {
  675. int len = StringLen(inputt);
  676. string out = "";
  677. bool inSpace = false;
  678. for(int i = 0; i < len; i++)
  679. {
  680. ushort ch = StringGetCharacter(inputt, i);
  681. // treat space, tab, CR, LF as whitespace
  682. bool isSpace = (ch == 32 || ch == 9 || ch == 10 || ch == 13);
  683. if(isSpace)
  684. {
  685. // mark that we are inside a whitespace run, but don't append yet
  686. inSpace = true;
  687. continue;
  688. }
  689. // when we hit a non-space after whitespace, add a single space (if out not empty)
  690. if(inSpace && StringLen(out) > 0)
  691. out += " ";
  692. // append the non-space character (use substr to preserve unicode chars)
  693. out += StringSubstr(inputt, i, 1);
  694. inSpace = false;
  695. }
  696. return(out);
  697. }
  698. //+------------------------------------------------------------------+
  699. //| |
  700. //+------------------------------------------------------------------+
  701. string removeExtraSpaces(string str)
  702. {
  703. string result = "";
  704. int len = StringLen(str);
  705. bool lastWasSpace = false;
  706. for(int i = 0; i < len; i++)
  707. {
  708. string currentChar = StringSubstr(str, i, 1);
  709. if(currentChar == " ")
  710. {
  711. // Skip adding this space if the last character was also a space
  712. if(lastWasSpace)
  713. continue;
  714. lastWasSpace = true;
  715. }
  716. else
  717. {
  718. lastWasSpace = false;
  719. }
  720. result += currentChar;
  721. }
  722. return result;
  723. }
  724. //+------------------------------------------------------------------+
  725. //| |
  726. //+------------------------------------------------------------------+
  727. bool HasAlphanumeric(string str)
  728. {
  729. //Print("String Length: ", StringLen(str));
  730. for(int i = 0; i <= StringLen(str); i++)
  731. {
  732. //Print("Here ", StringLen(str));
  733. if(IsAlphanumeric((char)str[i]))
  734. {
  735. return true;
  736. break;
  737. }
  738. }
  739. return false;
  740. }
  741. //+------------------------------------------------------------------+
  742. //| |
  743. //+------------------------------------------------------------------+
  744. bool IsAlphanumeric(char c)
  745. {
  746. if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
  747. return true;
  748. return false;
  749. }
  750. //+------------------------------------------------------------------+
  751. //| |
  752. //+------------------------------------------------------------------+
  753. string symbolMapping(string symbol)
  754. {
  755. for(int k = 0; k < ArraySize(symbolChart); k++)
  756. {
  757. StringToUpper(symbolChart[k]);
  758. if(symbol == symbolChart[k])
  759. {
  760. symbol = symbolSnd[k];
  761. }
  762. }
  763. symbol = prefix + symbol + suffix;
  764. return symbol;
  765. }
  766. //+------------------------------------------------------------------+
  767. //| |
  768. //+------------------------------------------------------------------+
  769. //+------------------------------------------------------------------+
  770. //+------------------------------------------------------------------+