handles

 

Hi, 

i am trying to undestand basic codes in  MQL5 and one thing i've got biggest problem with are indicator handles.

can someone tell me what is this line supposed to do:

 maHandle=iMA(_Symbol,_Period,MA_Period,0,MODE_EMA,PRICE_CLOSE);

Is the value in maHandle actualized every time MA changes?

Does the iMA function return a reference to the first element of the buffer where all MA values are stored? 

What exactly a handle is? 

If  maHandle is a single int, what is this line supposed to do?

CopyBuffer(maHandle,0,0,100,MA); 

assuming MA is a dynamic array. 

I am sorry if such topic already exists. If so please, give me the link. 

Witek 

 
nibylev:

Hi, 

i am trying to undestand basic codes in  MQL5 and one thing i've got biggest problem with are indicator handles.

can someone tell me what is this line supposed to do:

Is the value in maHandle actualized every time MA changes?

Does the iMA function return a reference to the first element of the buffer where all MA values are stored? 

What exactly a handle is? 

If  maHandle is a single int, what is this line supposed to do?

assuming MA is a dynamic array. 

I am sorry if such topic already exists. If so please, give me the link. 

Witek 

Hi nibylev,

You should try and read this article, it will guide you:

https://www.mql5.com/en/articles/31 

MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors
  • 2010.03.18
  • Sergey Pavlov
  • www.mql5.com
In order to obtain values of a built-in or custom indicator in an Expert Advisor, first its handle should be created using the corresponding function. Examples in the article show how to use this or that technical indicator while creating your own programs. The article describes indicators that are built n the MQL5 language. It is intended for those who don't have much experience in the development of trading strategies and offers simple and clear ways of working with indicators using the offered library of functions.
 
olowsam:

Hi nibylev,

You should try and read this article, it will guide you:

https://www.mql5.com/en/articles/31 

Thank You for your quick answer olowsam, but

i have already read that one,

i know something about how to use them, but i dont know the mechanics behind them

and the article doesnt explain it :(

I repeat queston that puzzles me the most: 

Does the iMA function return a reference to the first element of the buffer where all MA values are stored? 

 

 
Does the iMA function return a reference to the first element of the buffer where all MA values are stored?

The iMA, just like any other indicator functions returns the handle for that particular indicator. This is similar to opening a file.

// This code is taken from the manual 
int filehandle=FileOpen(filename,FILE_WRITE|FILE_CSV);
   if(filehandle<0)
     {
      Print("Failed to open the file by the absolute path ");
      Print("Error code ",GetLastError());
     }

Remember that when you get a handle for a file, all you need to manipulate the file is just the handle you have created. You can now write and do whatsoever you wish with the file using the handle.

The same goes for Indicators. You use the indicator function to obtain a handle which will now be used to get whatever values of the indicator at any point in time in a timeframe/chart.

The code :

maHandle=iMA(_Symbol,_Period,MA_Period,0,MODE_EMA,PRICE_CLOSE);

is used to obtain the handle for the indicator. This is mormally done only once within the OnInit() function of your expert advisor code.

Somewhere in your code within the OnTick() function, you need to get the corresponding values of the indicator for each bar on the timeframe you are considering. So we use the CopyBuffer function together with the handle we had obtained earlier, so we how have 

 if(CopyBuffer(maHandle,0,0,100,MA)<0)
     {
      Alert("Error copying MA indicator Buffers - error:",GetLastError(),"!!");
      return;
     }

As you said, MA is declared as a dynamic array and what CopyBuffer does is to copy the specified range of values (in the CopyBuffer function) for the indicator using the handle as a reference to the Indicator you working on.  What we have done above is to copy the values of the indicator from the current bar (Bar 0) to the previous 99 bars. These values will in turn be stored in the MA array for you to access as MA[0] => BAR 0 value MA[3] => bar 3 value etc.

I don't know if you are now clear with my explanation.

Take care 

 

 

great explanation! Thank You a lot.

Slowly i am getting much more confident in MQL, although some features are quite unintuitive for me. 

Thanks again :)

Witek 

Reason: