Geen omschrijving

Pulse_EA_project_MT5.mq5 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. #include <Trade\Trade.mqh>
  10. CTrade trade;
  11. #define MaxOrders 10000
  12. struct new_trade_store
  13. {
  14. int trade_ticket;
  15. int trade_type;
  16. int trade_magic_no;
  17. double trade_open_price;
  18. double trade_close_price;
  19. datetime trade_open_time;
  20. datetime trade_close_time;
  21. double trade_sl;
  22. double trade_tp;
  23. double trade_lot;
  24. double trade_profit;
  25. new_trade_store()
  26. {
  27. trade_ticket = -1;
  28. trade_type = -1;
  29. trade_magic_no = -1;
  30. trade_open_price = -1;
  31. trade_close_price = -1;
  32. trade_open_time = -1;
  33. trade_close_time = -1;
  34. trade_sl = -1;
  35. trade_tp = -1;
  36. trade_lot = -1;
  37. trade_profit = -1;
  38. }
  39. };
  40. new_trade_store od[MaxOrders];
  41. enum trade_typ
  42. {
  43. buy, // Buy
  44. reverse, // Reverse
  45. };
  46. enum EA_TYPE
  47. {
  48. Tally_Sim, // Tally Sim
  49. Tally_Trade, // Tally Trade
  50. };
  51. input string gnrlsettings = " ================ General Settings =================== ";//_
  52. input EA_TYPE eaType = Tally_Sim; // EA Option
  53. input double tpPips = 20; // Tp Pips
  54. input double slPips = 20; // SL Pips
  55. input double lot = 0.1; // Lot Size
  56. input string gnrlesettings = " ================ Tally Sim Settings =================== ";//_
  57. input trade_typ ordTyp = buy; // Order Type (Tally Sim)
  58. input int magicNo = 123; // Magic # (Tally Sim)
  59. input string fileName = "TradeDataFile.csv"; // File Name (Tally Sim)
  60. input string grlesettings = " ================ Tally Trade Settings =================== ";//_
  61. input trade_typ tradesDirection = buy; // Order Type (Tally Trade)
  62. input int magicNo1 = 1234; // Magic # (Tally Trade)
  63. input bool CopyTallyTrade = true; // Enable Copy Trade
  64. input string indSettings = " ================ Indicator Settings =================== ";//_
  65. input color Dot_Color = clrOrangeRed;
  66. input double StartingBalance = 10000;
  67. input string MagicNumbers = "123";
  68. input int MA_Period = 5;
  69. input ENUM_MA_METHOD MA_Method = MODE_SMA;
  70. input string fileName1 = "TradeDataFile.csv"; // File Name
  71. bool tpSlHit = false;
  72. int ticketAssigner = 1;
  73. int handler;
  74. double bufferData[];
  75. //+------------------------------------------------------------------+
  76. //| Expert initialization function |
  77. //+------------------------------------------------------------------+
  78. int OnInit()
  79. {
  80. //---
  81. tpSlHit = false;
  82. ticketAssigner = 1;
  83. if(eaType == Tally_Trade)
  84. {
  85. ArrayInitialize(bufferData,0.0);
  86. handler = iCustom(Symbol(),PERIOD_CURRENT,"Pulse Balance Indicator Sim",Dot_Color,StartingBalance,MagicNumbers,MA_Period,MA_Method,fileName1);
  87. ArraySetAsSeries(bufferData,true);
  88. }
  89. trade.SetExpertMagicNumber(magicNo1);
  90. trade.SetDeviationInPoints(10);
  91. trade.SetTypeFilling(ORDER_FILLING_IOC);
  92. trade.LogLevel(LOG_LEVEL_ALL);
  93. trade.SetAsyncMode(false);
  94. //---
  95. return(INIT_SUCCEEDED);
  96. }
  97. //+------------------------------------------------------------------+
  98. //| Expert deinitialization function |
  99. //+------------------------------------------------------------------+
  100. void OnDeinit(const int reason)
  101. {
  102. //---
  103. }
  104. //+------------------------------------------------------------------+
  105. //| Expert tick function |
  106. //+------------------------------------------------------------------+
  107. void OnTick()
  108. {
  109. //---
  110. placeTrade();
  111. checkTPSLHit();
  112. }
  113. //+------------------------------------------------------------------+
  114. //| |
  115. //+------------------------------------------------------------------+
  116. void placeTrade()
  117. {
  118. if(tpSlHit == false)
  119. {
  120. if(ordTyp == buy)
  121. {
  122. placeBuyTrade();
  123. }
  124. if(ordTyp == reverse)
  125. {
  126. placeSellTrade();
  127. }
  128. tpSlHit = true;
  129. }
  130. }
  131. //+------------------------------------------------------------------+
  132. //| |
  133. //+------------------------------------------------------------------+
  134. void placeBuyTrade()
  135. {
  136. double buySl = 0,buyTp=0;
  137. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  138. if(slPips != 0)
  139. {
  140. buySl = Ask - (slPips * Point() * 10);
  141. }
  142. if(tpPips != 0)
  143. {
  144. buyTp = Ask + (tpPips * Point() * 10);
  145. }
  146. AddToStructure(ticketAssigner,0,magicNo,Ask,0,TimeCurrent(),0,buySl,buyTp,lot,0);
  147. ticketAssigner++;
  148. if(eaType == Tally_Trade && CopyTallyTrade)
  149. {
  150. if(CopyBuffer(handler, 0, 0, 4, bufferData) < 0)
  151. {
  152. Print("Error in copying Buffer data ", GetLastError());
  153. }
  154. double balance = bufferData[1];
  155. if(CopyBuffer(handler, 1, 0, 4, bufferData) < 0)
  156. {
  157. Print("Error in copying Buffer data ", GetLastError());
  158. }
  159. double MA = bufferData[1];
  160. Print(" Balance = ",balance," MA = ",MA);
  161. if(tradesDirection == buy)
  162. {
  163. if(balance > MA)
  164. {
  165. if(!trade.Buy(lot, Symbol(),SymbolInfoDouble(Symbol(),SYMBOL_ASK),buySl,buyTp,"Buy Trade"))
  166. {
  167. Print(" Error in Placing Buy Trade : ",GetLastError());
  168. }
  169. }
  170. else
  171. {
  172. Print(" Trade is not Placed because Balance is less than MA Value ");
  173. }
  174. }
  175. else
  176. {
  177. if(balance > MA)
  178. {
  179. if(!trade.Sell(lot,Symbol(),SymbolInfoDouble(Symbol(),SYMBOL_BID),buyTp,buySl,"Sell Trade"))
  180. {
  181. Print(" Error in Placing Sell Trade : ",GetLastError());
  182. }
  183. }
  184. else
  185. {
  186. Print(" Trade is not Placed because Balance is less than MA Value ");
  187. }
  188. }
  189. }
  190. }
  191. //+------------------------------------------------------------------+
  192. //| |
  193. //+------------------------------------------------------------------+
  194. void placeSellTrade()
  195. {
  196. double sellSl = 0, sellTp = 0;
  197. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  198. if(slPips != 0)
  199. {
  200. sellSl = Bid + (slPips * 10 * Point());
  201. }
  202. if(tpPips != 0)
  203. {
  204. sellTp = Bid - (tpPips * 10 * Point());
  205. }
  206. AddToStructure(ticketAssigner,1,magicNo,Bid,0,TimeCurrent(),0,sellSl,sellTp,lot,0);
  207. ticketAssigner++;
  208. if(eaType == Tally_Trade && CopyTallyTrade)
  209. {
  210. if(CopyBuffer(handler, 0, 0, 4, bufferData) < 0)
  211. {
  212. Print("Error in copying Buffer data ", GetLastError());
  213. }
  214. double balance = bufferData[1];
  215. if(CopyBuffer(handler, 1, 0, 4, bufferData) < 0)
  216. {
  217. Print("Error in copying Buffer data ", GetLastError());
  218. }
  219. double MA = bufferData[1];
  220. Print(" Balance = ",balance," MA = ",MA);
  221. if(tradesDirection == reverse)
  222. {
  223. if(balance > MA)
  224. {
  225. if(!trade.Sell(lot,Symbol(),Bid,sellSl,sellTp,"Sell Trade"))
  226. {
  227. Print(" Error in Placing Sell Trade : ",GetLastError());
  228. }
  229. }
  230. else
  231. {
  232. Print(" Trade is not Placed because Balance is less than MA Value ");
  233. }
  234. }
  235. else
  236. {
  237. if(balance > MA)
  238. {
  239. if(!trade.Buy(lot,Symbol(),SymbolInfoDouble(Symbol(),SYMBOL_ASK),sellTp,sellSl,"Buy Trade"))
  240. {
  241. Print(" Error in Placing Buy Trade : ",GetLastError());
  242. }
  243. }
  244. else
  245. {
  246. Print(" Trade is not Placed because Balance is less than MA Value ");
  247. }
  248. }
  249. }
  250. }
  251. //+------------------------------------------------------------------+
  252. //| |
  253. //+------------------------------------------------------------------+
  254. void AddToStructure(int ticket, int type, int magic, double open_price,
  255. double close_price, datetime open_time, datetime close_time,
  256. double sl, double tp, double lotSize, double profit)
  257. {
  258. for(int i = 0; i < MaxOrders; i++)
  259. {
  260. if(od[i].trade_ticket == -1)
  261. {
  262. od[i].trade_ticket = ticket;
  263. od[i].trade_type = type;
  264. od[i].trade_magic_no = magic;
  265. od[i].trade_open_price = open_price;
  266. od[i].trade_close_price = close_price;
  267. od[i].trade_open_time = open_time;
  268. od[i].trade_close_time = close_time;
  269. od[i].trade_sl = sl;
  270. od[i].trade_tp = tp;
  271. od[i].trade_lot = lotSize;
  272. od[i].trade_profit = profit;
  273. Print(" ========== Trade Placed of Ticket ========== ",ticket);
  274. Print("[AddToStructure] Added at index: ", i,
  275. " | Ticket = ", ticket,
  276. " | Type = ", type,
  277. " | Magic = ", magic,
  278. " | OpenPrice = ", open_price,
  279. " | ClosePrice = ", close_price,
  280. " | SL = ", sl,
  281. " | TP = ", tp,
  282. " | Lots = ", lotSize,
  283. " | Profit = ", profit);
  284. break;
  285. }
  286. }
  287. }
  288. //+------------------------------------------------------------------+
  289. //| |
  290. //+------------------------------------------------------------------+
  291. void checkTPSLHit()
  292. {
  293. double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
  294. double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
  295. for(int i = 0; i < MaxOrders; i++)
  296. {
  297. if(od[i].trade_ticket != -1 && od[i].trade_close_price == 0)
  298. {
  299. int ticket = od[i].trade_ticket;
  300. int type = od[i].trade_type;
  301. double sl = od[i].trade_sl;
  302. double tp = od[i].trade_tp;
  303. double closePrice = 0;
  304. // ----------------------------------------------
  305. // BUY ORDER CHECK
  306. // ----------------------------------------------
  307. if(type == 0)
  308. {
  309. // TP hit → Bid >= TP
  310. if(bid >= tp && tp > 0)
  311. {
  312. closePrice = bid;
  313. Print(" ======== Buy Trade TP Hit ======= ",ticket);
  314. }
  315. // SL hit → Bid <= SL
  316. if(bid <= sl && sl > 0)
  317. {
  318. closePrice = bid;
  319. Print(" ======== Buy Trade SL Hit ======= ",ticket);
  320. }
  321. }
  322. // ----------------------------------------------
  323. // SELL ORDER CHECK
  324. // ----------------------------------------------
  325. if(type == 1)
  326. {
  327. // TP hit → Ask <= TP (price goes DOWN)
  328. if(ask <= tp && tp > 0)
  329. {
  330. closePrice = ask;
  331. Print(" ======== Sell Trade TP Hit ======= ",ticket);
  332. }
  333. // SL hit → Ask >= SL
  334. if(ask >= sl && sl > 0)
  335. {
  336. closePrice = ask;
  337. Print(" ======== Sell Trade SL Hit ======= ",ticket);
  338. }
  339. }
  340. // ----------------------------------------------
  341. // IF ANYTHING TRIGGERED → CLOSE TRADE
  342. // ----------------------------------------------
  343. if(closePrice > 0)
  344. {
  345. od[i].trade_close_price = closePrice;
  346. od[i].trade_close_time = TimeCurrent();
  347. 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();
  348. od[i].trade_profit += tpinDollar(od[i].trade_lot, tpdifference);
  349. Print("======= TRADE CLOSED of ticket =======",ticket);
  350. Print("Index: ", i,
  351. " | Ticket: ", ticket,
  352. " | Type: ", type,
  353. " | ClosePrice: ", closePrice,
  354. " | Time: ", od[i].trade_close_time,
  355. " | Profit: ", od[i].trade_profit);
  356. tpSlHit = false; // make these zero so new trade place's
  357. string line = MakeTradeString(
  358. od[i].trade_ticket,
  359. od[i].trade_type,
  360. od[i].trade_magic_no,
  361. od[i].trade_open_price,
  362. od[i].trade_close_price,
  363. od[i].trade_open_time,
  364. od[i].trade_close_time,
  365. od[i].trade_sl,
  366. od[i].trade_tp,
  367. od[i].trade_lot,
  368. od[i].trade_profit
  369. );
  370. writeDatainFile(line);
  371. }
  372. }
  373. }
  374. }
  375. //+------------------------------------------------------------------+
  376. //| |
  377. //+------------------------------------------------------------------+
  378. double tpinDollar(double lotSize, double distance)
  379. {
  380. double pipvalue = NormalizeDouble(((SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)/(SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE)/Point()))*10),2);
  381. pipvalue = pipvalue / 10;
  382. double dollar = lotSize * distance * pipvalue;
  383. return NormalizeDouble(dollar,2);
  384. }
  385. //+------------------------------------------------------------------+
  386. //| |
  387. //+------------------------------------------------------------------+
  388. void writeDatainFile(string str)
  389. {
  390. int filehandle = FileOpen(fileName,FILE_WRITE|FILE_CSV|FILE_COMMON|FILE_END,",");
  391. if(filehandle != INVALID_HANDLE)
  392. {
  393. FileSeek(filehandle, 0, SEEK_END);
  394. FileWrite(filehandle,str);
  395. Print(str);
  396. FileClose(filehandle);
  397. }
  398. }
  399. //+------------------------------------------------------------------+
  400. //| |
  401. //+------------------------------------------------------------------+
  402. string MakeTradeString(int ticket, int type, int magic,
  403. double open_price, double close_price,
  404. datetime open_time, datetime close_time,
  405. double sl, double tp, double lotSize, double profit)
  406. {
  407. string str = StringFormat("%d,%d,%d,%.5f,%.5f,%s,%s,%.5f,%.5f,%.2f,%.2f",
  408. ticket,
  409. type,
  410. magic,
  411. open_price,
  412. close_price,
  413. TimeToString(open_time, TIME_DATE|TIME_SECONDS),
  414. TimeToString(close_time, TIME_DATE|TIME_SECONDS),
  415. sl,
  416. tp,
  417. lotSize,
  418. profit);
  419. return str;
  420. }
  421. //+------------------------------------------------------------------+