Candle Data Reading in MQL5 Guide

x32x01
  • by x32x01 ||
When you're working with financial markets, your ability to read and extract candle data is one of the most important skills you'll ever develop. Candlesticks tell the story of price movement - where the market opened, how high it climbed, how low it fell, how it closed, and how much volume was traded.
In MQL5, reading candle data is extremely easy and gives you powerful control over automated trading strategies, indicators, and expert advisors.

In this guide, we’ll break down each candle element, explain how MQL5 functions like iOpen(), iHigh(), iLow(), and iClose() work, and walk through the example code you provided in a simple, human-written, beginner-friendly way.
We'll also cover how this data can be used to build trading bots and technical analysis tools. 🚀📈

Let’s jump in!



What Candle Data Represents in Trading 📉📈

Before reading candlestick data with MQL5, let’s quickly break down what a candle actually represents.
Each candle includes:
  • Open → the price when the candle started
  • High → the highest price during the candle
  • Low → the lowest price during the candle
  • Close → the price at the moment the candle ended
  • Time → when the candle started
  • Volume → how many ticks happened during the candle
These values help traders spot market trends, reversals, volatility, and momentum - which are essential for building smart EAs.



The MQL5 Functions Used to Read Candle Data 🧠💻

MQL5 provides built-in functions for reading candle information:
  • iTime() → gets the time of a candle
  • iOpen() → open price
  • iHigh() → highest price
  • iLow() → lowest price
  • iClose() → close price
  • iVolume() → tick volume
  • iBars() → number of candles in the chart

All of these functions require:
  • The symbol (like EURUSD)
  • The timeframe (like M5, H1, or Daily)
  • The shift number (0 is the current candle, 1 is the previous candle, and so on)
You already included all of these in your example code - great job.



Your Original Code Explained Step-by-Step 🛠️🔥

Here is the code you provided (formatted cleanly for readability):
C++:
input int shift = 0;

void OnTick()
{
    datetime time  = iTime(Symbol(), Period(), shift);
    double   open  = iOpen(Symbol(), Period(), shift);
    double   high  = iHigh(Symbol(), Period(), shift);
    double   low   = iLow(Symbol(), Period(), shift);
    double   close = iClose(NULL, PERIOD_CURRENT, shift);
    long     volume = iVolume(Symbol(), 0, shift);
    int      bars   = iBars(NULL, 0);

    Comment(Symbol(), ",", EnumToString(Period()), "\n",
            "Time: "  , TimeToString(time, TIME_DATE | TIME_SECONDS), "\n",
            "Open: "  , DoubleToString(open, Digits()), "\n",
            "High: "  , DoubleToString(high, Digits()), "\n",
            "Low: "   , DoubleToString(low, Digits()), "\n",
            "Close: " , DoubleToString(close, Digits()), "\n",
            "Volume: ", IntegerToString(volume), "\n",
            "Bars: "  , IntegerToString(bars), "\n");
}

Let’s explain what this script does in simple English:

1. shift lets you choose which candle to read

  • shift = 0 → current candle
  • shift = 1 → previous candle
  • shift = 2 → the candle before that
This is extremely useful for strategies based on historical data.



How MQL5 Reads Candle Time 🕒

The line:
C++:
datetime time = iTime(Symbol(), Period(), shift);
tells you the exact timestamp of the candle you’re reading.

Example usage:
  • Detecting new candles
  • Time-based strategies
  • Daily open price logic
The TimeToString() formatting in the Comment displays it like:
2025.11.29 12:30:00
Super clean and useful.



Getting the Candle’s Open, High, Low, Close (OHLC) Prices 📊

These lines extract the main candle data:
C++:
double open  = iOpen(Symbol(), Period(), shift);
double high  = iHigh(Symbol(), Period(), shift);
double low   = iLow(Symbol(), Period(), shift);
double close = iClose(NULL, PERIOD_CURRENT, shift);

These values are pure gold in trading because:​

  • Open helps detect gaps
  • High identifies resistance
  • Low identifies support
  • Close is used for most indicators (MA, RSI, MACD…)

If you want to compare candle direction:
C++:
if(close > open)
    Print("Bullish candle!");
else
    Print("Bearish candle!");

This allows you to build patterns like:
  • Hammer
  • Doji
  • Engulfing
  • Pin Bar
  • Morning Star



Reading Candle Volume 🔊📈

Volume tells you how active the candle was.
C++:
long volume = iVolume(Symbol(), 0, shift);
High volume = strong market activity
Low volume = weak movement or consolidation

You can use volume in strategies like:
  • Trend confirmation
  • Fake breakout filtering
  • Detecting reversal areas

Example:
C++:
if(volume > 200)
    Print("Strong activity on this candle!");



Counting How Many Bars Are on the Chart 📏

This line:
C++:
int bars = iBars(NULL, 0);
lets you know the total number of candles.
This is useful for:
  • Backtesting logic
  • Indicator initialization
  • Ensuring enough historical data exists



Displaying Candle Data on the Chart Using Comment() 📝✨

Your use of Comment() is perfect.
It prints the data directly onto the chart in real time.

Your output will look something like:
Code:
EURUSD,M15
Time: 2025.11.29 15:48:10
Open: 1.08520
High: 1.08710
Low: 1.08490
Close: 1.08670
Volume: 259
Bars: 48952
This is extremely helpful when debugging any EA.



Practical Use Cases for Candle Data in Trading Strategies 🧠⚡

Now that you understand how candle data works, here’s how traders use it:

1. Detecting Trend Direction

If highs and lows are rising → uptrend
If highs and lows are falling → downtrend

2. Spotting Reversals

Candle patterns combined with volume help you detect reversals.

3. Building Entry and Exit Rules

Example:
C++:
if(close > open && volume > 200)
{
    // Possible bullish trade
}

4. Creating Indicators

Most indicators depend heavily on OHLC values.

5. Risk Management

Using candle high/low as stop loss levels is extremely common.



Example: Detecting a Bullish Engulfing Pattern 🟩🟩🔥

Here’s a simple example to help beginners:
C++:
double prevOpen  = iOpen(Symbol(), Period(), 1);
double prevClose = iClose(Symbol(), Period(), 1);

double currOpen  = iOpen(Symbol(), Period(), 0);
double currClose = iClose(Symbol(), Period(), 0);

if(currClose > currOpen && prevClose < prevOpen &&
   currClose > prevOpen && currOpen < prevClose)
{
    Print("Bullish Engulfing Detected!");
}
This can be plugged directly into a real EA.



Final Thoughts 💡📌

Understanding how to read candlestick data is the foundation of every expert advisor, indicator, or automated system in MQL5.
The functions you used in your script are the exact same functions used by professional algorithmic traders worldwide.

Once you master candle data, you’ll unlock the ability to build:
  • Trend-following robots
  • Scalping systems
  • Smart breakout detectors
  • Price action strategies
  • Reversal indicators
  • Volume-based trading tools
So keep experimenting, keep testing, and never stop learning. 🚀🔥
This is exactly how professional algo traders start.
 
Last edited:
Related Threads
x32x01
Replies
0
Views
2K
x32x01
x32x01
Register & Login Faster
Forgot your password?
Forum Statistics
Threads
664
Messages
672
Members
67
Latest Member
TraceySet
Back
Top