A Bar is Born: The MQL Shift
I’m asked a lot of questions about the MQL shift parameter.
If you’re not sure about the shift parameter, don’t worry, you’ve got lots of company. Plus, I’ve got a great way to explain it.
But let’s back up first. Every bar (or candle) on a MetaTrader price chart has a number.
Get this – the bars are numbered from right to left! Yup, that’s right. Backwards for us Arabic-based-language readers.
But this does make sense. The bar on the right-most side of the chart, the one that is being drawn, is always bar number 0 (we’ll call it bar#0 for short).
Remember, it takes some time for a bar to be drawn. How long?
Depends on the price chart. 1 minute on a 1-minute chart, 15 minutes on a 15-minute chart, and so on. I’m sure you get it.
Also, recall that a bar (or candle) has 4 price values: open, high, low and close. The bar isn’t complete until the close value is known, and that happens at the very end of the period.
So, when bar#0 is finished, 3 things happen:
- It moves one spot to the left
- Its number is incremented (it becomes bar#1)
- A new bar#0 is born!
This is happening all the time (when the market is open). A new bar is added at the end of each time period and the rest of the bar numbers are incremented by one.
What does this have to do with the shift parameter? Well, what I call the bar number, basically is the shift.
For example, the MQL function iOpen gives you the value of the open price of any bar. The iOpen function has a shift parameter:
- If the shift parameter is 0, you get the open value of the current, right-most bar
- If the shift parameter is 1, you get the open value of the bar to the left of the current bar
- If the shift parameter is 2, you get the open value of the bar that is 2 bars to the left of the current bar
- and so on and so on …
And this same shift parameter is used in every MetaTrader Indicator, including custom indicators.
It has the same meaning: shift = 0 calculates the indicator value for the current bar, shift = 1 calculates the indicator value for the bar to the right, etc. etc.
I’ve written a simple Expert Advisor that draws the number of each bar on a price chart. It really helps to see this concept in action.
I’ve posted the plain MQL code, so you don’t need VTS or anything to load the EA. Also you are welcome to edit the code if you like.
Click or copy and paste this link into your browser to get to the BarLabel Expert Advisor:
FREE MQL COURSE: If your interested in learning more about MQL, I’ve put together a free email MQL programming course. You can sign up here: http://www.iexpertadvisor.com/free-metatrader4-mql-course.html
VTS UPDATE: I’ve added this entry-screen to VTS for every Shift parameter. To see it, use the pull-down menu next to the Shift parameter on any Indicator configuration screen, scroll up and select the “choose” menu item.