In MQL4 there was IndicatorCounted (), is there an equivalent function in MQL5 to access this value from anywhere in the program? - page 2

To add comments, please log in or register
Jose Francisco Casado Fernandez
Jose Francisco Casado Fernandez  
Fernando Carreiro:

I stand corrected!

EDIT: After looking at your code again and how you "invent" a unique shortname based on time, in order to circumvent the limitations of finding the the handle, I am reconsidering my previous comment.

Sorry, but I can no longer agree as I believe it to be a "forced hack" to get around the situation and I do not consider it a clear, efficient universal solution.

It's just an example, Fernando. You can do it in any other way that comes to your mind.

The developers of MetaQuotes recommend the shortname to be unique, for which they advise things like include in it the input parameters values, etc, etc. This is also important for attaching and removing indicators from the charts through mql programs, etc.

From documentation:


When creating an indicator, be careful forming its short name, which is written in the INDICATOR_SHORTNAME property using the IndicatorSetString() function. It is recommended that a short name should contain the values of input parameters of the indicator, since the indicator is identified in the ChartIndicatorGet() function based on its short name.

Another way to identify the indicator is to get a list of its parameters for a given handle using the IndicatorParameters() function and then to analyze the obtained values.

This particular indicator is very simple and only has one input parameter (the Timer lag), and I added it at the end, before it did not have any parameters. That's why I used a reference to the time it is initialized.

But it does not matter,
the point is that I showed that it's possible. At least you could have apologized for insuring the opposite and for your tone of the previous message.

Regards and good afternoon.

Alain Verleyen
Alain Verleyen  

BarsCalculated() is NOT an mql5 equivalent of IndicatorCounted(). Fernando is right on this point.

Of course, as demonstrated by Jose, it can be used as a workaround. However that seems a very big hammer for a little nail. Just use a global variable and update it in OnCalculate, simple and fast.

Jose Francisco Casado Fernandez
Jose Francisco Casado Fernandez  
Alain Verleyen:

BarsCalculated() is NOT an mql5 equivalent of IndicatorCounted(). Fernando is right on this point.

Of course, as demonstrated by Jose, it can be used as a workaround. However that seems a very big hammer for a little nail. Just use a global variable and update it in OnCalculate, simple and fast.

It's an opinion.  I already explained the reasons why I consider them equivalent, whereas 'prev_calculated'  clearly is 'another way of doing things' that the developers implemented when developing mql5 (and this is a better way if it is to calculate a normal indicator with only the current symbol and the current timeframe data).

But, apart from opinions for or against, as you say, the important thing is that I showed that an indicator can get the handle of itself,
and therefore can use BarsCalculated() and even CopuBuffer() referred to itself, contrary to what was said that was not possible. Fernando was wrong on this.

Storing the values of 'rates_total' and 'prev_calculated' in global variables was also evident in my example that has its disadvantages.

The method that I exposed also has a disadvantage mainly (nothing is perfect). I leave you to realize it for yourself.

Regards and thanks for your opinion Alain.
Jose Francisco Casado Fernandez:

Why not ???.  For example, you can use ChartIndicatorGet() to retrieve the indicator's own handle, from within its own code. There are also other ways to do it.

The fact that you did not know how to do it,  does not mean that it is not possible.

Well, I did not plan to code, but since you challenged me, here's an example:

As you can see, the indicator can retrieve the handle of itself, and use the BarsCalculated() function with it, and it can also use the CopyBuffer() function to obtain its own values.

Imagine that we want the indicator to do something every time a new candle appears, and only once for each candle (for example, to print indicator values for the newly closed candle in the log). Well, this is the result:

As you can see, everything is correct, we have obtained what we wanted. For a better visual appreciation, I highlighted the printed data for each candle in a different color.

Now imagine that we want to do something similar with the method you proposed (store the values of rates_total and prev_calculated in global variables), and use them in another part of the code (inside the OnTimer () function, for example). For this we use an indicator similar to the previous one:

First, from the code you showed above, I had to remove the line 'prev_calculated_global = rates_total;'  that was just above 'return (rates_total);',  because otherwise the condition configured inside OnTimer() was never fulfilled.

Removing this line, the condition is met, but not only once for each candle, but many times for each candle. Here are the results:

As you can see, inside OnTimer(), the condition 'if (rates_total_global > prev_calculated_global)'  is fulfilled many times for each candle (not all fit in the image). Therefore, with the method you proposed, we have not achieved the goal we wanted.

These are just examples. Prev_calculated is useful (I use it a lot) for normal indicators that only use the OnCalculate() function and only take into account the current symbol and timeframe candles. But storing these values in global variables and trying to use them in other functions handled by other events (such as OnTimer ()) can lead to desynchronization problems.
Depending on what you want to develop, one or the other is more appropriate.


1. If you do not know something with certainty, please do not affirm categorically that it is not possible, that it is false, (do not use uppercase words such as NOT, NEVER, etc.), much less even challenge someone who does.

2. I said that BarsCalculated () is the equivalent (extended and universalized) of IndicatorCounted (), because both are functions (prev_calculated is not, but it is a variable of constant type passed to the function that handles the OnCalculate event), also because both can be used anywhere in the indicator code (which is what OP requested) and because they serve the same functionality, with the difference that BarCalculated () is universal and IndicatorCounted () could only be used from within the code of the indicator itself.
Exactly as I mentioned. This unique difference is motivated by the way in which the indicators are managed in MT5 (that is, by handles).

3. And yes, as you see, BarsCalculated() is universal, whereas prev_calculated is not the equivalent of IndicatorCounted(), but it is another way of doing things, initially implemented in mql5 and later incorporated also in mql4 (currently it exists and is used equally in both languages) and it has exactly the functionality that I mentioned.

4. I hope that today you have learned something, at least regarding your manners, which are not usually the most appropriate.


Tks your solution, its working, i had try your code , i got a error "Unable to retrieve the own handle by itself.  Error code: ", but now i use ICustom to call handle, its wonderful, its working in my EA to call another pre_calculated not in Indicator :D, tks again

You can also simply drag an image to the text or insert it using Ctrl+V

Alain Verleyen
Alain Verleyen  

I don't have time or envy to argument more on this, but from my experience with MT5 indicators, including very heavy and MTF indicators, I can assure that the "hack", as Fernando said and he is perfectly right, using ChartIndicatorGet() to get an handle to reference itself is not a good idea. It may works...or not depending of the numerous different circumstances in the life of an indicator and how MT5 deal with it internally, you have NO control on this.

From a Metaquotes developper :

When you write

handle=ChartIndicatorGet(0,0,shortname);  -

it means "create for me the same indicator as this one on that chart with this name and return me its handle (possible new handle - not equal to already existed)"

And I effectively have seen several instances of the exact same indicator created using this 'hack' (or other similar hacks using indicator's handle, as well as other 'weird' things.

I strongly suggest to not use it.

To add comments, please log in or register