When Running CopyTicks from an EA no data is returned

 

When running

CopyTicks(Symbol(), ticks_array, COPY_TICKS_ALL, 0, 2000);

from the OnTick() function of a EA the return value is 0 and no ticks are returned even when the function is retried 3 times when no data is returned.

The symbol is EURUSD. ticks_array is a dynamic array but it is allocated 2000 elements with ArrayResize. Also it is set as a series using ArraySetAsSeries.

I am using the MetaQuotes Demo to run this.

When checking for the error code I get error code 0 - ERR_SUCCESS: The operation completed successfully and the number of copied ticks as 0 though I am passing 2000 as the count.

What might be going wrong.

OOP in MQL5 by Example: Processing Warning and Error Codes
OOP in MQL5 by Example: Processing Warning and Error Codes
  • www.mql5.com
The article describes an example of creating a class for working with the trade server return codes and all the errors that occur during the MQL-program run. Read the article, and you will learn how to work with classes and objects in MQL5. At the same time, this is a convenient tool for handling errors; and you can further change this tool according to your specific needs.
 

Put the cursor on CopyRates and press F1 and whether the parameters fit or not.

CopyRates is not CopyTicks!

 
Suminda Dharmasena:When running from the OnTick() function of a EA the return value is 0 and no ticks are returned even when the function is retried 3 times when no data is returned. The symbol is EURUSD. ticks_array is a dynamic array but it is allocated 2000 elements with ArrayResize. Also it is set as a series using ArraySetAsSeries. I am using the MetaQuotes Demo to run this. When checking for the error code I get error code 0 - ERR_SUCCESS: The operation completed successfully and the number of copied ticks as 0 though I am passing 2000 as the count.What might be going wrong.

CopyRates and CopyTicks are two different functions that copy two totally different types of data. You are mixing the two.

Rates refers to the OHLC, Volume and Spread data and Ticks to the Bid/Ask/Last data.

CopyRates

Gets history data of the Rates structure for a specified symbol and period into an array

CopyTicks

Gets ticks in the MqlTick format into ticks_array

 

Sorry my bad I mixed up the code.

I have:

      //---
      bool err = false;
      //---
      for(int i = 0; i < 3; i++)
        {
         const int copied_rates = CopyRates(symbol, timeframe, rates_start, rates_count, Rates);
         if(copied_rates < rates_count)
           {
            Print(__FILE__, ":", __LINE__, " | ", __FUNCSIG__, " Error: Rates - ", ErrCode(GetLastError()), ", copied_rates: ", copied_rates);
            err = true;
           }
         else
           {
            break;
           }
        }
      //---
      for(int i = 0; i < 3; i++)
        {
         const int copied_ticks = CopyTicks(symbol, Ticks, COPY_TICKS_ALL, ticks_start, ticks_count);
         if(copied_ticks < ticks_count)
           {
            Print(__FILE__, ":", __LINE__, " | ", __FUNCSIG__, " Error: Ticks - ", ErrCode(GetLastError()), ", copied_ticks: ", copied_ticks);
            err = true;
           }
         else
           {
            break;
           }
        }
      //---
      if(err)
        {
         Print(__FILE__, ":", __LINE__, " | ", __FUNCSIG__, " Errors ^");
         return;
        }

It is CopyTicks which doesn't work. copied_ticks is after execution is 0. symbol = Symbol(); ticks_start = 0; ticks_count = 2000;

 
Suminda Dharmasena #: Sorry my bad I mixed up the code. I have: It is CopyTicks which doesn't work. copied_ticks is after execution is 0. symbol = Symbol(); ticks_start = 0; ticks_count = 2000;

Why are you looping 3 times? Just exit the OnTick() handler and check the tick data on the next call.

Give us more details.

What error is reported in the log?

Print the symbol, ticks_start and ticks_count to the log as well to make sure the variables being passed are correct.

 
Fernando Carreiro #:

Why are you looping 3 times? Just exit the OnTick() handler and check the tick data on the next call.

Give us more details.

What error is reported in the log?

Print the symbol, ticks_start and ticks_count to the log as well to make sure the variables being passed are correct.

I use to get:


CS 0 15:58:01.826 EPPScalp (EURUSD,M1) 2022.09.26 00:02:00   EATemplate.mqh:580 | void CEATemplate::OnTick() Error: Ticks - 0 - ERR_SUCCESS: The operation completed successfully, copied_ticks: 0

2022.12.06 22:04:49.257 2022.09.26 00:32:00   EATemplate.mqh:580 | void CEATemplate::OnTick() Error: Ticks - 4401 - ERR_HISTORY_NOT_FOUND: Requested history not found, copied_ticks: -1


From newer logs:


2022.12.06 22:40:31.601 2022.09.26 00:14:37   Error: Ticks - 4401 - ERR_HISTORY_NOT_FOUND: Requested history not found, copied_ticks: -1, symbol: EURUSD, ticks_start: 0, ticks_count: 2000


I removed the looping as suggested:


      //---
      bool err1 = false;
      const int copied_rates = CopyRates(symbol, timeframe, rates_start, rates_count, Rates);
      if(copied_rates < rates_count)
        {
         Print(__FILE__, ":", __LINE__, " | ", __FUNCSIG__, "\r\nError: Rates - ", ErrCode(GetLastError()), ", copied_rates: ", copied_rates, ", symbol: ", symbol, ", timeframe: ", timeframe, ", rates_start: ", rates_start, ", rates_count: ", rates_count);
         err1 = true;
        }
      //---
      bool err2 = false;
      const int copied_ticks = CopyTicks(symbol, Ticks, COPY_TICKS_ALL, ticks_start, ticks_count);
      if(copied_ticks < ticks_count)
        {
         Print(__FILE__, ":", __LINE__, " | ", __FUNCSIG__, "\r\nError: Ticks - ", ErrCode(GetLastError()), ", copied_ticks: ", copied_ticks, ", symbol: ", symbol, ", ticks_start: ", ticks_start, ", ticks_count: ", ticks_count);
         err2 = true;
        }
      //---
      if(err1 || err2)
        {
         Print(__FILE__, ":", __LINE__, " | ", __FUNCSIG__, "\r\nErrors ^");
         return;
        }
      //---


I also make sure it is trading times:


bool TradingTime(const string symbol, const uint session_index = 0)
  {
   MqlDateTime current_datetime;
   const datetime now = TimeCurrent(current_datetime);
   current_datetime.year = 1970;
   current_datetime.mon = 1;
   current_datetime.day = 1;
   const datetime time_now = StructToTime(current_datetime);
   datetime from;
   datetime to;
   const bool result = SymbolInfoSessionTrade(symbol, (ENUM_DAY_OF_WEEK) current_datetime.day_of_week, session_index, from, to);
   if(!result)
     {
      Print(__FILE__, ":", __LINE__, " | ", __FUNCSIG__, "\r\nError in checking times.", "Symbol: ", symbol, ", Now: ", time_now, ", from: ", from, ", to: ", to, ", ", DateTimeStructToString(current_datetime));
      return(false);
     }
   return(from <= time_now && time_now < to);
  }
 
Suminda Dharmasena #: Error: Ticks - 4401 - ERR_HISTORY_NOT_FOUND: Requested history not found, copied_ticks: -1

It reports that there is no Tick Data History, so did you verify the situation?

Did you open up the Symbols (Ctrl-U) properties and verify if there is any tick data being provided by the Broker for that symbol?


 
Fernando Carreiro #:

It reports that there is no Tick Data History, so did you verify the situation?

Did you open up the Symbols (Ctrl-U) properties and verify if there is any tick data being provided by the Broker for that symbol?


I think I have ticks:


 

I think ticks worth a few days are missing. The date on the 1st tick is a few days later.

What is happening here?

 

Please test the following code and show the log results:

void OnTick() {
   // Acquire new tick data
      MqlTick oTicks[];
      ResetLastError();
      int nTicksCount = CopyTicks( _Symbol, oTicks, COPY_TICKS_ALL, 0 );
      if( nTicksCount > 0 ) {
         PrintFormat( "Fount %d ticks, Starting time: %lld, Ending Time: %lld",
            nTicksCount, oTicks[ 0 ].time_msc, oTicks[ nTicksCount - 1 ].time_msc );
      } else {
         // Report any errors while acquiring the tick data
            if( ( nTicksCount < 0 ) || ( _LastError != ERR_SUCCESS ) )
               PrintFormat( "Error %d: Unable to acquire tick data for symbol %s",
                  _LastError, _Symbol );
      };
};
2022.12.06 17:41:26.378 TestTicks (EURUSD,M1)   Fount 2000 ticks, Starting time: 1670347019202, Ending Time: 1670348486300
2022.12.06 17:41:26.489 TestTicks (EURUSD,M1)   Fount 2000 ticks, Starting time: 1670347019312, Ending Time: 1670348486413
2022.12.06 17:41:26.886 TestTicks (EURUSD,M1)   Fount 2000 ticks, Starting time: 1670347019438, Ending Time: 1670348486809
2022.12.06 17:41:29.347 TestTicks (EURUSD,M1)   Fount 2000 ticks, Starting time: 1670347019844, Ending Time: 1670348489272
2022.12.06 17:41:30.454 TestTicks (EURUSD,M1)   Fount 2000 ticks, Starting time: 1670347019965, Ending Time: 1670348490358
 

There doesn't seem to be anything wrong.

The log is attached.

Files:
20221207.zip  4385 kb
Reason: