Açıklama Yok

line_level_ea.mq5 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. input string string_0 = "<><><><><><> General SETTINGS <><><><><><>"; //__
  17. input int magic_no = 333; // Magic no
  18. input tradeType selectSide = buyTrades; // Select Side
  19. input double lot_size = 0.1; // Lot Size
  20. input double lot_multiplier = 2; // Lot Multiplier on Loss Trade
  21. input double levels_distence = 10; // Levels Distance in Dollars
  22. input double stoploss = 10; // Fixed Stop Loss in Pips
  23. input double takeprofit = 10; // Fixed Take Profit in Pips
  24. input bool countinueCycleAfterProfit = true; // Continue Trading After Profit
  25. input string time_setting = "<><><><><> Time Filter Settings <><><><><>"; //_
  26. input bool EnableTimeFilter = false; // Enable Time Filter
  27. input string startTime = "03:00"; // Start Time Session
  28. input string endTime = "09:00"; // End Time Session
  29. // Global Variables
  30. double above_level = 0, below_level = 0;
  31. static double tickCurrentBid = 0, tickCurrentAsk = 0;
  32. double tickPreviousBid = 0, tickPreviousAsk = 0;
  33. datetime startTradingTime = 0, endTradingTime = 0;
  34. string sep = ":"; // A separator as a character
  35. ushort u_sep; // The code of the separator character
  36. string result1[];
  37. datetime ea_start_time = 0;
  38. bool tradeNow = true;
  39. bool printingDisplay = true;
  40. //+------------------------------------------------------------------+
  41. //| Expert initialization function |
  42. //+------------------------------------------------------------------+
  43. int OnInit()
  44. {
  45. //---
  46. // Fill Values above and below levels
  47. trade.SetExpertMagicNumber(magic_no);
  48. trade.SetDeviationInPoints(10);
  49. trade.SetTypeFilling(ORDER_FILLING_IOC);
  50. trade.LogLevel(LOG_LEVEL_ALL);
  51. trade.SetAsyncMode(false);
  52. ea_start_time = TimeCurrent();
  53. double currentPrice = iClose(Symbol(), PERIOD_CURRENT, 0);
  54. getNearestLevels(currentPrice, levels_distence, below_level, above_level);
  55. //---
  56. return(INIT_SUCCEEDED);
  57. }
  58. //+------------------------------------------------------------------+
  59. //| Expert deinitialization function |
  60. //+------------------------------------------------------------------+
  61. void OnDeinit(const int reason)
  62. {
  63. //---
  64. }
  65. //+------------------------------------------------------------------+
  66. //| Expert tick function |
  67. //+------------------------------------------------------------------+
  68. void OnTick()
  69. {
  70. //---
  71. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  72. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  73. tickPreviousBid = tickCurrentBid;
  74. tickCurrentBid = Bid;
  75. tickPreviousAsk = tickCurrentAsk;
  76. tickCurrentAsk = Ask;
  77. timeConversion();
  78. // Comment(" Below Value is: ", below_level, " Above Value: ", above_level, " Previous Tick: ", tickPreviousAsk, " Tick Current Ask: ", tickCurrentAsk);
  79. double lastLot = 0;
  80. if(!countinueCycleAfterProfit)
  81. if(selectLatestTicket(lastLot) > 0)
  82. {
  83. tradeNow = false;
  84. }
  85. // Comment(" Trades Count: ", orderCount(), " Below Value is: ", below_level, " Above Value: ", above_level);
  86. if(orderCount() == 0)
  87. {
  88. double latestClosePrice = getClosePriceByTicket();
  89. if(latestClosePrice > 0)
  90. {
  91. getNearestLevels(latestClosePrice, levels_distence, below_level, above_level);
  92. if(printingDisplay)
  93. {
  94. Print(" ---------------------------------- Resetting Level no trade open. Setting new Levels using last ticket close price: ", latestClosePrice, " ------------------------------------------------ ");
  95. Print(" Current Price: ", latestClosePrice, " Below Value is: ", below_level, " Above Value: ", above_level);
  96. printingDisplay = false;
  97. }
  98. }
  99. }
  100. else
  101. {
  102. printingDisplay = true;
  103. }
  104. if(tradeNow)
  105. if((EnableTimeFilter && TimeCurrent() >= startTradingTime && TimeCurrent() < endTradingTime) || !EnableTimeFilter)
  106. {
  107. if(above_level == 0 && below_level == 0)
  108. {
  109. double currentPrice = iClose(Symbol(), PERIOD_CURRENT, 0);
  110. getNearestLevels(currentPrice, levels_distence, below_level, above_level);
  111. Print(" Price Levels Updated on New Session Start. ");
  112. }
  113. if(selectSide == buyTrades) // Buy trade case
  114. {
  115. if(((tickPreviousAsk < above_level) && (tickCurrentAsk >= above_level)) ||
  116. ((tickPreviousAsk > below_level) && (tickCurrentAsk <= below_level)))
  117. {
  118. placeBuyTrade();
  119. Print(" ----------------------------- Buy Trade Executed and Levels Updated -----------------------------------------------------");
  120. double currentPrice = 0;
  121. if((tickPreviousAsk < above_level) && (tickCurrentAsk >= above_level))
  122. {
  123. currentPrice = above_level;
  124. }
  125. else
  126. if(((tickPreviousAsk > below_level) && (tickCurrentAsk <= below_level)))
  127. {
  128. currentPrice = below_level;
  129. }
  130. getNearestLevels(currentPrice, levels_distence, below_level, above_level);
  131. Print(" Current Price: ", currentPrice, " Below Value is: ", below_level, " Above Value: ", above_level);
  132. }
  133. }
  134. if(selectSide == sellTrades)
  135. {
  136. if(((tickPreviousBid < above_level) && (tickCurrentBid >= above_level)) ||
  137. ((tickPreviousBid > below_level) && (tickCurrentBid <= below_level)))
  138. {
  139. placeSellTrade();
  140. Print(" ----------------------------- Sell Trade Executed and Levels Updated -----------------------------------------------------");
  141. double currentPrice = 0;
  142. if((tickPreviousBid < above_level) && (tickCurrentBid >= above_level))
  143. {
  144. currentPrice = above_level;
  145. }
  146. else
  147. if(((tickPreviousBid > below_level) && (tickCurrentBid <= below_level)))
  148. {
  149. currentPrice = below_level;
  150. }
  151. getNearestLevels(currentPrice, levels_distence, below_level, above_level);
  152. Print(" Current Price: ", currentPrice, " Below Value: ", below_level, " Above Value: ", above_level);
  153. }
  154. }
  155. }
  156. else
  157. {
  158. below_level = 0;
  159. above_level = 0;
  160. }
  161. }
  162. //+------------------------------------------------------------------+
  163. //| |
  164. //+------------------------------------------------------------------+
  165. double calculateBaseLevel(double price, double step)
  166. {
  167. return step * MathFloor(price / step);
  168. }
  169. //+------------------------------------------------------------------+
  170. //| |
  171. //+------------------------------------------------------------------+
  172. void getNearestLevels(double currentPrice, double step,
  173. double &LowerLevel, double &UpperLevel)
  174. {
  175. double epsilon = 0.000001;
  176. // Compute the anchor as the nearest multiple of step.
  177. double anchor = MathRound(currentPrice / step) * step;
  178. double diff = fabs(currentPrice - anchor);
  179. // If currentPrice exactly equals the anchor, use symmetric levels.
  180. if(diff < epsilon)
  181. {
  182. LowerLevel = anchor - step;
  183. UpperLevel = anchor + step;
  184. return;
  185. }
  186. // Special handling for step == 10: use non–symmetric levels.
  187. if(step == 10)
  188. {
  189. if(currentPrice > anchor)
  190. {
  191. LowerLevel = anchor;
  192. UpperLevel = anchor + step;
  193. }
  194. else // currentPrice < anchor
  195. {
  196. LowerLevel = anchor - step;
  197. UpperLevel = anchor;
  198. }
  199. return;
  200. }
  201. // For all other steps, use the ratio threshold logic.
  202. double ratio = diff / step;
  203. double threshold = 0.45; // Adjusted threshold
  204. if(currentPrice > anchor)
  205. {
  206. if(ratio > threshold)
  207. {
  208. LowerLevel = anchor - step;
  209. UpperLevel = anchor + step;
  210. }
  211. else
  212. {
  213. LowerLevel = anchor;
  214. UpperLevel = anchor + step;
  215. }
  216. }
  217. else // currentPrice < anchor
  218. {
  219. if(ratio > threshold)
  220. {
  221. LowerLevel = anchor - step;
  222. UpperLevel = anchor + step;
  223. }
  224. else
  225. {
  226. LowerLevel = anchor - step;
  227. UpperLevel = anchor;
  228. }
  229. }
  230. }
  231. //+------------------------------------------------------------------+
  232. //| |
  233. //+------------------------------------------------------------------+
  234. void placeBuyTrade()
  235. {
  236. double buySL = 0, buyTp=0;
  237. //openPrice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  238. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  239. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  240. if(stoploss != 0)
  241. {
  242. buySL = Ask - (stoploss * 10 * Point());
  243. }
  244. if(takeprofit != 0)
  245. {
  246. buyTp = Ask + (takeprofit * 10 * Point());
  247. }
  248. double lot = 0;
  249. double lastLot = 0;
  250. if(selectLatestTicket(lastLot) < 0)
  251. {
  252. lot = lastLot * lot_multiplier;
  253. }
  254. else
  255. {
  256. lot = lot_size;
  257. }
  258. if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,NormalizeDouble(lot, 2),Ask,buySL,buyTp,"Buy Trade Placed"))
  259. {
  260. Print("Buy Trade Placed: ", trade.ResultOrder());
  261. }
  262. else
  263. {
  264. Print("Error in placing Buy: "+Symbol()+" ",GetLastError());
  265. }
  266. }
  267. //+------------------------------------------------------------------+
  268. //| |
  269. //+------------------------------------------------------------------+
  270. void placeSellTrade()
  271. {
  272. double sellSL = 0, sellTp = 0;
  273. //openPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  274. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  275. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  276. if(stoploss != 0)
  277. {
  278. sellSL = Bid + (stoploss * 10 * Point());
  279. }
  280. if(takeprofit != 0)
  281. {
  282. sellTp = Bid - (takeprofit * 10 * Point());
  283. }
  284. double lot = 0;
  285. double lastLot = 0;
  286. if(selectLatestTicket(lastLot) < 0)
  287. {
  288. lot = lastLot * lot_multiplier;
  289. }
  290. else
  291. {
  292. lot = lot_size;
  293. }
  294. if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,NormalizeDouble(lot, 2),Bid,sellSL,sellTp,"Sell Trade Placed"))
  295. {
  296. Print("Sell Trade PLaced: ", trade.ResultOrder());
  297. }
  298. else
  299. {
  300. Print("Error in placing Sell: "+Symbol()+" ",GetLastError());
  301. }
  302. }
  303. //+------------------------------------------------------------------+
  304. //| |
  305. //+------------------------------------------------------------------+
  306. void timeConversion()
  307. {
  308. MqlDateTime date, date1;
  309. TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date);
  310. u_sep=StringGetCharacter(sep,0);
  311. StringSplit(startTime,u_sep,result1);
  312. date.hour = (int)StringToInteger(result1[0]);
  313. date.min = (int)StringToInteger(result1[1]);
  314. startTradingTime = StructToTime(date);
  315. TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date1);
  316. StringSplit(endTime,u_sep,result1);
  317. date.hour = (int)StringToInteger(result1[0]);
  318. date.min = (int)StringToInteger(result1[1]);
  319. endTradingTime = StructToTime(date);
  320. }
  321. //+------------------------------------------------------------------+
  322. //| |
  323. //+------------------------------------------------------------------+
  324. double selectLatestTicket(double &lastOrderLot)
  325. {
  326. int count = 0;
  327. ulong ticket_deal_Out=0, ticket_deal_In = 0;
  328. datetime latestCloseTime = 0;
  329. double orderProfit = 0;
  330. ulong latestTicket = 0;
  331. if(HistorySelect(ea_start_time, TimeCurrent()))
  332. {
  333. int total = HistoryDealsTotal();
  334. for(int i = total-1; i >= 0 ; i--)
  335. {
  336. ticket_deal_Out = HistoryDealGetTicket(i);
  337. if((HistoryDealGetInteger(ticket_deal_Out,DEAL_MAGIC) == magic_no) && HistoryDealGetInteger(ticket_deal_Out,DEAL_ENTRY) == DEAL_ENTRY_OUT
  338. && HistoryDealGetString(ticket_deal_Out,DEAL_SYMBOL) == Symbol()) // here is the problem solved after break
  339. {
  340. datetime orderCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
  341. if(orderCloseTime > latestCloseTime)
  342. {
  343. latestCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
  344. orderProfit = HistoryDealGetDouble(ticket_deal_Out, DEAL_PROFIT);
  345. latestTicket = ticket_deal_Out;
  346. lastOrderLot = HistoryDealGetDouble(ticket_deal_Out, DEAL_VOLUME);
  347. // Print(" Last order Lot: ", lastOrderLot);
  348. }
  349. }
  350. }
  351. }
  352. // Print(" Latest Selected Ticket: ", latestTicket, " Order Close Time: ", latestCloseTime, " Ticket Profit: ", orderProfit);
  353. return orderProfit;
  354. }
  355. //+------------------------------------------------------------------+
  356. //| |
  357. //+------------------------------------------------------------------+
  358. double getClosePriceByTicket()
  359. {
  360. ulong ticket_deal_Out=0, ticket_deal_In = 0;
  361. datetime latestCloseTime = 0;
  362. double orderClosePrice = 0;
  363. ulong latestTicket = 0;
  364. if(HistorySelect(ea_start_time, TimeCurrent()))
  365. {
  366. int total = HistoryDealsTotal();
  367. for(int i = total-1; i >= 0 ; i--)
  368. {
  369. ticket_deal_Out = HistoryDealGetTicket(i);
  370. if((HistoryDealGetInteger(ticket_deal_Out,DEAL_MAGIC) == magic_no) && HistoryDealGetInteger(ticket_deal_Out,DEAL_ENTRY) == DEAL_ENTRY_OUT
  371. && HistoryDealGetString(ticket_deal_Out,DEAL_SYMBOL) == Symbol()) // here is the problem solved after break
  372. {
  373. datetime orderCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
  374. if(orderCloseTime > latestCloseTime)
  375. {
  376. orderClosePrice = HistoryDealGetDouble(ticket_deal_Out, DEAL_PRICE);
  377. latestCloseTime = orderCloseTime;
  378. }
  379. }
  380. }
  381. }
  382. return orderClosePrice;
  383. }
  384. //+------------------------------------------------------------------+
  385. //| |
  386. //+------------------------------------------------------------------+
  387. int orderCount()
  388. {
  389. int count = 0;
  390. for(int i= PositionsTotal()-1; i>=0; i--)
  391. {
  392. ulong ticket = PositionGetTicket(i);
  393. if(PositionSelectByTicket(ticket))
  394. {
  395. if(PositionGetInteger(POSITION_MAGIC) == magic_no && PositionGetString(POSITION_SYMBOL) == Symbol())
  396. {
  397. // if(PositionGetInteger(POSITION_TYPE) == type)
  398. {
  399. count++;
  400. }
  401. }
  402. }
  403. }
  404. return count;
  405. }
  406. //+------------------------------------------------------------------+
  407. //| |
  408. //+------------------------------------------------------------------+
  409. //+------------------------------------------------------------------+