Aucune description

line_level_ea.mq5 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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.00"
  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. //+------------------------------------------------------------------+
  40. //| Expert initialization function |
  41. //+------------------------------------------------------------------+
  42. int OnInit()
  43. {
  44. //---
  45. // Fill Values above and below levels
  46. trade.SetExpertMagicNumber(magic_no);
  47. trade.SetDeviationInPoints(10);
  48. trade.SetTypeFilling(ORDER_FILLING_IOC);
  49. trade.LogLevel(LOG_LEVEL_ALL);
  50. trade.SetAsyncMode(false);
  51. ea_start_time = TimeCurrent();
  52. double currentPrice = iClose(Symbol(), PERIOD_CURRENT, 0);
  53. getNearestLevels(currentPrice, levels_distence, below_level, above_level);
  54. //---
  55. return(INIT_SUCCEEDED);
  56. }
  57. //+------------------------------------------------------------------+
  58. //| Expert deinitialization function |
  59. //+------------------------------------------------------------------+
  60. void OnDeinit(const int reason)
  61. {
  62. //---
  63. }
  64. //+------------------------------------------------------------------+
  65. //| Expert tick function |
  66. //+------------------------------------------------------------------+
  67. void OnTick()
  68. {
  69. //---
  70. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  71. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  72. tickPreviousBid = tickCurrentBid;
  73. tickCurrentBid = Bid;
  74. tickPreviousAsk = tickCurrentAsk;
  75. tickCurrentAsk = Ask;
  76. timeConversion();
  77. // Comment(" Below Value is: ", below_level, " Above Value: ", above_level, " Previous Tick: ", tickPreviousAsk, " Tick Current Ask: ", tickCurrentAsk);
  78. double lastLot = 0;
  79. if(!countinueCycleAfterProfit)
  80. if(selectLatestTicket(lastLot) > 0)
  81. {
  82. tradeNow = false;
  83. }
  84. if(tradeNow)
  85. if((EnableTimeFilter && TimeCurrent() >= startTradingTime && TimeCurrent() < endTradingTime) || !EnableTimeFilter)
  86. {
  87. if(above_level == 0 && below_level == 0)
  88. {
  89. double currentPrice = iClose(Symbol(), PERIOD_CURRENT, 0);
  90. getNearestLevels(currentPrice, levels_distence, below_level, above_level);
  91. Print(" Price Levels Updated on New Session Start. ");
  92. }
  93. if(selectSide == buyTrades) // Buy trade case
  94. {
  95. if(((tickPreviousAsk < above_level) && (tickCurrentAsk >= above_level)) ||
  96. ((tickPreviousAsk > below_level) && (tickCurrentAsk <= below_level)))
  97. {
  98. placeBuyTrade();
  99. Print(" ----------------------------- Buy Trade Executed and Levels Updated -----------------------------------------------------");
  100. double currentPrice = 0;
  101. if((tickPreviousAsk < above_level) && (tickCurrentAsk >= above_level))
  102. {
  103. currentPrice = above_level;
  104. }
  105. else
  106. if(((tickPreviousAsk > below_level) && (tickCurrentAsk <= below_level)))
  107. {
  108. currentPrice = below_level;
  109. }
  110. getNearestLevels(currentPrice, levels_distence, below_level, above_level);
  111. Print(" Current Price: ", currentPrice, " Below Value is: ", below_level, " Above Value: ", above_level);
  112. }
  113. }
  114. if(selectSide == sellTrades)
  115. {
  116. if(((tickPreviousBid < above_level) && (tickCurrentBid >= above_level)) ||
  117. ((tickPreviousBid > below_level) && (tickCurrentBid <= below_level)))
  118. {
  119. placeSellTrade();
  120. Print(" ----------------------------- Sell Trade Executed and Levels Updated -----------------------------------------------------");
  121. double currentPrice = 0;
  122. if((tickPreviousBid < above_level) && (tickCurrentBid >= above_level))
  123. {
  124. currentPrice = above_level;
  125. }
  126. else
  127. if(((tickPreviousBid > below_level) && (tickCurrentBid <= below_level)))
  128. {
  129. currentPrice = below_level;
  130. }
  131. getNearestLevels(currentPrice, levels_distence, below_level, above_level);
  132. Print(" Current Price: ", currentPrice, " Below Value: ", below_level, " Above Value: ", above_level);
  133. }
  134. }
  135. }
  136. else
  137. {
  138. below_level = 0;
  139. above_level = 0;
  140. }
  141. }
  142. //+------------------------------------------------------------------+
  143. //| |
  144. //+------------------------------------------------------------------+
  145. double calculateBaseLevel(double price, double step)
  146. {
  147. return step * MathFloor(price / step);
  148. }
  149. //+------------------------------------------------------------------+
  150. //| |
  151. //+------------------------------------------------------------------+
  152. void getNearestLevels(double currentPrice, double step,
  153. double &LowerLevel, double &UpperLevel)
  154. {
  155. double epsilon = 0.000001;
  156. // Compute the anchor as the nearest multiple of step.
  157. double anchor = MathRound(currentPrice / step) * step;
  158. double diff = fabs(currentPrice - anchor);
  159. // If currentPrice exactly equals the anchor, use symmetric levels.
  160. if(diff < epsilon)
  161. {
  162. LowerLevel = anchor - step;
  163. UpperLevel = anchor + step;
  164. return;
  165. }
  166. // Special handling for step == 10: use non–symmetric levels.
  167. if(step == 10)
  168. {
  169. if(currentPrice > anchor)
  170. {
  171. LowerLevel = anchor;
  172. UpperLevel = anchor + step;
  173. }
  174. else // currentPrice < anchor
  175. {
  176. LowerLevel = anchor - step;
  177. UpperLevel = anchor;
  178. }
  179. return;
  180. }
  181. // For all other steps, use the ratio threshold logic.
  182. double ratio = diff / step;
  183. double threshold = 0.45; // Adjusted threshold
  184. if(currentPrice > anchor)
  185. {
  186. if(ratio > threshold)
  187. {
  188. LowerLevel = anchor - step;
  189. UpperLevel = anchor + step;
  190. }
  191. else
  192. {
  193. LowerLevel = anchor;
  194. UpperLevel = anchor + step;
  195. }
  196. }
  197. else // currentPrice < anchor
  198. {
  199. if(ratio > threshold)
  200. {
  201. LowerLevel = anchor - step;
  202. UpperLevel = anchor + step;
  203. }
  204. else
  205. {
  206. LowerLevel = anchor - step;
  207. UpperLevel = anchor;
  208. }
  209. }
  210. }
  211. //+------------------------------------------------------------------+
  212. //| |
  213. //+------------------------------------------------------------------+
  214. void placeBuyTrade()
  215. {
  216. double buySL = 0, buyTp=0;
  217. //openPrice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  218. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  219. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  220. if(stoploss != 0)
  221. {
  222. buySL = Ask - (stoploss * 10 * Point());
  223. }
  224. if(takeprofit != 0)
  225. {
  226. buyTp = Ask + (takeprofit * 10 * Point());
  227. }
  228. double lot = 0;
  229. double lastLot = 0;
  230. if(selectLatestTicket(lastLot) < 0)
  231. {
  232. lot = lastLot * lot_multiplier;
  233. }
  234. else
  235. {
  236. lot = lot_size;
  237. }
  238. if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,NormalizeDouble(lot, 2),Ask,buySL,buyTp,"Buy Trade Placed"))
  239. {
  240. Print("Buy Trade Placed: ",trade.ResultOrder());
  241. }
  242. else
  243. {
  244. Print("Error in placing Buy: "+Symbol()+" ",GetLastError());
  245. }
  246. }
  247. //+------------------------------------------------------------------+
  248. //| |
  249. //+------------------------------------------------------------------+
  250. void placeSellTrade()
  251. {
  252. double sellSL = 0, sellTp = 0;
  253. //openPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  254. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  255. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  256. if(stoploss != 0)
  257. {
  258. sellSL = Bid + (stoploss * 10 * Point());
  259. }
  260. if(takeprofit != 0)
  261. {
  262. sellTp = Bid - (takeprofit * 10 * Point());
  263. }
  264. double lot = 0;
  265. double lastLot = 0;
  266. if(selectLatestTicket(lastLot) < 0)
  267. {
  268. lot = lastLot * lot_multiplier;
  269. }
  270. else
  271. {
  272. lot = lot_size;
  273. }
  274. if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,NormalizeDouble(lot, 2),Bid,sellSL,sellTp,"Sell Trade Placed"))
  275. {
  276. Print("Sell Trade PLaced: ",trade.ResultOrder());
  277. }
  278. else
  279. {
  280. Print("Error in placing Sell: "+Symbol()+" ",GetLastError());
  281. }
  282. }
  283. //+------------------------------------------------------------------+
  284. //| |
  285. //+------------------------------------------------------------------+
  286. void timeConversion()
  287. {
  288. MqlDateTime date, date1;
  289. TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date);
  290. u_sep=StringGetCharacter(sep,0);
  291. StringSplit(startTime,u_sep,result1);
  292. date.hour = (int)StringToInteger(result1[0]);
  293. date.min = (int)StringToInteger(result1[1]);
  294. startTradingTime = StructToTime(date);
  295. TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date1);
  296. StringSplit(endTime,u_sep,result1);
  297. date.hour = (int)StringToInteger(result1[0]);
  298. date.min = (int)StringToInteger(result1[1]);
  299. endTradingTime = StructToTime(date);
  300. }
  301. //+------------------------------------------------------------------+
  302. //| |
  303. //+------------------------------------------------------------------+
  304. double selectLatestTicket(double &lastOrderLot)
  305. {
  306. int count = 0;
  307. ulong ticket_deal_Out=0, ticket_deal_In = 0;
  308. datetime latestCloseTime = 0;
  309. double orderProfit = 0;
  310. ulong latestTicket = 0;
  311. if(HistorySelect(ea_start_time, TimeCurrent()))
  312. {
  313. int total = HistoryDealsTotal();
  314. for(int i = total-1; i >= 0 ; i--)
  315. {
  316. ticket_deal_Out = HistoryDealGetTicket(i);
  317. if((HistoryDealGetInteger(ticket_deal_Out,DEAL_MAGIC) == magic_no) && HistoryDealGetInteger(ticket_deal_Out,DEAL_ENTRY) == DEAL_ENTRY_OUT
  318. && HistoryDealGetString(ticket_deal_Out,DEAL_SYMBOL) == Symbol()) // here is the problem solved after break
  319. {
  320. datetime orderCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
  321. if(orderCloseTime > latestCloseTime)
  322. {
  323. latestCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
  324. orderProfit = HistoryDealGetDouble(ticket_deal_Out, DEAL_PROFIT);
  325. latestTicket = ticket_deal_Out;
  326. lastOrderLot = HistoryDealGetDouble(ticket_deal_Out, DEAL_VOLUME);
  327. // Print(" Last order Lot: ", lastOrderLot);
  328. }
  329. }
  330. }
  331. }
  332. // Print(" Latest Selected Ticket: ", latestTicket, " Order Close Time: ", latestCloseTime, " Ticket Profit: ", orderProfit);
  333. return orderProfit;
  334. }
  335. //+------------------------------------------------------------------+
  336. //| |
  337. //+------------------------------------------------------------------+
  338. //+------------------------------------------------------------------+