Often when developing an EA you will work with bar price data, such as the high, low, open, or close of a particular bar.
In this article, we will explore four levels of price data:
Let us go over each of these four levels of price data.
If you simply want to refer to the price data of the current bar of the current chart and time frame, you can use any of the following price data methods: High, Low, Open, Close, Bid, Ask.
Keep in mind all must have an initial capital letter to be read by the program (Bid is valid, bid is not). If you want to refer to the current close price, you can use Close, but you can also use Bid, because the MT4 closing prices are defaulted as Bid prices. For instance, if you wanted your EA to look for a condition whereby the current closing price must be greater than the 200 period moving average, you can state it with:
or, alternatively, as:
Note: Keep in mind that this simple level of price data can only be used for the chart symbol, chart time frame, and current bar. If you want to have the flexibility of working with different bars, you must turn to level 2 or level 3 price data. If you want to have the flexibility of working with different chart symbols, or different time frames, as well as different bars, you must work with level 3 price data.
If you want to work with price data for the current chart, but you want to be in control of the bar you are referring to, you can use the predefined series arrays: High, Low, Open, and Close.
An array is a variable that stores multiple values. They act like the list tables, wherein you can group items in the table and access them by row number, called Indexes. The numbered rows or indexes start from from 0, then proceed to 1, 2, 3 etc. These numbered indexes are contained in the brackets, and in the particular case above, each numbered index refers to a particular bar in time and changing the number changes the bar in time you want to refer to. For example, Close  is the open price of the current bar, where 0 is the index, and by changing it, we can get the close price of other bars: Close  refers to the bar previous to the current bar, Close  refers to the bar 2 bars back from the current, etc.
In most of the EAs we create we will be using either the current bar or the previous bar's price values. However, if you desire even greater flexibility of working with price data of not only different historical bars, but also different symbols other than your current chart, or different time frames other than your current chart, then you must turn to level 3 Price data.
If you want to work with price data (high, low, open, close) for a symbol other than the current chart, or if you need price data for a period other than the current chart period, you can use the following price data functions: iHigh(), iLow(), iOpen(), and iClose(). These will give you the price data value over a single period or bar.
The table below illustrates the syntax of the iClose() function:
For instance, say for instance you have open a 1 hour chart but you want to check the close of the price of the previous bar on a daily chart.
Here is yesterday's highest high and lowest low sample:
The Parameters signify:
Note: The iHigh() and iLow() functions gives you the shift of the maximum value of a single bar. If you want the shift of the maximum value over a range of bars, you must use the iHighest() and iLowest() functions, discussed further below.
There are two ways to put in time frames: one by period constants and one by integer values, as in the example below.
You see that PERIOD_D1 can be substituted with 1440 -- the minutes comprising the day period.
Here is a full table of the constant values and their integer equivalents:
These minute value substitutions are easy enough to remember, and once memorized, I find them easier to type into their designated parameter box than constants. I also find that these integers can be more readily used with extern variables. For instance, what if we wanted to refer to a previous Close different from our own H1 chart but we did not know exactly which time frame to use. We can then can construct the timeframe parameter as an extern variable, as in the example below:
TimeFrame is my identifier that refers to the timeframe parameter, and my default value is 30, which refers to the previous M30 bar. I know it is the previous bar because I put in a 1 in the shift parameter. If I want to refer to a current M5 close, I simply put in 5 in the second parameter, and if I wanted to refer to the current bar I simply put in 0 in the third parameter.
Now you have an easy way of making your EA reference multiple time frames, and you can even set the strategy tester to optimize between the timeframes if you so wanted.
NoteYou can use the alternative time period method (PERIOD_M30 = 30) and extern int method (extern int CloseTime = 30) for not only any price data function, but for all indicators and custom indicators; they all have the second parameter that refers to the time frame, which is usually defaulted to 0, or the time frame of your chart, but can be changed to any of the above time frames.
What is cool is that, not only can you easily reference multiple time frames, you can also easily reference multiple currency pairs. NULL stands for the symbol of the current chart, but it can be replaced with any currency symbol, even the currency outside the EA's own chart. To do so is simple: you just replace NULL with the symbol you want to use (it must appear in your market window) and wrap it in quotation markets.
Sample if you wanted to reference EURUSD symbol:
Moreover, you have the flexibility of constructing an extern variable for this parameter, as in the example below:
As you can see we substituted NULL with an extern string variable, we identified as CurrencyName, and we defaulted to "EURUSD" (though it could be any currency pairthat you can see in your market window, so long as you put it within quotation marks). Also note that all currency names that you want to attach to an extern variable must use the extern string variable.
Why would you need to refer to a currency pair that is not the same as your chart?
I can think of many reasons, but the foremost that comes to mind is when you are trading by means of correlation. For instance, suppose that you are trading the GBPUSD, but that you consider EURUSD as a leader currency for most of the majors, and thus you want to first check to see the trend direction of the EURUSD as a prior condition for opening up trades on the GBPUSD.
NoteYou can use the currency substitution method (NULL = EURUSD), or the extern string method (extern string CurrencyName = "EURUSD") for not only price data functions, but all indicator and custom indicator functions; they all have the first parameter referring to the currency pair. The default is usually NULL, which stands for the current chart symbol, but it can be changed to any currency symbol that appears in your market watch window.
In addition to working with price data functions like iHigh() and iLow of a single period, there is the ability to work with price data functions of maximum value over a range of periods: iHighest() and iLowest(). With each of these functions, you can work with the h/l of a number of bars in a range.
These two functions have their own parameter set:
Series array identifier table:
Note: It appears as if you have the option to use any Type above, but in general practice, you will be using MODE_HIGH with iHighest() and MODE_LOW with iLowest().
Sample of lowest low and highest high of the last 3 bars for a stop loss:
The above is a clever technique of using a different type of stop than the regular one.
What if you wanted to get the lowest value between bars 10 to 20?