Bez popisu

CribMarketEA.mq5 9.5KB

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