MetaTrader already ships an RSI, so why write your own? Two reasons. First, building it yourself means you understand it and can tweak it — a custom levels display, an alert, a different smoothing — without paying for someone’s marketplace “RSI Pro.” Second, a custom buffer is what you’ll wire into an Expert Advisor later. It’s the frugal, own-your-tools move.
What you’ll need
- MetaTrader 5 with MetaEditor (bundled free)
-
Save the file as
AlgoGen.RSI.mq5underMQL5/Indicators/
The source
This computes the RSI exactly the way the explainer
describes: seed the first average with a simple mean over period bars, then
apply Wilder’s 1/period smoothing for every bar after.
//+------------------------------------------------------------------+
//| AlgoGen.RSI.mq5 |
//| Copyright 2026, AlgoGen |
//| https://www.algogen.io |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, AlgoGen"
#property link "https://www.algogen.io"
#property version "1.00"
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_label1 "RSI"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_width1 2
#property indicator_level1 30.0
#property indicator_level2 70.0
#property indicator_levelcolor clrSilver
input int InpPeriod = 14; // RSI period
double RSIBuffer[];
int OnInit()
{
SetIndexBuffer(0, RSIBuffer, INDICATOR_DATA);
IndicatorSetString(INDICATOR_SHORTNAME, "AlgoGen RSI(" + (string)InpPeriod + ")");
IndicatorSetInteger(INDICATOR_DIGITS, 2);
return(INIT_SUCCEEDED);
}
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
if(rates_total <= InpPeriod)
return(0);
double avgGain = 0.0, avgLoss = 0.0;
int start;
if(prev_calculated == 0)
{
// Seed the first RSI value with a simple average of the first window.
double sumGain = 0.0, sumLoss = 0.0;
for(int i = 1; i <= InpPeriod; i++)
{
double diff = close[i] - close[i - 1];
if(diff > 0) sumGain += diff; else sumLoss += -diff;
}
avgGain = sumGain / InpPeriod;
avgLoss = sumLoss / InpPeriod;
RSIBuffer[InpPeriod] = (avgLoss == 0.0) ? 100.0
: 100.0 - 100.0 / (1.0 + avgGain / avgLoss);
start = InpPeriod + 1;
}
else
start = prev_calculated - 1;
for(int i = start; i < rates_total; i++)
{
double diff = close[i] - close[i - 1];
double gain = (diff > 0) ? diff : 0.0;
double loss = (diff < 0) ? -diff : 0.0;
// Wilder smoothing: reuse the previous bar's averages.
avgGain = (avgGain * (InpPeriod - 1) + gain) / InpPeriod;
avgLoss = (avgLoss * (InpPeriod - 1) + loss) / InpPeriod;
RSIBuffer[i] = (avgLoss == 0.0) ? 100.0
: 100.0 - 100.0 / (1.0 + avgGain / avgLoss);
}
return(rates_total);
}
//+------------------------------------------------------------------+
Install & compile
-
Drop
AlgoGen.RSI.mq5inMQL5/Indicators/(Ctrl+Shift+D in MT5 → Indicators folder). - In MetaEditor, press F7 to compile. Zero errors expected.
- In MT5, drag “AlgoGen RSI” from the Navigator onto a chart. Set the period.
You’ll get the separate-window RSI with the 30/70 level lines, matching the output chart above.
Gotchas
-
The running averages are held in local statics between calls via the
prev_calculatedfast path — MT5 only recomputes new bars, so we re-seed from the last stored buffer values rather than looping the whole history every tick. -
MQL4? The logic is identical. Swap
OnCalculate‘s signature for the MQL4 form, declare the buffer withSetIndexBuffer(0, RSIBuffer)inOnInit, and use#property indicator_separate_window. The math doesn’t change.
Same indicator, other platforms: Python, Pine Script, EasyLanguage, NinjaScript. And when you’re ready to see whether an RSI rule actually made money, backtest it in AlgoGen.
This post is educational, not financial advice. Indicators describe the past; they don’t predict the future. Backtest anything before you risk real money on it.