Our partnerSkilling
  • Spreads from 0.1 pips
  • 1,000+ instruments
  • Free demo account

65% of retail investor accounts lose money when trading CFDs with this provider.

Open account
Module 2 · Programming for traders

2.3 How to read indicators in code

Lesson 6 of 19Reading time: 2 min

Almost all robots use indicators. The way you read them differs in the three languages – but the idea is the same: Get the value for a specific candle.

Candles 1 and 2

A crossover means that something was below on candle 2 and above on candle 1. That is why we almost always read two values:

  • Candle 1: the last closed candle.
  • Candle 2: the candle before it.

In MetaTrader the number is called the shift. In cTrader you use .Last(1) and .Last(2).

MQL4: directly

In MQL4 you get a value with one line. The last number is the shift:

MQL4
double fast1 = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE, 1);   // EMA(10) on candle 1
double rsi1  = iRSI(NULL, 0, 14, PRICE_CLOSE, 1);               // RSI(14) on candle 1

NULL means “this market”, and 0 means “this time frame”.

MQL5: handles and CopyBuffer

In MQL5 you create the indicator once in OnInit() and get a handle (a number). You read the values with CopyBuffer:

MQL5
int hFast;

int OnInit()
{
   hFast = iMA(_Symbol, _Period, 10, 0, MODE_EMA, PRICE_CLOSE);
   if(hFast == INVALID_HANDLE) return(INIT_FAILED);
   return(INIT_SUCCEEDED);
}

double Value(int handle, int buffer, int shift)
{
   double buf[1];
   if(CopyBuffer(handle, buffer, shift, 1, buf) != 1) return(EMPTY_VALUE);
   return(buf[0]);
}

// in OnTick:  double fast1 = Value(hFast, 0, 1);

Some indicators have several buffers – for example, Bollinger Bands have a middle line (0), upper band (1) and lower band (2), and the MACD has a main line (0) and a signal line (1).

cTrader: the Indicators object

In cTrader you create the indicator in OnStart() and read .Result:

C# (cTrader)
private MovingAverage _fast;

protected override void OnStart()
{
    _fast = Indicators.MovingAverage(Bars.ClosePrices, 10, MovingAverageType.Exponential);
}

protected override void OnBar()
{
    double fast1 = _fast.Result.Last(1);   // EMA(10) on candle 1
}

Overview

IndicatorMQL4MQL5cTrader
Moving averageiMA(...)iMA(...) + CopyBufferIndicators.MovingAverage(...)
RSIiRSI(...)iRSI(...)Indicators.RelativeStrengthIndex(...)
Bollinger BandsiBands(...)iBands(...)Indicators.BollingerBands(...)
MACDiMACD(...)iMACD(...)Indicators.MacdCrossOver(...)
ATRiATR(...)iATR(...)Indicators.AverageTrueRange(...)

Look it up in the documentation

All functions are described in the official documentation: docs.mql4.com, mql5.com/docs and help.ctrader.com. When you are unsure about the order of parameters, that is where to go.

Try it yourself

Change the period of the moving average in the code, compile, and see whether the signals change in the tester.

Key takeaways

  • Read values from candle 1 (last closed) and candle 2 to find crossovers.
  • MQL4 reads indicators directly, MQL5 uses handles and CopyBuffer.
  • cTrader creates indicators in OnStart and reads them with .Result.Last(n).
Test yourself · Module 2

How much do you remember from “Programming for traders”?

Recalling what you have read is one of the most effective ways to learn. Pick the answer you think is right – you get the explanation straight away.

  1. 1What does an input parameter do?

  2. 2How does MQL5 read indicator values?

Practise what you have learned – without riskTry it on a free demo account with Skilling, with real prices and virtual money.
Open a free demo account

65% of retail investor accounts lose money when trading CFDs with this provider.