Nessuna descrizione

Pulse_EA_project_MT5.mq5 92KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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. int filehandle = FileOpen(filename,FILE_WRITE|FILE_CSV|FILE_COMMON|FILE_END,",");
  632. if(filehandle != INVALID_HANDLE && GlobalVariableGet(global) == 0)
  633. {
  634. GlobalVariableSet(global,1);
  635. FileSeek(filehandle, 0, SEEK_END);
  636. FileWrite(filehandle,str);
  637. Print(str);
  638. FileClose(filehandle);
  639. GlobalVariableSet(global,0);
  640. }
  641. }
  642. //+------------------------------------------------------------------+
  643. //| |
  644. //+------------------------------------------------------------------+
  645. string MakeTradeString(int ticket, int type, int magic,
  646. double open_price, double close_price,
  647. datetime open_time, datetime close_time,
  648. double sl, double tp, double lotSize, double profit)
  649. {
  650. string str = StringFormat("%d,%d,%d,%.5f,%.5f,%s,%s,%.5f,%.5f,%.2f,%.2f",
  651. ticket,
  652. type,
  653. magic,
  654. open_price,
  655. close_price,
  656. TimeToString(open_time, TIME_DATE|TIME_SECONDS),
  657. TimeToString(close_time, TIME_DATE|TIME_SECONDS),
  658. sl,
  659. tp,
  660. lotSize,
  661. profit);
  662. return str;
  663. }
  664. //+------------------------------------------------------------------+
  665. //| |
  666. //+------------------------------------------------------------------+
  667. void setStart_EndTime(bool onInit,string start,string end,datetime & sessionStart,datetime & sessionEnd)
  668. {
  669. int newYorkStartHour = 0, newYorkStartMin = 0, newYorkEndHour = 0, newYorkEndMin = 0;
  670. datetime newYorkStartTrading,newYorkEndTrading;
  671. string time[];
  672. StringSplit(start,':',time);
  673. newYorkStartHour = (int)StringToInteger(time[0]);
  674. newYorkStartMin = (int)StringToInteger(time[1]);
  675. EventSetMillisecondTimer(500);
  676. time[0] = "";
  677. time[1] = "";
  678. StringSplit(end,':',time);
  679. newYorkEndHour = (int)StringToInteger(time[0]);
  680. newYorkEndMin = (int)StringToInteger(time[1]);
  681. // Print(" Start Time Hour: ",newYorkStartHour," Start Time Min: ",newYorkStartMin);
  682. // Print(" End Time Hour: ",newYorkEndHour," End Time Min: ",newYorkEndMin);
  683. datetime startDateTime;
  684. MqlDateTime st;
  685. TimeCurrent(st); // get current date
  686. st.hour = newYorkStartHour;
  687. st.min = newYorkStartMin;
  688. st.sec = 0;
  689. startDateTime = StructToTime(st);
  690. datetime endDateTime;
  691. MqlDateTime et;
  692. TimeCurrent(et); // get current date
  693. et.hour = newYorkEndHour;
  694. et.min = newYorkEndMin;
  695. et.sec = 0;
  696. endDateTime = StructToTime(et);
  697. MqlDateTime sdate,edate;
  698. datetime start_Time = 0, end_Time = 0;
  699. if(startDateTime > endDateTime)
  700. {
  701. if(onInit)
  702. {
  703. start_Time = iTime(Symbol(),PERIOD_D1,1);
  704. end_Time = iTime(Symbol(),PERIOD_D1,0);
  705. }
  706. else
  707. {
  708. start_Time = sessionStart;
  709. end_Time = sessionEnd;
  710. if(TimeCurrent() >= sessionEnd && sessionEnd != 0)
  711. {
  712. start_Time = iTime(Symbol(),PERIOD_D1,0);
  713. end_Time = start_Time + 86400;
  714. }
  715. }
  716. }
  717. else
  718. {
  719. start_Time = iTime(Symbol(),PERIOD_D1,0);
  720. end_Time = iTime(Symbol(),PERIOD_D1,0);
  721. }
  722. if(TimeToStruct(end_Time,edate))
  723. {
  724. edate.hour = newYorkEndHour;
  725. edate.min = newYorkEndMin;
  726. edate.sec = 0;
  727. }
  728. else
  729. Print("Error in Converting Time: ",GetLastError());
  730. newYorkEndTrading = StructToTime(edate);
  731. if(TimeToStruct(start_Time,sdate))
  732. {
  733. sdate.hour = newYorkStartHour;
  734. sdate.min = newYorkStartMin;
  735. sdate.sec = 0;
  736. }
  737. else
  738. Print("Error in Converting Time: ",GetLastError());
  739. newYorkStartTrading = StructToTime(sdate);
  740. sessionStart = newYorkStartTrading;
  741. sessionEnd = newYorkEndTrading;
  742. }
  743. //+------------------------------------------------------------------+
  744. //| |
  745. //+------------------------------------------------------------------+
  746. void setStart_EndTime_modify(bool onInit,string start,string end,datetime & sessionStart,datetime & sessionEnd)
  747. {
  748. int newYorkStartHour = 0, newYorkStartMin = 0, newYorkEndHour = 0, newYorkEndMin = 0;
  749. datetime newYorkStartTrading,newYorkEndTrading;
  750. string time[];
  751. StringSplit(start,':',time);
  752. newYorkStartHour = (int)StringToInteger(time[0]);
  753. newYorkStartMin = (int)StringToInteger(time[1]);
  754. time[0] = "";
  755. time[1] = "";
  756. StringSplit(end,':',time);
  757. newYorkEndHour = (int)StringToInteger(time[0]);
  758. newYorkEndMin = (int)StringToInteger(time[1]);
  759. // Print(" Start Time Hour: ",newYorkStartHour," Start Time Min: ",newYorkStartMin);
  760. // Print(" End Time Hour: ",newYorkEndHour," End Time Min: ",newYorkEndMin);
  761. datetime startDateTime;
  762. MqlDateTime st;
  763. TimeCurrent(st); // get current date
  764. st.hour = newYorkStartHour;
  765. st.min = newYorkStartMin;
  766. st.sec = 0;
  767. startDateTime = StructToTime(st);
  768. datetime endDateTime;
  769. MqlDateTime et;
  770. TimeCurrent(et); // get current date
  771. et.hour = newYorkEndHour;
  772. et.min = newYorkEndMin;
  773. et.sec = 0;
  774. endDateTime = StructToTime(et);
  775. MqlDateTime sdate,edate;
  776. datetime start_Time = 0, end_Time = 0;
  777. if(startDateTime > endDateTime)
  778. {
  779. start_Time = iTime(Symbol(),PERIOD_D1,1);
  780. end_Time = iTime(Symbol(),PERIOD_D1,0);
  781. }
  782. else
  783. {
  784. start_Time = iTime(Symbol(),PERIOD_D1,0);
  785. end_Time = iTime(Symbol(),PERIOD_D1,0);
  786. }
  787. if(TimeToStruct(end_Time,edate))
  788. {
  789. edate.hour = newYorkEndHour;
  790. edate.min = newYorkEndMin;
  791. edate.sec = 0;
  792. }
  793. else
  794. Print("Error in Converting Time: ",GetLastError());
  795. newYorkEndTrading = StructToTime(edate);
  796. if(TimeToStruct(start_Time,sdate))
  797. {
  798. sdate.hour = newYorkStartHour;
  799. sdate.min = newYorkStartMin;
  800. sdate.sec = 0;
  801. }
  802. else
  803. Print("Error in Converting Time: ",GetLastError());
  804. newYorkStartTrading = StructToTime(sdate);
  805. sessionStart = newYorkStartTrading;
  806. sessionEnd = newYorkEndTrading;
  807. }
  808. //+------------------------------------------------------------------+
  809. //| Check Virtual TP/SL for all positions |
  810. //+------------------------------------------------------------------+
  811. void CheckVirtualGlobalTpSl()
  812. {
  813. double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
  814. double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
  815. for(int i=PositionsTotal()-1; i >=0 ; i--)
  816. {
  817. ulong ticket = PositionGetTicket(i);
  818. if(PositionSelectByTicket(ticket))
  819. {
  820. if(PositionGetInteger(POSITION_MAGIC) == magicNo &&
  821. PositionGetString(POSITION_SYMBOL) == _Symbol)
  822. {
  823. if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
  824. {
  825. double tp_price = PositionGetDouble(POSITION_PRICE_OPEN) + glTp * 10 * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
  826. double sl_price = PositionGetDouble(POSITION_PRICE_OPEN) - glSl * 10 * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
  827. if(bid >= tp_price || bid <= sl_price)
  828. {
  829. closeAllActiveOrders();
  830. return;
  831. }
  832. }
  833. if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
  834. {
  835. double tp_price = PositionGetDouble(POSITION_PRICE_OPEN) - glTp * 10 * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
  836. double sl_price = PositionGetDouble(POSITION_PRICE_OPEN) + glSl * 10 * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
  837. if(ask <= tp_price || ask >= sl_price)
  838. {
  839. closeAllActiveOrders();
  840. return;
  841. }
  842. }
  843. }
  844. }
  845. }
  846. }
  847. //+------------------------------------------------------------------+
  848. //| |
  849. //+------------------------------------------------------------------+
  850. void CheckDailyDrawdown()
  851. {
  852. if(!UseDailyDrawdownLimit)
  853. return;
  854. double equity = AccountInfoDouble(ACCOUNT_EQUITY);
  855. double drawdown = g_dailyStartBalance - equity;
  856. if(drawdown >= DailyDrawdownAmount)
  857. {
  858. Print("Daily Drawdown Limit Hit: ", drawdown, " >= ", DailyDrawdownAmount);
  859. closeAllActiveOrders();
  860. }
  861. }
  862. //+------------------------------------------------------------------+
  863. //| |
  864. //+------------------------------------------------------------------+
  865. void markClosedOrder(datetime eaTime)
  866. {
  867. HistorySelect(eaTime, TimeCurrent());
  868. for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
  869. {
  870. ulong dealTicket = HistoryDealGetTicket(i);
  871. long dealMagic = HistoryDealGetInteger(dealTicket, DEAL_MAGIC);
  872. if(dealMagic != magicNo && dealMagic != 0)
  873. continue;
  874. if(HistoryDealGetString(dealTicket, DEAL_SYMBOL) == Symbol())
  875. {
  876. //Print("Condition 2 ",HistoryDealGetInteger(dealTicket, DEAL_ENTRY)," HistoryDealsTotal(): ",HistoryDealsTotal());
  877. if(HistoryDealGetInteger(dealTicket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
  878. {
  879. //Print("deal out close: ",dealTicket);
  880. long dealType = HistoryDealGetInteger(dealTicket, DEAL_TYPE);
  881. if(dealType != DEAL_TYPE_BUY && dealType != DEAL_TYPE_SELL)
  882. continue;
  883. double profit = HistoryDealGetDouble(dealTicket, DEAL_PROFIT);
  884. profit = NormalizeDouble(profit, 2);
  885. string lineName = "TradeLine_" + (string)dealTicket;
  886. if(ObjectFind(0, lineName) >= 0)
  887. continue;
  888. ulong entryDeal = findEntryDeal(dealTicket);
  889. if(entryDeal == 0)
  890. continue;
  891. datetime openTime = (datetime)HistoryDealGetInteger(entryDeal, DEAL_TIME);
  892. double openPrice = HistoryDealGetDouble(entryDeal, DEAL_PRICE);
  893. datetime closeTime = (datetime)HistoryDealGetInteger(dealTicket, DEAL_TIME);
  894. double closePrice = HistoryDealGetDouble(dealTicket, DEAL_PRICE);
  895. createTrendLine(lineName, openTime, openPrice, closeTime, closePrice, dealType, profit);
  896. PrintFormat("Trend line created for closed trade #%I64u | Profit: %.2f", dealTicket, profit);
  897. }
  898. }
  899. }
  900. }
  901. //+------------------------------------------------------------------+
  902. //| |
  903. //+------------------------------------------------------------------+
  904. void createTrendLine(string name,
  905. datetime openTime, double openPrice,
  906. datetime closeTime, double closePrice,
  907. long dealType, double profit)
  908. {
  909. if(ObjectFind(0, name) >= 0)
  910. return;
  911. if(!ObjectCreate(0, name, OBJ_TREND, 0, openTime, openPrice, closeTime, closePrice))
  912. {
  913. Print("Error creating trend line: ", GetLastError());
  914. return;
  915. }
  916. ObjectSetInteger(0, name, OBJPROP_WIDTH, tradeLineThickness);
  917. ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_SOLID);
  918. ObjectSetInteger(0, name, OBJPROP_RAY, false);
  919. color lineColor = (dealType == DEAL_TYPE_BUY) ? clrRed : clrBlue;
  920. ObjectSetInteger(0, name, OBJPROP_COLOR, lineColor);
  921. createProfitLabel(name + "_label", openTime, openPrice, closeTime, closePrice, dealType, profit);
  922. }
  923. //+------------------------------------------------------------------+
  924. //| |
  925. //+------------------------------------------------------------------+
  926. void createProfitLabel(string name,
  927. datetime openTime, double openPrice,
  928. datetime closeTime, double closePrice,
  929. long dealType, double profit)
  930. {
  931. if(ObjectFind(0, name) >= 0)
  932. return;
  933. datetime middleTime = openTime + (closeTime - openTime) / 2;
  934. double midPrice = (openPrice + closePrice) / 2;
  935. if(!ObjectCreate(0, name, OBJ_TEXT, 0, middleTime, midPrice))
  936. {
  937. Print("Error creating profit label: ", GetLastError());
  938. return;
  939. }
  940. string profitText = "$" + DoubleToString(profit, 2);
  941. ObjectSetString(0, name, OBJPROP_TEXT, profitText);
  942. ObjectSetInteger(0, name, OBJPROP_FONTSIZE, dollarFontSize);
  943. ObjectSetInteger(0, name, OBJPROP_COLOR, dollarFontColor);
  944. ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_TOP);
  945. ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
  946. }
  947. //+------------------------------------------------------------------+
  948. //| |
  949. //+------------------------------------------------------------------+
  950. ulong findEntryDeal(ulong closeDeal)
  951. {
  952. string symbol = HistoryDealGetString(closeDeal, DEAL_SYMBOL);
  953. long positionID = HistoryDealGetInteger(closeDeal, DEAL_POSITION_ID);
  954. int dealOutCount = 0;
  955. for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
  956. {
  957. ulong deal = HistoryDealGetTicket(i);
  958. if(HistoryDealGetInteger(deal, DEAL_POSITION_ID) == positionID)
  959. {
  960. if(HistoryDealGetInteger(deal, DEAL_ENTRY) == DEAL_ENTRY_OUT)
  961. {
  962. dealOutCount++;
  963. }
  964. }
  965. }
  966. if(dealOutCount < 2)
  967. {
  968. for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
  969. {
  970. ulong deal = HistoryDealGetTicket(i);
  971. if(HistoryDealGetInteger(deal, DEAL_POSITION_ID) == positionID &&
  972. HistoryDealGetInteger(deal, DEAL_ENTRY) == DEAL_ENTRY_IN &&
  973. HistoryDealGetString(deal, DEAL_SYMBOL) == symbol)
  974. return deal;
  975. //Print("Deal: ", deal);
  976. }
  977. }
  978. else
  979. {
  980. for(int i = 0; i < HistoryDealsTotal(); i++)
  981. {
  982. ulong deal = HistoryDealGetTicket(i);
  983. if(HistoryDealGetInteger(deal, DEAL_POSITION_ID) == positionID &&
  984. HistoryDealGetInteger(deal, DEAL_ENTRY) == DEAL_ENTRY_OUT)
  985. {
  986. return deal;
  987. }
  988. //Print("Deal: ", deal);
  989. }
  990. }
  991. return 0;
  992. }
  993. //+------------------------------------------------------------------+
  994. //| |
  995. //+------------------------------------------------------------------+
  996. void DrawArrow(string name, datetime time, double price, color clr, int arrow)
  997. {
  998. if(ObjectFind(0, name) == -1)
  999. {
  1000. ObjectCreate(0, name, OBJ_ARROW, 0, time, price);
  1001. ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
  1002. ObjectSetInteger(0, name, OBJPROP_WIDTH, 1);
  1003. ObjectSetInteger(0, name, OBJPROP_ARROWCODE, arrow);
  1004. }
  1005. }
  1006. //+------------------------------------------------------------------+
  1007. //| |
  1008. //+------------------------------------------------------------------+
  1009. void DrawVirtualBuyOpen(double openPrice)
  1010. {
  1011. datetime t = TimeCurrent();
  1012. string name = "VBUY_OPEN_" + IntegerToString(t);
  1013. // DrawArrow(name, t, openPrice, clrLime, 233);
  1014. ObjectCreate(0,name,OBJ_ARROW_BUY,0,TimeCurrent(),openPrice);
  1015. }
  1016. //+------------------------------------------------------------------+
  1017. //| |
  1018. //+------------------------------------------------------------------+
  1019. void DrawVirtualBuyClose(double closePrice)
  1020. {
  1021. datetime t = TimeCurrent();
  1022. string name = "VBUY_CLOSE_" + IntegerToString(t);
  1023. // DrawArrow(name, t, closePrice, clrRed, 234);
  1024. ObjectCreate(0,name,OBJ_ARROW_SELL,0,TimeCurrent(),closePrice);
  1025. }
  1026. //+------------------------------------------------------------------+
  1027. //| |
  1028. //+------------------------------------------------------------------+
  1029. void DrawDottedTrendline(string name, datetime time1, double price1, datetime time2, double price2, color clr)
  1030. {
  1031. // Delete if already exists
  1032. if(ObjectFind(0, name) != -1)
  1033. ObjectDelete(0, name);
  1034. // Create the trendline
  1035. if(!ObjectCreate(0, name, OBJ_TREND, 0, time1, price1, time2, price2))
  1036. Print("Error creating trendline: ", GetLastError());
  1037. // Set properties
  1038. ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
  1039. ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_DOT); // Dotted line
  1040. ObjectSetInteger(0, name, OBJPROP_WIDTH, 1); // Line width
  1041. }
  1042. //+------------------------------------------------------------------+
  1043. //| |
  1044. //+------------------------------------------------------------------+
  1045. int orderCount()
  1046. {
  1047. int index = -1;
  1048. if(GlobalVariableGet(global) == 0)
  1049. {
  1050. int handle = FileOpen(filename, FILE_READ | FILE_COMMON);
  1051. if(handle == INVALID_HANDLE)
  1052. {
  1053. Print("EA=> Handler invalid: orderCount() ", filename," ",GetLastError()," eaType: ",eaType);
  1054. return -1;
  1055. }
  1056. while(!FileIsEnding(handle))
  1057. {
  1058. string line = FileReadString(handle);
  1059. // Skip empty line
  1060. if(StringLen(line) < 2)
  1061. continue;
  1062. string parts[];
  1063. int count = StringSplit(line, ',', parts);
  1064. int ticket = (int)StringToInteger(parts[0]);
  1065. int type = (int)StringToInteger(parts[1]);
  1066. int magic = (int)StringToInteger(parts[2]);
  1067. double open_price = StringToDouble(parts[3]);
  1068. double close_price = StringToDouble(parts[4]);
  1069. datetime open_time = StringToTime(parts[5]);
  1070. datetime close_time = StringToTime(parts[6]);
  1071. double sl = StringToDouble(parts[7]);
  1072. double tp = StringToDouble(parts[8]);
  1073. double lotS = StringToDouble(parts[9]);
  1074. double profit = StringToDouble(parts[10]);
  1075. index++;
  1076. }
  1077. FileClose(handle);
  1078. }
  1079. return index;
  1080. }
  1081. //+------------------------------------------------------------------+
  1082. //| |
  1083. //+------------------------------------------------------------------+
  1084. int getTicketByNumber(int id,double & buySl,double & buyTp)
  1085. {
  1086. int index = -1;
  1087. if(GlobalVariableGet(global) == 0)
  1088. {
  1089. int handle = FileOpen(filename, FILE_READ | FILE_COMMON);
  1090. if(handle == INVALID_HANDLE)
  1091. {
  1092. Print("EA=> Handler invalid: getTicketByNumber() ", filename," error: ",GetLastError()," eaType: ",eaType);
  1093. return 0;
  1094. }
  1095. while(!FileIsEnding(handle))
  1096. {
  1097. string line = FileReadString(handle);
  1098. // Skip empty line
  1099. if(StringLen(line) < 2)
  1100. continue;
  1101. string parts[];
  1102. int count = StringSplit(line, ',', parts);
  1103. int ticket = (int)StringToInteger(parts[0]);
  1104. int type = (int)StringToInteger(parts[1]);
  1105. int magic = (int)StringToInteger(parts[2]);
  1106. double open_price = StringToDouble(parts[3]);
  1107. double close_price = StringToDouble(parts[4]);
  1108. datetime open_time = StringToTime(parts[5]);
  1109. datetime close_time = StringToTime(parts[6]);
  1110. double sl = StringToDouble(parts[7]);
  1111. double tp = StringToDouble(parts[8]);
  1112. double lotS = StringToDouble(parts[9]);
  1113. double profit = StringToDouble(parts[10]);
  1114. if(id == ticket)
  1115. {
  1116. buySl = sl;
  1117. buyTp = tp;
  1118. index = id;
  1119. }
  1120. }
  1121. FileClose(handle);
  1122. }
  1123. return index;
  1124. }
  1125. //+------------------------------------------------------------------+
  1126. //| |
  1127. //+------------------------------------------------------------------+
  1128. void deleteObjects()
  1129. {
  1130. int total = ObjectsTotal(0);
  1131. for(int i = total - 1; i >= 0; i--)
  1132. {
  1133. string name = ObjectName(0, i);
  1134. if(StringFind(name, "VBUY_OPEN_") >= 0 || StringFind(name, "VBUY_CLOSE_") >= 0 || StringFind(name, "Ticket ") >= 0)
  1135. {
  1136. ObjectDelete(0, name);
  1137. }
  1138. }
  1139. }
  1140. //+------------------------------------------------------------------+