EA keeps repeating orders and payments.

 
When I run this EA, it keeps repeating orders and payments.
And only for sell orders.
It works fine for buy orders.

The logic of this EA is simple.
Enter when the price reaches the Bollinger Bands.
Close when price reaches SMA.

Please tell me what to do.

extern int BB_period = 25;
extern double Lots = 0.01;
extern int SL = 500;
extern int order_MagicNo = 1111;
  
datetime prevtime;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---
            
//---
   return(INIT_SUCCEEDED);
  }
         
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int start()
{
int orderPtn=0; 
int total=0;
int nottwice;         
int errorcode;               
int i=0;
int count=0;
int ticket_No;
int order_entry_Type=0;  
double order_stop_price=0,order_good_price=0,order_entry_price=0; 
bool orderClose=0 ;
bool orderselect;        
//---
   //Get value of MA
   double MA    = iMA   (NULL,PERIOD_M5,BB_period,0,MODE_SMA,PRICE_CLOSE,0);   
   
   //one position only      
   if(nottwice == 1)
   {
      if(Bid < MA)
      {
         nottwice = 0;
      }
   }
   else if(nottwice == 2)
   {
      if(Bid > MA)
      {
         nottwice = 0;
      }
   }         
         
   //Get value of BB
   double BB3UP = iBands(NULL,PERIOD_M5,BB_period,1,0,PRICE_CLOSE,MODE_UPPER,0); 
   double BB3LO = iBands(NULL,PERIOD_M5,BB_period,1,0,PRICE_CLOSE,MODE_LOWER,0); 
   
//***Trading judgment point***//
         
   if(BB3UP <= Bid && nottwice == 0) 
   {
      orderPtn=1;
      nottwice = 1;
   }
   else if(BB3LO >= Bid && nottwice == 0)
   {
      orderPtn=2;
      nottwice =2;
   }
   else
   {
      orderPtn=0;
   }
            
//***Close order judgment***//
   
   for(i = OrdersTotal() - 1; i >= 0; i--)
   {
      orderselect = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderMagicNumber() == order_MagicNo)
      {
         count++;
         break;
      }
   }
   if(count == 1)
   {    
      if( order_entry_Type == OP_BUY && Bid >= MA) 
      {
         orderClose = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),100,clrBlue);
            if( orderClose == false )
            {
               Print("Close order judgment=",GetLastError());
            }
      }
      if( order_entry_Type == OP_SELL && MA >= Bid)  
      {
         orderClose = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),100,clrBlue);
            if( orderClose == false )
            {
               Print("Close order judgment=",GetLastError());
            }
      }
      
   }
//***Entry point judgment***//
   else if(count == 0 && orderPtn > 0)
   {
      if(orderPtn == 1)
      {
         order_entry_price = Bid;               
         order_entry_Type = OP_SELL;                 
         order_stop_price = Bid + SL * Point;  
      }
      else if(orderPtn == 2)
      {
         order_entry_price = Ask;               
         order_entry_Type = OP_BUY;             
         order_stop_price = Ask - SL * Point;  
      }
                        
      ticket_No = OrderSend(NULL,order_entry_Type,Lots,order_entry_price,100,order_stop_price,order_good_price,"good entry",order_MagicNo,0,clrRed);
                 
      if ( ticket_No == -1)           
      {
         errorcode = GetLastError();      
         if( errorcode != ERR_NO_ERROR)   
         {
             printf("error");
         }
      }
      else {    
         Print("new order ticket_No",ticket_No);
      }
   }
         
         
   return(0);
}
 

Do not double post!

I have deleted your duplicated topic.

 
goodman:

I have just deleted another duplicate topic that you have opened despite my previous post!

I will ban you for one day. If you persist, you can expect a longer ban next time.

Reason: