Нема описа

CribMarketEA.mq5 9.7KB

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