Açıklama Yok

Pulse EA.mq5 92KB

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