Selaa lähdekoodia

Ticket : 4751 Entry Conditions

1) Symbol fetching Updated
2) Basket TP Added
3) Closing Functionality Added
faiz ali 7 kuukautta sitten
vanhempi
commit
21fdf15459
2 muutettua tiedostoa jossa 136 lisäystä ja 13 poistoa
  1. BIN
      CribMarketEA.ex5
  2. 136 13
      CribMarketEA.mq5

BIN
CribMarketEA.ex5


+ 136 - 13
CribMarketEA.mq5

@@ -14,6 +14,8 @@ CTrade  trade;
14 14
 input       string               Settings              = " ------------- General Settings ------------- ";   //_
15 15
 input       int                  magicNo               = 333;                 // Magic no
16 16
 input       double               lotSize               = 0.1;                 // Lot Size
17
+input       bool                 enableBasketTP        = false;               // Enable Basket TP
18
+input       double               basketTakeProfit      = 150.0;               // Basket Take Profit
17 19
 
18 20
 
19 21
 string goldPairs[];
@@ -53,6 +55,30 @@ void OnTick()
53 55
   {
54 56
 //---
55 57
 
58
+   if(enableBasketTP == true)
59
+     {
60
+      checkBasketTakeProfit();
61
+     }
62
+
63
+   string symbolToBuy = getSymbolWithLowestAsk();
64
+   string symbolToSell = getSymbolWithHighestBid();
65
+
66
+   if(noOfActiveOrdersOfType(POSITION_TYPE_BUY) == 0)
67
+     {
68
+      if(symbolToBuy != NULL)
69
+        {
70
+         placeBuyTrade(symbolToBuy);
71
+        }
72
+     }
73
+
74
+   if(noOfActiveOrdersOfType(POSITION_TYPE_SELL) == 0)
75
+     {
76
+      if(symbolToSell != NULL)
77
+        {
78
+         placeSellTrade(symbolToSell);
79
+        }
80
+     }
81
+
56 82
   }
57 83
 //+------------------------------------------------------------------+
58 84
 //|                                                                  |
@@ -82,11 +108,11 @@ void placeBuyTrade(string symbol)
82 108
 
83 109
    if(trade.PositionOpen(symbol, ORDER_TYPE_BUY, lotSize, ask, buySL, buyTP, "Buy Trade Placed"))
84 110
      {
85
-      Print("Buy Trade Placed on ", symbol, trade.ResultOrder());
111
+      Print("Buy Trade Placed on ", symbol, ": ", trade.ResultOrder());
86 112
      }
87 113
    else
88 114
      {
89
-      Print("Buy Failed on ", symbol, " Error: ", GetLastError());
115
+      Print("Error in placing Buy on ", symbol, ": ", GetLastError());
90 116
      }
91 117
   }
92 118
 //+------------------------------------------------------------------+
@@ -99,11 +125,11 @@ void placeSellTrade(string symbol)
99 125
 
100 126
    if(trade.PositionOpen(symbol, ORDER_TYPE_SELL, lotSize, bid, sellSL, sellTP, "Sell Trade Placed"))
101 127
      {
102
-      Print("Sell Trade Placed on ", symbol, trade.ResultOrder());
128
+      Print("Sell Trade Placed on ", symbol, ": ", trade.ResultOrder());
103 129
      }
104 130
    else
105 131
      {
106
-      Print("Sell Failed on ", symbol, " Error: ", GetLastError());
132
+      Print("Error in placing Sell on ", symbol, ": ", GetLastError());
107 133
      }
108 134
   }
109 135
 //+------------------------------------------------------------------+
@@ -112,19 +138,27 @@ void placeSellTrade(string symbol)
112 138
 void getGoldPairsFromMarketWatch()
113 139
   {
114 140
    int totalSymbols = SymbolsTotal(true);
115
-   ArrayResize(goldPairs, totalSymbols);
141
+   ArrayResize(goldPairs, 0);
142
+   totalGoldPairs = 0;
116 143
 
117 144
    for(int i = 0; i < totalSymbols; i++)
118 145
      {
119 146
       string symbolName = SymbolName(i, true);
120 147
       if(StringFind(symbolName, "GOLD") != -1 || StringFind(symbolName, "XAU") != -1)
121 148
         {
122
-         goldPairs[totalGoldPairs] = symbolName;
123
-         totalGoldPairs++;
149
+         ArrayResize(goldPairs, totalGoldPairs + 1);
150
+         if(totalGoldPairs < ArraySize(goldPairs))
151
+           {
152
+            goldPairs[totalGoldPairs] = symbolName;
153
+            totalGoldPairs++;
154
+           }
155
+         else
156
+           {
157
+            Print("Error: Array resize failed for symbol ", symbolName);
158
+           }
124 159
         }
125 160
      }
126 161
 
127
-   ArrayResize(goldPairs, totalGoldPairs);
128 162
    Print("Found ", totalGoldPairs, " gold pairs in Market Watch");
129 163
   }
130 164
 //+------------------------------------------------------------------+
@@ -136,10 +170,10 @@ string getSymbolWithLowestAsk()
136 170
    if(totalGoldPairs == 0)
137 171
       return NULL;
138 172
 
139
-   string lowestSymbol = goldPairs[0];
173
+   string lowestSymbol = "";
140 174
    double lowestAsk = SymbolInfoDouble(lowestSymbol, SYMBOL_ASK);
141 175
 
142
-   for(int i = 1; i < totalGoldPairs; i++)
176
+   for(int i = 0; i < totalGoldPairs; i++)
143 177
      {
144 178
       double currentAsk = SymbolInfoDouble(goldPairs[i], SYMBOL_ASK);
145 179
       if(currentAsk < lowestAsk)
@@ -149,11 +183,12 @@ string getSymbolWithLowestAsk()
149 183
         }
150 184
      }
151 185
 
186
+//Print("Lowest Ask Pair: ", lowestSymbol, " Lowest Ask: ", lowestAsk);
152 187
    return lowestSymbol;
153 188
   }
154 189
 
155 190
 //+------------------------------------------------------------------+
156
-//|                               |
191
+//|                                                                  |
157 192
 //+------------------------------------------------------------------+
158 193
 string getSymbolWithHighestBid()
159 194
   {
@@ -161,10 +196,10 @@ string getSymbolWithHighestBid()
161 196
    if(totalGoldPairs == 0)
162 197
       return NULL;
163 198
 
164
-   string highestSymbol = goldPairs[0];
199
+   string highestSymbol = "";
165 200
    double highestBid = SymbolInfoDouble(highestSymbol, SYMBOL_BID);
166 201
 
167
-   for(int i = 1; i < totalGoldPairs; i++)
202
+   for(int i = 0; i < totalGoldPairs; i++)
168 203
      {
169 204
       double currentBid = SymbolInfoDouble(goldPairs[i], SYMBOL_BID);
170 205
       if(currentBid > highestBid)
@@ -174,6 +209,94 @@ string getSymbolWithHighestBid()
174 209
         }
175 210
      }
176 211
 
212
+//Print("Highest Bid Pair: ", highestSymbol, " Highest Bid: ", highestBid);
177 213
    return highestSymbol;
178 214
   }
179 215
 //+------------------------------------------------------------------+
216
+//|                                                                  |
217
+//+------------------------------------------------------------------+
218
+int noOfActiveOrdersOfType(ENUM_POSITION_TYPE type)
219
+  {
220
+   int count = 0;
221
+
222
+   for(int i= PositionsTotal()-1; i>=0; i--)
223
+     {
224
+      ulong ticket = PositionGetTicket(i);
225
+      if(PositionSelectByTicket(ticket))
226
+        {
227
+         if(PositionGetInteger(POSITION_MAGIC)    == magicNo
228
+            && (PositionGetInteger(POSITION_TYPE) == type)
229
+            && isGoldPair(PositionGetString(POSITION_SYMBOL)))
230
+           {
231
+            count++;
232
+           }
233
+        }
234
+     }
235
+   return count;
236
+  }
237
+//+------------------------------------------------------------------+
238
+//|                                                                  |
239
+//+------------------------------------------------------------------+
240
+void checkBasketTakeProfit()
241
+  {
242
+   double netProfit = 0;
243
+
244
+   for(int i = PositionsTotal() - 1; i >= 0; i--)
245
+     {
246
+      ulong ticket = PositionGetTicket(i);
247
+      if(PositionSelectByTicket(ticket))
248
+        {
249
+         if(isGoldPair(PositionGetString(POSITION_SYMBOL)) &&
250
+            PositionGetInteger(POSITION_MAGIC) == magicNo)
251
+           {
252
+            netProfit += PositionGetDouble(POSITION_PROFIT)+PositionGetDouble(POSITION_SWAP);
253
+           }
254
+        }
255
+     }
256
+
257
+   if(netProfit >= basketTakeProfit)
258
+     {
259
+      Print("Basket TP hit: Closing all trades. Profit = ", netProfit);
260
+      closeAllActiveOrders();
261
+     }
262
+  }
263
+//+------------------------------------------------------------------+
264
+//|                                                                  |
265
+//+------------------------------------------------------------------+
266
+void closeAllActiveOrders()
267
+  {
268
+   for(int i=PositionsTotal()-1; i >=0 ; i--)
269
+     {
270
+      ulong ticket = PositionGetTicket(i);
271
+      if(PositionSelectByTicket(ticket))
272
+        {
273
+         if(PositionGetInteger(POSITION_MAGIC) == magicNo &&
274
+            isGoldPair(PositionGetString(POSITION_SYMBOL)))
275
+           {
276
+            if(trade.PositionClose(ticket))
277
+              {
278
+               Print("Position closed ", ticket);
279
+              }
280
+            else
281
+              {
282
+               Print("Cannot close order: ",GetLastError());
283
+              }
284
+           }
285
+        }
286
+     }
287
+
288
+  }
289
+//+------------------------------------------------------------------+
290
+//|                                                                  |
291
+//+------------------------------------------------------------------+
292
+bool isGoldPair(string symbol)
293
+  {
294
+   for(int i = 0; i < totalGoldPairs; i++)
295
+     {
296
+      //Print("Total Gold pairs: ", totalGoldPairs, " Array: ", goldPairs[i], " Symbol: ", symbol);
297
+      if(goldPairs[i] == symbol)
298
+         return true;
299
+     }
300
+   return false;
301
+  }
302
+//+------------------------------------------------------------------+