暂无描述

Pulse_EA_project_MT5.mq5 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //+------------------------------------------------------------------+
  2. //| Pulse_EA_project_MT5.mq5 |
  3. //| Copyright 2024, MQL Development |
  4. //| https://www.mqldevelopment.com/ |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2024, MQL Development"
  7. #property link "https://www.mqldevelopment.com/"
  8. #property version "1.00"
  9. #define MaxOrders 10000
  10. struct new_trade_store
  11. {
  12. int trade_ticket;
  13. int trade_type;
  14. int trade_magic_no;
  15. double trade_open_price;
  16. double trade_close_price;
  17. datetime trade_open_time;
  18. datetime trade_close_time;
  19. double trade_sl;
  20. double trade_tp;
  21. double trade_lot;
  22. double trade_profit;
  23. new_trade_store()
  24. {
  25. trade_ticket = -1;
  26. trade_type = -1;
  27. trade_magic_no = -1;
  28. trade_open_price = -1;
  29. trade_close_price = -1;
  30. trade_open_time = -1;
  31. trade_close_time = -1;
  32. trade_sl = -1;
  33. trade_tp = -1;
  34. trade_lot = -1;
  35. trade_profit = -1;
  36. }
  37. };
  38. new_trade_store od[MaxOrders];
  39. enum trade_typ
  40. {
  41. buy, // Buy
  42. reverse, // Reverse
  43. };
  44. input trade_typ ordTyp = buy;
  45. input double tpPips = 20;
  46. input double slPips = 20;
  47. input double lot = 0.1;
  48. input int magicNo = 123;
  49. input string fileName = "TradeDataFile.csv"; // File Name
  50. bool tpSlHit = false;
  51. int ticketAssigner = 1;
  52. //+------------------------------------------------------------------+
  53. //| Expert initialization function |
  54. //+------------------------------------------------------------------+
  55. int OnInit()
  56. {
  57. //---
  58. tpSlHit = false;
  59. ticketAssigner = 1;
  60. //---
  61. return(INIT_SUCCEEDED);
  62. }
  63. //+------------------------------------------------------------------+
  64. //| Expert deinitialization function |
  65. //+------------------------------------------------------------------+
  66. void OnDeinit(const int reason)
  67. {
  68. //---
  69. }
  70. //+------------------------------------------------------------------+
  71. //| Expert tick function |
  72. //+------------------------------------------------------------------+
  73. void OnTick()
  74. {
  75. //---
  76. placeTrade();
  77. checkTPSLHit();
  78. }
  79. //+------------------------------------------------------------------+
  80. //| |
  81. //+------------------------------------------------------------------+
  82. void placeTrade()
  83. {
  84. if(tpSlHit == false)
  85. {
  86. if(ordTyp == buy)
  87. {
  88. placeBuyTrade();
  89. }
  90. if(ordTyp == reverse)
  91. {
  92. placeSellTrade();
  93. }
  94. tpSlHit = true;
  95. }
  96. }
  97. //+------------------------------------------------------------------+
  98. //| |
  99. //+------------------------------------------------------------------+
  100. void placeBuyTrade()
  101. {
  102. double buySl = 0,buyTp=0;
  103. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  104. if(slPips != 0)
  105. {
  106. buySl = Ask - (slPips * Point() * 10);
  107. }
  108. if(tpPips != 0)
  109. {
  110. buyTp = Ask + (tpPips * Point() * 10);
  111. }
  112. AddToStructure(ticketAssigner,0,magicNo,Ask,0,TimeCurrent(),0,buySl,buyTp,lot,0);
  113. ticketAssigner++;
  114. }
  115. //+------------------------------------------------------------------+
  116. //| |
  117. //+------------------------------------------------------------------+
  118. void placeSellTrade()
  119. {
  120. double sellSl = 0, sellTp = 0;
  121. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  122. if(slPips != 0)
  123. {
  124. sellSl = Bid + (slPips * 10 * Point());
  125. }
  126. if(tpPips != 0)
  127. {
  128. sellTp = Bid - (tpPips * 10 * Point());
  129. }
  130. AddToStructure(ticketAssigner,1,magicNo,Bid,0,TimeCurrent(),0,sellSl,sellTp,lot,0);
  131. ticketAssigner++;
  132. }
  133. //+------------------------------------------------------------------+
  134. //| |
  135. //+------------------------------------------------------------------+
  136. void AddToStructure(int ticket, int type, int magic, double open_price,
  137. double close_price, datetime open_time, datetime close_time,
  138. double sl, double tp, double lotSize, double profit)
  139. {
  140. for(int i = 0; i < MaxOrders; i++)
  141. {
  142. if(od[i].trade_ticket == -1)
  143. {
  144. od[i].trade_ticket = ticket;
  145. od[i].trade_type = type;
  146. od[i].trade_magic_no = magic;
  147. od[i].trade_open_price = open_price;
  148. od[i].trade_close_price = close_price;
  149. od[i].trade_open_time = open_time;
  150. od[i].trade_close_time = close_time;
  151. od[i].trade_sl = sl;
  152. od[i].trade_tp = tp;
  153. od[i].trade_lot = lotSize;
  154. od[i].trade_profit = profit;
  155. Print(" ========== Trade Placed of Ticket ========== ",ticket);
  156. Print("[AddToStructure] Added at index: ", i,
  157. " | Ticket = ", ticket,
  158. " | Type = ", type,
  159. " | Magic = ", magic,
  160. " | OpenPrice = ", open_price,
  161. " | ClosePrice = ", close_price,
  162. " | SL = ", sl,
  163. " | TP = ", tp,
  164. " | Lots = ", lotSize,
  165. " | Profit = ", profit);
  166. break;
  167. }
  168. }
  169. }
  170. //+------------------------------------------------------------------+
  171. //| |
  172. //+------------------------------------------------------------------+
  173. void checkTPSLHit()
  174. {
  175. double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
  176. double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
  177. for(int i = 0; i < MaxOrders; i++)
  178. {
  179. if(od[i].trade_ticket != -1 && od[i].trade_close_price == 0)
  180. {
  181. int ticket = od[i].trade_ticket;
  182. int type = od[i].trade_type;
  183. double sl = od[i].trade_sl;
  184. double tp = od[i].trade_tp;
  185. double closePrice = 0;
  186. // ----------------------------------------------
  187. // BUY ORDER CHECK
  188. // ----------------------------------------------
  189. if(type == 0)
  190. {
  191. // TP hit → Bid >= TP
  192. if(bid >= tp && tp > 0)
  193. {
  194. closePrice = bid;
  195. Print(" ======== Buy Trade TP Hit ======= ",ticket);
  196. }
  197. // SL hit → Bid <= SL
  198. if(bid <= sl && sl > 0)
  199. {
  200. closePrice = bid;
  201. Print(" ======== Buy Trade SL Hit ======= ",ticket);
  202. }
  203. }
  204. // ----------------------------------------------
  205. // SELL ORDER CHECK
  206. // ----------------------------------------------
  207. if(type == 1)
  208. {
  209. // TP hit → Ask <= TP (price goes DOWN)
  210. if(ask <= tp && tp > 0)
  211. {
  212. closePrice = ask;
  213. Print(" ======== Sell Trade TP Hit ======= ",ticket);
  214. }
  215. // SL hit → Ask >= SL
  216. if(ask >= sl && sl > 0)
  217. {
  218. closePrice = ask;
  219. Print(" ======== Sell Trade SL Hit ======= ",ticket);
  220. }
  221. }
  222. // ----------------------------------------------
  223. // IF ANYTHING TRIGGERED → CLOSE TRADE
  224. // ----------------------------------------------
  225. if(closePrice > 0)
  226. {
  227. od[i].trade_close_price = closePrice;
  228. od[i].trade_close_time = TimeCurrent();
  229. double tpdifference = type == 1 ? (od[i].trade_open_price - od[i].trade_close_price) / Point() : (od[i].trade_close_price - od[i].trade_open_price) / Point();
  230. od[i].trade_profit += tpinDollar(od[i].trade_lot, tpdifference);
  231. Print("======= TRADE CLOSED of ticket =======",ticket);
  232. Print("Index: ", i,
  233. " | Ticket: ", ticket,
  234. " | Type: ", type,
  235. " | ClosePrice: ", closePrice,
  236. " | Time: ", od[i].trade_close_time,
  237. " | Profit: ", od[i].trade_profit);
  238. tpSlHit = false; // make these zero so new trade place's
  239. string line = MakeTradeString(
  240. od[i].trade_ticket,
  241. od[i].trade_type,
  242. od[i].trade_magic_no,
  243. od[i].trade_open_price,
  244. od[i].trade_close_price,
  245. od[i].trade_open_time,
  246. od[i].trade_close_time,
  247. od[i].trade_sl,
  248. od[i].trade_tp,
  249. od[i].trade_lot,
  250. od[i].trade_profit
  251. );
  252. writeDatainFile(line);
  253. }
  254. }
  255. }
  256. }
  257. //+------------------------------------------------------------------+
  258. //| |
  259. //+------------------------------------------------------------------+
  260. double tpinDollar(double lotSize, double distance)
  261. {
  262. double pipvalue = NormalizeDouble(((SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)/(SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE)/Point()))*10),2);
  263. pipvalue = pipvalue / 10;
  264. double dollar = lotSize * distance * pipvalue;
  265. return NormalizeDouble(dollar,2);
  266. }
  267. //+------------------------------------------------------------------+
  268. //| |
  269. //+------------------------------------------------------------------+
  270. void writeDatainFile(string str)
  271. {
  272. int filehandle = FileOpen(fileName,FILE_WRITE|FILE_CSV|FILE_COMMON|FILE_END,",");
  273. if(filehandle != INVALID_HANDLE)
  274. {
  275. FileSeek(filehandle, 0, SEEK_END);
  276. FileWrite(filehandle,str);
  277. Print(str);
  278. FileClose(filehandle);
  279. }
  280. }
  281. //+------------------------------------------------------------------+
  282. //| |
  283. //+------------------------------------------------------------------+
  284. string MakeTradeString(int ticket, int type, int magic,
  285. double open_price, double close_price,
  286. datetime open_time, datetime close_time,
  287. double sl, double tp, double lotSize, double profit)
  288. {
  289. string str = StringFormat("%d,%d,%d,%.5f,%.5f,%s,%s,%.5f,%.5f,%.2f,%.2f",
  290. ticket,
  291. type,
  292. magic,
  293. open_price,
  294. close_price,
  295. TimeToString(open_time, TIME_DATE|TIME_SECONDS),
  296. TimeToString(close_time, TIME_DATE|TIME_SECONDS),
  297. sl,
  298. tp,
  299. lotSize,
  300. profit);
  301. return str;
  302. }