How can I read iMA values in real time?

To add comments, please log in or register
Marco Tonchella
24
Marco Tonchella  
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:
Marco Tonchella
24
Marco Tonchella  

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.

Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
  • www.mql5.com
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). When copying the yet unknown amount of data, it is recommended to use a dynamic array as a buffer[]...
Marco vd Heijden
Moderator
12459
Marco vd Heijden  

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 
   );

Marco Tonchella
24
Marco Tonchella  
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.
nicholi shen
2391
nicholi shen  

But my code does not seem to work very well, I just do not understand what I'm doing wrong.

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);
}
Marco Tonchella
24
Marco Tonchella  
nicholishen:

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.

Ok very, very, thanks Nicholishen.
Your post is very intersting.

For the use OOP I'm reading this:
https://www.mql5.com/en/articles/100
https://www.mql5.com/en/docs/standardlibrary/expertclasses/expertbaseclasses/cexpert

In fact, after these tests I thought I would better study the standard library and the CExpert base class.

I understand that CExpert can be derived by creating a new class in which, by implementing virtual methods, you get an EA complete with indicators, money manager and methods for order execution according to the logic that we can implement.

Are there any suggestions or articles to follow for those who start?

The official documentation, albeit good, is lacking in my opinion, not have more/sufficient examples.

Fortunately there are you guys: D: D

Documentation on MQL5: Standard Library / Strategy Modules / Base classes for Expert Advisors / CExpert
Documentation on MQL5: Standard Library / Strategy Modules / Base classes for Expert Advisors / CExpert
  • www.mql5.com
It already has some elementary trading "skills". It has built-in algorithms for working with time series and indicators and a set of virtual methods for trading strategy. Description Note A position is recognized as belonging to an Expert Advisor and managed...
To add comments, please log in or register