Ver código fonte

Ticket : 4751 Entry Conditions

1) Symbol fetching Updated
2) Basket TP Added
3) Closing Functionality Added
faiz ali 1 ano atrás
pai
commit
21fdf15459
2 arquivos alterados com 136 adições e 13 exclusões
  1. BIN
      CribMarketEA.ex5
  2. 136 13
      CribMarketEA.mq5

BIN
CribMarketEA.ex5


+ 136 - 13
CribMarketEA.mq5

@@ -14,6 +14,8 @@ CTrade  trade;
 input       string               Settings              = " ------------- General Settings ------------- ";   //_
 input       int                  magicNo               = 333;                 // Magic no
 input       double               lotSize               = 0.1;                 // Lot Size
+input       bool                 enableBasketTP        = false;               // Enable Basket TP
+input       double               basketTakeProfit      = 150.0;               // Basket Take Profit
 
 
 string goldPairs[];
@@ -53,6 +55,30 @@ void OnTick()
   {
 //---
 
+   if(enableBasketTP == true)
+     {
+      checkBasketTakeProfit();
+     }
+
+   string symbolToBuy = getSymbolWithLowestAsk();
+   string symbolToSell = getSymbolWithHighestBid();
+
+   if(noOfActiveOrdersOfType(POSITION_TYPE_BUY) == 0)
+     {
+      if(symbolToBuy != NULL)
+        {
+         placeBuyTrade(symbolToBuy);
+        }
+     }
+
+   if(noOfActiveOrdersOfType(POSITION_TYPE_SELL) == 0)
+     {
+      if(symbolToSell != NULL)
+        {
+         placeSellTrade(symbolToSell);
+        }
+     }
+
   }
 //+------------------------------------------------------------------+
 //|                                                                  |
@@ -82,11 +108,11 @@ void placeBuyTrade(string symbol)
 
    if(trade.PositionOpen(symbol, ORDER_TYPE_BUY, lotSize, ask, buySL, buyTP, "Buy Trade Placed"))
      {
-      Print("Buy Trade Placed on ", symbol, trade.ResultOrder());
+      Print("Buy Trade Placed on ", symbol, ": ", trade.ResultOrder());
      }
    else
      {
-      Print("Buy Failed on ", symbol, " Error: ", GetLastError());
+      Print("Error in placing Buy on ", symbol, ": ", GetLastError());
      }
   }
 //+------------------------------------------------------------------+
@@ -99,11 +125,11 @@ void placeSellTrade(string symbol)
 
    if(trade.PositionOpen(symbol, ORDER_TYPE_SELL, lotSize, bid, sellSL, sellTP, "Sell Trade Placed"))
      {
-      Print("Sell Trade Placed on ", symbol, trade.ResultOrder());
+      Print("Sell Trade Placed on ", symbol, ": ", trade.ResultOrder());
      }
    else
      {
-      Print("Sell Failed on ", symbol, " Error: ", GetLastError());
+      Print("Error in placing Sell on ", symbol, ": ", GetLastError());
      }
   }
 //+------------------------------------------------------------------+
@@ -112,19 +138,27 @@ void placeSellTrade(string symbol)
 void getGoldPairsFromMarketWatch()
   {
    int totalSymbols = SymbolsTotal(true);
-   ArrayResize(goldPairs, totalSymbols);
+   ArrayResize(goldPairs, 0);
+   totalGoldPairs = 0;
 
    for(int i = 0; i < totalSymbols; i++)
      {
       string symbolName = SymbolName(i, true);
       if(StringFind(symbolName, "GOLD") != -1 || StringFind(symbolName, "XAU") != -1)
         {
-         goldPairs[totalGoldPairs] = symbolName;
-         totalGoldPairs++;
+         ArrayResize(goldPairs, totalGoldPairs + 1);
+         if(totalGoldPairs < ArraySize(goldPairs))
+           {
+            goldPairs[totalGoldPairs] = symbolName;
+            totalGoldPairs++;
+           }
+         else
+           {
+            Print("Error: Array resize failed for symbol ", symbolName);
+           }
         }
      }
 
-   ArrayResize(goldPairs, totalGoldPairs);
    Print("Found ", totalGoldPairs, " gold pairs in Market Watch");
   }
 //+------------------------------------------------------------------+
@@ -136,10 +170,10 @@ string getSymbolWithLowestAsk()
    if(totalGoldPairs == 0)
       return NULL;
 
-   string lowestSymbol = goldPairs[0];
+   string lowestSymbol = "";
    double lowestAsk = SymbolInfoDouble(lowestSymbol, SYMBOL_ASK);
 
-   for(int i = 1; i < totalGoldPairs; i++)
+   for(int i = 0; i < totalGoldPairs; i++)
      {
       double currentAsk = SymbolInfoDouble(goldPairs[i], SYMBOL_ASK);
       if(currentAsk < lowestAsk)
@@ -149,11 +183,12 @@ string getSymbolWithLowestAsk()
         }
      }
 
+//Print("Lowest Ask Pair: ", lowestSymbol, " Lowest Ask: ", lowestAsk);
    return lowestSymbol;
   }
 
 //+------------------------------------------------------------------+
-//|                               |
+//|                                                                  |
 //+------------------------------------------------------------------+
 string getSymbolWithHighestBid()
   {
@@ -161,10 +196,10 @@ string getSymbolWithHighestBid()
    if(totalGoldPairs == 0)
       return NULL;
 
-   string highestSymbol = goldPairs[0];
+   string highestSymbol = "";
    double highestBid = SymbolInfoDouble(highestSymbol, SYMBOL_BID);
 
-   for(int i = 1; i < totalGoldPairs; i++)
+   for(int i = 0; i < totalGoldPairs; i++)
      {
       double currentBid = SymbolInfoDouble(goldPairs[i], SYMBOL_BID);
       if(currentBid > highestBid)
@@ -174,6 +209,94 @@ string getSymbolWithHighestBid()
         }
      }
 
+//Print("Highest Bid Pair: ", highestSymbol, " Highest Bid: ", highestBid);
    return highestSymbol;
   }
 //+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+int noOfActiveOrdersOfType(ENUM_POSITION_TYPE type)
+  {
+   int count = 0;
+
+   for(int i= PositionsTotal()-1; i>=0; i--)
+     {
+      ulong ticket = PositionGetTicket(i);
+      if(PositionSelectByTicket(ticket))
+        {
+         if(PositionGetInteger(POSITION_MAGIC)    == magicNo
+            && (PositionGetInteger(POSITION_TYPE) == type)
+            && isGoldPair(PositionGetString(POSITION_SYMBOL)))
+           {
+            count++;
+           }
+        }
+     }
+   return count;
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void checkBasketTakeProfit()
+  {
+   double netProfit = 0;
+
+   for(int i = PositionsTotal() - 1; i >= 0; i--)
+     {
+      ulong ticket = PositionGetTicket(i);
+      if(PositionSelectByTicket(ticket))
+        {
+         if(isGoldPair(PositionGetString(POSITION_SYMBOL)) &&
+            PositionGetInteger(POSITION_MAGIC) == magicNo)
+           {
+            netProfit += PositionGetDouble(POSITION_PROFIT)+PositionGetDouble(POSITION_SWAP);
+           }
+        }
+     }
+
+   if(netProfit >= basketTakeProfit)
+     {
+      Print("Basket TP hit: Closing all trades. Profit = ", netProfit);
+      closeAllActiveOrders();
+     }
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void closeAllActiveOrders()
+  {
+   for(int i=PositionsTotal()-1; i >=0 ; i--)
+     {
+      ulong ticket = PositionGetTicket(i);
+      if(PositionSelectByTicket(ticket))
+        {
+         if(PositionGetInteger(POSITION_MAGIC) == magicNo &&
+            isGoldPair(PositionGetString(POSITION_SYMBOL)))
+           {
+            if(trade.PositionClose(ticket))
+              {
+               Print("Position closed ", ticket);
+              }
+            else
+              {
+               Print("Cannot close order: ",GetLastError());
+              }
+           }
+        }
+     }
+
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+bool isGoldPair(string symbol)
+  {
+   for(int i = 0; i < totalGoldPairs; i++)
+     {
+      //Print("Total Gold pairs: ", totalGoldPairs, " Array: ", goldPairs[i], " Symbol: ", symbol);
+      if(goldPairs[i] == symbol)
+         return true;
+     }
+   return false;
+  }
+//+------------------------------------------------------------------+