Working with Timeseries and Indicator Data in MQL5

Watch on YouTube: Mastering Timeseries and Indicator Data in MQL5

In MQL5, you can access timeseries and indicator data using specialized functions. These allow for real-time and historical data collection, enabling the development of complex trading algorithms.

Indexing for Timeseries and Indicator Data

In MQL5, timeseries and indicator data are indexed in reverse chronological order, with index 0 representing the most recent data point (current bar or tick) and higher indices corresponding to older data. This structure enables efficient access to the latest market conditions while supporting historical evaluation for trading strategies.

Here are examples of how to access timeseries and indicator data, along with explanations.

Accessing Price Data (Open, High, Low, Close)
void OnStart() 
  {
  double close = iClose(_Symbol, PERIOD_CURRENT, 0); // last bar (current) close, index 0
  double open = iOpen(_Symbol, PERIOD_CURRENT, 0);
  double high = iHigh(_Symbol, PERIOD_CURRENT, 0);
  double low = iLow(_Symbol, PERIOD_CURRENT, 0);
  Print("Close = ", close, " Open = ", open, " High = ", high, " Low = ", low);  
  }

Explanation:

Function: iClose, iOpen, iHigh, iLow
These functions fetch the corresponding values for a specific bar in a timeseries.

  • _Symbol: Refers to the currently active trading symbol.
  • PERIOD_CURRENT: Specifies the current chart’s timeframe.
  • 0: Indicates the most recent bar (index 0).
  • Print: Outputs the retrieved prices in the terminal.

Likewise, you can retrieve time and volume data for specific bars using the iTime and iVolume functions.

Indicator Data: Accessing Indicator Values

MQL5 allows direct access to commonly used technical indicators. While we’ll dedicate an entire chapter to working with indicators, let’s briefly explore a few examples to get a basic understanding.

Accessing Moving Average Data (iMA)
int    MA_Handle;          // Handle for the Moving Average (MA) indicator
double MA_Buffer[];        // Array to store the MA values
void OnStart() 
{   
   // Create a handle for the Simple Moving Average (SMA) indicator with a period of 10
   MA_Handle = iMA(Symbol(), Period(), 10, 0, MODE_SMA, PRICE_CLOSE);
   // Copy the last 3 values of the indicator buffer into the MA_Buffer array
   CopyBuffer(MA_Handle, 0, 0, 3, MA_Buffer); 
   // Reverse the order of the MA_Buffer array for timeseries-style indexing (most recent value at index 0)
   ArraySetAsSeries(MA_Buffer, true);
   // Print the most recent MA value (index 0 of the array)
   Print("Most recent 10 period SMA: ", MA_Buffer[0]);
}

Key Points:

  1. Indicator Handle (iMA): The iMA function creates a handle for the Simple Moving Average indicator. This handle allows you to retrieve the computed values.
  2. Copying Buffer (CopyBuffer): This function retrieves the calculated values of the indicator and stores them in an array. It is essential for accessing indicator data in your script.
  3. Reversing Array Order (ArraySetAsSeries): Timeseries arrays in MQL5 are indexed in reverse order (most recent value at index 0). This function ensures the array follows the same structure.
  4. Printing Results (Print): The script prints the most recent MA value, making it suitable for debugging or further use in trading strategies.
Accessing Relative Strength Index Data (iRSI)
int    RSI_Handle;          // Handle for the Relative Strength Index (RSI) indicator
double RSI_Buffer[];        // Array to store the RSI values
void OnStart() 
{   
   // Create a handle for the RSI indicator with a period of 14
   RSI_Handle = iRSI(Symbol(), Period(), 14, PRICE_CLOSE);
   // Copy the last 3 RSI values into the RSI_Buffer array
   CopyBuffer(RSI_Handle, 0, 0, 3, RSI_Buffer); 
   // Reverse the order of the RSI_Buffer array for timeseries-style indexing (most recent value at index 0)
   ArraySetAsSeries(RSI_Buffer, true);
   // Print the most recent RSI value (index 0 of the array)
   Print("Most recent RSI value: ", RSI_Buffer[0]);
}

Key Points:

  1. Indicator Handle (iRSI): The iRSI function creates a handle for the RSI indicator, specifying its parameters such as period and price type.
  2. Copying Buffer (CopyBuffer): This retrieves the computed RSI values from the indicator buffer for use in the script.
  3. Reversing Array Order (ArraySetAsSeries): Ensures the RSI values follow timeseries-style indexing for consistency with other price-related data.
  4. Printing Results (Print): Outputs the most recent RSI value for debugging or integration into a trading strategy.

Conclusion

MQL5 provides a powerful set of tools to work with timeseries and indicator data. By using functions like CopyRates, CopyTime, CopyVolume, and CopyBuffer, you can efficiently retrieve market data and apply technical analysis in your trading algorithms. These functions allow you to access a wide range of market data and indicators, enabling the creation of complex and responsive trading strategies.

Next: Arrays

Previous: Event Handlers