Education

A Bar is Born: The MQL Shift

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:

  1. It moves one spot to the left
  2. Its number is incremented (it becomes bar#1)
  3. 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:

http://www.iexpertadvisor.com/forum/messages.aspx?TopicID=222&MessageID=655#post655 

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.

MQL Shift Entry Screen

 

 

New Way to Get More MetaTrader Price Data

New Way to Get More MetaTrader Price Data

If you do any back testing using the Strategy Tester, you may be interested in this.

If you don’t already know, the Strategy Tester is a back tester that comes with MetaTrader (MT).  To find it in your MT platform,  go to the View tab and select Strategy Tester.

Used to be when you needed more historical data you just pressed the PageUp key on your keyboard.

Pressing the PageUp key scrolled back through time on your chart. When you hit the point where you had no more price data, your MT platform would connect to your broker’s server and load more data.

For example, suppose you opened a 1 minute EURUSD chart and pressed the PageUp key.  It might go back to about 2 weeks ago and stop.  Then you’d press the PageUp key a few times and it would load more data.

It doesn’t work like that anymore.  I’m not sure why, but it’s not a big deal.  There’s another way that’s just as easy.

In your MT platform, go to the Tools tab and select History Center.  This opens a window that shows all of your available Symbols.

Expand the Forex menu, choose your Symbol and press the Download button.

This will download all of the price data available for that Symbol and Timeframe.   The download may take a while –  it  depends on your network connection.

I’ve posted some screenshots of this on the forum.

Here is the link:

http://www.iexpertadvisor.com/forum/messages.aspx?TopicID=221#post654

Note:  If you have a live account and need more data, don’t be afraid to ask your broker.  I’ve been able to get tick data for some currencies just by asking (nicely).