暂无描述

CribMarketEAV2.mq5 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. #define MaxOrders 10000
  12. struct new_trade_store
  13. {
  14. ulong buyTicket; // Buy Ticket
  15. ulong sellTicket; // Sell Ticket
  16. string buySymbol; // Buy Symbol Name
  17. string sellSymbol; // Sell Symbol Name
  18. new_trade_store()
  19. {
  20. buyTicket = -1;
  21. sellTicket = -1;
  22. }
  23. };
  24. new_trade_store newTradeStore[MaxOrders];
  25. input string Settings = " ------------- General Settings ------------- "; //_
  26. input int magicNo = 333; // Magic no
  27. input double lotSize = 0.01; // Lot Size
  28. input bool enableBasketTP = true; // Enable Basket TP
  29. input double basketTakeProfit = 1.0; // Basket Take Profit
  30. input int maxOpenPositions = 3; // Maximum number of Combinations
  31. string goldPairs[];
  32. int totalGoldPairs = 0;
  33. int timmer = 0;
  34. //+------------------------------------------------------------------+
  35. //| Expert initialization function |
  36. //+------------------------------------------------------------------+
  37. int OnInit()
  38. {
  39. //---
  40. trade.SetExpertMagicNumber(magicNo);
  41. trade.SetDeviationInPoints(10);
  42. trade.SetTypeFilling(ORDER_FILLING_IOC);
  43. trade.LogLevel(LOG_LEVEL_ALL);
  44. trade.SetAsyncMode(false);
  45. getSymbolsFromMarketWatch();
  46. // addToStructure(153718680, 153718681, "EURUSD", "XAUUSD.");
  47. //--- create timer
  48. EventSetMillisecondTimer(1000);
  49. //---
  50. return(INIT_SUCCEEDED);
  51. }
  52. //+------------------------------------------------------------------+
  53. //| Expert deinitialization function |
  54. //+------------------------------------------------------------------+
  55. void OnDeinit(const int reason)
  56. {
  57. //---
  58. EventKillTimer();
  59. }
  60. //+------------------------------------------------------------------+
  61. //| Expert tick function |
  62. //+------------------------------------------------------------------+
  63. void OnTick()
  64. {
  65. //---
  66. }
  67. //+------------------------------------------------------------------+
  68. //| |
  69. //+------------------------------------------------------------------+
  70. void OnTimer()
  71. {
  72. //---
  73. timmer++;
  74. Print("<><><><><><><> Timer Start: ",timmer," <><><><><><><>");
  75. removeFromStruct();
  76. if(enableBasketTP == true)
  77. {
  78. checkBasketTakeProfit();
  79. }
  80. string symbolToBuy = getSymbolWithLowestAsk();
  81. string symbolToSell = getSymbolWithHighestBid();
  82. int buyTickett = -1, sellTickett = -1;
  83. //Print(" Symbol to Buy is: ", symbolToBuy, " Symbol to Sell: ", symbolToSell);
  84. if(noOfActiveCombinations() < maxOpenPositions)
  85. {
  86. if(canTradeSymbol(symbolToBuy, symbolToSell, POSITION_TYPE_BUY, POSITION_TYPE_SELL))
  87. {
  88. if(symbolToBuy != symbolToSell)
  89. {
  90. if(symbolToBuy != NULL && symbolToBuy != "")
  91. {
  92. buyTickett = placeBuyTrade(symbolToBuy);
  93. }
  94. if(symbolToSell != NULL && symbolToSell != "")
  95. {
  96. sellTickett = placeSellTrade(symbolToSell);
  97. }
  98. }
  99. }
  100. }
  101. if(buyTickett != -1 && sellTickett != -1)
  102. {
  103. addToStructure(buyTickett, sellTickett, symbolToBuy, symbolToSell);
  104. }
  105. Print("<><><><><><><> Timer End: ",timmer," <><><><><><><>");
  106. }
  107. //+------------------------------------------------------------------+
  108. //| |
  109. //+------------------------------------------------------------------+
  110. bool newBar()
  111. {
  112. static datetime lastbar;
  113. datetime curbar = iTime(Symbol(), PERIOD_CURRENT, 0);
  114. if(lastbar != curbar)
  115. {
  116. lastbar = curbar;
  117. Print(" ---------------------- New Bar :: ---------------------- ",lastbar);
  118. return (true);
  119. }
  120. else
  121. {
  122. return (false);
  123. }
  124. }
  125. //+------------------------------------------------------------------+
  126. //| |
  127. //+------------------------------------------------------------------+
  128. int placeBuyTrade(string symbol)
  129. {
  130. double ask = SymbolInfoDouble(symbol, SYMBOL_ASK);
  131. double buySL = 0, buyTP = 0;
  132. if(trade.PositionOpen(symbol, ORDER_TYPE_BUY, lotSize, ask, buySL, buyTP, "Buy Trade Placed"))
  133. {
  134. Print("Buy Trade Placed on ", symbol, ": ", trade.ResultOrder());
  135. return (int)trade.ResultOrder();
  136. }
  137. else
  138. {
  139. Print("Error in placing Buy on ", symbol, ": ", GetLastError());
  140. }
  141. return 0;
  142. }
  143. //+------------------------------------------------------------------+
  144. //| |
  145. //+------------------------------------------------------------------+
  146. int placeSellTrade(string symbol)
  147. {
  148. double bid = SymbolInfoDouble(symbol, SYMBOL_BID);
  149. double sellSL = 0, sellTP = 0;
  150. if(trade.PositionOpen(symbol, ORDER_TYPE_SELL, lotSize, bid, sellSL, sellTP, "Sell Trade Placed"))
  151. {
  152. Print("Sell Trade Placed on ", symbol, ": ", trade.ResultOrder());
  153. return (int)trade.ResultOrder();
  154. }
  155. else
  156. {
  157. Print("Error in placing Sell on ", symbol, ": ", GetLastError());
  158. }
  159. return 0;
  160. }
  161. //+------------------------------------------------------------------+
  162. //| |
  163. //+------------------------------------------------------------------+
  164. void getSymbolsFromMarketWatch()
  165. {
  166. int totalSymbols = SymbolsTotal(true);
  167. ArrayResize(goldPairs, 0);
  168. totalGoldPairs = 0;
  169. for(int i = 0; i < totalSymbols; i++)
  170. {
  171. string symbolName = SymbolName(i, true);
  172. //if(StringFind(symbolName, "GOLD") != -1 || StringFind(symbolName, "XAU") != -1)
  173. {
  174. ArrayResize(goldPairs, totalGoldPairs + 1);
  175. if(totalGoldPairs < ArraySize(goldPairs))
  176. {
  177. goldPairs[totalGoldPairs] = symbolName;
  178. totalGoldPairs++;
  179. }
  180. else
  181. {
  182. Print("Error: Array resize failed for symbol ", symbolName);
  183. }
  184. }
  185. }
  186. Print("Found ", totalGoldPairs, " Symbol pairs in Market Watch");
  187. }
  188. //+------------------------------------------------------------------+
  189. //| |
  190. //+------------------------------------------------------------------+
  191. string getSymbolWithLowestAsk()
  192. {
  193. if(totalGoldPairs == 0)
  194. return NULL;
  195. string lowestSymbol = "";
  196. //double lowestAsk = INT_MAX; // SymbolInfoDouble(lowestSymbol, SYMBOL_ASK);
  197. double lowestAsk = DBL_MAX;
  198. for(int i = 0; i < totalGoldPairs; i++)
  199. {
  200. double currentAsk = SymbolInfoDouble(goldPairs[i], SYMBOL_ASK);
  201. Print(" Ask Price: ",currentAsk," || on Pair: ",goldPairs[i]);
  202. if(currentAsk < lowestAsk)
  203. {
  204. lowestAsk = currentAsk;
  205. lowestSymbol = goldPairs[i];
  206. }
  207. }
  208. Print("Lowest Ask Pair: ", lowestSymbol, " || Lowest Ask: ", lowestAsk);
  209. return lowestSymbol;
  210. }
  211. //+------------------------------------------------------------------+
  212. //| |
  213. //+------------------------------------------------------------------+
  214. string getSymbolWithHighestBid()
  215. {
  216. if(totalGoldPairs == 0)
  217. return NULL;
  218. string highestSymbol = "";
  219. //double highestBid = INT_MIN; // SymbolInfoDouble(highestSymbol, SYMBOL_BID);
  220. double highestBid = 0;
  221. for(int i = 0; i < totalGoldPairs; i++)
  222. {
  223. double currentBid = SymbolInfoDouble(goldPairs[i], SYMBOL_BID);
  224. Print(" Bid Price: ",currentBid," || on Pair: ",goldPairs[i]);
  225. if(currentBid > highestBid)
  226. {
  227. highestBid = currentBid;
  228. highestSymbol = goldPairs[i];
  229. }
  230. }
  231. Print("Highest Bid Pair: ", highestSymbol, " || Highest Bid: ", highestBid);
  232. return highestSymbol;
  233. }
  234. //+------------------------------------------------------------------+
  235. //| |
  236. //+------------------------------------------------------------------+
  237. int noOfActiveCombinations()
  238. {
  239. int count = 0;
  240. for(int i = 0; i < MaxOrders; i++)
  241. {
  242. if(newTradeStore[i].buyTicket != -1 && newTradeStore[i].sellTicket != -1)
  243. {
  244. count++;
  245. }
  246. }
  247. return count;
  248. }
  249. //+------------------------------------------------------------------+
  250. //| |
  251. //+------------------------------------------------------------------+
  252. void checkBasketTakeProfit()
  253. {
  254. for(int i = 0; i < MaxOrders; i++)
  255. {
  256. if(newTradeStore[i].buyTicket != -1 && newTradeStore[i].sellTicket != -1)
  257. {
  258. double combinationProfit = 0;
  259. if(PositionSelectByTicket(newTradeStore[i].buyTicket))
  260. {
  261. combinationProfit += PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP);
  262. }
  263. if(PositionSelectByTicket(newTradeStore[i].sellTicket))
  264. {
  265. combinationProfit += PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP);
  266. }
  267. if(combinationProfit >= basketTakeProfit)
  268. {
  269. Print("Combination TP hit: Closing trades. Profit = ", combinationProfit);
  270. if(!trade.PositionClose(newTradeStore[i].buyTicket))
  271. {
  272. Print(" Error Closing Buy Trade : ", newTradeStore[i].buyTicket," ",GetLastError());
  273. }
  274. else
  275. Print("Buy Trade Closed: ", newTradeStore[i].buyTicket);
  276. if(!trade.PositionClose(newTradeStore[i].sellTicket))
  277. {
  278. Print(" Error Closing Sell Trade : ", newTradeStore[i].sellTicket," ",GetLastError());
  279. }
  280. else
  281. Print("Sell Trade Closed: ", newTradeStore[i].sellTicket);
  282. }
  283. }
  284. }
  285. }
  286. //+------------------------------------------------------------------+
  287. //| |
  288. //+------------------------------------------------------------------+
  289. bool isGoldPair(string symbol)
  290. {
  291. for(int i = 0; i < totalGoldPairs; i++)
  292. {
  293. //Print("Total Gold pairs: ", totalGoldPairs, " Array: ", goldPairs[i], " Symbol: ", symbol);
  294. if(goldPairs[i] == symbol)
  295. return true;
  296. }
  297. return false;
  298. }
  299. //+------------------------------------------------------------------+
  300. //| |
  301. //+------------------------------------------------------------------+
  302. void addToStructure(ulong r_buyTicket,ulong r_sellTicket, string r_buySymbol, string r_sellSymbol)
  303. {
  304. for(int i=0; i<MaxOrders; i++)
  305. {
  306. if(newTradeStore[i].buyTicket==-1 && newTradeStore[i].sellTicket==-1)
  307. {
  308. newTradeStore[i].buyTicket = r_buyTicket;
  309. newTradeStore[i].sellTicket = r_sellTicket;
  310. newTradeStore[i].buySymbol = r_buySymbol;
  311. newTradeStore[i].sellSymbol = r_sellSymbol;
  312. Print("Stored new ticket in structure. Buy Ticket: ", newTradeStore[i].buyTicket, " Sell Ticket: ", newTradeStore[i].sellTicket,
  313. " Buy Symbol: ", newTradeStore[i].buySymbol, " Sell Symbol: ", newTradeStore[i].sellSymbol);
  314. break;
  315. }
  316. }
  317. }
  318. //+------------------------------------------------------------------+
  319. //| |
  320. //+------------------------------------------------------------------+
  321. void removeFromStruct()
  322. {
  323. for(int i = 0 ; i < MaxOrders ; i++)
  324. {
  325. bool buyPresent = false;
  326. bool sellPresent = false;
  327. if(newTradeStore[i].buyTicket !=-1 && newTradeStore[i].sellTicket !=-1)
  328. {
  329. for(int j = PositionsTotal()-1; j>=0; j--)
  330. {
  331. ulong ticket = PositionGetTicket(j);
  332. if(PositionSelectByTicket(ticket))
  333. {
  334. if(ticket == newTradeStore[i].buyTicket)
  335. {
  336. buyPresent = true;
  337. }
  338. if(ticket == newTradeStore[i].sellTicket)
  339. {
  340. sellPresent = true;
  341. }
  342. }
  343. }
  344. if(!buyPresent && !sellPresent)
  345. {
  346. Print("Buy ticket closed so removed from struct: ", newTradeStore[i].buyTicket);
  347. newTradeStore[i].buyTicket = -1;
  348. newTradeStore[i].buySymbol = "";
  349. Print("Sell ticket closed so removed from struct: ", newTradeStore[i].sellTicket);
  350. newTradeStore[i].sellTicket = -1;
  351. newTradeStore[i].sellSymbol = "";
  352. }
  353. }
  354. }
  355. }
  356. //+------------------------------------------------------------------+
  357. //| |
  358. //+------------------------------------------------------------------+
  359. bool canTradeSymbol(string symbol1, string symbol2, ENUM_POSITION_TYPE type1, ENUM_POSITION_TYPE type2)
  360. {
  361. for(int i = 0; i < MaxOrders; i++)
  362. {
  363. if(newTradeStore[i].buyTicket != -1 && newTradeStore[i].sellTicket != -1)
  364. {
  365. if(newTradeStore[i].buySymbol == symbol1 && newTradeStore[i].sellSymbol == symbol2)
  366. {
  367. if(type1 == POSITION_TYPE_BUY && type2 == POSITION_TYPE_SELL)
  368. {
  369. Print("Already Have Buy and Sell on this Pair");
  370. Print("----------- Symbol Buy: ", symbol1, " Symbol Sell: ", symbol2, " Buy Ticket: ", newTradeStore[i].buyTicket,
  371. " Sell Ticket: ", newTradeStore[i].sellTicket," ---------------");
  372. return false;
  373. }
  374. }
  375. }
  376. }
  377. return true;
  378. }
  379. //+------------------------------------------------------------------+
  380. //| |
  381. //+------------------------------------------------------------------+
  382. //+------------------------------------------------------------------+