Working with Timeseries and Indicator Data: MQL4 Guide

Watch on YouTube: MQL4 Basics: Accessing Timeseries and Indicator Data

Accessing timeseries and indicator data in MQL4 is crucial for creating effective trading algorithms, as it enables the retrieval of key historical and real-time data points like prices, volume, and indicators for technical analysis. MQL4 provides specialized functions to retrieve this information efficiently, allowing users to analyze and incorporate it into trading strategies.

Timeseries Data

Timeseries data functions allow you to access information on price, volume, and time for each bar in a chart. The primary functions include:

  • iOpen, iHigh, iLow, and iClose: These retrieve the respective values for any given bar index, with 0 representing the latest bar and increasing integers moving back in time.
  • iTime and iVolume: iTime retrieves the time of a specified bar, while iVolume provides volume information, useful for analyzing market strength over time.

Example:

void OnStart() {
   double openPrice = iOpen(NULL, PERIOD_H1, 1);  // Opening price of the last completed 1-hour bar
   double closePrice = iClose(NULL, PERIOD_H1, 0);  // Closing price of the current (last) bar
   datetime barTime = iTime(NULL, PERIOD_D1, 0);  // Time of the last daily bar
   int volume = iVolume(NULL, PERIOD_M5, 3);  // Volume of the bar 3 bars ago on the 5-minute chart
   
   // Show result
   Print("Opening price of the last completed 1-hour bar: ", openPrice);
   Print("Closing price of the current (last) bar: ", closePrice);
   Print("Time of the last daily bar: ", TimeToStr(barTime));
   Print("Volume of the bar 3 bars ago on the 5-minute chart: ", volume);
}

Indicator Data

MQL4 also includes functions for accessing built-in technical indicators, allowing developers to incorporate indicators like moving averages, RSI, and MACD directly into their code. These functions begin with i, followed by the indicator abbreviation. Examples include:

  • iMA for Moving Averages: This function calculates and retrieves the value of a moving average based on parameters like symbol, period, averaging period, and price type.
void OnStart() {
   double movingAvg = iMA(NULL, 0, 14, 0, MODE_SMA, PRICE_CLOSE, 0);  // 14-period SMA for the last bar
   // Print last SMA value
   Print("14-period SMA for the last bar: ", movingAvg); 
}
  • iRSI for Relative Strength Index: This function provides RSI values, allowing developers to measure momentum and potential overbought/oversold conditions.
void OnStart() {
   double rsi = iRSI(NULL, 0, 14, PRICE_CLOSE, 0);  // 14-period RSI for the last bar
   // Print last RSI value
   Print("14-period RSI for the last bar: ", rsi); 
}

Practical Application

Using timeseries and indicator functions, a developer can design automated trading strategies that react to market conditions in real-time. For example, a simple moving average crossover strategy might check if a short-term moving average crosses above a longer-term average, signaling a potential buy.

void OnStart() {
   double shortMA = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, 0); // 10-period SMA
   double longMA = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);  // 50-period SMA
   
   if (shortMA > longMA) {
       Print("Buy Signal: Short MA above Long MA");
   } else {
       Print("No Buy Signal");
   }
}

Conclusion

These functions provide critical access to historical and real-time data, empowering developers to build complex, responsive trading systems that align with market trends and trading rules. Understanding these functions and how to apply them helps traders create optimized, data-driven strategies in MQL4.

Next: Arrays in MQL4

Previous: Event Handlers