Nenhuma descrição

line_level_ea.mq5 52KB

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