MetaTrader MQL Course. Module 4: Using an MT Technical Indicator

Using an MT Technical Indicator

In the last module, we went over using an Expert Advisor to open a trade.  If you’d like to review that module, click here: Opening a Trade Using OrderSend.  Today we are going to cover how to get the value of any of the technical indicators offered by the MetaTrader platform.

They are pretty much all the same – once you know how to use one indicator, you’ll probably be able to work with any indicator.  Of course, you will need to understand how to use the indicator.  I’m not going to cover that here, I’m just going to talk about how to get the value. How you interpret the value is up to you (and every trader interprets their indicators differently).

This is a list of the function names of the indicators that are available from MetaTrader (you can usually figure out the actual indicator from the name – ignore the i at the beginning):

iAC iAD iAlligator iADX iATR iAO iBearsPower iBands iBandsOnArray
iBullsPower iCCI iCCIOnArray iCustom iDeMarker iEnvelopes iEnvelopesOnArray
iForce iFractals iGator iIchimoku iBWMFI iMomentum iMomentumOnArray

iMFI iMA iMAOnArray iOsMA iMACD iOBV iSAR iRSI iRSIOnArray iRVI iStdDev
iStdDevOnArray iStochastic iWPR

Let’s start with an easy one: the Relative Strength Indicator (RSI).

This is the function definition of the iRSI function:

           double iRSI( string symbol, int timeframe, int period, int applied_price, int shift)

The iRSI function returns the value of the RSI – that’s what the “double” means.

Now, for the RSI parameters:

The parameter “symbol”, of course, is the currency symbol that you want to calculate the RSI for – you can use the MT built in function “Symbol()”.

The parameter “timeframe” is usually the time frame of the chart.  The built in function “Period()” is used for this.  Or you can specify the time frame using one of these values:

PERIOD_M1
PERIOD_M5
PERIOD_M15
PERIOD_M30
PERIOD_H1
PERIOD_H4
PERIOD_D1
PERIOD_W1
PERIOD_MN1

If you ask me, it’s more than a little confusing that the parameter named “timeframe” uses the function named “Period()”, especially when the next parameter is named “period”!

Oh well.  The parameter “period” refers to the number of past periods you want to include in the calculation. This value depends on what you are trying to do, but generally lower numbers are referred to as fast indicators and larger numbers are referred to as slow indicators.  The parameter “applied_price” is one of the following values:

PRICE_CLOSE
PRICE_OPEN
PRICE_HIGH
PRICE_LOW
PRICE_MEDIAN
PRICE_TYPICAL
PRICE_WEIGHTED

The parameter “shift” is used in combination with the “period” parameter.  While the “period” parameter determines how many candles (or bars) to use in the calculation, the “shift” parameter determines where to start the calculation. A shift value of zero starts the calculation with the last fully formed candle, a value of 1 starts the calculation at 1 candle to the left of the last fully formed candle, etc.

Many times traders want to be able to set the “period” parameter when configuring the Expert Advisors.  We already know from past modules that’s as easy as using the “extern” keyword before the variable.

This MQL programming code shows how to get the RSI value.

extern int rsi_period=12;
double rsi_value=0;

int start()
{
rsi_value = iRSI(Symbol(), Period(), rsi_period, PRICE_CLOSE, 0);
Comment(“RSI=”, rsi_value);
return(0);
}

Each indicator function is a little different, but they all support the same minimum parameters of symbol, timeframe, period and shift.

The more complicated indicators require more (and specific) parameters.  Some examples:

double iADX( string symbol, int timeframe, int period, int applied_price, int mode, int shift)

The ADX (iADX) indicator requires a “mode” parameter, which is one of the following values”

MODE_MAIN 0 Base indicator line.
MODE_PLUSDI 1 +DI indicator line.
MODE_MINUSDI 2 -DI indicator line.

 

double iBands( string symbol, int timeframe, int period, int deviation, int bands_shift, int applied_price, int mode, int shift)

The Bollinger Bands (iBands) indicator requires a “deviation” parameter and one of the following values for the “mode” parameter:

MODE_UPPER 1 Upper line.
MODE_LOWER 2 Lower line.

When you visually look at these indicators, you’ll see that they draw more than 1 line on the chart.  The mode parameter is used to specify what line to get the value for.

This may seem a little confusing, but the bottom line is that if you are familiar with using the indicator, you’ll be able to figure out how to use it in an EA.  After all, most of the parameters are the same ones that you need to set when you manually add an indicator to a price chart.

That’s all for this module.  Next time we are going to put it all together and build a real, working Expert Advisor.