Procházet zdrojové kódy

Ticket : 4797 Multi Pair Entry Bot

1) Works on all Pairs in Market watch
2) Now Combination Logic Added
faiz ali před 1 rokem
rodič
revize
fb8ea12512
2 změnil soubory, kde provedl 53 přidání a 57 odebrání
  1. binární
      V2/CribMarketEAV2.ex5
  2. 53 57
      V2/CribMarketEAV2.mq5

binární
V2/CribMarketEAV2.ex5


+ 53 - 57
V2/CribMarketEAV2.mq5

@@ -54,31 +54,8 @@ int OnInit()
 
    getSymbolsFromMarketWatch();
 
+// addToStructure(153718680, 153718681, "EURUSD", "XAUUSD.");
 
-//   if(enableBasketTP == true)
-//     {
-//      checkBasketTakeProfit();
-//     }
-//
-//   string symbolToBuy = getSymbolWithLowestAsk();
-//   string symbolToSell = getSymbolWithHighestBid();
-////Print(" Symbol to Buy is: ", symbolToBuy, " Symbol to Sell: ", symbolToSell);
-//
-//   if(noOfActiveOrdersOfType(POSITION_TYPE_BUY) < maxOpenPositions)
-//     {
-//      if(symbolToBuy != NULL && symbolToBuy != "")
-//        {
-//         placeBuyTrade(symbolToBuy);
-//        }
-//     }
-//
-//   if(noOfActiveOrdersOfType(POSITION_TYPE_SELL) < maxOpenPositions)
-//     {
-//      if(symbolToSell != NULL && symbolToSell != "")
-//        {
-//         placeSellTrade(symbolToSell);
-//        }
-//     }
 
 //--- create timer
    EventSetMillisecondTimer(1000);
@@ -121,23 +98,32 @@ void OnTimer()
 
    string symbolToBuy = getSymbolWithLowestAsk();
    string symbolToSell = getSymbolWithHighestBid();
+   int buyTickett = -1, sellTickett = -1;
 //Print(" Symbol to Buy is: ", symbolToBuy, " Symbol to Sell: ", symbolToSell);
 
-   if(noOfActiveOrdersOfType(POSITION_TYPE_BUY) < maxOpenPositions)
+   if(canTradeSymbol(symbolToBuy, symbolToSell, POSITION_TYPE_BUY, POSITION_TYPE_SELL))
      {
-      if(symbolToBuy != NULL && symbolToBuy != "")
+      if(noOfActiveOrdersOfType(POSITION_TYPE_BUY) < maxOpenPositions)
         {
-         placeBuyTrade(symbolToBuy);
+         if(symbolToBuy != NULL && symbolToBuy != "")
+           {
+            buyTickett = placeBuyTrade(symbolToBuy);
+           }
         }
-     }
 
-   if(noOfActiveOrdersOfType(POSITION_TYPE_SELL) < maxOpenPositions)
-     {
-      if(symbolToSell != NULL && symbolToSell != "")
+      if(noOfActiveOrdersOfType(POSITION_TYPE_SELL) < maxOpenPositions)
         {
-         placeSellTrade(symbolToSell);
+         if(symbolToSell != NULL && symbolToSell != "")
+           {
+            sellTickett = placeSellTrade(symbolToSell);
+           }
         }
      }
+
+   if(buyTickett != -1 && sellTickett != -1)
+     {
+      addToStructure(buyTickett, sellTickett, symbolToBuy, symbolToSell);
+     }
   }
 //+------------------------------------------------------------------+
 //|                                                                  |
@@ -160,7 +146,7 @@ bool newBar()
 //+------------------------------------------------------------------+
 //|                                                                  |
 //+------------------------------------------------------------------+
-void placeBuyTrade(string symbol)
+int placeBuyTrade(string symbol)
   {
 
    double ask = SymbolInfoDouble(symbol, SYMBOL_ASK);
@@ -169,16 +155,18 @@ 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());
+      return (int)trade.ResultOrder();
      }
    else
      {
       Print("Error in placing Buy on ", symbol, ": ", GetLastError());
      }
+   return 0;
   }
 //+------------------------------------------------------------------+
 //|                                                                  |
 //+------------------------------------------------------------------+
-void placeSellTrade(string symbol)
+int placeSellTrade(string symbol)
   {
 
    double bid = SymbolInfoDouble(symbol, SYMBOL_BID);
@@ -187,11 +175,13 @@ 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());
+      return (int)trade.ResultOrder();
      }
    else
      {
       Print("Error in placing Sell on ", symbol, ": ", GetLastError());
      }
+   return 0;
   }
 //+------------------------------------------------------------------+
 //|                                                                  |
@@ -395,7 +385,7 @@ void removeFromStruct()
       bool buyPresent = false;
       bool sellPresent = false;
 
-      if(newTradeStore[i].buyTicket !=-1 || newTradeStore[i].sellTicket !=-1)
+      if(newTradeStore[i].buyTicket !=-1 && newTradeStore[i].sellTicket !=-1)
         {
          for(int j = PositionsTotal()-1; j>=0; j--)
            {
@@ -413,37 +403,43 @@ void removeFromStruct()
               }
            }
 
-         for(int j = OrdersTotal()-1; j>=0; j--)
+         if(!buyPresent && !sellPresent)
            {
-            ulong ticket = OrderGetTicket(j);
-            if(OrderSelect(ticket))
-              {
-               if(ticket == newTradeStore[i].buyTicket)
-                 {
-                  buyPresent = true;
-                 }
-               if(ticket == newTradeStore[i].sellTicket)
-                 {
-                  sellPresent = true;
-                 }
-              }
-           }
-
-         if(!buyPresent)
-           {
-            Print("Buy ticket closed: ", newTradeStore[i].buyTicket);
+            Print("Buy ticket closed so removed from struct: ", newTradeStore[i].buyTicket);
             newTradeStore[i].buyTicket = -1;
             newTradeStore[i].buySymbol = "";
-           }
-         if(!sellPresent)
-           {
-            Print("Sell ticket closed: ", newTradeStore[i].sellTicket);
+
+            Print("Sell ticket closed so removed from struct: ", newTradeStore[i].sellTicket);
             newTradeStore[i].sellTicket = -1;
             newTradeStore[i].sellSymbol = "";
            }
+        }
+     }
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+bool canTradeSymbol(string symbol1, string symbol2, ENUM_POSITION_TYPE type1, ENUM_POSITION_TYPE type2)
+  {
+   for(int i = 0; i < MaxOrders; i++)
+     {
 
+      if(newTradeStore[i].buyTicket != -1 && newTradeStore[i].sellTicket != -1)
+        {
+         if(newTradeStore[i].buySymbol == symbol1 && newTradeStore[i].sellSymbol == symbol2)
+           {
+            if(type1 == POSITION_TYPE_BUY && type2 == POSITION_TYPE_SELL)
+              {
+               Print("Already Have Buy and Sell on this Pair");
+               Print("----------- Symbol Buy: ", symbol1, " Symbol Sell: ", symbol2, " Buy Ticket: ", newTradeStore[i].buyTicket,
+                     " Sell Ticket: ", newTradeStore[i].sellTicket," ---------------");
+               return false;
+              }
+           }
         }
+
      }
+   return true;
   }
 //+------------------------------------------------------------------+
 //|                                                                  |