Download MetaTrader 5

Need help with a line of code

To add comments, please log in or register
John
125
John  

Trying to decipher the last line of code in this segment:

int counted_bars=IndicatorCounted();
   
   if(counted_bars<0) return(-1);            //check for possible errors, -1 returned in log if true. start() ends and waits until next tick
   if(counted_bars>0) counted_bars--;
   
   int limit=Bars-counted_bars;
   if(counted_bars==0) limit-=1+1;           //subtracting 1 from the integer variable "limit" and adding 1 to it ...?

I'm just trying to understand what the last line of code actually does. I get that it relates to the first 2 lines and seems to be part of an error checking process.

I'm just trying to figure out why it is there and exactly what it does under what conditions. I know that if counted_bars == 0, then limit will be equal to Bars.

Then, in this case, 1 is subtracted from limit from the expression limit-=1.

Questions:

1) Why is it the "+1" there following the assignment expression: limit-=1 ? And why is 1 being subtracted from limit ... and then 1 added back to it ...? Or ... is 2 (1+1) being subtracted from it? But is this necessary, or just an illogical piece of code that is not needed?

2) Another related question: How would IndicatorCounted() which is = to counted_bars ever be 0? (offline charts perhaps ....?)

Thanks.

Alain Verleyen
Moderator
33284
Alain Verleyen  

This is the same as :

if(counted_bars==0) limit=limit-2; 

No idea why it's written like that.

All this snippet of code is confusing and obsolete. You should use new syntax with prev_calculated and rates_total.

whroeder1
15902
whroeder1  
IndicatorCounted is deprecated, start using the new events. See How to do your lookbacks correctly.
John
125
John  
Alain Verleyen:

This is the same as :

No idea why it's written like that.

All this snippet of code is confusing and obsolete. You should use new syntax with prev_calculated and rates_total.

OK, thanks. I will certainly look into the new syntax with prev_calculated and rates_total. I've been away from MT4 for a while. I see it is now merged with MQL5. That's OK with me, as I've always wanted to learn more about c++

My coding skills are that of 'slightly' better than noob level.


Anyways, just out of curiosity, do you know under what conditions IndicatorCounted() is ever 0?

Keith Watford
Moderator
10652
Keith Watford  
John:

Anyways, just out of curiosity, do you know under what conditions IndicatorCounted() is ever 0?

When the indicator is initialized or when more than one new bar is added to the chart (ie when updating due to not being connected)
To add comments, please log in or register