Trade function opening multiple trades instead of one

 
Hi guys, I have created a function for opening buy/ sell trades, but the EA opens more than 1 trades when I set the trade number 0 someone kindly help, I have tried adding magic number but still same result. Thanks in advance
//+------------------------------------------------------------------+
//|Buy Orders                                                        |
//+------------------------------------------------------------------+
void BuyTrades( string symbol, double volume, int Trades){
   double ASK=NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_ASK),_Digits);
   int Buy=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)
     Buy++;
    }
   }
    if(Buy<=Trades)
    { Trade.Buy(volume,symbol,ASK,0,0,NULL);}
}


//+------------------------------------------------------------------+
//|Sell Orders                                                       |
//+------------------------------------------------------------------+
void SellTrades( string symbol, double volume, int Trades){
   double BID=NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_BID),_Digits);
   int Sell=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_SELL)
     Sell++;
    }
   }
    if(Sell<=Trades)
    { Trade.Sell(volume,symbol,BID,0,0,NULL);}
}
 
Jones John:
Hi guys, I have created a function for opening buy/ sell trades, but the EA opens more than 1 trades when I set the trade number 0 someone kindly help, I have tried adding magic number but still same result. Thanks in advance

Use POSITION_SYMBOL and POSITION_TYPE to check the orders

 
Roberto Jacobs #:

Use POSITION_SYMBOL and POSITION_TYPE to check the orders

Thanks let me try that

 
Jones John:
Hi guys, I have created a function for opening buy/ sell trades, but the EA opens more than 1 trades when I set the trade number 0 someone kindly help, I have tried adding magic number but still same result. Thanks in advance

It depends on what you have in your events handling function which is not indicated here:

void OnTick()
  {
  // ...
  }
 
assuming that "int trades" is the number of trades that you want the ea to open, then, on 2nd last line of both functions, you have <=, which might need to be <, otherwise you have not added much code for us to analyse. It is most often the lines in your Event Handler that are the cause of this, and not your custom functions, so add more of your code for us.
 
I have the same issue. Any solution???
 
mwahab2000 #: I have the same issue. Any solution???

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem

We can't see your broken code.

 
CodeFx #:

It depends on what you have in your events handling function which is not indicated here:

Exactly. You could use a condition
if(PositionsTotal()==0) to ensure that there is only one position open at all times.