瀏覽代碼

Ticket : 4797 Multi Pair Entry Bot

1) All symbol fetching Added
2) Max positions Added
faiz ali 6 月之前
父節點
當前提交
af54ded092
共有 2 個文件被更改,包括 452 次插入0 次删除
  1. 二進制
      V2/CribMarketEAV2.ex5
  2. 452 0
      V2/CribMarketEAV2.mq5

二進制
V2/CribMarketEAV2.ex5


+ 452 - 0
V2/CribMarketEAV2.mq5

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