More orders are opened than expected

 

Hello, 

Could you please help me why my orders are opened more than 0.1 on every hour as I would like to have actually ?  Plus, the codes do not open order on every hour sometimes. Why do you think this happens ?

Thank you.

 

#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict


 
double ema;   //exponential moving average
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int start()
  {

double total;
   int cnt;
   while(OpenTradesBuy()>0)
   {
      // close opened orders first
      total = OrdersTotal();
      for (cnt = total-1; cnt >=0 ; cnt--)
      {
         if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) 
         {
            switch(OrderType())
            {
               case OP_BUY       :
                  OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Violet);break;
                  
            }             
         }
      }
   }


// checking OrderSend 

bool check;
check = checkMA();

if (check==true)
return(true);
  
if (check==false)            
return(false);  

return(INIT_SUCCEEDED);
}


bool checkMA() 
{

ema =iMA("EURUSD",0,50,0,MODE_EMA,PRICE_CLOSE,1);           //exponential moving average

//checking the hour to open order on every hour

if ( (Hour()== 0 || Hour()== 1 || Hour()== 2 || Hour()== 3 || Hour()== 4                     
   || Hour()== 5 || Hour()== 6 || Hour()== 7 || Hour()== 8 || Hour()== 9
   || Hour()== 10 || Hour()== 11 || Hour()== 12 || Hour()== 13 || Hour()== 14
   || Hour()== 15 || Hour()== 16 || Hour()== 17 || Hour()== 18 || Hour()== 19
   || Hour()== 20 || Hour()== 21 || Hour()== 22 || Hour()== 23  ) && (Minute()==0) && (Seconds()==0) )
{
if ((Ask > ema))
OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"otomatik al",PERIOD_H1,Blue);       //open buy order

else
return(EMPTY_VALUE);
}
return(0);
}



int OpenTradesBuy()
{
   int icnt, itotal, retval;

   retval=0;
   itotal=OrdersTotal(); 
   ema = iMA (NULL,0,50,0,MODE_EMA,PRICE_CLOSE,1); 
      for(icnt=itotal-1;icnt>=0;icnt--) 
      {
         if (Ask < ema - 200*Point)
         {
         OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
         if(OrderType()==OP_BUY)
             retval++;             
         } 
      }

   return(retval);
}
 
int start()
  {
//
//
//
// checking OrderSend 

bool check;
check = checkMA();

if (check==true)
return(true);
  
if (check==false)            
return(false); 
// All the above 6 lines can be replaced with simply return(checkMA()) 

return(INIT_SUCCEEDED);
}

Tidy your code up, it looks like a lot of cut and pastes

 start() returns an integer, so why are you returning either a bool or INIT_SUCCEEDED

 

if ( (Hour()== 0 || Hour()== 1 || Hour()== 2 || Hour()== 3 || Hour()== 4                     
   || Hour()== 5 || Hour()== 6 || Hour()== 7 || Hour()== 8 || Hour()== 9
   || Hour()== 10 || Hour()== 11 || Hour()== 12 || Hour()== 13 || Hour()== 14
   || Hour()== 15 || Hour()== 16 || Hour()== 17 || Hour()== 18 || Hour()== 19
   || Hour()== 20 || Hour()== 21 || Hour()== 22 || Hour()== 23  ) && (Minute()==0) && (Seconds()==0) )

 You've covered every possible Hour, so what's the point?

It is exactly the same as

if (Minute()==0) && Seconds()==0) )

That means that if there is not an incoming tick in the first second of the hour, the code will not be executed

 
GumRai:

Tidy your code up, it looks like a lot of cut and pastes

 start() returns an integer, so why are you returning either a bool or INIT_SUCCEEDED

 

What do you suggest me to do so if start() returns an integer ? 

 

 You've covered every possible Hour, so what's the point?

It is exactly the same as

That means that if there is not an incoming tick in the first second of the hour, the code will not be executed

 

My point is to open order on every hour. 

Is it possible to make the code executed in the first second of the hours even though there is no tick at that time ?

 

    
 

As there is little or no apparent logic to your code, it is difficult to offer any advice.

  static int static_hour=Hour();
  if(static_hour!=Hour())
     {
     static_hour=Hour()
     //New Hour so can check for a new trade condition
     }

 will execute code only on the first tick of a new hour

 
GumRai:

As there is little or no apparent logic to your code, it is difficult to offer any advice.

 will execute code only on the first tick of a new hour

 

You have already replied my question. 

I appreciate it.