Ingen beskrivning

line_level_ea.mq5 52KB

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