Popular results

Advertisements
$ £ ¥
¥ £ $

How to Get Candlestick and Bar Prices in MQL4

When you start building indicators, expert advisors, and scripts with MQL4 and your goal is to perform some technical analysis, you need to be able to read the data coming from the current and past prices. You need to learn how to get candlestick and bar prices with MQL4. This guide will show you two methods you can use to do it.

Our other guides talk about the types of charts offered by MetaTrader, and to understand this guide, you also need to have an understanding of how arrays work, you will also see an example that uses a "for" loop, so if you aren't yet familiar with it, it is better to read about operators first.

You should already know that when you see a candlestick or bar chart, the representation shows you the Open, High, Low, and Close prices for the period (1 minute, 5 minutes, 1 hour, and so on). You should also already understand the meanings of these price points:

  • Open is the opening price, which is the first price when the bar or candle starts.
  • High is the highest price during the time the bar/candle was formed.
  • Low is the lowest price during the time the bar/candle was formed.
  • Close is the last price at the moment or when the bar or candle finished forming.

Visually, this can be presented as follows:

MetaTrader - Candlestick Structure
Candlestick Structure
MetaTrader - Bar Structure

Price Arrays (Timeseries)

So how do you get this information with MQL4? It isn't as hard as you may think! MetaTrader and MQL4 provide arrays with these prices and you just need to read them.

First, imagine all the bars as an array where the index increments going backwards, so the current candle/bar, the one that is forming, is index number 0 (zero), the previous candle has index 1, then 2, and so forth. Using this method your index 50 on a candlestick chart of timeframe M1 (1 minute) represents the candlestick of 50 minutes ago.

You can see it better with the following image:

MQL4/MetaTrader Numeration of Bars and Candles on a Chart

Such arrays (associated with bars and candles) are called timeseries in MetaTrader.

Now, each candle/bar has four arrays of interest associated with where the prices are stored. You may guess what these four arrays are:

  • Open[] is the array for the Open prices.
  • High[] is the array for the High prices.
  • Low[] is the array for the Low prices.
  • Close[] is the array for the Close prices.

Knowing how indexing works for timeseries arrays, you can now get the values at specific points in time using MQL4 code. For example, Open[0], High[0], Low[0], and Close[0] are the Open, High, Low, and Close prices of the current candle, and while the Open will not change, Close, High, and Low may change as the candle is still forming. If we want the close price of the last completely formed candle, you can use index 1: Close[1]. If you want the open price of 5 candles ago, including the one that is forming, you can use index 4: Open[4].

This is one method of retrieving the required data. However, in this case, you cannot specify which pair or instrument to take the price from. The arrays are populated with the prices of the current Symbol(), the current chart. Also, the price arrays presented above have the limitations of only storing the prices for the current timeframe of the chart. You will see a way to overcome these limitations further into this guide.

Hopefully, by now you have a fair idea of how the arrays of prices work. You can see an example of code with the use of them. Assume that you want to find the highest and the lowest prices in the last 30 candles. Using a "for" loop and arrays, it is pretty simple to achieve.

#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict

#property show_inputs

// You want to be able to change how many candles to scan.
extern int Window = 30;   // Number of candles to scan

void OnStart()
{
   // Initializing the variables.
   double Highest = High[0];
   double Lowest = Low[0];
   
   // Scan the 30 candles and update the values of the highest and lowest.
   for (int i = 0; i <= Window; i++)
   {
      if (High[i] > Highest) Highest = High[i];
      if (Low[i] < Lowest) Lowest = Low[i];
   }
   
   // Print the result.
   Print("Highest price found is ", Highest, " - Lowest price found is ", Lowest);
}

Running this code will result in:

How to Get Candlestick and Bar Prices in MQL4 - Test Script Input Parameters Getting Highest and Lowest Values with Test Script

The possibilities you have using these arrays are numerous. You can use them in scripts, indicators, and expert advisors — they will provide you with useful ready-to-use information.

Price Functions

Another method of getting price data in MQL4 is using the following four simple functions for this:

  • iClose()
  • iOpen()
  • iLow()
  • iHigh()

The functions above all work in the same way. You will see the details for one, but remember that all of them have the same arguments:

double  iClose(
   string           symbol,          // symbol
   int              timeframe,       // timeframe
   int              shift            // shift
   );
  • symbol is the symbol you want the price for; for example, "EURUSD" for the EUR/USD currency pair.
  • timeframe is the timeframe you want the price for; for example, PERIOD_M1 is the timeframe for a 1-minute chart.
  • shift is like the index seen in the price arrays method above: 0 for the current candle, 1 for the latest fully formed one, 2 is the one before that one, and so on.

It is quite simple to get the open price for the USD/JPY currency pair on the hourly chart for the third fully formed candle using iOpen("USDJPY", PERIOD_H1, 3).

What can you do with this? Well, you can do whatever you want or need to do in your indicator, script, or expert advisor. For example, if you want to check if the EUR is strong right now, you could setup a comparison between several currency pairs involving the EUR and see if the price is rising. Here issome code for that:

#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict

#property show_inputs

void OnStart()
{
   // Basically, what it does is comparing the current close price with the previous candle's high.
   // If the current price is higher than the high of the previous one, likely the price is rising.
   // It does the same check on different pairs involving the EUR to further validate the theory.
   // If for all pairs the price is rising, then it prints "EUR is strong!"
   if((iClose("EURUSD", PERIOD_M5, 0) > iHigh("EURUSD", PERIOD_M5, 1)) &&
      (iClose("EURCHF", PERIOD_M5, 0) > iHigh("EURCHF", PERIOD_M5, 1)) &&
      (iClose("EURAUD", PERIOD_M5, 0) > iHigh("EURAUD", PERIOD_M5, 1)) &&
      (iClose("EURJPY", PERIOD_M5, 0) > iHigh("EURJPY", PERIOD_M5, 1)))
   {
      Print("EUR is strong!");
   }
   else
   {
      Print("Nothing to report");
   }
}

The code presented above is pretty basic, but with some more work, it may be parametrized to use other pairs or timeframes. It could also become an entry signal for an expert advisor, or an indicator.

There is no preferable way to get price data in MQL4 — both arrays (e.g., Close[]) and functions (e.g., iClose()) are acceptable. It really depends on your needs in the script/indicator/expert advisor.

 

One important distinction is that in MQL5 (when coding for MT5), you can only use the arrays method (e.g., Close[]) inside the OnCalculate() function of indicators, whereas the functions method (e.g., iClose()) can be used everywhere in the code.