Geen omschrijving

Pulse_EA_project_MT5.mq5 92KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. //+------------------------------------------------------------------+
  2. //| Pulse_EA_project_MT5.mq5 |
  3. //| Copyright 2024, MQL Development |
  4. //| https://www.mqldevelopment.com/ |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2024, MQL Development"
  7. #property link "https://www.mqldevelopment.com/"
  8. #property version "1.00"
  9. #include <Trade\Trade.mqh>
  10. CTrade trade;
  11. #define global "var"
  12. #define MaxOrders 10000
  13. struct new_trade_store
  14. {
  15. int trade_ticket;
  16. int trade_type;
  17. int trade_magic_no;
  18. double trade_open_price;
  19. double trade_close_price;
  20. datetime trade_open_time;
  21. datetime trade_close_time;
  22. double trade_sl;
  23. double trade_tp;
  24. double trade_lot;
  25. double trade_profit;
  26. new_trade_store()
  27. {
  28. trade_ticket = -1;
  29. trade_type = -1;
  30. trade_magic_no = -1;
  31. trade_open_price = -1;
  32. trade_close_price = -1;
  33. trade_open_time = -1;
  34. trade_close_time = -1;
  35. trade_sl = -1;
  36. trade_tp = -1;
  37. trade_lot = -1;
  38. trade_profit = -1;
  39. }
  40. };
  41. new_trade_store od[MaxOrders];
  42. enum trade_typ
  43. {
  44. buy, // Buy
  45. reverse, // Reverse
  46. };
  47. enum EA_TYPE
  48. {
  49. Tally_Sim, // Tally Sim
  50. Tally_Trade, // Tally Trade
  51. };
  52. input string gnrlsettings = " ================ General Settings =================== ";//_
  53. input EA_TYPE eaType = Tally_Sim; // EA Option
  54. input double tpPips = 20; // Tp Pips
  55. input double slPips = 20; // SL Pips
  56. input double lot = 0.1; // Lot Size
  57. input string gnrlesettings = " ================ Tally Sim Settings =================== ";//_
  58. trade_typ ordTyp = buy; // Order Type (Tally Sim)
  59. input int magicNo = 123; // Magic #
  60. input string fileName = "TradeDataFile"; // File Name (Tally Sim)
  61. input string grlesettings = " ================ Tally Trade Settings =================== ";//_
  62. input trade_typ tradesDirection = buy; // Order Type (Tally Trade)
  63. input int magicNo1 = 1; // Copy Trade Magic #
  64. input bool CopyTallyTrade = true; // Enable Copy Trade
  65. input double glTp = 20; // Copy Take Profit
  66. input double glSl = 10; // Copy Stop Loss
  67. input string Settings6 = "------------- Display Settings -------------"; //_
  68. input int tradeLineThickness = 2; // Trade Line Thickness
  69. input int dollarFontSize = 10; // Dollar Text Font Size
  70. input color dollarFontColor = clrAqua; // Dollar Text Font Colour
  71. input string horizontalLineName = "close"; // Horizontal Line Name
  72. input string Settings5 = "------------- Time Filter Settings -------------"; //_
  73. input bool enableTimeFilter = false; // Enable Time Filter
  74. input string startTime = "22:00"; // Start Time
  75. input string endTime = "01:00"; // End Time
  76. input bool useFridayClose = false; // Use Friday Close
  77. input string closeFriday = "23:00"; // Friday Close Time
  78. input bool useMondayOpen = false; // Use Monday Open
  79. input string mondayOpen = "01:00"; // Monday Open Time
  80. input string Settings25 = "------------- Daily Draw Limit Settings -------------"; //_
  81. input bool UseDailyDrawdownLimit = true; // Enable Daily Draw Down
  82. input double DailyDrawdownAmount = 2000; // $2000 limit
  83. input string DrawdownResetTime = "05:00"; // Enable Trading At
  84. input string indSettings = " ================ Indicator Settings =================== ";//_
  85. input color Dot_Color = clrOrangeRed;
  86. input double StartingBalance = 10000;
  87. input string MagicNumbers = "123";
  88. input int MA_Period = 5;
  89. input ENUM_MA_METHOD MA_Method = MODE_SMA;
  90. input string fileName1 = "TradeDataFile"; // File Name
  91. input int historyTrades = 50;
  92. bool tpSlHit = false;
  93. int ticketAssigner = 1;
  94. int handler;
  95. double bufferData[];
  96. ushort u_sep; // The code of the separator character
  97. string sep = ":"; // A separator as a character
  98. string result1[];
  99. datetime startTradingTime = 0, endTradingTime = 0, dailyDrawDownTime = 0;
  100. datetime mondayTradingStart = 0, fridayTradingClose = 0;
  101. datetime eaStartTime; // Store EA start time
  102. double g_dailyStartBalance;
  103. bool enableTradingDaily = true;
  104. bool doTrading = true;
  105. int prevOrderCount = 0;
  106. //+------------------------------------------------------------------+
  107. //| |
  108. //+------------------------------------------------------------------+
  109. string filename = fileName + Symbol() + ".csv";
  110. //+------------------------------------------------------------------+
  111. //| Expert initialization function |
  112. //+------------------------------------------------------------------+
  113. int OnInit()
  114. {
  115. //---
  116. if(!GlobalVariableCheck(global)) //for creating
  117. {
  118. GlobalVariableSet(global,0);
  119. // 0 means Free
  120. // 1 means busy
  121. }
  122. if(eaType == Tally_Sim)
  123. {
  124. FileDelete(filename,FILE_WRITE|FILE_CSV|FILE_COMMON|FILE_END);
  125. int filehandle = FileOpen(filename,FILE_WRITE|FILE_CSV|FILE_COMMON|FILE_END,",");
  126. if(filehandle != INVALID_HANDLE)
  127. {
  128. FileClose(filehandle);
  129. }
  130. }
  131. else
  132. {
  133. prevOrderCount = orderCount();
  134. }
  135. string typ = eaType == 0 ? "Tally Sim" :"Tally Trade";
  136. Comment(" Current Mode: ",typ);
  137. tpSlHit = false;
  138. doTrading = true;
  139. ticketAssigner = 1;
  140. if(eaType == Tally_Trade)
  141. {
  142. ArrayInitialize(bufferData,0.0);
  143. handler = iCustom(Symbol(),PERIOD_CURRENT,"Pulse Balance Indicator Sim",Dot_Color,StartingBalance,MagicNumbers,MA_Period,MA_Method,fileName1,historyTrades);
  144. ArraySetAsSeries(bufferData,true);
  145. }
  146. trade.SetExpertMagicNumber(magicNo1);
  147. trade.SetDeviationInPoints(10);
  148. trade.SetTypeFilling(ORDER_FILLING_IOC);
  149. trade.LogLevel(LOG_LEVEL_ALL);
  150. trade.SetAsyncMode(false);
  151. setStart_EndTime_modify(true,startTime,endTime,startTradingTime,endTradingTime);
  152. setStart_EndTime(true,startTime,endTime,startTradingTime,endTradingTime);
  153. g_dailyStartBalance = AccountInfoDouble(ACCOUNT_BALANCE);
  154. enableTradingDaily = true;
  155. eaStartTime = TimeCurrent(); // Track EA start time
  156. //---
  157. return(INIT_SUCCEEDED);
  158. }
  159. //+------------------------------------------------------------------+
  160. //| Expert deinitialization function |
  161. //+------------------------------------------------------------------+
  162. void OnDeinit(const int reason)
  163. {
  164. //---
  165. GlobalVariableSet(global,0);
  166. if(eaType == Tally_Sim)
  167. {
  168. deleteObjects();
  169. }
  170. Comment("");
  171. }
  172. //+------------------------------------------------------------------+
  173. //| Expert tick function |
  174. //+------------------------------------------------------------------+
  175. void OnTick()
  176. {
  177. //---
  178. doTrading = true;
  179. timeConversion();
  180. if(TimeCurrent() >= dailyDrawDownTime && enableTradingDaily == false)
  181. {
  182. enableTradingDaily = true;
  183. }
  184. if(eaType == Tally_Trade)
  185. {
  186. markClosedOrder(eaStartTime);
  187. CheckVirtualGlobalTpSl();
  188. }
  189. if(eaType == Tally_Sim)
  190. checkTPSLHit();
  191. newBar();
  192. if(newDayBar())
  193. {
  194. g_dailyStartBalance = AccountInfoDouble(ACCOUNT_BALANCE);
  195. }
  196. MqlDateTime dt;
  197. TimeToStruct(TimeCurrent(), dt);
  198. if(useMondayOpen)
  199. {
  200. if(dt.day_of_week == 1 && TimeCurrent() < mondayTradingStart) // it's Monday
  201. {
  202. doTrading = false;
  203. }
  204. }
  205. if(useFridayClose)
  206. {
  207. if(dt.day_of_week == 5 && TimeCurrent() >= fridayTradingClose) // it's Friday
  208. {
  209. doTrading = false;
  210. closeAllActiveOrders();
  211. }
  212. }
  213. setStart_EndTime_modify(false,startTime,endTime,startTradingTime,endTradingTime);
  214. setStart_EndTime(false,startTime,endTime,startTradingTime,endTradingTime);
  215. if(enableTimeFilter && TimeCurrent() >= startTradingTime && TimeCurrent() < endTradingTime)
  216. {
  217. return;
  218. }
  219. placeTrade();
  220. }
  221. //+------------------------------------------------------------------+
  222. //| |
  223. //+------------------------------------------------------------------+
  224. bool newBar()
  225. {
  226. static datetime lastbar;
  227. datetime curbar = iTime(Symbol(), PERIOD_CURRENT, 0);
  228. if(lastbar != curbar)
  229. {
  230. lastbar = curbar;
  231. Print(" ---------------------- New Bar :: ---------------------- ",lastbar);
  232. return (true);
  233. }
  234. else
  235. {
  236. return (false);
  237. }
  238. }
  239. //+------------------------------------------------------------------+
  240. //| |
  241. //+------------------------------------------------------------------+
  242. bool newDayBar()
  243. {
  244. static datetime lastbar1;
  245. datetime curbar1 = iTime(Symbol(), PERIOD_D1, 0);
  246. if(lastbar1 != curbar1)
  247. {
  248. lastbar1 = curbar1;
  249. Print(" ---------------------- New Day Bar :: ---------------------- ",lastbar1);
  250. return (true);
  251. }
  252. else
  253. {
  254. return (false);
  255. }
  256. }
  257. //+------------------------------------------------------------------+
  258. //| |
  259. //+------------------------------------------------------------------+
  260. void closeAllActiveOrders()
  261. {
  262. for(int i=PositionsTotal()-1; i >=0 ; i--)
  263. {
  264. ulong ticket = PositionGetTicket(i);
  265. if(PositionSelectByTicket(ticket))
  266. {
  267. if(PositionGetInteger(POSITION_MAGIC) == magicNo &&
  268. PositionGetString(POSITION_SYMBOL) == Symbol())
  269. {
  270. if(trade.PositionClose(ticket))
  271. {
  272. Print("Closing All Active Orders");
  273. Print("Position closed ", ticket);
  274. }
  275. else
  276. {
  277. Print("Cannot close order: ",GetLastError());
  278. }
  279. }
  280. }
  281. }
  282. }
  283. //+------------------------------------------------------------------+
  284. //| |
  285. //+------------------------------------------------------------------+
  286. void timeConversion()
  287. {
  288. MqlDateTime date1, date_1,date_2;
  289. TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date1);
  290. u_sep=StringGetCharacter(sep,0);
  291. StringSplit(mondayOpen,u_sep,result1);
  292. date1.hour = (int)StringToInteger(result1[0]);
  293. date1.min = (int)StringToInteger(result1[1]);
  294. mondayTradingStart = StructToTime(date1);
  295. TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date_1);
  296. StringSplit(closeFriday,u_sep,result1);
  297. date_1.hour = (int)StringToInteger(result1[0]);
  298. date_1.min = (int)StringToInteger(result1[1]);
  299. fridayTradingClose = StructToTime(date_1);
  300. TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date_2);
  301. StringSplit(DrawdownResetTime,u_sep,result1);
  302. date_2.hour = (int)StringToInteger(result1[0]);
  303. date_2.min = (int)StringToInteger(result1[1]);
  304. dailyDrawDownTime = StructToTime(date_2);
  305. }
  306. //+------------------------------------------------------------------+
  307. //| |
  308. //+------------------------------------------------------------------+
  309. void placeTrade()
  310. {
  311. if(tpSlHit == false || eaType == Tally_Trade)
  312. {
  313. if(ordTyp == buy)
  314. {
  315. placeBuyTrade();
  316. }
  317. if(ordTyp == reverse)
  318. {
  319. placeSellTrade();
  320. }
  321. tpSlHit = true;
  322. }
  323. }
  324. //+------------------------------------------------------------------+
  325. //| |
  326. //+------------------------------------------------------------------+
  327. void placeBuyTrade()
  328. {
  329. double buySl = 0,buyTp=0;
  330. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  331. if(slPips != 0)
  332. {
  333. buySl = Ask - (slPips * Point() * 10);
  334. }
  335. if(tpPips != 0)
  336. {
  337. buyTp = Ask + (tpPips * Point() * 10);
  338. }
  339. if(eaType == Tally_Sim)
  340. {
  341. AddToStructure(ticketAssigner,0,magicNo,Ask,0,TimeCurrent(),0,buySl,buyTp,lot,0);
  342. ticketAssigner++;
  343. DrawVirtualBuyOpen(Ask);
  344. }
  345. else
  346. {
  347. int count = orderCount();
  348. if(prevOrderCount != count && count != -1)
  349. {
  350. for(int k = prevOrderCount + 1;k <= count;k++)
  351. {
  352. if(getTicketByNumber(k,buySl,buyTp) != -1)
  353. {
  354. if(enableTradingDaily == true && doTrading == true)
  355. {
  356. if(eaType == Tally_Trade && CopyTallyTrade)
  357. {
  358. if(CopyBuffer(handler, 0, 0, 4, bufferData) < 0)
  359. {
  360. Print("Error in copying Buffer data ", GetLastError());
  361. }
  362. double balance = bufferData[1];
  363. if(CopyBuffer(handler, 1, 0, 4, bufferData) < 0)
  364. {
  365. Print("Error in copying Buffer data ", GetLastError());
  366. }
  367. double MA = bufferData[1];
  368. Print(" Balance = ",balance," MA = ",MA);
  369. if(tradesDirection == buy)
  370. {
  371. if(balance > MA)
  372. {
  373. if(!trade.Buy(lot, Symbol(),SymbolInfoDouble(Symbol(),SYMBOL_ASK),buySl,buyTp,"Buy Trade"))
  374. {
  375. Print(" Error in Placing Buy Trade : ",GetLastError());
  376. }
  377. }
  378. else
  379. {
  380. Print(" Trade is not Placed because Balance is less than MA Value ");
  381. }
  382. }
  383. else
  384. {
  385. if(balance < MA)
  386. {
  387. if(!trade.Sell(lot,Symbol(),SymbolInfoDouble(Symbol(),SYMBOL_BID),buyTp,buySl,"Sell Trade"))
  388. {
  389. Print(" Error in Placing Sell Trade : ",GetLastError());
  390. }
  391. }
  392. else
  393. {
  394. Print(" Trade is not Placed because Balance is less than MA Value ");
  395. }
  396. }
  397. }
  398. }
  399. prevOrderCount = count;
  400. }
  401. }
  402. }
  403. }
  404. }
  405. //+------------------------------------------------------------------+
  406. //| |
  407. //+------------------------------------------------------------------+
  408. void placeSellTrade()
  409. {
  410. double sellSl = 0, sellTp = 0;
  411. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  412. if(slPips != 0)
  413. {
  414. sellSl = Bid + (slPips * 10 * Point());
  415. }
  416. if(tpPips != 0)
  417. {
  418. sellTp = Bid - (tpPips * 10 * Point());
  419. }
  420. if(eaType == Tally_Sim)
  421. {
  422. AddToStructure(ticketAssigner,1,magicNo,Bid,0,TimeCurrent(),0,sellSl,sellTp,lot,0);
  423. ticketAssigner++;
  424. }
  425. else
  426. {
  427. int count = orderCount();
  428. if(prevOrderCount != count && count != -1)
  429. {
  430. for(int k = prevOrderCount + 1;k <= count;k++)
  431. {
  432. getTicketByNumber(k,sellSl,sellTp);
  433. if(getTicketByNumber(k,sellSl,sellTp) != -1)
  434. {
  435. if(enableTradingDaily == true)
  436. {
  437. if(eaType == Tally_Trade && CopyTallyTrade)
  438. {
  439. if(CopyBuffer(handler, 0, 0, 4, bufferData) < 0)
  440. {
  441. Print("Error in copying Buffer data ", GetLastError());
  442. }
  443. double balance = bufferData[1];
  444. if(CopyBuffer(handler, 1, 0, 4, bufferData) < 0)
  445. {
  446. Print("Error in copying Buffer data ", GetLastError());
  447. }
  448. double MA = bufferData[1];
  449. Print(" Balance = ",balance," MA = ",MA);
  450. if(tradesDirection == reverse)
  451. {
  452. if(balance < MA)
  453. {
  454. if(!trade.Sell(lot,Symbol(),Bid,sellSl,sellTp,"Sell Trade"))
  455. {
  456. Print(" Error in Placing Sell Trade : ",GetLastError());
  457. }
  458. }
  459. else
  460. {
  461. Print(" Trade is not Placed because Balance is less than MA Value ");
  462. }
  463. }
  464. else
  465. {
  466. if(balance > MA)
  467. {
  468. if(!trade.Buy(lot,Symbol(),SymbolInfoDouble(Symbol(),SYMBOL_ASK),sellTp,sellSl,"Buy Trade"))
  469. {
  470. Print(" Error in Placing Buy Trade : ",GetLastError());
  471. }
  472. }
  473. else
  474. {
  475. Print(" Trade is not Placed because Balance is less than MA Value ");
  476. }
  477. }
  478. }
  479. }
  480. prevOrderCount = count;
  481. }
  482. }
  483. }
  484. }
  485. }
  486. //+------------------------------------------------------------------+
  487. //| |
  488. //+------------------------------------------------------------------+
  489. void AddToStructure(int ticket, int type, int magic, double open_price,
  490. double close_price, datetime open_time, datetime close_time,
  491. double sl, double tp, double lotSize, double profit)
  492. {
  493. for(int i = 0; i < MaxOrders; i++)
  494. {
  495. if(od[i].trade_ticket == -1)
  496. {
  497. od[i].trade_ticket = ticket;
  498. od[i].trade_type = type;
  499. od[i].trade_magic_no = magic;
  500. od[i].trade_open_price = open_price;
  501. od[i].trade_close_price = close_price;
  502. od[i].trade_open_time = open_time;
  503. od[i].trade_close_time = close_time;
  504. od[i].trade_sl = sl;
  505. od[i].trade_tp = tp;
  506. od[i].trade_lot = lotSize;
  507. od[i].trade_profit = profit;
  508. Print(" ========== Trade Placed of Ticket ========== ",ticket);
  509. Print("[AddToStructure] Added at index: ", i,
  510. " | Ticket = ", ticket,
  511. " | Type = ", type,
  512. " | Magic = ", magic,
  513. " | OpenPrice = ", open_price,
  514. " | ClosePrice = ", close_price,
  515. " | SL = ", sl,
  516. " | TP = ", tp,
  517. " | Lots = ", lotSize,
  518. " | Profit = ", profit);
  519. break;
  520. }
  521. }
  522. }
  523. //+------------------------------------------------------------------+
  524. //| |
  525. //+------------------------------------------------------------------+
  526. void checkTPSLHit()
  527. {
  528. double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
  529. double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
  530. for(int i = 0; i < MaxOrders; i++)
  531. {
  532. if(od[i].trade_ticket != -1 && od[i].trade_close_price == 0)
  533. {
  534. int ticket = od[i].trade_ticket;
  535. int type = od[i].trade_type;
  536. double sl = od[i].trade_sl;
  537. double tp = od[i].trade_tp;
  538. double closePrice = 0;
  539. // ----------------------------------------------
  540. // BUY ORDER CHECK
  541. // ----------------------------------------------
  542. if(type == 0)
  543. {
  544. // TP hit → Bid >= TP
  545. if(bid >= tp && tp > 0)
  546. {
  547. closePrice = bid;
  548. Print(" ======== Buy Trade TP Hit ======= ",ticket);
  549. }
  550. // SL hit → Bid <= SL
  551. if(bid <= sl && sl > 0)
  552. {
  553. closePrice = bid;
  554. Print(" ======== Buy Trade SL Hit ======= ",ticket);
  555. }
  556. }
  557. // ----------------------------------------------
  558. // SELL ORDER CHECK
  559. // ----------------------------------------------
  560. if(type == 1)
  561. {
  562. // TP hit → Ask <= TP (price goes DOWN)
  563. if(ask <= tp && tp > 0)
  564. {
  565. closePrice = ask;
  566. Print(" ======== Sell Trade TP Hit ======= ",ticket);
  567. }
  568. // SL hit → Ask >= SL
  569. if(ask >= sl && sl > 0)
  570. {
  571. closePrice = ask;
  572. Print(" ======== Sell Trade SL Hit ======= ",ticket);
  573. }
  574. }
  575. // ----------------------------------------------
  576. // IF ANYTHING TRIGGERED → CLOSE TRADE
  577. // ----------------------------------------------
  578. if(closePrice > 0)
  579. {
  580. od[i].trade_close_price = closePrice;
  581. od[i].trade_close_time = TimeCurrent();
  582. double tpdifference = type == 1 ? (od[i].trade_open_price - od[i].trade_close_price) / Point() : (od[i].trade_close_price - od[i].trade_open_price) / Point();
  583. od[i].trade_profit += tpinDollar(od[i].trade_lot, tpdifference);
  584. Print("======= TRADE CLOSED of ticket =======",ticket);
  585. Print("Index: ", i,
  586. " | Ticket: ", ticket,
  587. " | Type: ", type,
  588. " | ClosePrice: ", closePrice,
  589. " | Time: ", od[i].trade_close_time,
  590. " | Profit: ", od[i].trade_profit);
  591. DrawVirtualBuyClose(closePrice);
  592. datetime t1 = od[i].trade_open_time;
  593. double price1 = od[i].trade_open_price;
  594. datetime t2 = od[i].trade_close_time;
  595. double price2 = od[i].trade_close_price;
  596. DrawDottedTrendline("Ticket "+string(ticket), t1, price1, t2, price2, clrGreen);
  597. tpSlHit = false; // make these zero so new trade place's
  598. string line = MakeTradeString(
  599. od[i].trade_ticket,
  600. od[i].trade_type,
  601. od[i].trade_magic_no,
  602. od[i].trade_open_price,
  603. od[i].trade_close_price,
  604. od[i].trade_open_time,
  605. od[i].trade_close_time,
  606. od[i].trade_sl,
  607. od[i].trade_tp,
  608. od[i].trade_lot,
  609. od[i].trade_profit
  610. );
  611. writeDatainFile(line);
  612. }
  613. }
  614. }
  615. }
  616. //+------------------------------------------------------------------+
  617. //| |
  618. //+------------------------------------------------------------------+
  619. double tpinDollar(double lotSize, double distance)
  620. {
  621. double pipvalue = NormalizeDouble(((SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)/(SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE)/Point()))*10),2);
  622. pipvalue = pipvalue / 10;
  623. double dollar = lotSize * distance * pipvalue;
  624. return NormalizeDouble(dollar,2);
  625. }
  626. //+------------------------------------------------------------------+
  627. //| |
  628. //+------------------------------------------------------------------+
  629. void writeDatainFile(string str)
  630. {
  631. if(GlobalVariableGet(global) == 0)
  632. {
  633. int filehandle = FileOpen(filename,FILE_WRITE|FILE_CSV|FILE_COMMON|FILE_END,",");
  634. if(filehandle != INVALID_HANDLE)
  635. {
  636. GlobalVariableSet(global,1);
  637. FileSeek(filehandle, 0, SEEK_END);
  638. FileWrite(filehandle,str);
  639. Print(str);
  640. FileClose(filehandle);
  641. GlobalVariableSet(global,0);
  642. }
  643. }
  644. }
  645. //+------------------------------------------------------------------+
  646. //| |
  647. //+------------------------------------------------------------------+
  648. string MakeTradeString(int ticket, int type, int magic,
  649. double open_price, double close_price,
  650. datetime open_time, datetime close_time,
  651. double sl, double tp, double lotSize, double profit)
  652. {
  653. string str = StringFormat("%d,%d,%d,%.5f,%.5f,%s,%s,%.5f,%.5f,%.2f,%.2f",
  654. ticket,
  655. type,
  656. magic,
  657. open_price,
  658. close_price,
  659. TimeToString(open_time, TIME_DATE|TIME_SECONDS),
  660. TimeToString(close_time, TIME_DATE|TIME_SECONDS),
  661. sl,
  662. tp,
  663. lotSize,
  664. profit);
  665. return str;
  666. }
  667. //+------------------------------------------------------------------+
  668. //| |
  669. //+------------------------------------------------------------------+
  670. void setStart_EndTime(bool onInit,string start,string end,datetime & sessionStart,datetime & sessionEnd)
  671. {
  672. int newYorkStartHour = 0, newYorkStartMin = 0, newYorkEndHour = 0, newYorkEndMin = 0;
  673. datetime newYorkStartTrading,newYorkEndTrading;
  674. string time[];
  675. StringSplit(start,':',time);
  676. newYorkStartHour = (int)StringToInteger(time[0]);
  677. newYorkStartMin = (int)StringToInteger(time[1]);
  678. EventSetMillisecondTimer(500);
  679. time[0] = "";
  680. time[1] = "";
  681. StringSplit(end,':',time);
  682. newYorkEndHour = (int)StringToInteger(time[0]);
  683. newYorkEndMin = (int)StringToInteger(time[1]);
  684. // Print(" Start Time Hour: ",newYorkStartHour," Start Time Min: ",newYorkStartMin);
  685. // Print(" End Time Hour: ",newYorkEndHour," End Time Min: ",newYorkEndMin);
  686. datetime startDateTime;
  687. MqlDateTime st;
  688. TimeCurrent(st); // get current date
  689. st.hour = newYorkStartHour;
  690. st.min = newYorkStartMin;
  691. st.sec = 0;
  692. startDateTime = StructToTime(st);
  693. datetime endDateTime;
  694. MqlDateTime et;
  695. TimeCurrent(et); // get current date
  696. et.hour = newYorkEndHour;
  697. et.min = newYorkEndMin;
  698. et.sec = 0;
  699. endDateTime = StructToTime(et);
  700. MqlDateTime sdate,edate;
  701. datetime start_Time = 0, end_Time = 0;
  702. if(startDateTime > endDateTime)
  703. {
  704. if(onInit)
  705. {
  706. start_Time = iTime(Symbol(),PERIOD_D1,1);
  707. end_Time = iTime(Symbol(),PERIOD_D1,0);
  708. }
  709. else
  710. {
  711. start_Time = sessionStart;
  712. end_Time = sessionEnd;
  713. if(TimeCurrent() >= sessionEnd && sessionEnd != 0)
  714. {
  715. start_Time = iTime(Symbol(),PERIOD_D1,0);
  716. end_Time = start_Time + 86400;
  717. }
  718. }
  719. }
  720. else
  721. {
  722. start_Time = iTime(Symbol(),PERIOD_D1,0);
  723. end_Time = iTime(Symbol(),PERIOD_D1,0);
  724. }
  725. if(TimeToStruct(end_Time,edate))
  726. {
  727. edate.hour = newYorkEndHour;
  728. edate.min = newYorkEndMin;
  729. edate.sec = 0;
  730. }
  731. else
  732. Print("Error in Converting Time: ",GetLastError());
  733. newYorkEndTrading = StructToTime(edate);
  734. if(TimeToStruct(start_Time,sdate))
  735. {
  736. sdate.hour = newYorkStartHour;
  737. sdate.min = newYorkStartMin;
  738. sdate.sec = 0;
  739. }
  740. else
  741. Print("Error in Converting Time: ",GetLastError());
  742. newYorkStartTrading = StructToTime(sdate);
  743. sessionStart = newYorkStartTrading;
  744. sessionEnd = newYorkEndTrading;
  745. }
  746. //+------------------------------------------------------------------+
  747. //| |
  748. //+------------------------------------------------------------------+
  749. void setStart_EndTime_modify(bool onInit,string start,string end,datetime & sessionStart,datetime & sessionEnd)
  750. {
  751. int newYorkStartHour = 0, newYorkStartMin = 0, newYorkEndHour = 0, newYorkEndMin = 0;
  752. datetime newYorkStartTrading,newYorkEndTrading;
  753. string time[];
  754. StringSplit(start,':',time);
  755. newYorkStartHour = (int)StringToInteger(time[0]);
  756. newYorkStartMin = (int)StringToInteger(time[1]);
  757. time[0] = "";
  758. time[1] = "";
  759. StringSplit(end,':',time);
  760. newYorkEndHour = (int)StringToInteger(time[0]);
  761. newYorkEndMin = (int)StringToInteger(time[1]);
  762. // Print(" Start Time Hour: ",newYorkStartHour," Start Time Min: ",newYorkStartMin);
  763. // Print(" End Time Hour: ",newYorkEndHour," End Time Min: ",newYorkEndMin);
  764. datetime startDateTime;
  765. MqlDateTime st;
  766. TimeCurrent(st); // get current date
  767. st.hour = newYorkStartHour;
  768. st.min = newYorkStartMin;
  769. st.sec = 0;
  770. startDateTime = StructToTime(st);
  771. datetime endDateTime;
  772. MqlDateTime et;
  773. TimeCurrent(et); // get current date
  774. et.hour = newYorkEndHour;
  775. et.min = newYorkEndMin;
  776. et.sec = 0;
  777. endDateTime = StructToTime(et);
  778. MqlDateTime sdate,edate;
  779. datetime start_Time = 0, end_Time = 0;
  780. if(startDateTime > endDateTime)
  781. {
  782. start_Time = iTime(Symbol(),PERIOD_D1,1);
  783. end_Time = iTime(Symbol(),PERIOD_D1,0);
  784. }
  785. else
  786. {
  787. start_Time = iTime(Symbol(),PERIOD_D1,0);
  788. end_Time = iTime(Symbol(),PERIOD_D1,0);
  789. }
  790. if(TimeToStruct(end_Time,edate))
  791. {
  792. edate.hour = newYorkEndHour;
  793. edate.min = newYorkEndMin;
  794. edate.sec = 0;
  795. }
  796. else
  797. Print("Error in Converting Time: ",GetLastError());
  798. newYorkEndTrading = StructToTime(edate);
  799. if(TimeToStruct(start_Time,sdate))
  800. {
  801. sdate.hour = newYorkStartHour;
  802. sdate.min = newYorkStartMin;
  803. sdate.sec = 0;
  804. }
  805. else
  806. Print("Error in Converting Time: ",GetLastError());
  807. newYorkStartTrading = StructToTime(sdate);
  808. sessionStart = newYorkStartTrading;
  809. sessionEnd = newYorkEndTrading;
  810. }
  811. //+------------------------------------------------------------------+
  812. //| Check Virtual TP/SL for all positions |
  813. //+------------------------------------------------------------------+
  814. void CheckVirtualGlobalTpSl()
  815. {
  816. double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
  817. double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
  818. for(int i=PositionsTotal()-1; i >=0 ; i--)
  819. {
  820. ulong ticket = PositionGetTicket(i);
  821. if(PositionSelectByTicket(ticket))
  822. {
  823. if(PositionGetInteger(POSITION_MAGIC) == magicNo &&
  824. PositionGetString(POSITION_SYMBOL) == _Symbol)
  825. {
  826. if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
  827. {
  828. double tp_price = PositionGetDouble(POSITION_PRICE_OPEN) + glTp * 10 * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
  829. double sl_price = PositionGetDouble(POSITION_PRICE_OPEN) - glSl * 10 * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
  830. if(bid >= tp_price || bid <= sl_price)
  831. {
  832. closeAllActiveOrders();
  833. return;
  834. }
  835. }
  836. if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
  837. {
  838. double tp_price = PositionGetDouble(POSITION_PRICE_OPEN) - glTp * 10 * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
  839. double sl_price = PositionGetDouble(POSITION_PRICE_OPEN) + glSl * 10 * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
  840. if(ask <= tp_price || ask >= sl_price)
  841. {
  842. closeAllActiveOrders();
  843. return;
  844. }
  845. }
  846. }
  847. }
  848. }
  849. }
  850. //+------------------------------------------------------------------+
  851. //| |
  852. //+------------------------------------------------------------------+
  853. void CheckDailyDrawdown()
  854. {
  855. if(!UseDailyDrawdownLimit)
  856. return;
  857. double equity = AccountInfoDouble(ACCOUNT_EQUITY);
  858. double drawdown = g_dailyStartBalance - equity;
  859. if(drawdown >= DailyDrawdownAmount)
  860. {
  861. Print("Daily Drawdown Limit Hit: ", drawdown, " >= ", DailyDrawdownAmount);
  862. closeAllActiveOrders();
  863. }
  864. }
  865. //+------------------------------------------------------------------+
  866. //| |
  867. //+------------------------------------------------------------------+
  868. void markClosedOrder(datetime eaTime)
  869. {
  870. HistorySelect(eaTime, TimeCurrent());
  871. for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
  872. {
  873. ulong dealTicket = HistoryDealGetTicket(i);
  874. long dealMagic = HistoryDealGetInteger(dealTicket, DEAL_MAGIC);
  875. if(dealMagic != magicNo && dealMagic != 0)
  876. continue;
  877. if(HistoryDealGetString(dealTicket, DEAL_SYMBOL) == Symbol())
  878. {
  879. //Print("Condition 2 ",HistoryDealGetInteger(dealTicket, DEAL_ENTRY)," HistoryDealsTotal(): ",HistoryDealsTotal());
  880. if(HistoryDealGetInteger(dealTicket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
  881. {
  882. //Print("deal out close: ",dealTicket);
  883. long dealType = HistoryDealGetInteger(dealTicket, DEAL_TYPE);
  884. if(dealType != DEAL_TYPE_BUY && dealType != DEAL_TYPE_SELL)
  885. continue;
  886. double profit = HistoryDealGetDouble(dealTicket, DEAL_PROFIT);
  887. profit = NormalizeDouble(profit, 2);
  888. string lineName = "TradeLine_" + (string)dealTicket;
  889. if(ObjectFind(0, lineName) >= 0)
  890. continue;
  891. ulong entryDeal = findEntryDeal(dealTicket);
  892. if(entryDeal == 0)
  893. continue;
  894. datetime openTime = (datetime)HistoryDealGetInteger(entryDeal, DEAL_TIME);
  895. double openPrice = HistoryDealGetDouble(entryDeal, DEAL_PRICE);
  896. datetime closeTime = (datetime)HistoryDealGetInteger(dealTicket, DEAL_TIME);
  897. double closePrice = HistoryDealGetDouble(dealTicket, DEAL_PRICE);
  898. createTrendLine(lineName, openTime, openPrice, closeTime, closePrice, dealType, profit);
  899. PrintFormat("Trend line created for closed trade #%I64u | Profit: %.2f", dealTicket, profit);
  900. }
  901. }
  902. }
  903. }
  904. //+------------------------------------------------------------------+
  905. //| |
  906. //+------------------------------------------------------------------+
  907. void createTrendLine(string name,
  908. datetime openTime, double openPrice,
  909. datetime closeTime, double closePrice,
  910. long dealType, double profit)
  911. {
  912. if(ObjectFind(0, name) >= 0)
  913. return;
  914. if(!ObjectCreate(0, name, OBJ_TREND, 0, openTime, openPrice, closeTime, closePrice))
  915. {
  916. Print("Error creating trend line: ", GetLastError());
  917. return;
  918. }
  919. ObjectSetInteger(0, name, OBJPROP_WIDTH, tradeLineThickness);
  920. ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_SOLID);
  921. ObjectSetInteger(0, name, OBJPROP_RAY, false);
  922. color lineColor = (dealType == DEAL_TYPE_BUY) ? clrRed : clrBlue;
  923. ObjectSetInteger(0, name, OBJPROP_COLOR, lineColor);
  924. createProfitLabel(name + "_label", openTime, openPrice, closeTime, closePrice, dealType, profit);
  925. }
  926. //+------------------------------------------------------------------+
  927. //| |
  928. //+------------------------------------------------------------------+
  929. void createProfitLabel(string name,
  930. datetime openTime, double openPrice,
  931. datetime closeTime, double closePrice,
  932. long dealType, double profit)
  933. {
  934. if(ObjectFind(0, name) >= 0)
  935. return;
  936. datetime middleTime = openTime + (closeTime - openTime) / 2;
  937. double midPrice = (openPrice + closePrice) / 2;
  938. if(!ObjectCreate(0, name, OBJ_TEXT, 0, middleTime, midPrice))
  939. {
  940. Print("Error creating profit label: ", GetLastError());
  941. return;
  942. }
  943. string profitText = "$" + DoubleToString(profit, 2);
  944. ObjectSetString(0, name, OBJPROP_TEXT, profitText);
  945. ObjectSetInteger(0, name, OBJPROP_FONTSIZE, dollarFontSize);
  946. ObjectSetInteger(0, name, OBJPROP_COLOR, dollarFontColor);
  947. ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_TOP);
  948. ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
  949. }
  950. //+------------------------------------------------------------------+
  951. //| |
  952. //+------------------------------------------------------------------+
  953. ulong findEntryDeal(ulong closeDeal)
  954. {
  955. string symbol = HistoryDealGetString(closeDeal, DEAL_SYMBOL);
  956. long positionID = HistoryDealGetInteger(closeDeal, DEAL_POSITION_ID);
  957. int dealOutCount = 0;
  958. for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
  959. {
  960. ulong deal = HistoryDealGetTicket(i);
  961. if(HistoryDealGetInteger(deal, DEAL_POSITION_ID) == positionID)
  962. {
  963. if(HistoryDealGetInteger(deal, DEAL_ENTRY) == DEAL_ENTRY_OUT)
  964. {
  965. dealOutCount++;
  966. }
  967. }
  968. }
  969. if(dealOutCount < 2)
  970. {
  971. for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
  972. {
  973. ulong deal = HistoryDealGetTicket(i);
  974. if(HistoryDealGetInteger(deal, DEAL_POSITION_ID) == positionID &&
  975. HistoryDealGetInteger(deal, DEAL_ENTRY) == DEAL_ENTRY_IN &&
  976. HistoryDealGetString(deal, DEAL_SYMBOL) == symbol)
  977. return deal;
  978. //Print("Deal: ", deal);
  979. }
  980. }
  981. else
  982. {
  983. for(int i = 0; i < HistoryDealsTotal(); i++)
  984. {
  985. ulong deal = HistoryDealGetTicket(i);
  986. if(HistoryDealGetInteger(deal, DEAL_POSITION_ID) == positionID &&
  987. HistoryDealGetInteger(deal, DEAL_ENTRY) == DEAL_ENTRY_OUT)
  988. {
  989. return deal;
  990. }
  991. //Print("Deal: ", deal);
  992. }
  993. }
  994. return 0;
  995. }
  996. //+------------------------------------------------------------------+
  997. //| |
  998. //+------------------------------------------------------------------+
  999. void DrawArrow(string name, datetime time, double price, color clr, int arrow)
  1000. {
  1001. if(ObjectFind(0, name) == -1)
  1002. {
  1003. ObjectCreate(0, name, OBJ_ARROW, 0, time, price);
  1004. ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
  1005. ObjectSetInteger(0, name, OBJPROP_WIDTH, 1);
  1006. ObjectSetInteger(0, name, OBJPROP_ARROWCODE, arrow);
  1007. }
  1008. }
  1009. //+------------------------------------------------------------------+
  1010. //| |
  1011. //+------------------------------------------------------------------+
  1012. void DrawVirtualBuyOpen(double openPrice)
  1013. {
  1014. datetime t = TimeCurrent();
  1015. string name = "VBUY_OPEN_" + IntegerToString(t);
  1016. // DrawArrow(name, t, openPrice, clrLime, 233);
  1017. ObjectCreate(0,name,OBJ_ARROW_BUY,0,TimeCurrent(),openPrice);
  1018. }
  1019. //+------------------------------------------------------------------+
  1020. //| |
  1021. //+------------------------------------------------------------------+
  1022. void DrawVirtualBuyClose(double closePrice)
  1023. {
  1024. datetime t = TimeCurrent();
  1025. string name = "VBUY_CLOSE_" + IntegerToString(t);
  1026. // DrawArrow(name, t, closePrice, clrRed, 234);
  1027. ObjectCreate(0,name,OBJ_ARROW_SELL,0,TimeCurrent(),closePrice);
  1028. }
  1029. //+------------------------------------------------------------------+
  1030. //| |
  1031. //+------------------------------------------------------------------+
  1032. void DrawDottedTrendline(string name, datetime time1, double price1, datetime time2, double price2, color clr)
  1033. {
  1034. // Delete if already exists
  1035. if(ObjectFind(0, name) != -1)
  1036. ObjectDelete(0, name);
  1037. // Create the trendline
  1038. if(!ObjectCreate(0, name, OBJ_TREND, 0, time1, price1, time2, price2))
  1039. Print("Error creating trendline: ", GetLastError());
  1040. // Set properties
  1041. ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
  1042. ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_DOT); // Dotted line
  1043. ObjectSetInteger(0, name, OBJPROP_WIDTH, 1); // Line width
  1044. }
  1045. //+------------------------------------------------------------------+
  1046. //| |
  1047. //+------------------------------------------------------------------+
  1048. int orderCount()
  1049. {
  1050. int index = -1;
  1051. if(GlobalVariableGet(global) == 0)
  1052. {
  1053. GlobalVariableSet(global,1);
  1054. int handle = FileOpen(filename, FILE_READ | FILE_COMMON);
  1055. if(handle == INVALID_HANDLE)
  1056. {
  1057. Print("EA=> Handler invalid: orderCount() ", filename," ",GetLastError()," eaType: ",eaType);
  1058. return -1;
  1059. }
  1060. while(!FileIsEnding(handle))
  1061. {
  1062. string line = FileReadString(handle);
  1063. // Skip empty line
  1064. if(StringLen(line) < 2)
  1065. continue;
  1066. string parts[];
  1067. int count = StringSplit(line, ',', parts);
  1068. int ticket = (int)StringToInteger(parts[0]);
  1069. int type = (int)StringToInteger(parts[1]);
  1070. int magic = (int)StringToInteger(parts[2]);
  1071. double open_price = StringToDouble(parts[3]);
  1072. double close_price = StringToDouble(parts[4]);
  1073. datetime open_time = StringToTime(parts[5]);
  1074. datetime close_time = StringToTime(parts[6]);
  1075. double sl = StringToDouble(parts[7]);
  1076. double tp = StringToDouble(parts[8]);
  1077. double lotS = StringToDouble(parts[9]);
  1078. double profit = StringToDouble(parts[10]);
  1079. index++;
  1080. }
  1081. FileClose(handle);
  1082. GlobalVariableSet(global,0);
  1083. }
  1084. return index;
  1085. }
  1086. //+------------------------------------------------------------------+
  1087. //| |
  1088. //+------------------------------------------------------------------+
  1089. int getTicketByNumber(int id,double & buySl,double & buyTp)
  1090. {
  1091. int index = -1;
  1092. if(GlobalVariableGet(global) == 0)
  1093. {
  1094. GlobalVariableSet(global,1);
  1095. int handle = FileOpen(filename, FILE_READ | FILE_COMMON);
  1096. if(handle == INVALID_HANDLE)
  1097. {
  1098. Print("EA=> Handler invalid: getTicketByNumber() ", filename," error: ",GetLastError()," eaType: ",eaType);
  1099. return 0;
  1100. }
  1101. while(!FileIsEnding(handle))
  1102. {
  1103. string line = FileReadString(handle);
  1104. // Skip empty line
  1105. if(StringLen(line) < 2)
  1106. continue;
  1107. string parts[];
  1108. int count = StringSplit(line, ',', parts);
  1109. int ticket = (int)StringToInteger(parts[0]);
  1110. int type = (int)StringToInteger(parts[1]);
  1111. int magic = (int)StringToInteger(parts[2]);
  1112. double open_price = StringToDouble(parts[3]);
  1113. double close_price = StringToDouble(parts[4]);
  1114. datetime open_time = StringToTime(parts[5]);
  1115. datetime close_time = StringToTime(parts[6]);
  1116. double sl = StringToDouble(parts[7]);
  1117. double tp = StringToDouble(parts[8]);
  1118. double lotS = StringToDouble(parts[9]);
  1119. double profit = StringToDouble(parts[10]);
  1120. if(id == ticket)
  1121. {
  1122. buySl = sl;
  1123. buyTp = tp;
  1124. index = id;
  1125. }
  1126. }
  1127. FileClose(handle);
  1128. GlobalVariableSet(global,0);
  1129. }
  1130. return index;
  1131. }
  1132. //+------------------------------------------------------------------+
  1133. //| |
  1134. //+------------------------------------------------------------------+
  1135. void deleteObjects()
  1136. {
  1137. int total = ObjectsTotal(0);
  1138. for(int i = total - 1; i >= 0; i--)
  1139. {
  1140. string name = ObjectName(0, i);
  1141. if(StringFind(name, "VBUY_OPEN_") >= 0 || StringFind(name, "VBUY_CLOSE_") >= 0 || StringFind(name, "Ticket ") >= 0)
  1142. {
  1143. ObjectDelete(0, name);
  1144. }
  1145. }
  1146. }
  1147. //+------------------------------------------------------------------+