OnCalculate code not working in Strategy Tester

 

Hi,

this is an example code that is working correctly in terminal, but it is not working in stratey tester.

It give me error 4004 on CopyTicks call, but if i code it whitout "while" it is working correctly.


Can someone explain me the reason, please?


ulong last_tick_time = 1;
int to_copy = 2000;

int OnCalculate(const int32_t rates_total,
                const int32_t prev_calculated,
                const int32_t begin,
                const double &price[])
{

   MqlTick ticks[];
   while(1 && !IsStopped()){
   
      ResetLastError();
      int copied = CopyTicks(_Symbol, ticks, COPY_TICKS_ALL, last_tick_time, to_copy);
      if(copied>0){
         Print("Copied "+copied+" ticks from "+TimeToString(last_tick_time/1000, TIME_DATE|TIME_SECONDS)+"'"+(last_tick_time%1000)+ " to "+TimeToString(ticks[copied - 1].time_msc/1000, TIME_DATE|TIME_SECONDS)+"'"+(ticks[copied - 1].time_msc%1000));         
            
         last_tick_time = ticks[copied - 1].time_msc;
         
      }else if(copied<0){
         Print("COPIED: "+copied+" -> "+GetLastError());
      }
      
      if(copied!=-1 && (copied!=to_copy || copied==0) ) break;
   }


   return(rates_total);
}

Documentation on MQL5: CopyTicks / Matrix and Vector Methods
Documentation on MQL5: CopyTicks / Matrix and Vector Methods
  • www.mql5.com
Get ticks from an MqlTick structure into a matrix or a vector. Elements are counted from the past to the present, which means that the tick with...
 
antony23:

Hi,

this is an example code that is working correctly in terminal, but it is not working in stratey tester.

It give me error 4004 on CopyTicks call, but if i code it whitout "while" it is working correctly.


Can someone explain me the reason, please?


First thing to do in such cases is to read the error explicated in documentations.

It says in your case that you have not enough memory to perform the action.

So the code needs more memory than what is remaining in your laptop while running it.

 

In the Strategy Tester, your while(1) loop causes the problem. Unlike live trading, the tester can’t keep giving ticks on demand, so repeated calls inside the same calculation cycle trigger error 4004.

The solution is simple: call CopyTicks() just once per OnCalculate() and let the tester advance time naturally. Also, set last_tick_time = ticks[copied-1].time_msc + 1 to avoid requesting the same tick again.

 
Fasih Saqib #:

In the Strategy Tester, your while(1) loop causes the problem. Unlike live trading, the tester can’t keep giving ticks on demand, so repeated calls inside the same calculation cycle trigger error 4004.

The solution is simple: call CopyTicks() just once per OnCalculate() and let the tester advance time naturally. Also, set last_tick_time = ticks[copied-1].time_msc + 1 to avoid requesting the same tick again.

Hi, yes i understood that While(1) is not supprted in TESTER.

In Tester, can i be sure onCalculate and onTick() are called for each tick in real tick mode? Or, like live chart, they can miss ticks?

About last_tick_time, yes, mine was an example.