Bot opening multiple trades when IF function included

 
Hi everyone, I have a question, the below function can open 1 sell stop and buy stop when trades number is set at 0. But when I include the function in time and date to open trade at specific time and day of the week, it opens multiple trades what might the issue?
void StopTrades( string symbol, double volume, int BuyPips, int SellPips, int Trades_Number, ENUM_TIMEFRAMES period, int EntryType){
   double ASK=NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_ASK),_Digits);
   double BID=NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_BID),_Digits);
   double Entry= NormalizeDouble(iClose(symbol,period,1),_Digits);
   double buyentryprice;
   double sellentryprice;
   if(EntryType==1)
   {
     buyentryprice= NormalizeDouble(Entry+(BuyPips* _Point),_Digits);
     sellentryprice=NormalizeDouble(Entry-(SellPips* _Point),_Digits);
   }
   if(EntryType==2)
   {
     buyentryprice= NormalizeDouble(ASK+(BuyPips* _Point),_Digits);
     sellentryprice=NormalizeDouble(BID-(SellPips* _Point),_Digits);
   }
   int PendingBuy=0;
   int PendingSell=0;
   for(int b=OrdersTotal()-1;b>=0;b--)
   {
    OrderSelect(OrderGetTicket(b));
    string OrderSymbol=OrderGetString(ORDER_SYMBOL);
    if(OrderSymbol==symbol)
    {
     ENUM_ORDER_TYPE Type=(ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE);
     if(Type==ORDER_TYPE_BUY_STOP)
     PendingBuy++;
     if(Type==ORDER_TYPE_SELL_STOP)
     PendingSell++;
    }
   }
    if(PendingBuy<=Trades_Number)
    { Trade.BuyStop(volume,buyentryprice,symbol,0,0,ORDER_TIME_GTC,0,NULL);}
    if(PendingSell<=Trades_Number)
    { Trade.SellStop(volume,sellentryprice,symbol,0,0,ORDER_TIME_GTC,0,NULL);}
}

void OnTick()
 {
  StopTrades(symbol,....)//Opens Correct Trades
  
  if(DayofWeek==1 && Time="08:00"//Example of time I have Time function)
   {
    StopTrades(...)//Opens Multiple Trades once if function is added
   }
 }
 
Jones John:
Hi everyone, I have a question, the below function can open 1 sell stop and buy stop when trades number is set at 0. But when I include the function in time and date to open trade at specific time and day of the week, it opens multiple trades what might the issue?

maybe need to increase TradesNumber after stop buystop/sellstop order has been sent?

or add else between those two if's (pending<=Trades_Number)
 
Jones John it opens multiple trades what might the issue?

Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
          Code debugging - Developing programs - MetaEditor Help
          Error Handling and Logging in MQL5 - MQL5 Articles (2015)
          Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
          Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)