2.3 How to read indicators in code
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:
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 1NULL 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:
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:
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
| Indicator | MQL4 | MQL5 | cTrader |
|---|---|---|---|
| Moving average | iMA(...) | iMA(...) + CopyBuffer | Indicators.MovingAverage(...) |
| RSI | iRSI(...) | iRSI(...) | Indicators.RelativeStrengthIndex(...) |
| Bollinger Bands | iBands(...) | iBands(...) | Indicators.BollingerBands(...) |
| MACD | iMACD(...) | iMACD(...) | Indicators.MacdCrossOver(...) |
| ATR | iATR(...) | 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).
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.
1What does an input parameter do?
In cTrader it is called [Parameter].
2How does MQL5 read indicator values?
MQL4 reads directly, MQL5 uses handles and CopyBuffer.
65% of retail investor accounts lose money when trading CFDs with this provider.