Refresh() with GetData() and CopyBuffer()

 

According to https://www.mql5.com/en/docs/standardlibrary/technicalindicators/cindicators/cindicator/cindicatorrefresh, it is recommended to always call Refresh() when retrieving data with GetData(). However, looking at the standard lib code (e.g., ExpertBase.mqh),it does not follow this pattern:

//+------------------------------------------------------------------+
//| Access to data of the Open timeseries.                           |
//+------------------------------------------------------------------+
double CExpertBase::Open(int ind) const
  {
//--- check pointer
   if(m_open==NULL)
      return(EMPTY_VALUE);
//--- return the result
   return(m_open.GetData(ind));
  }


First Question:  Does this mean, that I should I call m_open.Refresh() before calling signal.Open(0), e.g.:

// correct way?
m_open.Refresh();
signal.Open(0)


Second Question:  Am I right in saying that using CopyBuffer() eliminates the need to call Refresh()? If so, would the CopyBuffer() method be faster than the GetData() method?

 

The answers to your questions are as follows:

  • No, you do not need to call m_open.Refresh() before signal.Open(0) when working within the Standard Library framework. The framework already ensures that the necessary series are refreshed.

  • Yes, CopyBuffer() always retrieves up-to-date data. However, once a refresh has been performed, GetData() is typically faster for repeated element access because it works directly with the internal buffers.

I hope this helps clarify the distinction, though please note this is based on my understanding and interpretation.

 
Wilna Barnard #:

The answers to your questions are as follows:

  • No, you do not need to call m_open.Refresh() before signal.Open(0) when working within the Standard Library framework. The framework already ensures that the necessary series are refreshed.

  • Yes, CopyBuffer() always retrieves up-to-date data. However, once a refresh has been performed, GetData() is typically faster for repeated element access because it works directly with the internal buffers.

I hope this helps clarify the distinction, though please note this is based on my understanding and interpretation.

Looking at the code of Indicators::GetData, it already calls CopyBuffer internally. 

// FROM StandardLib "Indicator.mqh"

//+------------------------------------------------------------------+
//| API access method "Copying the buffer of indicator by specifying |
//| a start position and number of elements"                         |
//+------------------------------------------------------------------+
int CIndicator::GetData(const int start_pos,const int count,const int buffer_num,double &buffer[]) const
  {
//--- check
   if(m_handle==INVALID_HANDLE)
     {
      SetUserError(ERR_USER_INVALID_HANDLE);
      return(-1);
     }
   if(buffer_num>=m_buffers_total)
     {
      SetUserError(ERR_USER_INVALID_BUFF_NUM);
      return(-1);
     }
//---
   return(CopyBuffer(m_handle,buffer_num,start_pos,count,buffer));
  }


Therefore, I don't understand why the documentation recommends always calling indicator.Refresh() when working with indicator.GetData() (https://www.mql5.com/en/docs/standardlibrary/technicalindicators/cindicators/cindicator/cindicatorrefresh). That's confusing..

Documentation on MQL5: Standard Library / Indicators / Base classes / CIndicator / Refresh
Documentation on MQL5: Standard Library / Indicators / Base classes / CIndicator / Refresh
  • www.mql5.com
Updates the indicator data. It is recommended calling the method before using GetData() . Parameters flags=OBJ_ALL_PERIODS [in]  Timeframe...
 
harryma23 #:

Looking at the code of Indicators::GetData, it already calls CopyBuffer internally. 


Therefore, I don't understand why the documentation recommends always calling indicator.Refresh() when working with indicator.GetData() (https://www.mql5.com/en/docs/standardlibrary/technicalindicators/cindicators/cindicator/cindicatorrefresh). That's confusing..

There are several GetData() method for CIndicator, and you are mixing them. The one the documentation is talking about is not the same as the one you posted the code. Check the function signatures.

 
Alain Verleyen #:

There are several GetData() method for CIndicator, and you are mixing them. The one the documentation is talking about is not the same as the one you posted the code. Check the function signatures.

Ok, makes sense. Thanks.