Newbie trying to understand handles.

 
I was told I need to use handles for indicators in mql5. However, looking at the examples for RSI and custom moving average scripts I do not see any handles being used. Are handles only used some of the time? When should you, or should you not, use them?
 
cgleckman:
I was told I need to use handles for indicators in mql5. However, looking at the examples for RSI and custom moving average scripts I do not see any handles being used. Are handles only used some of the time? When should you, or should you not, use them?
When working with files, you need to open such files to be able to do anything. When such files are opened, they are stored in "handles", which is used when writing, reading, closing etc the file itself.
MQL5 Programming Basics: Files
MQL5 Programming Basics: Files
  • www.mql5.com
This practice-oriented article focuses on working with files in MQL5. It offers a number of simple tasks allowing you to grasp the basics and hone your skills.
 
Thank-god Avwerosuoghene Odukudu #: When working with files, you need to open such files to be able to do anything. When such files are opened, they are stored in "handles", which is used when writing, reading, closing etc the file itself. You could read https://www.mql5.com/en/articles/2720

Did you not read the OP's post? Where on earth was he talking about files? He is talking about Indicator handles, not files.

 
cgleckman: I was told I need to use handles for indicators in mql5. However, looking at the examples for RSI and custom moving average scripts I do not see any handles being used. Are handles only used some of the time? When should you, or should you not, use them?

I am assuming that by examples, you mean their implementations, not their usage. You only use handles when retrieving the Indicator values from an EA or Script, not when creating the Indicator itself (unless it needs to get data from another Indicator itself).

Unfortunately in the CopyBuffer() documentation, their example is of an Indicator calling another indicator, which just makes it even more confusing for beginner coders.

In essence you should retrieve the handle in the OnInit() event handler, and then in the other event handling functions, you use the CopyBuffer() function to retrieve values when needed via that handle.

 
cgleckman:
I was told I need to use handles for indicators in mql5. However, looking at the examples for RSI and custom moving average scripts I do not see any handles being used. Are handles only used some of the time? When should you, or should you not, use them?

As stated above, the user program of the indicator creates the handle once (By IndicatorCreate/iCustom/or one of the technical indicators built in Mql5) In OnInit and uses that handle with CopyBuffer - on its first parameter.

 
Ah I see. Ty guys. So I build out my scripts for the indicator and then create a separate expert advisor file that includes that script and, when I go to use said script and create it in the EA file, I use a handle to reference it right?
 
cgleckman #:
Ah I see. Ty guys. So I build out my scripts for the indicator and then create a separate expert advisor file that includes that script and, when I go to use said script and create it in the EA file, I use a handle to reference it right?

You don't need to include the indicator, for each method of creation of it (with IndicatorCreate or iCustom) you have to supply the path of the indicator as parameter.

The output of the creation method you save in an int variable - that is your handle.

You have to check that it's > 0 otherwise the creation failed.

You then use CopyBuffer to get data you need from the indicator - you have to supply the handle as parameter.

 

Perhaps you should read the manual, especially the examples.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020.03.08)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
          How to call indicators in MQL5 - MQL5 Articles (2010)

 

Amir Yacoby #:

You don't need to include the indicator, for each method of creation of it (with IndicatorCreate or iCustom) you have to supply the path of the indicator as parameter.

The output of the creation method you save in an int variable - that is your handle.

You have to check that it's > 0 otherwise the creation failed.

You then use CopyBuffer to get data you need from the indicator - you have to supply the handle as parameter.


This makes alot more sense now thank you very much!

 
William Roeder #:

Perhaps you should read the manual, especially the examples.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020.03.08)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
          How to call indicators in MQL5 - MQL5 Articles (2010)

Thank you. Ive read some stuff on it but I do not think ive seen these articles except the timeseries one! I was just confused when I saw example scripts not using handles but I understand why now.
 
Fernando Carreiro #:

I am assuming that by examples, you mean their implementations, not their usage. You only use handles when retrieving the Indicator values from an EA or Script, not when creating the Indicator itself (unless it needs to get data from another Indicator itself).

Unfortunately in the CopyBuffer() documentation, their example is of an Indicator calling another indicator, which just makes it even more confusing for beginner coders.

In essence you should retrieve the handle in the OnInit() event handler, and then in the other event handling functions, you use the CopyBuffer() function to retrieve values when needed via that handle.

Thank you for this btw. Was wondering if anyone could shed insight into the reasoning behind building an indicator and not using handles? https://www.mql5.com/en/articles/10 ... assuming this is the answer but wanted to triple check I understand that correctly " You only use handles when retrieving the Indicator values from an EA or Script, not when creating the Indicator itself (unless it needs to get data from another Indicator itself)." thx in advance. 
MQL5: Create Your Own Indicator
MQL5: Create Your Own Indicator
  • www.mql5.com
What is an indicator? It is a set of calculated values that we want to be displayed on the screen in a convenient way. Sets of values are represented in programs as arrays. Thus, creation of an indicator means writing an algorithm that handles some arrays (price arrays) and records results of handling to other arrays (indicator values). By describing creation of True Strength Index, the author shows how to write indicators in MQL5.