説明なし

line_level_ea.mq5 54KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. //+------------------------------------------------------------------+
  2. //| line_level_ea.mq5 |
  3. //| Copyright 2025, MQL Development |
  4. //| https://www.mqldevelopment.com/ |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2025, MQL Development"
  7. #property link "https://www.mqldevelopment.com/"
  8. #property version "1.1"
  9. #include <Trade\Trade.mqh>
  10. CTrade trade;
  11. enum tradeType
  12. {
  13. buyTrades, // Buy
  14. sellTrades, // Sell
  15. };
  16. enum NewsCloseOrder
  17. {
  18. CloseAllRunningOrder=0,
  19. LetTheOrderRun=1,
  20. };
  21. enum selectLine
  22. {
  23. LineOnNewsBar =0,
  24. LineOnNewsStop=1
  25. };
  26. #import "line_level_news_filter.ex5"
  27. //+------------------------------------------------------------------+
  28. //| |
  29. //+------------------------------------------------------------------+
  30. void initiateNews();
  31. int returnNewsStatus(bool High_Impact_News=true
  32. ,int High_Start_Time=60//Stop Trade before high News (min)
  33. ,int High_Stop_Time=15 //Stop Trade after high News (min)
  34. ,bool show_high_line=true//Show verticle Line when high news comes
  35. ,bool Medium_Impact_News=true
  36. ,int Medium_Start_Time=60//Stop Trade before medium News (min)
  37. ,int Medium_Stop_Time=15 //Stop Trade after medium News (min)
  38. ,bool show_medium_line=true//Show verticle Line when medium news comes
  39. ,bool Low_Impact_News=true
  40. ,int Low_Start_Time=60//Stop Trade before low News (min)
  41. ,int Low_Stop_Time=15 //Stop Trade after low News (min)
  42. ,bool show_low_line=true//Show verticle Line when low news comes
  43. ,string symbol=""
  44. ,string expert=""
  45. ,int GMT_Broker_Time=2
  46. ,selectLine sl=0
  47. ,string extraSymbol=""
  48. );
  49. #import
  50. input string string_0 = "<><><><><><> General SETTINGS <><><><><><>"; //__
  51. input int magic_no = 333; // Magic no
  52. input tradeType selectSide = buyTrades; // Select Side
  53. input double lot_size = 0.1; // Lot Size
  54. input double lot_multiplier = 2; // Lot Multiplier on Loss Trade
  55. input double levels_distence = 10; // Levels Distance in Dollars
  56. input double stoploss = 10; // Fixed Stop Loss in Pips
  57. input double takeprofit = 10; // Fixed Take Profit in Pips
  58. input bool countinueCycleAfterProfit = true; // Continue Trading After Profit
  59. input bool notOpenTradeOnSame = false; // Don't open trade on same
  60. input string time_setting = "<><><><><> Time Filter Settings <><><><><>"; //_
  61. input bool EnableTimeFilter = false; // Enable Time Filter
  62. input string startTime = "03:00"; // Start Time Session
  63. input string endTime = "09:00"; // End Time Session
  64. input string news = "<><><><><><> News Settings <><><><><><>"; // News
  65. input NewsCloseOrder newsClose = CloseAllRunningOrder; // On News Action on Running Orders
  66. input bool High_Impact_News = true; //High Impact News
  67. input int High_Start_Time = 60; //Stop Trade before high News (min)
  68. input int High_Stop_Time = 15; //Stop Trade after high News (min)
  69. input bool show_high_line = true; //Show verticle Line when high news comes
  70. input bool Medium_Impact_News = true; //Medium Impact News
  71. input int Medium_Start_Time = 60; //Stop Trade before medium News (min)
  72. input int Medium_Stop_Time = 15; //Stop Trade after medium News (min)
  73. input bool show_medium_line = true; //Show verticle Line when medium news comes
  74. input bool Low_Impact_News = true; //Low Impact News
  75. input int Low_Start_Time = 60; //Stop Trade before low News (min)
  76. input int Low_Stop_Time = 15; //Stop Trade after low News (min)
  77. input bool show_low_line = true; //Show verticle Line when low news comes
  78. input selectLine Select_News_Line = 0; //News Line
  79. input bool mobileAlert = true; //Mobile Alert
  80. int GMT_Broker_Time = +2; //GMT_Broker_Time Time of your Broker
  81. // Global Variables
  82. double above_level = 0, below_level = 0;
  83. static double tickCurrentBid = 0, tickCurrentAsk = 0;
  84. double tickPreviousBid = 0, tickPreviousAsk = 0;
  85. datetime startTradingTime = 0, endTradingTime = 0;
  86. string sep = ":"; // A separator as a character
  87. ushort u_sep; // The code of the separator character
  88. string result1[];
  89. datetime ea_start_time = 0;
  90. bool tradeNow = true;
  91. bool printingDisplay = true;
  92. string Lname="newsLabel3";
  93. double lastOrderPrice = 0;
  94. //+------------------------------------------------------------------+
  95. //| Expert initialization function |
  96. //+------------------------------------------------------------------+
  97. int OnInit()
  98. {
  99. //---
  100. // news
  101. int timeDifference = (int)TimeCurrent() - (int)TimeGMT();
  102. Print("Time Difference is: ", timeDifference);
  103. if(timeDifference > 0)
  104. {
  105. GMT_Broker_Time = +timeDifference/3600;
  106. }
  107. else
  108. if(timeDifference < 0)
  109. {
  110. GMT_Broker_Time = timeDifference/3600;
  111. }
  112. else
  113. if(timeDifference == 0)
  114. {
  115. GMT_Broker_Time = timeDifference;
  116. }
  117. Print("Gmt Time: ", TimeGMT(), " Broker Gmt Time: ", GMT_Broker_Time);
  118. if(!MQLInfoInteger(MQL_TESTER))
  119. {
  120. initiateNews();
  121. }
  122. // Fill Values above and below levels
  123. trade.SetExpertMagicNumber(magic_no);
  124. trade.SetDeviationInPoints(10);
  125. trade.SetTypeFilling(ORDER_FILLING_IOC);
  126. trade.LogLevel(LOG_LEVEL_ALL);
  127. trade.SetAsyncMode(false);
  128. ea_start_time = TimeCurrent();
  129. double currentPrice = iClose(Symbol(), PERIOD_CURRENT, 0);
  130. getNearestLevels(currentPrice, levels_distence, below_level, above_level);
  131. //---
  132. return(INIT_SUCCEEDED);
  133. }
  134. //+------------------------------------------------------------------+
  135. //| Expert deinitialization function |
  136. //+------------------------------------------------------------------+
  137. void OnDeinit(const int reason)
  138. {
  139. //---
  140. ObjectsDeleteAll(0, -1, OBJ_HLINE);
  141. }
  142. //+------------------------------------------------------------------+
  143. //| Main Activity function |
  144. //+------------------------------------------------------------------+
  145. void mainActivity()
  146. {
  147. //---
  148. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  149. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  150. tickPreviousBid = tickCurrentBid;
  151. tickCurrentBid = Bid;
  152. tickPreviousAsk = tickCurrentAsk;
  153. tickCurrentAsk = Ask;
  154. timeConversion();
  155. // Comment(" Below Value is: ", below_level, " Above Value: ", above_level, " Previous Tick: ", tickPreviousAsk, " Tick Current Ask: ", tickCurrentAsk);
  156. double lastLot = 0;
  157. if(!countinueCycleAfterProfit)
  158. if(selectLatestTicket(lastLot) > 0)
  159. {
  160. tradeNow = false;
  161. }
  162. // Comment(" Trades Count: ", orderCount(), " Below Value is: ", below_level, " Above Value: ", above_level);
  163. if(orderCount() == 0)
  164. {
  165. double latestClosePrice = getClosePriceByTicket();
  166. if(latestClosePrice > 0)
  167. {
  168. getNearestLevels(latestClosePrice, levels_distence, below_level, above_level);
  169. if(printingDisplay)
  170. {
  171. Print(" ---------------------------------- Resetting Level no trade open. Setting new Levels using last ticket close price: ", latestClosePrice, " ------------------------------------------------ ");
  172. Print(" Current Price: ", latestClosePrice, " Below Value is: ", below_level, " Above Value: ", above_level);
  173. printingDisplay = false;
  174. }
  175. }
  176. }
  177. else
  178. {
  179. printingDisplay = true;
  180. }
  181. if(tradeNow)
  182. if((EnableTimeFilter && TimeCurrent() >= startTradingTime && TimeCurrent() < endTradingTime) || !EnableTimeFilter)
  183. {
  184. if(above_level == 0 && below_level == 0)
  185. {
  186. double currentPrice = iClose(Symbol(), PERIOD_CURRENT, 0);
  187. getNearestLevels(currentPrice, levels_distence, below_level, above_level);
  188. Print(" Price Levels Updated on New Session Start. ");
  189. }
  190. if(selectSide == buyTrades) // Buy trade case
  191. {
  192. if(((tickPreviousAsk < above_level) && (tickCurrentAsk >= above_level)) ||
  193. ((tickPreviousAsk > below_level) && (tickCurrentAsk <= below_level)))
  194. {
  195. double currentPrice = 0;
  196. if((tickPreviousAsk < above_level) && (tickCurrentAsk >= above_level))
  197. {
  198. currentPrice = above_level;
  199. }
  200. else
  201. if(((tickPreviousAsk > below_level) && (tickCurrentAsk <= below_level)))
  202. {
  203. currentPrice = below_level;
  204. }
  205. if((notOpenTradeOnSame && lastOrderPrice != currentPrice) || !notOpenTradeOnSame)
  206. {
  207. lastOrderPrice = currentPrice;
  208. placeBuyTrade();
  209. }
  210. else
  211. {
  212. Print(" Buy Current Price is: ", currentPrice, " Last Order Price: ", lastOrderPrice);
  213. }
  214. Print(" ----------------------------- Buy Trade Executed and Levels Updated -----------------------------------------------------");
  215. getNearestLevels(currentPrice, levels_distence, below_level, above_level);
  216. Print(" Current Price: ", currentPrice, " Below Value is: ", below_level, " Above Value: ", above_level);
  217. }
  218. }
  219. if(selectSide == sellTrades)
  220. {
  221. double currentPrice = 0;
  222. if((tickPreviousBid < above_level) && (tickCurrentBid >= above_level))
  223. {
  224. currentPrice = above_level;
  225. }
  226. else
  227. if(((tickPreviousBid > below_level) && (tickCurrentBid <= below_level)))
  228. {
  229. currentPrice = below_level;
  230. }
  231. if(((tickPreviousBid < above_level) && (tickCurrentBid >= above_level)) ||
  232. ((tickPreviousBid > below_level) && (tickCurrentBid <= below_level)))
  233. {
  234. if((notOpenTradeOnSame && lastOrderPrice != currentPrice) || !notOpenTradeOnSame)
  235. {
  236. lastOrderPrice = currentPrice;
  237. placeSellTrade();
  238. }
  239. else
  240. {
  241. Print(" Sell Current Price is: ", currentPrice, " Last Order Price: ", lastOrderPrice);
  242. }
  243. Print(" ----------------------------- Sell Trade Executed and Levels Updated -----------------------------------------------------");
  244. getNearestLevels(currentPrice, levels_distence, below_level, above_level);
  245. Print(" Current Price: ", currentPrice, " Below Value: ", below_level, " Above Value: ", above_level);
  246. }
  247. }
  248. }
  249. else
  250. {
  251. below_level = 0;
  252. above_level = 0;
  253. }
  254. }
  255. //+------------------------------------------------------------------+
  256. //| Expert tick function |
  257. //+------------------------------------------------------------------+
  258. void OnTick()
  259. {
  260. if(!MQLInfoInteger(MQL_TESTER))
  261. {
  262. static int status=-1,preStatus=-1;
  263. status=returnNewsStatus(High_Impact_News
  264. ,High_Start_Time
  265. ,High_Stop_Time
  266. ,show_high_line
  267. ,Medium_Impact_News
  268. ,Medium_Start_Time
  269. ,Medium_Stop_Time
  270. ,show_medium_line
  271. ,Low_Impact_News
  272. ,Low_Start_Time
  273. ,Low_Stop_Time
  274. ,show_low_line
  275. ,Symbol()
  276. ,MQLInfoString(MQL_PROGRAM_NAME)
  277. ,GMT_Broker_Time
  278. ,Select_News_Line
  279. ,""
  280. );
  281. if(status==0)
  282. {
  283. mainActivity();
  284. }
  285. if(status==1 || status==2 || status==3)
  286. {
  287. if(newsClose==0)
  288. {
  289. if(orderCount_1(POSITION_TYPE_BUY,magic_no)>0)
  290. closeTrades(POSITION_TYPE_BUY,magic_no);
  291. if(orderCount_1(POSITION_TYPE_SELL,magic_no)>0)
  292. closeTrades(POSITION_TYPE_SELL,magic_no);
  293. }
  294. else
  295. if(newsClose==1)
  296. {
  297. mainActivity();
  298. }
  299. }
  300. if(status!=preStatus)
  301. {
  302. if(status==0 && preStatus!=-1)
  303. {
  304. {
  305. ObjectSetString(0,Lname,OBJPROP_TEXT,"");
  306. }
  307. Alert("Trading is start");
  308. if(mobileAlert)
  309. SendNotification("Trading is start");
  310. preStatus=status;
  311. }
  312. else
  313. if(status==1)
  314. {
  315. ObjectSetInteger(0,Lname,OBJPROP_COLOR,clrRed);
  316. ObjectSetString(0,Lname,OBJPROP_TEXT,"High Impact News");
  317. Alert("Trading Stop due to high impact news");
  318. if(mobileAlert)
  319. SendNotification("Trading Stop due to high impact news");
  320. preStatus=status;
  321. }
  322. else
  323. if(status==2)
  324. {
  325. ObjectSetInteger(0,Lname,OBJPROP_COLOR,clrBlue);
  326. ObjectSetString(0,Lname,OBJPROP_TEXT,"Medium Impact News");
  327. Alert("Trading Stop due to medium impact news");
  328. if(mobileAlert)
  329. SendNotification("Trading Stop due to medium impact news");
  330. preStatus=status;
  331. }
  332. else
  333. if(status==3)
  334. {
  335. ObjectSetInteger(0,Lname,OBJPROP_COLOR,clrGreen);
  336. ObjectSetString(0,Lname,OBJPROP_TEXT,"Low Impact News");
  337. Alert("Trading Stop due to low impact news");
  338. if(mobileAlert)
  339. SendNotification("Trading Stop due to low impact news");
  340. preStatus=status;
  341. }
  342. }
  343. }
  344. else
  345. {
  346. mainActivity();
  347. }
  348. }
  349. //+------------------------------------------------------------------+
  350. //| Calculate Base Level Function |
  351. //+------------------------------------------------------------------+
  352. double calculateBaseLevel(double price, double step)
  353. {
  354. return step * MathFloor(price / step);
  355. }
  356. //+------------------------------------------------------------------+
  357. //| Get Nearest Levels Function |
  358. //+------------------------------------------------------------------+
  359. void getNearestLevels(double currentPrice, double step,
  360. double &LowerLevel, double &UpperLevel)
  361. {
  362. double epsilon = 0.000001;
  363. // Compute the anchor as the nearest multiple of step.
  364. double anchor = MathRound(currentPrice / step) * step;
  365. double diff = fabs(currentPrice - anchor);
  366. // If currentPrice exactly equals the anchor, use symmetric levels.
  367. if(diff < epsilon)
  368. {
  369. LowerLevel = anchor - step;
  370. UpperLevel = anchor + step;
  371. return;
  372. }
  373. // Special handling for step == 10: use non–symmetric levels.
  374. if(step == 10)
  375. {
  376. if(currentPrice > anchor)
  377. {
  378. LowerLevel = anchor;
  379. UpperLevel = anchor + step;
  380. }
  381. else // currentPrice < anchor
  382. {
  383. LowerLevel = anchor - step;
  384. UpperLevel = anchor;
  385. }
  386. return;
  387. }
  388. // For all other steps, use the ratio threshold logic.
  389. double ratio = diff / step;
  390. double threshold = 0.45; // Adjusted threshold
  391. if(currentPrice > anchor)
  392. {
  393. if(ratio > threshold)
  394. {
  395. LowerLevel = anchor - step;
  396. UpperLevel = anchor + step;
  397. }
  398. else
  399. {
  400. LowerLevel = anchor;
  401. UpperLevel = anchor + step;
  402. }
  403. }
  404. else // currentPrice < anchor
  405. {
  406. if(ratio > threshold)
  407. {
  408. LowerLevel = anchor - step;
  409. UpperLevel = anchor + step;
  410. }
  411. else
  412. {
  413. LowerLevel = anchor - step;
  414. UpperLevel = anchor;
  415. }
  416. }
  417. }
  418. //+------------------------------------------------------------------+
  419. //| Place Buy Trade Function |
  420. //+------------------------------------------------------------------+
  421. void placeBuyTrade()
  422. {
  423. double buySL = 0, buyTp=0;
  424. //openPrice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  425. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  426. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  427. if(stoploss != 0)
  428. {
  429. buySL = Ask - (stoploss * 10 * Point());
  430. }
  431. if(takeprofit != 0)
  432. {
  433. buyTp = Ask + (takeprofit * 10 * Point());
  434. }
  435. double lot = 0;
  436. double lastLot = 0;
  437. if(selectLatestTicket(lastLot) < 0)
  438. {
  439. lot = lastLot * lot_multiplier;
  440. }
  441. else
  442. {
  443. lot = lot_size;
  444. }
  445. if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,NormalizeDouble(lot, 2),Ask,buySL,buyTp,"Buy Trade Placed"))
  446. {
  447. Print("Buy Trade Placed: ", trade.ResultOrder());
  448. }
  449. else
  450. {
  451. Print("Error in placing Buy: "+Symbol()+" ",GetLastError());
  452. }
  453. }
  454. //+------------------------------------------------------------------+
  455. //| Place Sell Trade Function |
  456. //+------------------------------------------------------------------+
  457. void placeSellTrade()
  458. {
  459. double sellSL = 0, sellTp = 0;
  460. //openPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  461. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  462. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  463. if(stoploss != 0)
  464. {
  465. sellSL = Bid + (stoploss * 10 * Point());
  466. }
  467. if(takeprofit != 0)
  468. {
  469. sellTp = Bid - (takeprofit * 10 * Point());
  470. }
  471. double lot = 0;
  472. double lastLot = 0;
  473. if(selectLatestTicket(lastLot) < 0)
  474. {
  475. lot = lastLot * lot_multiplier;
  476. }
  477. else
  478. {
  479. lot = lot_size;
  480. }
  481. if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,NormalizeDouble(lot, 2),Bid,sellSL,sellTp,"Sell Trade Placed"))
  482. {
  483. Print("Sell Trade PLaced: ", trade.ResultOrder());
  484. }
  485. else
  486. {
  487. Print("Error in placing Sell: "+Symbol()+" ",GetLastError());
  488. }
  489. }
  490. //+------------------------------------------------------------------+
  491. //| Time Conversion Function |
  492. //+------------------------------------------------------------------+
  493. void timeConversion()
  494. {
  495. MqlDateTime date, date1;
  496. TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date);
  497. u_sep=StringGetCharacter(sep,0);
  498. StringSplit(startTime,u_sep,result1);
  499. date.hour = (int)StringToInteger(result1[0]);
  500. date.min = (int)StringToInteger(result1[1]);
  501. startTradingTime = StructToTime(date);
  502. TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date1);
  503. StringSplit(endTime,u_sep,result1);
  504. date.hour = (int)StringToInteger(result1[0]);
  505. date.min = (int)StringToInteger(result1[1]);
  506. endTradingTime = StructToTime(date);
  507. }
  508. //+------------------------------------------------------------------+
  509. //| Select Latest Ticket Function |
  510. //+------------------------------------------------------------------+
  511. double selectLatestTicket(double &lastOrderLot)
  512. {
  513. int count = 0;
  514. ulong ticket_deal_Out=0, ticket_deal_In = 0;
  515. datetime latestCloseTime = 0;
  516. double orderProfit = 0;
  517. ulong latestTicket = 0;
  518. if(HistorySelect(ea_start_time, TimeCurrent()))
  519. {
  520. int total = HistoryDealsTotal();
  521. for(int i = total-1; i >= 0 ; i--)
  522. {
  523. ticket_deal_Out = HistoryDealGetTicket(i);
  524. if((HistoryDealGetInteger(ticket_deal_Out,DEAL_MAGIC) == magic_no) && HistoryDealGetInteger(ticket_deal_Out,DEAL_ENTRY) == DEAL_ENTRY_OUT
  525. && HistoryDealGetString(ticket_deal_Out,DEAL_SYMBOL) == Symbol()) // here is the problem solved after break
  526. {
  527. datetime orderCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
  528. if(orderCloseTime > latestCloseTime)
  529. {
  530. latestCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
  531. orderProfit = HistoryDealGetDouble(ticket_deal_Out, DEAL_PROFIT);
  532. latestTicket = ticket_deal_Out;
  533. lastOrderLot = HistoryDealGetDouble(ticket_deal_Out, DEAL_VOLUME);
  534. // Print(" Last order Lot: ", lastOrderLot);
  535. }
  536. }
  537. }
  538. }
  539. // Print(" Latest Selected Ticket: ", latestTicket, " Order Close Time: ", latestCloseTime, " Ticket Profit: ", orderProfit);
  540. return orderProfit;
  541. }
  542. //+------------------------------------------------------------------+
  543. //| Get Close Price By Ticket Function |
  544. //+------------------------------------------------------------------+
  545. double getClosePriceByTicket()
  546. {
  547. ulong ticket_deal_Out=0, ticket_deal_In = 0;
  548. datetime latestCloseTime = 0;
  549. double orderClosePrice = 0;
  550. ulong latestTicket = 0;
  551. if(HistorySelect(ea_start_time, TimeCurrent()))
  552. {
  553. int total = HistoryDealsTotal();
  554. for(int i = total-1; i >= 0 ; i--)
  555. {
  556. ticket_deal_Out = HistoryDealGetTicket(i);
  557. if((HistoryDealGetInteger(ticket_deal_Out,DEAL_MAGIC) == magic_no) && HistoryDealGetInteger(ticket_deal_Out,DEAL_ENTRY) == DEAL_ENTRY_OUT
  558. && HistoryDealGetString(ticket_deal_Out,DEAL_SYMBOL) == Symbol()) // here is the problem solved after break
  559. {
  560. datetime orderCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
  561. if(orderCloseTime > latestCloseTime)
  562. {
  563. orderClosePrice = HistoryDealGetDouble(ticket_deal_Out, DEAL_PRICE);
  564. latestCloseTime = orderCloseTime;
  565. }
  566. }
  567. }
  568. }
  569. return orderClosePrice;
  570. }
  571. //+------------------------------------------------------------------+
  572. //| Order Count Function |
  573. //+------------------------------------------------------------------+
  574. int orderCount()
  575. {
  576. int count = 0;
  577. for(int i= PositionsTotal()-1; i>=0; i--)
  578. {
  579. ulong ticket = PositionGetTicket(i);
  580. if(PositionSelectByTicket(ticket))
  581. {
  582. if(PositionGetInteger(POSITION_MAGIC) == magic_no && PositionGetString(POSITION_SYMBOL) == Symbol())
  583. {
  584. //if(PositionGetInteger(POSITION_TYPE) == type)
  585. {
  586. count++;
  587. }
  588. }
  589. }
  590. }
  591. return count;
  592. }
  593. //+------------------------------------------------------------------+
  594. //| Order Count 1 with Parameters Function |
  595. //+------------------------------------------------------------------+
  596. int orderCount_1(int type,int magic)
  597. {
  598. int count1=0;
  599. for(int i= PositionsTotal()-1; i>=0; i--)
  600. {
  601. ulong ticket = PositionGetTicket(i);
  602. if(PositionSelectByTicket(ticket))
  603. {
  604. if(PositionGetInteger(POSITION_MAGIC) == magic && PositionGetString(POSITION_SYMBOL) == Symbol())
  605. {
  606. if(PositionGetInteger(POSITION_TYPE) == type)
  607. {
  608. count1++;
  609. }
  610. }
  611. }
  612. }
  613. return count1;
  614. }
  615. //+------------------------------------------------------------------+
  616. //| Close Trades Function |
  617. //+------------------------------------------------------------------+
  618. void closeTrades(int type,int magicno)
  619. {
  620. Print("Total order: ",OrdersTotal());
  621. for(int i= PositionsTotal()-1; i>=0; i--)
  622. {
  623. ulong ticket = PositionGetTicket(i);
  624. if(PositionSelectByTicket(ticket))
  625. {
  626. if(PositionGetInteger(POSITION_TYPE) == type)
  627. {
  628. if(PositionGetInteger(POSITION_MAGIC) == magicno && PositionGetString(POSITION_SYMBOL) == Symbol())
  629. {
  630. if(!trade.PositionClose(PositionGetInteger(POSITION_TICKET)))
  631. {Print("Problem in closing order order ",PositionGetInteger(POSITION_TICKET)); }
  632. else
  633. {
  634. Print("Order Closed by closeTrades() new filter",PositionGetInteger(POSITION_TICKET));
  635. }
  636. }
  637. }
  638. }
  639. }
  640. }
  641. //+------------------------------------------------------------------+