cTrade.Buy() does not work between certain dates in the history

 

I wrote the following code. I expected to have one operation per year. However I see that many operations are missing.

For example: EURUSD (1971-2021) it has 50 years of history but I only get 23 trades.


I have tried with many currency pairs and commodities and many operations are missing as well.

What is the problem? The code is wrong? Any problem with the history data? Do I need to configure something else in MT5? or...?

Thank you very much for your help



#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>

CTrade cTrade;
CSymbolInfo cSymbol;
CPositionInfo cPositionInfo;

MqlDateTime  dt_struct;     
int magicNumber = 1;
//+------------------------------------------------------------------+
void OnInit()
{
   cTrade.SetExpertMagicNumber(magicNumber);
}
//+------------------------------------------------------------------+
void OnTick()
{

   TimeCurrent(dt_struct);   

   
   bool isTrading = cPositionInfo.SelectByMagic(Symbol(), magicNumber);
   
   if( dt_struct.day_of_week==1 && dt_struct.hour>=3 && dt_struct.mon==5 && !isTrading)
   {      
      if(cTrade.Buy(0.01,Symbol(), cSymbol.Ask(), cSymbol.Ask()-1000*cSymbol.Point(), cSymbol.Ask()+1000*cSymbol.Point()))
      {
         printf("Position open");
      }
      else
      {
         printf("Position open FAILL: %i",  cTrade.ResultRetcode()); 
      }
   }
   
   if(dt_struct.day_of_week==1 && dt_struct.hour>=3 && dt_struct.mon==6 && isTrading)
   {
     if(cTrade.PositionClose(cPositionInfo.Ticket()) && cTrade.ResultRetcode()==TRADE_RETCODE_DONE)
     {
         printf("Position close");       
     }
     else
     {
         printf("Position close FAILL: %i",  cTrade.ResultRetcode()); 
     }
   }
    
}
Files:
Settings.png  136 kb
Backtest.png  145 kb
 
UPDATE:

I simplified the code to the maximum and I tested it in the GOLD between 2008-2013 in H1 and no operation was opened (absolute zero).

I suspect now that the problem may be related to the quality of the data history ...
Does anyone know how to fix this problem please?

The following is the simplified code:

#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>

CTrade cTrade;
CSymbolInfo cSymbol;
CPositionInfo cPositionInfo;
    
int magicNumber = 1;
//+------------------------------------------------------------------+
void OnInit()
{
   cTrade.SetExpertMagicNumber(magicNumber);
}
//+------------------------------------------------------------------+
void OnTick()
{

   
   bool isTrading = cPositionInfo.SelectByMagic(Symbol(), magicNumber);
   

   if(!isTrading)   
   {      
      if(cTrade.Buy(0.01,Symbol(), cSymbol.Ask(), cSymbol.Ask()-1000*cSymbol.Point(), cSymbol.Ask()+1000*cSymbol.Point()))
      {
         printf("Position open");
      }
      else
      {
         printf("Position open FAILL: %i",  cTrade.ResultRetcode()); 
      }
   }
   
   if(isTrading)
   {
     if(cTrade.PositionClose(cPositionInfo.Ticket()) && cTrade.ResultRetcode()==TRADE_RETCODE_DONE)
     {
         printf("Position close");       
     }
     else
     {
         printf("Position close FAILL: %i",  cTrade.ResultRetcode()); 
     }
   }
  
    
Files:
 

In OnInit, it is necessary to initialize an object of the CSymbolInfo trade class. Example:

   ResetLastError();
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
   RefreshRates();


Before referring to prices, you need to update the data:

   if(!isTrading)   
     { 
      RefreshRates();
***
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates()
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: ","RefreshRates error");
      return(false);
     }
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: ","Ask == 0.0 OR Bid == 0.0");
      return(false);
     }
//---
   return(true);
  }
 
Vladimir Karputov #:

In OnInit, it is necessary to initialize an object of the CSymbolInfo trade class. Example:


Before referring to prices, you need to update the data:

I have added the code provided by you. However my code still has the same problem. No positions was opened between the indicated dates.

Could it be that my data has hidden holes and that causes the problem? Or maybe it is something else?


I Have this warrnig in the Journal:


OLD MT5 version:

2021.08.28 14:30:06.253    Tester    quality of analyzed history is 0% (1230 minute bars, tick volume errors 0, spread errors 1230)


NEW MT5 version:

2021.08.28 14:43:09.438    Tester    XAUEUR: history check timeout


Thank you very much for your help Vladimir ! !