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

 
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:

Note

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.

 

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.

 
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.


Conclusions:

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.



Regards.

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

 

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.

 
William Roeder:
  1. So pass it to your functions or store it in a global. What's the problem?
  2. The real question is why do you think you need it. Do you loop in OnCalculate and pass the bar index to your functions. They shouldn't need to know anything else.
              How to do your lookbacks correctly.
Fernando Carreiro:

That is why @whroeder1 also stated:  "So pass it to your functions or store it in a global ..."


using a global variable works well when the market is openned,  but when it is closed  like today, it fails to go back to zero when more history is loaded, ie by scrolling towards the left of the chart.

Mt4 has a special option  stated here :

If the price data have been changed since the last call of the OnCalculate() function (a deeper history has been loaded or gaps in the history have been filled), the value of the prev_calculated input parameter is set to zero by the terminal itself.

https://www.mql5.com/en/docs/event_handlers/oncalculate


is there  a way to code for this feature of sending prev_calculated to zero ?

Documentation on MQL5: Event Handling / OnCalculate
Documentation on MQL5: Event Handling / OnCalculate
  • www.mql5.com
OnCalculate - Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: