Technical Indicators in MQL4 and How to Access Their Values

Watch on YouTube: MQL4 Basics: Mastering Technical Indicators

Technical indicators are core components in algorithmic trading, helping traders make data-driven decisions. In MQL4, each indicator has its function, which retrieves its values directly. Here’s how to access some popular indicators using built-in functions.

Accessing Indicator Values in MQL4

Each indicator function allows you to specify parameters such as timeframe, period, and price type.

1. Moving Average (MA)

The iMA function calculates the moving average. Here’s an example of a 10-period simple moving average (SMA) based on the closing price:

void OnStart() {
   // Calculate 10-period SMA based on closing prices for the current chart
   double movingAverage = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, 0);
   // Print last bar 10-period SMA
   Print("Last 10-period SMA value: ", movingAverage); 
}
  • NULL: Uses the current symbol.
  • 0: Uses the current timeframe.
  • 10: Period of the moving average.
  • MODE_SMA: Type of moving average (simple).
  • PRICE_CLOSE: Applied price (closing price).
  • Last 0: Shift of zero (no offset).
2. Relative Strength Index (RSI)

The iRSI function retrieves the RSI for a given period. Here’s an example of a 14-period RSI:

void OnStart() {
   // Retrieve RSI value for a 14-period based on the closing prices
   double rsiValue = iRSI(NULL, 0, 14, PRICE_CLOSE, 0);
   // Print last 14-period RSI
   Print("Last 14-period RSI value: ", rsiValue); 
}
  • 14: RSI period length.
  • PRICE_CLOSE: Applied price.
  • Last 0: No shift.
3. Bollinger Bands

Bollinger Bands consist of three lines: upper, middle, and lower. Here’s how to access the upper and lower bands with a 20-period Bollinger Band:

void OnStart() {
   // Calculate 20-period Bollinger Bands with 2 standard deviations
   double upperBand = iBands(NULL, 0, 20, 2, 0, PRICE_CLOSE, MODE_UPPER, 0); // Upper band
   double lowerBand = iBands(NULL, 0, 20, 2, 0, PRICE_CLOSE, MODE_LOWER, 0); // Lower band
   // Print last Upper and Lower Band 
   Print("Last 20-period BB Upper Band value: ", upperBand); 
   Print("Last 20-period BB Lower Band value: ", lowerBand); 
}
  • 20: Period length.
  • 2: Number of standard deviations.
  • PRICE_CLOSE: Applied price.
  • MODE_UPPER / MODE_LOWER: Selects the upper or lower band.
  • Last 0: No shift.
4. Average True Range (ATR)

The iATR function provides the average true range, an indicator of market volatility. Here’s how to get the ATR value:

void OnStart() {
   // Get the 14-period ATR to gauge market volatility
   double atrValue = iATR(NULL, 0, 14, 0);
   // Print last 14-period ATR 
   Print("Last 14-period ATR value: ", atrValue); 
}
  • 14: Period of the ATR.
  • Last 0: Refers to the current candle with no shift.
5. Moving Average Convergence Divergence (MACD)

MACD provides both a main line and a signal line, retrieved here using the iMACD function:

void OnStart() {
   // Calculate MACD with standard 12, 26, and 9 periods for fast, slow, and signal lines
   double macdMain = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 0);     // Main MACD line
   double macdSignal = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, 0); // Signal line
   // Print last MACD Main and Signal line values 
   Print("Last MACD Main value: ", macdMain); 
   Print("Last MACD Signal line value: ", macdSignal); 
}
  • 12, 26, 9: Standard periods for the MACD calculation.
  • PRICE_CLOSE: Applied price.
  • MODE_MAIN / MODE_SIGNAL: Selects the MACD main or signal line.

Conclusion

These examples illustrate the use of technical indicators in MQL4, showing how each function and its parameters work. By knowing the details of indicator functions, you can streamline data retrieval in your scripts and Expert Advisors.

Next: Conclusion Beginner Series

Previous: Arrays in MQL4