Geen omschrijving

CribMarketEA.mq5 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //+------------------------------------------------------------------+
  2. //| CribMarketEA.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. input string Settings = " ------------- General Settings ------------- "; //_
  12. input int magicNo = 333; // Magic no
  13. input double lotSize = 0.01; // Lot Size
  14. input bool enableBasketTP = true; // Enable Basket TP
  15. input double basketTakeProfit = 1.0; // Basket Take Profit
  16. string goldPairs[];
  17. int totalGoldPairs = 0;
  18. //+------------------------------------------------------------------+
  19. //| Expert initialization function |
  20. //+------------------------------------------------------------------+
  21. int OnInit()
  22. {
  23. //---
  24. trade.SetExpertMagicNumber(magicNo);
  25. trade.SetDeviationInPoints(10);
  26. trade.SetTypeFilling(ORDER_FILLING_IOC);
  27. trade.LogLevel(LOG_LEVEL_ALL);
  28. trade.SetAsyncMode(false);
  29. getGoldPairsFromMarketWatch();
  30. //--- create timer
  31. EventSetMillisecondTimer(1000);
  32. //---
  33. return(INIT_SUCCEEDED);
  34. }
  35. //+------------------------------------------------------------------+
  36. //| Expert deinitialization function |
  37. //+------------------------------------------------------------------+
  38. void OnDeinit(const int reason)
  39. {
  40. //---
  41. EventKillTimer();
  42. }
  43. //+------------------------------------------------------------------+
  44. //| Expert tick function |
  45. //+------------------------------------------------------------------+
  46. void OnTick()
  47. {
  48. //---
  49. }
  50. //+------------------------------------------------------------------+
  51. //| |
  52. //+------------------------------------------------------------------+
  53. void OnTimer()
  54. {
  55. //---
  56. if(enableBasketTP == true)
  57. {
  58. checkBasketTakeProfit();
  59. }
  60. string symbolToBuy = getSymbolWithLowestAsk();
  61. string symbolToSell = getSymbolWithHighestBid();
  62. //Print(" Symbol to Buy is: ", symbolToBuy, " Symbol to Sell: ", symbolToSell);
  63. if(noOfActiveOrdersOfType(POSITION_TYPE_BUY) == 0)
  64. {
  65. if(symbolToBuy != NULL && symbolToBuy != "")
  66. {
  67. placeBuyTrade(symbolToBuy);
  68. }
  69. }
  70. if(noOfActiveOrdersOfType(POSITION_TYPE_SELL) == 0)
  71. {
  72. if(symbolToSell != NULL && symbolToSell != "")
  73. {
  74. placeSellTrade(symbolToSell);
  75. }
  76. }
  77. }
  78. //+------------------------------------------------------------------+
  79. //| |
  80. //+------------------------------------------------------------------+
  81. bool newBar()
  82. {
  83. static datetime lastbar;
  84. datetime curbar = iTime(Symbol(), PERIOD_CURRENT, 0);
  85. if(lastbar != curbar)
  86. {
  87. lastbar = curbar;
  88. Print(" ---------------------- New Bar :: ---------------------- ",lastbar);
  89. return (true);
  90. }
  91. else
  92. {
  93. return (false);
  94. }
  95. }
  96. //+------------------------------------------------------------------+
  97. //| |
  98. //+------------------------------------------------------------------+
  99. void placeBuyTrade(string symbol)
  100. {
  101. double ask = SymbolInfoDouble(symbol, SYMBOL_ASK);
  102. double buySL = 0, buyTP = 0;
  103. if(trade.PositionOpen(symbol, ORDER_TYPE_BUY, lotSize, ask, buySL, buyTP, "Buy Trade Placed"))
  104. {
  105. Print("Buy Trade Placed on ", symbol, ": ", trade.ResultOrder());
  106. }
  107. else
  108. {
  109. Print("Error in placing Buy on ", symbol, ": ", GetLastError());
  110. }
  111. }
  112. //+------------------------------------------------------------------+
  113. //| |
  114. //+------------------------------------------------------------------+
  115. void placeSellTrade(string symbol)
  116. {
  117. double bid = SymbolInfoDouble(symbol, SYMBOL_BID);
  118. double sellSL = 0, sellTP = 0;
  119. if(trade.PositionOpen(symbol, ORDER_TYPE_SELL, lotSize, bid, sellSL, sellTP, "Sell Trade Placed"))
  120. {
  121. Print("Sell Trade Placed on ", symbol, ": ", trade.ResultOrder());
  122. }
  123. else
  124. {
  125. Print("Error in placing Sell on ", symbol, ": ", GetLastError());
  126. }
  127. }
  128. //+------------------------------------------------------------------+
  129. //| |
  130. //+------------------------------------------------------------------+
  131. void getGoldPairsFromMarketWatch()
  132. {
  133. int totalSymbols = SymbolsTotal(true);
  134. ArrayResize(goldPairs, 0);
  135. totalGoldPairs = 0;
  136. for(int i = 0; i < totalSymbols; i++)
  137. {
  138. string symbolName = SymbolName(i, true);
  139. if(StringFind(symbolName, "GOLD") != -1 || StringFind(symbolName, "XAU") != -1)
  140. {
  141. ArrayResize(goldPairs, totalGoldPairs + 1);
  142. if(totalGoldPairs < ArraySize(goldPairs))
  143. {
  144. goldPairs[totalGoldPairs] = symbolName;
  145. totalGoldPairs++;
  146. }
  147. else
  148. {
  149. Print("Error: Array resize failed for symbol ", symbolName);
  150. }
  151. }
  152. }
  153. Print("Found ", totalGoldPairs, " gold pairs in Market Watch");
  154. }
  155. //+------------------------------------------------------------------+
  156. //| |
  157. //+------------------------------------------------------------------+
  158. string getSymbolWithLowestAsk()
  159. {
  160. if(totalGoldPairs == 0)
  161. return NULL;
  162. string lowestSymbol = "";
  163. //double lowestAsk = INT_MAX; // SymbolInfoDouble(lowestSymbol, SYMBOL_ASK);
  164. double lowestAsk = DBL_MAX;
  165. for(int i = 0; i < totalGoldPairs; i++)
  166. {
  167. double currentAsk = SymbolInfoDouble(goldPairs[i], SYMBOL_ASK);
  168. Print("Pair:",goldPairs[i]," Price Ask:",currentAsk);
  169. if(currentAsk < lowestAsk)
  170. {
  171. lowestAsk = currentAsk;
  172. lowestSymbol = goldPairs[i];
  173. }
  174. }
  175. Print("Lowest Ask Pair: ", lowestSymbol, " Lowest Ask: ", lowestAsk);
  176. return lowestSymbol;
  177. }
  178. //+------------------------------------------------------------------+
  179. //| |
  180. //+------------------------------------------------------------------+
  181. string getSymbolWithHighestBid()
  182. {
  183. if(totalGoldPairs == 0)
  184. return NULL;
  185. string highestSymbol = "";
  186. //double highestBid = INT_MIN; // SymbolInfoDouble(highestSymbol, SYMBOL_BID);
  187. double highestBid = 0;
  188. for(int i = 0; i < totalGoldPairs; i++)
  189. {
  190. double currentBid = SymbolInfoDouble(goldPairs[i], SYMBOL_BID);
  191. Print("Pair:",goldPairs[i]," Price Bid:",currentBid);
  192. if(currentBid > highestBid)
  193. {
  194. highestBid = currentBid;
  195. highestSymbol = goldPairs[i];
  196. }
  197. }
  198. Print("Highest Bid Pair: ", highestSymbol, " Highest Bid: ", highestBid);
  199. return highestSymbol;
  200. }
  201. //+------------------------------------------------------------------+
  202. //| |
  203. //+------------------------------------------------------------------+
  204. int noOfActiveOrdersOfType(ENUM_POSITION_TYPE type)
  205. {
  206. int count = 0;
  207. for(int i= PositionsTotal()-1; i>=0; i--)
  208. {
  209. ulong ticket = PositionGetTicket(i);
  210. if(PositionSelectByTicket(ticket))
  211. {
  212. if(PositionGetInteger(POSITION_MAGIC) == magicNo
  213. && (PositionGetInteger(POSITION_TYPE) == type)
  214. && isGoldPair(PositionGetString(POSITION_SYMBOL)))
  215. {
  216. count++;
  217. }
  218. }
  219. }
  220. return count;
  221. }
  222. //+------------------------------------------------------------------+
  223. //| |
  224. //+------------------------------------------------------------------+
  225. void checkBasketTakeProfit()
  226. {
  227. double netProfit = 0;
  228. for(int i = PositionsTotal() - 1; i >= 0; i--)
  229. {
  230. ulong ticket = PositionGetTicket(i);
  231. if(PositionSelectByTicket(ticket))
  232. {
  233. if(isGoldPair(PositionGetString(POSITION_SYMBOL)) &&
  234. PositionGetInteger(POSITION_MAGIC) == magicNo)
  235. {
  236. netProfit += PositionGetDouble(POSITION_PROFIT)+PositionGetDouble(POSITION_SWAP);
  237. }
  238. }
  239. }
  240. if(netProfit >= basketTakeProfit)
  241. {
  242. Print("Basket TP hit: Closing all trades. Profit = ", netProfit);
  243. closeAllActiveOrders();
  244. }
  245. }
  246. //+------------------------------------------------------------------+
  247. //| |
  248. //+------------------------------------------------------------------+
  249. void closeAllActiveOrders()
  250. {
  251. for(int i=PositionsTotal()-1; i >=0 ; i--)
  252. {
  253. ulong ticket = PositionGetTicket(i);
  254. if(PositionSelectByTicket(ticket))
  255. {
  256. if(PositionGetInteger(POSITION_MAGIC) == magicNo &&
  257. isGoldPair(PositionGetString(POSITION_SYMBOL)))
  258. {
  259. if(trade.PositionClose(ticket))
  260. {
  261. Print("Position closed ", ticket);
  262. }
  263. else
  264. {
  265. Print("Cannot close order: ",GetLastError());
  266. }
  267. }
  268. }
  269. }
  270. }
  271. //+------------------------------------------------------------------+
  272. //| |
  273. //+------------------------------------------------------------------+
  274. bool isGoldPair(string symbol)
  275. {
  276. for(int i = 0; i < totalGoldPairs; i++)
  277. {
  278. //Print("Total Gold pairs: ", totalGoldPairs, " Array: ", goldPairs[i], " Symbol: ", symbol);
  279. if(goldPairs[i] == symbol)
  280. return true;
  281. }
  282. return false;
  283. }
  284. //+------------------------------------------------------------------+