Basic question about how many bars to copy when using CopyBuffer in an EA.

 

I need a large number of bars (E.g. 1000) of indicator data in my EA. When the EA starts, obviously it will then need to copy 1000 bars from the indicator in an array. On subsequent ticks (or in my case, subsequent bars, since it is stopped from doing the copy every tick), should I copy 1000 bars again, or can I just copy the most recent bar and everything will shift along, with the old now unneeded data falling off the back of the array?


void OnTick()
{     
   static datetime prevTime=0; 
   datetime lastTime[1];
   
   if(CopyTime(_Symbol,_Period,0,1,lastTime)==1 && prevTime!=lastTime[0])
   { 
      int copy_count = (prevTime==0) ? 1000 : 2; // is this correct? or should it be 1000 every bar?
      prevTime=lastTime[0];
      
      CopyBufferFunction(ATR_indicator_handle,1,0,copy_count,atr_main_buff);   
      CopyBufferFunction(ATR_indicator_handle,0,0,copy_count,atr_av_buff); 
   }  
}   
   
int CopyBufferFunction(int indicator_handle,int buffer_number,int start_index,int count,double &destination_buffer[])
{
   bool success = false; 
   int  attempts = 0;
   while(!success && attempts<100)
   {
      if(CopyBuffer(indicator_handle,buffer_number,start_index,count,destination_buffer)<=0)
      {
         success = false; 
         Print("Copying of buffer ",IntegerToString(buffer_number)," has failed: ",ErrorDescription(GetLastError()));
         Sleep(1000);
         attempts++;
      } 
      else 
      {
         success = true; 
      }     
   }
   
   if(success) return 1;
   else        return -1;
}
 
Tom Clayson:

I need a large number of bars (E.g. 1000) of indicator data in my EA. When the EA starts, obviously it will then need to copy 1000 bars from the indicator in an array. On subsequent ticks (or in my case, subsequent bars, since it is stopped from doing the copy every tick), should I copy 1000 bars again, or can I just copy the most recent bar and everything will shift along, with the old now unneeded data falling off the back of the array?


As much as I know, values in buffer will not be "shifted" once, you fill it with new values, but when you copy to it, the old values will be overwritten. So it only looks like they shift. Ok, you could shift them by looping through it and reposition all 999 values. Actually it depends on what you need, I tend to copy what I need, when I need and not more than I need, so that EA doesn't consume more resouces than necessary.
 
Tom Clayson:

I need a large number of bars (E.g. 1000) of indicator data in my EA. When the EA starts, obviously it will then need to copy 1000 bars from the indicator in an array. On subsequent ticks (or in my case, subsequent bars, since it is stopped from doing the copy every tick), should I copy 1000 bars again, or can I just copy the most recent bar and everything will shift along, with the old now unneeded data falling off the back of the array?



copy the 1000 values once, then after a new bar add only the last one

 
Each time you need to look at the indicator, copy the indicator's buffer. Look at it.