Tutorial MQL4 Bar Counting and Indexing Guide
Tutorial MQL4 Bar Counting and Indexing Guide
crazy, and for the most part I agree that it is. However, that is how it is, and as the
saying goes the way it is, is the way it is.
However, zero based numbering is more rational for computers. Why? Why do men
have nipples? Who knows why? The reason why is because computer hardware
switching circuits referenced the first state of a switch, 0 as Off, and the second
state 1 as On.
It became apparent that 0s and 1s, also known as the Base 2 Binary Numbering
system was the best math representation to use for describing digital electronics
functionality. Later this math system was applied in electronics in vast switching
circuits such as memory, and other Logic devices. Computer software followed suit in
microprocessor Assembly Language, and then later in higher level languages such as
C which evolved using the same zero-based numbering convention.
Let us review an example of how people can get all screwed up with this concept. I
put 10 candy bars in a row in front of you, and I say: You can have candy bars 5
through 10. How many candy bars do you get? 5 right? WRONG!
The actual answer is 6, because in this case 5 is counted too. 5 (1), 6 (2), 7 (3), 8
(4), 9 (5), 10 (6). I was referring to the candy bars by their index number, and that
is where you took a left turn into the desert of confusion. Or even more confusing,
what if I tell you that you can have candy bars 10 through 5? It is very easy to get
confused.
At this point, it is important to emphasis another counter intuitive issue: That is that
we count UP as we go BACK in time. It is very confusing for a person to look at a
chart, and to see time counting up as you go from left to right, and bars indexing up
from right to left. The human brain has a lot to keep track of in this situation.
Consider this: In a chart of bars, regardless of time period, time is indexed up as it
goes forward into the future, but on those exact same bars, in MQL4 we are
incrementing the indexing of the bars as we go into the past. This makes you a little
cross eyed at first because you see time going forward, and bar indexing doing the
reverse.
The actual count of the items in a One Based numbering system will always be
equal to the total count of the items. In a Zero-Based Indexed counting scheme,
the total count is found from counting from zero to the (maximum index count 1).
The indexed count is always equal to the actual count, but the base number of the
Index is what is really changing the final number you come up with. Are you still
confused? Look at this table:
1-Based
Array Index
Count
-1
Actual
Bar Count
-1
10
10
11
10
11
12
11
12
13
12
13
101
100
101
1001
1000
1001
1520
1519
1520
When you are programming, and you want to reference a Bar in MQL4, you MUST
use the Zero-Based Array Index numbering system, and count up as you move
backwards in time. Always start counting with 0. The actual bar you want to access
is indexed as the actual bar number 1.
Pop quiz: The Bar in a Zero-Based Array indexed list is 1528. What is actual Bar
number in a One-Based Indexed system?
If you guessed the number 1528, then re-read this article from the beginning. If you
guessed 1529, then good for you. Nice job.
Individual Bars and Bar components, such as High, Low, Close, Open, are all
indexed. Every time there is a NEW bar, all index values increment by 1.
int Bars
This internally updated variable returns the number of bars on the current chart,
including the incomplete current bar. Bars is a reserved word and can be used in
your code. It will reference the One Based numbering system count of all bars on the
chart.
int IndicatorCounted()
This Function could be better thought of as the
int Count_Of_Fully_Completed_Bars_Which_Are On_the_Chart ( ) .
This function actually returns the bar count of fully completed bars that has not
changed since the last time a quote was received, but does Not count the current
uncompleted bar. In most cases, the same total count of index numbers will have
not changed between individually received quotes and it is a big waste of time and
effort for the program to recount the Bar array index every time the int Start ()
function is executed because a new quote has been received.
It has a return value in the int Start () of the total count of Bars 1. The newest
Bar is not counted if it is an incomplete bar and still in the process of forming. Until
the new bar is complete, it will not be counted.
datetime Time[]
Open time of the Bar. Time [ ] is the number of seconds elapsed from 00:00 January
1, 1970. Time Increments as you go into the future!
double Volume[]
Returns the number of the total ticks counted for the referenced bar. Think of this as
how many price fluctuations there have been during the time period, NOT how many
contracts.