Accessing Technical Indicators in MQL5: Code Examples with Explanations

Watch on YouTube: Mastering Technical Indicators in MQL5: A Step-by-Step Guide

MQL5 allows seamless integration of technical indicators into your trading algorithms. Accessing indicator values in MQL5 involves the following steps: first, create a handle for the desired indicator. Then, copy the indicator data into a buffer array using functions like CopyBuffer. To ensure the array follows timeseries indexing (with index 0 representing the most recent bar), apply the ArraySetAsSeries function.

Below are examples with clear explanations for using various indicators.

Accessing Moving Average Data (iMA)

The iMA function calculates the moving average, often used to identify trends. Here’s an example of a 10-period simple moving average (SMA) based on closing prices for the current chart.

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

Explanation:

  • iMA: Creates a handle for the Moving Average indicator.
  • CopyBuffer: Retrieves the last 3 values of the MA into MA_Buffer.
  • ArraySetAsSeries: Ensures the array indexes the most recent value at 0.
  • Output: Displays the latest 10-period MA value.

Accessing Relative Strength Index Data (iRSI)

The iRSI function computes the Relative Strength Index (RSI), helpful for identifying overbought or oversold conditions. For example, a 14-period RSI based on closing prices.

int    RSI_Handle;           // Handle for the RSI indicator 
double RSI_Buffer[];         // Array to store RSI values 
void OnStart() 
{ 
   // Create a handle for a 14-period RSI using closing prices
   RSI_Handle = iRSI(Symbol(), Period(), 14, PRICE_CLOSE); 
   // Copy the last 5 RSI values into RSI_Buffer
   CopyBuffer(RSI_Handle, 0, 0, 5, RSI_Buffer); 
   // Set the array to timeseries format
   ArraySetAsSeries(RSI_Buffer, true); 
   // Print the most recent RSI value
   Print("Current RSI: ", RSI_Buffer[0]); 
}

Explanation:

  • iRSI: Initializes a 14-period RSI for the current chart and timeframe.
  • CopyBuffer: Fetches the latest 5 RSI values.
  • ArraySetAsSeries: Ensures the array’s most recent value is at index 0.
  • Output: Prints the current RSI value, useful for assessing momentum.

Accessing Bollinger Bands (iBands)

Bollinger Bands include upper, middle, and lower lines. For example, calculate 20-period Bollinger Bands with a standard deviation of 2.

void OnStart() 
{            
   // Create a handle for 20-period Bollinger Bands with 2 deviations
   int    Bb_Handle;   
   Bb_Handle = iBands(Symbol(), Period(), 20, 0, 2, PRICE_CLOSE);    
   // Arrays for bands 
   double upperBand[], lowerBand[], middleBand[];
   
    // Copy the Bollinger Bands values into arrays
      CopyBuffer(Bb_Handle, 0, 0, 3, middleBand);
      CopyBuffer(Bb_Handle, 1, 0, 3, upperBand);
      CopyBuffer(Bb_Handle, 2, 0, 3, lowerBand);
      
    // Change to timeseries 
      ArraySetAsSeries(upperBand, true);
      ArraySetAsSeries(middleBand, true);
      ArraySetAsSeries(lowerBand, true);
   
   // Print the most recent Bollinger Band values
   Print("Upper Band: ", upperBand[0], " Lower Band: ", lowerBand[0], "Middle Band: ", middleBand[0]); 
}

Explanation:

  • iBands: Creates Bollinger Bands using a 20-period SMA and a standard deviation of 2.
  • CopyBuffer: Retrieves bands values.
  • Output: Prints the upper, middle, and lower bands, highlighting potential overbought/oversold zones.

Accessing Average True Range Data (iATR)

The iATR function measures market volatility using the Average True Range (ATR), e.g., a 14-period ATR.

int    ATR_Handle;           // Handle for the ATR indicator 
double ATR_Buffer[];         // Array to store ATR values 
void OnStart() 
{ 
   // Create a handle for a 14-period ATR
   ATR_Handle = iATR(Symbol(), Period(), 14); 
   // Copy the last ATR value into ATR_Buffer
   CopyBuffer(ATR_Handle, 0, 0, 1, ATR_Buffer); 
   
   // Print the ATR value
   Print("Current ATR: ", ATR_Buffer[0]); 
}

Explanation:

  • iATR: Sets up a 14-period ATR for the chart.
  • CopyBuffer: Retrieves the latest ATR value.
  • Output: Displays the current ATR value, reflecting market volatility.

Accessing MACD Data (iMACD)

The iMACD function retrieves the Moving Average Convergence Divergence (MACD) with its main and signal lines, often used for momentum analysis. For instance, calculate MACD with periods of 12, 26, and 9.

int    MACD_Handle;           // Handle for the MACD indicator 
double MACD_Main[], MACD_Signal[]; // Arrays for MACD lines 
void OnStart() 
{ 
   // Create a handle for MACD with standard settings (12, 26, 9)
   MACD_Handle = iMACD(Symbol(), Period(), 12, 26, 9, PRICE_CLOSE); 
   // Copy values for MACD Main and Signal lines
   CopyBuffer(MACD_Handle, 0, 0, 1, MACD_Main); 
   CopyBuffer(MACD_Handle, 1, 0, 1, MACD_Signal); 
   // Print the MACD values
   Print("MACD Main: ", MACD_Main[0], " MACD Signal: ", MACD_Signal[0]); 
}

Explanation:

  • iMACD: Configures a MACD indicator with fast EMA (12), slow EMA (26), and signal line (9).
  • CopyBuffer: Retrieves the latest values for MACD and its signal line.
  • Output: Displays the momentum and trend strength via MACD.

MQL5 also provides options for accessing custom-made indicators using functions like iCustom. This topic will be explored in more detail in the custom indicators section.

Conclusion

The examples demonstrate how to create indicator handles, retrieve values, and interpret data for trading strategies. MQL5 provides a consistent approach to integrate indicator data for real-time and historical analysis.

Next: Conclusion

Previous: Arrays