EA - too many buys and sells in one minute

 

Good day,

I'm beginner in MQL 4 and building my first EA. I want that the EA will buy or sell everyday from 9:00 to 10:59 depends on High and Low of H1 candle starting at 8:00 same day and sell it if it reaches the stoploss or profit target. It is actually working in that time, but sometimes it is not buying or selling the order too early. You can see it in the statement from 19.8.2016, which i attach. Can you please help me fix this error. I really don't know, where the mistake is. And it is not caused by computer or user account. Tested on two different computers on two different account and it is still work the same. Thank you for you answers.

GunSpawn

PS: Sorry for my bad English

 

Here is my code:

//+------------------------------------------------------------------+
//|                                                         Test.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

extern double StopLoss_v_Pipech = 200;
extern double Profit_Target_v_Pipech = 200;
extern double Velikost_pozice = 0.01;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Print("assa");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int D = DayOfWeek(); // zjistí den v týdnu (neděle=0 … pátek=5)
   int m = TimeMinute(TimeCurrent()); // zjistí aktuální minutu
   int h = TimeHour(TimeCurrent()); // zjistí aktuální hodinu
   int ticket, close;
   double SL = StopLoss_v_Pipech * Point;
   double PT = Profit_Target_v_Pipech * Point;
   int Magic_number = 1552;
   string Text;
   MqlTick last_tick;
   
    /*Print("Current bar for GBPUSD+ H1: ",iTime("GBPUSD+",PERIOD_H1,1),", ",  iOpen("GBPUSD+",PERIOD_H1,1),", ",
                                      iHigh("GBPUSD+",PERIOD_H1,1),", ",  iLow("GBPUSD+",PERIOD_H1,1),", ",
                                     iClose("GBPUSD+",PERIOD_H1,1),", ", iVolume("GBPUSD+",PERIOD_H1,1));
   */
   
   //close
   for (int p = 0;p < OrdersTotal();p++)
   {
      if (OrderSelect(p, SELECT_BY_POS, MODE_TRADES) == true &&
      D >= 0 && h == 10 && m == 31 && OrderMagicNumber() == Magic_number)
      {
         if (OrderType() == OP_BUY)
         {  close = OrderClose(OrderTicket(),Velikost_pozice,Bid,2,Yellow);   }
         if (OrderType() == OP_SELL)
         {  close = OrderClose(OrderTicket(),Velikost_pozice,Ask,2,Yellow);   }
      }
   }
   //projde všechny příkazy
   for (int q = 0;q < OrdersTotal();q++)
   {
      //pokud je nějaké číslo rovno našemu, return 0
      if (OrderSelect(q,SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == Magic_number)
      { return 0;}
   }
   
   //buy
   if (SymbolInfoTick(Symbol(),last_tick))
   {
      if ((D >= 0 && h == 9 && m >= 00) && last_tick.ask >= iHigh(Symbol(),PERIOD_H1,1))
      {
         ticket = OrderSend(Symbol(),OP_BUY,0.01,Ask,0,Ask-SL,Ask+PT,Text, Magic_number,0,Blue);
         //symbol = měnový pár (univ.)
         //cmd = typ příkazu (market order = okamžitý, limit = za kolik se to má koupit, stop = za kolik se to má maximálně koupit
         //volume = velikost pozice v lotech
         //price = cena, za kterou vstupujeme (ask = buy, bid = sell)
         //slippage = max. odchylka příkazu
         //stoploss = podmínka ukončení odbchodu pokud jde obchod proti nám(ask = aktuální tržní cena)
         //takeprofit = podmínka ukončení obchodu, když jde s námi
         //comment = komentář 
         //magic = identifikační číslo příkazu
         //expiration = doba platnosti (pouze pro čekající pokyny)
         //arrow_color = barva grafu
      }
      else if ((D >= 0 && h == 10 && m >= 00) && last_tick.ask >= iHigh(Symbol(),PERIOD_H1,2))
      {
         ticket = OrderSend(Symbol(),OP_BUY,0.01,Ask,0,Ask-SL,Ask+PT,Text, Magic_number,0,Blue);
      }
      //sell
      else if ((D >= 0 && h == 9 && m >= 00) && last_tick.ask <= iLow(Symbol(),PERIOD_H1,1))
      {
         ticket = OrderSend(Symbol(),OP_SELL,0.01,Bid,0,Bid+SL,Bid-PT,Text, Magic_number,0,Red);  
      }
      else if ((D >= 0 && h == 10 && m >= 00) && last_tick.ask <= iLow(Symbol(),PERIOD_H1,2))
      {
         ticket = OrderSend(Symbol(),OP_SELL,0.01,Bid,0,Bid+SL,Bid-PT,Text, Magic_number,0,Red);
      }
   }
}
//+------------------------------------------------------------------+
Reason: