If you want to check the number of bars then use:
int iBars( string symbol, int timeframe)
Returns the number of bars on the specified chart.
For the current chart, the information about the amount of bars is in the predefined variable named Bars.
symbol - Symbol the data of which should be used to calculate indicator. NULL means the current symbol.
timeframe - Timeframe. It can be any of Timeframe enumeration values. 0 means the current chart timeframe.
Sample:
Print("Bar count on the 'EUROUSD' symbol with PERIOD_H1 is",iBars("EUROUSD",PERIOD_H1));
You may also use the variable "bars".
The reason you'll want to check iBars is for example: You have a moving_average indicator which provides the average of the last 100_bars. If you don't have 100_bars then whats produced by the indicator would be wrong. In this case, most people do the following.
void start(){ if(bars < 100) return; rest_of_your_code_below; }
This is usually an issue when running the program live. Even then, its rare because most people usually open the chart first before attaching the ea upon that chart. When this person opens the chart, meta_trader will download some data for the pre_calculations.
When you run programs in the back_tester, it usually loads a sizable amount of bars for your pre_calculations.
There are times when the issue of bars_being_downloaded when the start is executing; this is where you'll need checking. Such times would be during Dis_Connection; Multi_Currency on Symbol_Never_Opened... etc.
If you haven't read the mql_book yet, I'll recommend it.
The reason you'll want to check iBars is for example: You have a moving_average indicator which provides the average of the last 100_bars. If you don't have 100_bars then whats produced by the indicator would be wrong. In this case, most people do the following.
Thanks Ubzen.
I understand the concept of doing the bar check, to make sure calculations are correct. What I was struggling with is whether i need to have a secondary check in the EA, as I think each indicator i am using already does this check.
eg from the dtosc indicator
int start() { int i,limit; int counted_bars = IndicatorCounted(); if(counted_bars < 0) return(-1); if(counted_bars > 0) counted_bars--; limit=Bars-counted_bars;
so if the indicator does this check and i call the indicator using icustom in the ea, do i need to have a subsequent check for bars counted? I would have thought not, but then I don't understand why sometimes i run my backtests on data, and they don't want to start on the date i provide (despite there being data)? However is i shift the start date forward a bit it starts to run. The max period my indicators look back would be 21.
I guess from a live point of view, I just need to make sure there are enough bars in the window before adding an EA.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
This kinda follows on from my previous question - https://www.mql5.com/en/forum/146554
I contacted Birt about the issues with the dates...
His suggestion was it could be related to the way my EA is coded (no surprise really, as this is all still fairly new to me). His thoughts, that it could be related to "checking the number of available bars when starting up".
So i did a bit of research into this, but wasn't sure if I had to consider this or not?
(I read this article - https://www.mql5.com/en/articles/1456 and alos at this thread - https://www.mql5.com/en/forum/145036
Firstly - I presume the checking of available bars for the indicators that are run in the EA would only affect the backtesting and optimization results, but not impact a live EA so much?
Secondly - if the indicators I am using are called by icustom - do i still need to do some extra check in the EA outside of the indicators themselves?
So some example of code in the Start function
The Swings() function uses icustom with the ZigZag indicator
the other indicator is the DTOSC which is in the DTOSCCheck function, which again uses icustom to call the DTOSC indicator
So if i am calling these two indicators in the EA, do I need to check the number of bars available on startup, or will this be taken care of in the indicator code?
If I do need to have some kind of check, do I include it in the function for Swings and DTOSCCheck?
PS
I do get error messages for the DTOSC indicator is this related? - DTOSC 5 XAUUSD,M15: incorrect start position 5308 for ArrayMinimum function and also the same for ArrayMaximum
thanks