Is MQL4 Bugged?

 

I am trying one easy code. Open two orders at specific time. It works 6 times on 10. Why sometimes works and sometimes no?


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

extern int MagicNumber = 123;
extern double Lots = 0.01;
extern string TimeEntry = "17:41";
extern bool BuyStop = true;
extern bool SellStop = true;
extern double Distance = 50;
extern double TP = 100;
extern double MaxOrder = 1;
int ticket;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   
   RefreshRates();
   
   string myTime;
   
   myTime=GetTime();
   
   string TimeWith=TimeToStr(TimeLocal(),TIME_MINUTES);
   
   Comment(TimeWith);
  
   if(TimeWith==TimeEntry)
    {
      if(BuyStop==true && CountBuyStop()<MaxOrder)
      {
       int buyticket = OrderSend(_Symbol,OP_BUYSTOP,Lots,Ask+50*_Point,3,0,0,"BuyStop",MagicNumber,0,Green);
      }
      
     if(SellStop==true && CountSellStop()<MaxOrder)
      {
       int sellticket = OrderSend(_Symbol,OP_SELLSTOP,Lots,Bid-50*_Point,3,0,0,"SellStop",MagicNumber,0,Red);
      }
    }
   
  }
//+------------------------------------------------------------------+
string GetTime()
 {
  string TimeWith;
  TimeWith=TimeToStr(TimeLocal(),TIME_MINUTES);
  return TimeWith;
 }

//---------------------------------------------------------
int CountBuy()
 {
  int NumberofBuy=0;
  for(int i=OrdersTotal()-1; i >= 0; i--)
   {
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    string Pair=OrderSymbol();
    if(_Symbol==Pair)
    if(OrderType()==OP_BUY)
     {
      NumberofBuy=NumberofBuy+1;
     }
   }
  return NumberofBuy;
 }

int CountSell()
 {
  int NumberofSell=0;
  for(int i=OrdersTotal()-1; i >= 0; i--)
   {
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    string Pair=OrderSymbol();
    if(_Symbol==Pair)
    if(OrderType()==OP_SELL)
     {
      NumberofSell=NumberofSell+1;
     }
   }
  return NumberofSell;
 }

int CountBuyStop()
 {
  int NumberofBuyStop=0;
  for(int i=OrdersTotal()-1; i >= 0; i--)
   {
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    string Pair=OrderSymbol();
    if(_Symbol==Pair)
    if(OrderType()==OP_BUYSTOP)
     {
      NumberofBuyStop=NumberofBuyStop+1;
     }
   }
  return NumberofBuyStop;
 }

int CountSellStop()
 {
  int NumberofSellStop=0;
  for(int i=OrdersTotal()-1; i >= 0; i--)
   {
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    string Pair=OrderSymbol();
    if(_Symbol==Pair)
    if(OrderType()==OP_SELLSTOP)
     {
      NumberofSellStop=NumberofSellStop+1;
     }
   }
  return NumberofSellStop;
 }


void DeletePendingOrders()
{
   for( int i = OrdersTotal() - 1; i >= 0; i-- )
   {
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) )
      {
         if( ( OrderMagicNumber() == MagicNumber ) && ( OrderSymbol() == _Symbol ) )
         {
            if( ( OrderType() != OP_BUY ) && ( OrderType() != OP_SELL ) )
            {
               if( !OrderDelete( OrderTicket() ) )
               {
                  // Delete failed (need to deal with situation)
                  // Check Error Codes
               }
            }
         }
      }
   }
}


void CloseDeleteAllOrders()
{
   for( int i = OrdersTotal() - 1; i >= 0; i-- )
   {
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) )
      {
         if( ( OrderMagicNumber() == MagicNumber ) && ( OrderSymbol() == _Symbol ) )
         {
            if( ( OrderType() == OP_BUY ) || ( OrderType() == OP_SELL ) )
            {
               if( !OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 3) )
               {
                  // Close failed (need to deal with situation)
                  // Check Error Codes
               }
            }
            else
            {
               if( !OrderDelete( OrderTicket() ) )
               {
                  // Delete failed (need to deal with situation)
                  // Check Error Codes
               }
            }
         }
      }
   }
}
 
SIMONE MARELLI: t works 6 times on 10. Why sometimes works and sometimes no?
  1. "Doesn't work" is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
  2. 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?
 
William Roeder:
  1. "Doesn't work" is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
  2. 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?

No, i don't expect it. If i change Ordersend with an alert, works perfectly. I can't understand why sometimes goes and sometimes now

 
SIMONE MARELLI: I can't understand why sometimes goes and sometimes now
int buyticket = OrderSend(_Symbol,OP_BUYSTOP,Lots,Ask+50*_Point,3,0,0,"BuyStop",MagicNumber,0,Green);
  1. You would know why if you had checked your return codes for errors, and reported them including GLE/LE, your variable values and the market.
  2. At least we would know,  you are calling your code.
  3. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum
 
William Roeder:
  1. You would know why if you had checked your return codes for errors, and reported them including GLE/LE, your variable values and the market.
  2. At least we would know,  you are calling your code.
  3. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum

Whats is the best way to solve this? I have just alert from compiler, no error, and no error in charts

 
SIMONE MARELLI:

Whats is the best way to solve this? I have just alert from compiler, no error, and no error in charts

Check if the OrderSend is successful, if not successful take the GetLastError() value, and create an alert for you.

 
Roberto Jacobs:

Check if the OrderSend is successful, if not successful take the GetLastError() value, and create an alert for you.

I have no Alert with the error. It's simply ignoring the time and i don't know why!

Reason: