Handle providing data in reverse order

 
I'm trying to use data from the MACD, and according to the documentation index [0] should contain the most recent data point.

However I have found that the opposite is true. If I get three data points, [0] is the oldest whereas [2] is the most recent (changing on-tick as the price fluctuates).

The issue is that I'm obviously not understanding what is written on the MQL5 site.

   // Get the MACD data for current and last candle      
  void OnInnit()
  {
  MACDHandle = iMACD(_Symbol, PERIOD_M5, MACDFastEMA, MACDSlowEMA, MACDSMA, PRICE_CLOSE);
  }



  void OnTick()
  {

   double MACDMain[3], MACDSignal[3];
   int MACDMainCopied = CopyBuffer(MACDHandle, 0, 0, 3, MACDMain);
   int MACDSignalCopied = CopyBuffer(MACDHandle, 1, 0, 3, MACDSignal);
   
   Print("~~");
   Print("MACD Mainline preprevious value: " + MACDMain[2]); // Gets newest data
   Print("MACD Mainline previous value: " + MACDMain[1]);
   Print("MACD Mainline current value: " + MACDMain[0]); // gets oldest data?
   Print("~~");

   }
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
  • www.mql5.com
CopyBuffer - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Please don't post randomly in any section. Your topic has been moved to the section: Expert Advisors and Automated Trading
 
Robert_Clive: I'm trying to use data from the MACD, and according to the documentation index [0] should contain the most recent data point. However I have found that the opposite is true. If I get three data points, [0] is the oldest whereas [2] is the most recent (changing on-tick as the price fluctuates). The issue is that I'm obviously not understanding what is written on the MQL5 site.

Your posted coded shows "OnInnit" when it should be "OnInit". Was it just a typo here on the post or is that what you have in your real code?
 

This sentence:

Counting of elements of copied data (indicator buffer with the index buffer_num) from the starting position is performed from the present to the past, i.e., starting position of 0 means the current bar (indicator value for the current bar).

from the doc of CopyBuffer() is true if and only if you have set the result arrays to set_as_series - wich is not the default and has to be done separately by ArraySetAsSeries().

Documentation on MQL5: Array Functions / ArraySetAsSeries
Documentation on MQL5: Array Functions / ArraySetAsSeries
  • www.mql5.com
ArraySetAsSeries - Array Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:
Your posted coded shows "OnInnit" when it should be "OnInit". Was it just a typo here on the post or is that what you have in your real code?
No, that would throw a generic error and I could handle that pretty easily.
 
Carl Schreiber #:

This sentence:

from the doc of CopyBuffer() is true if and only if you have set the result arrays to set_as_series - wich is not the default and has to be done separately by ArraySetAsSeries().

Thank you!
 
Robert_Clive #: No, that would throw a generic error and I could handle that pretty easily.
No, it would not produce any error at all! It would just be considered a normal function, nor is OnInit() a required event handler.
 
Carl Schreiber #:

This sentence:

from the doc of CopyBuffer() is true if and only if you have set the result arrays to set_as_series - wich is not the default and has to be done separately by ArraySetAsSeries().


I tried to use ArraySetAsSeries and it had no effect - I assumed I had misunderstood this function also.

I made a reverse function of my own:

void ReverseArray(double &arr[]) {
    int size = ArraySize(arr);
    double temp[];
    ArrayResize(temp, size);
    for(int i = 0; i < size; i++) {
        temp[i] = arr[size - 1 - i];
    }
    ArrayCopy(arr, temp);
}
... which also cannot reverse the array.

I've obviously done something very bad somewhere which I will have to troubleshoot myself.
 
Fernando Carreiro #:
No, it would not produce any error at all! It would just be considered a normal function, nor is OnInit() a required event handler.
You're absolutely right.
 
Robert_Clive #: I made a reverse function of my own:

Simplify

void ReverseArray(double &arr[]) {
   ArraySetAsSeries( arr, !ArrayGetAsSeries(arr) );
}
 
Carl Schreiber #:

This sentence:

from the doc of CopyBuffer() is true if and only if you have set the result arrays to set_as_series - wich is not the default and has to be done separately by ArraySetAsSeries().

No. This sentence is always true and doesn't depend of the indexing of the resulting array.