暂无描述

line_level_ea.mq5 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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(selectSide == buyTrades) // Buy trade case
  88. {
  89. if(((tickPreviousAsk < above_level) && (tickCurrentAsk >= above_level)) ||
  90. ((tickPreviousAsk > below_level) && (tickCurrentAsk <= below_level)))
  91. {
  92. placeBuyTrade();
  93. Print(" ----------------------------- Buy Trade Executed and Levels Updated -----------------------------------------------------");
  94. double currentPrice = 0;
  95. if((tickPreviousAsk < above_level) && (tickCurrentAsk >= above_level))
  96. {
  97. currentPrice = above_level;
  98. }
  99. else
  100. if(((tickPreviousAsk > below_level) && (tickCurrentAsk <= below_level)))
  101. {
  102. currentPrice = below_level;
  103. }
  104. getNearestLevels(currentPrice, levels_distence, below_level, above_level);
  105. Print(" Current Price: ", currentPrice, " Below Value is: ", below_level, " Above Value: ", above_level);
  106. }
  107. }
  108. if(selectSide == sellTrades)
  109. {
  110. if(((tickPreviousBid < above_level) && (tickCurrentBid >= above_level)) ||
  111. ((tickPreviousBid > below_level) && (tickCurrentBid <= below_level)))
  112. {
  113. placeSellTrade();
  114. Print(" ----------------------------- Sell Trade Executed and Levels Updated -----------------------------------------------------");
  115. double currentPrice = 0;
  116. if((tickPreviousBid < above_level) && (tickCurrentBid >= above_level))
  117. {
  118. currentPrice = above_level;
  119. }
  120. else
  121. if(((tickPreviousBid > below_level) && (tickCurrentBid <= below_level)))
  122. {
  123. currentPrice = below_level;
  124. }
  125. getNearestLevels(currentPrice, levels_distence, below_level, above_level);
  126. Print(" Current Price: ", currentPrice, " Below Value: ", below_level, " Above Value: ", above_level);
  127. }
  128. }
  129. }
  130. }
  131. //+------------------------------------------------------------------+
  132. //| |
  133. //+------------------------------------------------------------------+
  134. double calculateBaseLevel(double price, double step)
  135. {
  136. return step * MathFloor(price / step);
  137. }
  138. //+------------------------------------------------------------------+
  139. //| |
  140. //+------------------------------------------------------------------+
  141. void getNearestLevels(double currentPrice, double step,
  142. double &LowerLevel, double &UpperLevel)
  143. {
  144. double epsilon = 0.000001;
  145. // Compute the anchor as the nearest multiple of step.
  146. double anchor = MathRound(currentPrice / step) * step;
  147. double diff = fabs(currentPrice - anchor);
  148. // If currentPrice is exactly on a multiple, use symmetric levels.
  149. if(diff < epsilon)
  150. {
  151. LowerLevel = anchor - step;
  152. UpperLevel = anchor + step;
  153. return;
  154. }
  155. double ratio = diff / step;
  156. double threshold = 0.45; // Adjusted threshold
  157. if(currentPrice > anchor)
  158. {
  159. if(ratio > threshold)
  160. {
  161. LowerLevel = anchor - step;
  162. UpperLevel = anchor + step;
  163. }
  164. else
  165. {
  166. LowerLevel = anchor;
  167. UpperLevel = anchor + step;
  168. }
  169. }
  170. else // currentPrice < anchor
  171. {
  172. if(ratio > threshold)
  173. {
  174. LowerLevel = anchor - step;
  175. UpperLevel = anchor + step;
  176. }
  177. else
  178. {
  179. LowerLevel = anchor - step;
  180. UpperLevel = anchor;
  181. }
  182. }
  183. }
  184. //+------------------------------------------------------------------+
  185. //| |
  186. //+------------------------------------------------------------------+
  187. void placeBuyTrade()
  188. {
  189. double buySL = 0, buyTp=0;
  190. //openPrice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  191. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  192. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  193. if(stoploss != 0)
  194. {
  195. buySL = Ask - (stoploss * 10 * Point());
  196. }
  197. if(takeprofit != 0)
  198. {
  199. buyTp = Ask + (takeprofit * 10 * Point());
  200. }
  201. double lot = 0;
  202. double lastLot = 0;
  203. if(selectLatestTicket(lastLot) < 0)
  204. {
  205. lot = lastLot * lot_multiplier;
  206. }
  207. else
  208. {
  209. lot = lot_size;
  210. }
  211. if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,NormalizeDouble(lot, 2),Ask,buySL,buyTp,"Buy Trade Placed"))
  212. {
  213. Print("Buy Trade Placed: ",trade.ResultOrder());
  214. }
  215. else
  216. {
  217. Print("Error in placing Buy: "+Symbol()+" ",GetLastError());
  218. }
  219. }
  220. //+------------------------------------------------------------------+
  221. //| |
  222. //+------------------------------------------------------------------+
  223. void placeSellTrade()
  224. {
  225. double sellSL = 0, sellTp = 0;
  226. //openPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  227. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  228. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  229. if(stoploss != 0)
  230. {
  231. sellSL = Bid + (stoploss * 10 * Point());
  232. }
  233. if(takeprofit != 0)
  234. {
  235. sellTp = Bid - (takeprofit * 10 * Point());
  236. }
  237. double lot = 0;
  238. double lastLot = 0;
  239. if(selectLatestTicket(lastLot) < 0)
  240. {
  241. lot = lastLot * lot_multiplier;
  242. }
  243. else
  244. {
  245. lot = lot_size;
  246. }
  247. if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,NormalizeDouble(lot, 2),Bid,sellSL,sellTp,"Sell Trade Placed"))
  248. {
  249. Print("Sell Trade PLaced: ",trade.ResultOrder());
  250. }
  251. else
  252. {
  253. Print("Error in placing Sell: "+Symbol()+" ",GetLastError());
  254. }
  255. }
  256. //+------------------------------------------------------------------+
  257. //| |
  258. //+------------------------------------------------------------------+
  259. void timeConversion()
  260. {
  261. MqlDateTime date, date1;
  262. TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date);
  263. u_sep=StringGetCharacter(sep,0);
  264. StringSplit(startTime,u_sep,result1);
  265. date.hour = (int)StringToInteger(result1[0]);
  266. date.min = (int)StringToInteger(result1[1]);
  267. startTradingTime = StructToTime(date);
  268. TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date1);
  269. StringSplit(endTime,u_sep,result1);
  270. date.hour = (int)StringToInteger(result1[0]);
  271. date.min = (int)StringToInteger(result1[1]);
  272. endTradingTime = StructToTime(date);
  273. }
  274. //+------------------------------------------------------------------+
  275. //| |
  276. //+------------------------------------------------------------------+
  277. double selectLatestTicket(double &lastOrderLot)
  278. {
  279. int count = 0;
  280. ulong ticket_deal_Out=0, ticket_deal_In = 0;
  281. datetime latestCloseTime = 0;
  282. double orderProfit = 0;
  283. ulong latestTicket = 0;
  284. if(HistorySelect(ea_start_time, TimeCurrent()))
  285. {
  286. int total = HistoryDealsTotal();
  287. for(int i = total-1; i >= 0 ; i--)
  288. {
  289. ticket_deal_Out = HistoryDealGetTicket(i);
  290. if((HistoryDealGetInteger(ticket_deal_Out,DEAL_MAGIC) == magic_no) && HistoryDealGetInteger(ticket_deal_Out,DEAL_ENTRY) == DEAL_ENTRY_OUT
  291. && HistoryDealGetString(ticket_deal_Out,DEAL_SYMBOL) == Symbol()) // here is the problem solved after break
  292. {
  293. datetime orderCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
  294. if(orderCloseTime > latestCloseTime)
  295. {
  296. latestCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
  297. orderProfit = HistoryDealGetDouble(ticket_deal_Out, DEAL_PROFIT);
  298. latestTicket = ticket_deal_Out;
  299. lastOrderLot = HistoryDealGetDouble(ticket_deal_Out, DEAL_VOLUME);
  300. // Print(" Last order Lot: ", lastOrderLot);
  301. }
  302. }
  303. }
  304. }
  305. // Print(" Latest Selected Ticket: ", latestTicket, " Order Close Time: ", latestCloseTime, " Ticket Profit: ", orderProfit);
  306. return orderProfit;
  307. }
  308. //+------------------------------------------------------------------+
  309. //| |
  310. //+------------------------------------------------------------------+
  311. //+------------------------------------------------------------------+