passing index to CopyBuffer/GetData

 

I have an array-based index (i.e., the index comes not from a time series based array). Now I want to use this index:

double high[];
m_high.GetData(index, numberOfBars, high); // Method from Indicator.GetData() with calls common CopyBuffer()-method

However, since buffer access is series-based (see https://www.mql5.com/en/docs/series/copybuffer), the specified index must also be series-based (i.e., if index = 3, I get the 3rd bar from the right). The resulting array high[] is still array-based (i.e., the oldest element is at the beginning) which is what I need.

Is there a way to tell GetData() or CopyBuffer() to start from the left instead of the right?

Something like this does not work:

double high[];
ArraySetAsSeries(high, false); // has no effect because the array is array-based by default and not series-based
m_high.GetData(index, numberOfBars, high); // index=0 means latest bar - ALWAYS? Can I change this direction?
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
  • www.mql5.com
Gets data of a specified buffer of a certain indicator in the necessary quantity. Counting of elements of copied data (indicator buffer with the...
 

With a dynamically assigned array like high[],  index 0 will only be the latest bar when you set that buffer as series (if more than one value is used in the buffer)

ArraySetAsSeries(high, false); // has no effect because the array is array-based by default and not series-based

That's correct


but to address this:

"index=0 means latest bar - ALWAYS?"

no, not when there is more than one value used, so you have to explicitly set the series then:

double high[];
ArrayResize(high, numberOfBars);
ArraySetAsSeries(high, true);


Use ArrayReverse if you want to reverse the elements. I don't know why you would want to though in an EA.

https://www.mql5.com/en/docs/array/array_reverse

Documentation on MQL5: Array Functions / ArrayReverse
Documentation on MQL5: Array Functions / ArrayReverse
  • www.mql5.com
Reverses the specified number of elements in the array starting with a specified index. Parameters array[] [in][out]  Array. start=0 [in]...
 
Conor Mcnamara #:

but to address this:

"index=0 means latest bar - ALWAYS?"

no, not when there is more than one value used, so you have to explicitly set the series then:


Use ArrayReverse if you want to reverse the elements. I don't know why you would want to though in an EA.

You misunderstood the question.

Let me put the question more clearly:

Is there a way to tell GetData() or CopyBuffer() to start from the left instead of the right when copying the data into the returned array? The specified index (=start_pos) is an array-based index (i.e., the index is not from a time series-based array). Now I want to use this index (e.g., 0, which should point to the first bar in the series, not the last, etc.). It is clear that the resulting array is by default array based and not series based (which is fine for my use case!). I only operate in array-based indices.The problem is, the method accepts an index which is series based.

int  CopyBuffer(
   int       indicator_handle,     // indicator handle
   int       buffer_num,           // indicator buffer number
   int       start_pos,            // start position (excepts always (?) a series based index, i.e. 0 means latest bar)
   int       count,                // amount to copy
   double    buffer[]              // target array to copy
   );
 
CopyBuffer reads an array as it is. You don't concern yourself with how copybuffer copies data. It's the array itself that you care about. The left-to-right filling in CopyBuffer is independent of how you use the array...
Index 0 will still be the current bar and shift. But if you confused things with the buffer series itself, then you'll run into indexing problems.

From what it might seem, you might be trying to do something edgy like using copybuffer inside a loop. Just use copybuffer to copy the data and work with the array independently.
 
Conor Mcnamara #:
CopyBuffer reads an array as it is. You don't concern yourself with how copybuffer copies data. It's the array itself that you care about. The left-to-right filling in CopyBuffer is independent of how you use the array...
Index 0 will still be the current bar and shift. But if you confused things with the buffer series itself, then you'll run into indexing problems.

From what it might seem, you might be trying to do something edgy like using copybuffer inside a loop. Just use copybuffer to copy the data and work with the array independently.
Yes, thanks that's a good idea!