How can I read iMA values in real time?

I want use different iMAs and then I want read iMAs values.

The results is very different values (see chart).

How can I read the previous and last data?

Thanks.
Screenshoot

Files:

Test_X_Forum.mq5 3 kb

I found the official CopyBuffer() documentation but I do not understand exactly how to use it.
https://www.mql5.com/en/docs/series/copybuffer

“starting position is performed from the present to the past”
So my buffer[0] is in real time when OnTick() and buffer[1] is the previous moment?
But my code does not seem to work very well, I just do not understand what I’m doing wrong.

Thank you.

copy to an target array;

  double    buffer[]              // target array to copy 

Call by the first position and the number of required elements

int  CopyBuffer( 
   int       indicator_handle,     // indicator handle 
   int       buffer_num,           // indicator buffer number 
   int       start_pos,            // start position 
   int       count,                // amount to copy 
   double    buffer[]              // target array to copy 
   );

Call by the start date and the number of required elements

int  CopyBuffer( 
   int       indicator_handle,     // indicator handle 
   int       buffer_num,           // indicator buffer number 
   datetime  start_time,           // start date and time 
   int       count,                // amount to copy 
   double    buffer[]              // target array to copy 
   );

Call by the start and end dates of a required time interval

int  CopyBuffer( 
   int       indicator_handle,     // indicator handle 
   int       buffer_num,           // indicator buffer number 
   datetime  start_time,           // start date and time 
   datetime  stop_time,            // end date and time 
   double    buffer[]              // target array to copy 
   );

Ok, thank you.

I read value but when I print buffer[0] and buffer[1] they are very different from the last value…

Do you try my code?

Where am I doing wrong?

Thank you for your time.

You need to set the Array series direction to true.

ArraySetAsSeries(buffer,true);

…just a friendly tip… the MQL standard library is what you should first look to for some pretty cool and time saving code. Here is an example of what you need using the CiMA class.

#property indicator_chart_window

#include <Indicators\Trend.mqh>
CiMA g_ma55;

int OnInit()
{
   if(!g_ma55.Create(_Symbol,_Period,55,0,MODE_SMA,PRICE_CLOSE))
      return INIT_FAILED;
   g_ma55.Redrawer(true);
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
{
   g_ma55.Refresh();
   printf("Current = %f, Previous = %f",g_ma55.Main(0),g_ma55.Main(1));
   return(rates_total);
}
1 Like