iRSI in Pine Script: ta.rsi is the same Wilder RSI
Short answer
Use ta.rsi(source, length). Both use Wilder's smoothing seeded with a simple average of the first n gains and losses, so they agree to the last decimal once the histories start on the same bar.
iRSI returns a handle to MetaTrader's Relative Strength Index; the values come out through CopyBuffer. The Pine Script equivalent is ta.rsi(source, length), and this is one of the cases where the two platforms agree to the last decimal once their histories start on the same bar: both use Wilder's smoothing seeded with a simple average of the first n gains and losses.
What iRSI computes
The signature is iRSI(symbol, timeframe, ma_period, applied_price). The stock RSI indicator takes the applied price p and, for every bar, the gain max(p − pprev, 0) and the loss max(pprev − p, 0). The first averages are plain means over bars 1 to n; after that each average is updated as (avgprev·(n−1) + x)/n, which is Wilder's smoothing. The line is 100 − 100/(1 + avgGain/avgLoss). When the average loss is zero the indicator writes 100 if there was any gain and 50 if the window was completely flat. Bars 0 to n−1 hold 0.0 in the buffer and are hidden by PLOT_DRAW_BEGIN. The applied_price values are the usual seven: close, open, high, low, median, typical and weighted.
The Pine Script equivalent
ta.rsi(source, length) computes the same thing: upward and downward changes smoothed with ta.rma, whose seed is an SMA of the first length values followed by α = 1/length smoothing. The applied price becomes the source argument, so PRICE_TYPICAL is ta.rsi(hlc3, 14).
//@version=6
indicator("iRSI port", overlay = false)
len = input.int(14, "Period", minval = 1)
src = input.source(close, "Applied price")
rsi = ta.rsi(src, len)
plot(rsi, "RSI", color.rgb(30, 144, 255))
hline(70, "Upper", color.gray)
hline(30, "Lower", color.gray)The MQL5 side is a complete indicator that reads the built-in RSI through its handle. The window limits and level lines are declared with #property, which is what hline does in Pine.
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1 30
#property indicator_level2 70
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
input int InpPeriod = 14; // period
input ENUM_APPLIED_PRICE InpPrice = PRICE_CLOSE; // applied_price
double RsiBuffer[];
int rsiHandle;
int OnInit()
{
SetIndexBuffer(0, RsiBuffer, INDICATOR_DATA);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpPeriod);
rsiHandle = iRSI(_Symbol, _Period, InpPeriod, InpPrice);
if(rsiHandle == INVALID_HANDLE)
return(INIT_FAILED);
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(BarsCalculated(rsiHandle) < rates_total)
return(0);
int to_copy = (prev_calculated == 0) ? rates_total : rates_total - prev_calculated + 1;
if(CopyBuffer(rsiHandle, 0, 0, to_copy, RsiBuffer) <= 0)
return(0);
return(rates_total);
}Where the two differ
| Topic | MQL5 iRSI | Pine ta.rsi |
|---|---|---|
| Formula | Wilder smoothing, SMA seed over bars 1..n | Identical |
| First value | Bar index n; earlier cells are 0.0 | Bar index n (the change on bar 0 is na); earlier bars are na |
| Flat window | 50 when gains and losses are both zero, 100 when losses are zero | Division by zero handling differs; treat flat data as a special case if it matters |
| History start | Seeded at the first bar MetaTrader has loaded for the symbol | Seeded at the first bar TradingView loads; the two differ for a few hundred bars, then converge |
| Source | Enum applied price only | Any series, including another indicator's output |
| Realtime bar | Recomputed on each tick for the forming bar | Recomputed on each tick; rsi[1] is the last closed bar |
| Levels | #property indicator_level1 | hline() |
What the Stratlate converter emits
The MQL5 → Pine engine maps iRSI directly to ta.rsi(<applied price as a Pine series>, int(period)) (PineCatalog, entry iRSI), substitutes that series for the handle variable and turns the CopyBuffer call into an assignment to the plot buffer. Our probe of a handle-plus-CopyBuffer indicator converted with no warnings and the output was B0 := ta.rsi(close, int(14)). Because the formulas are the same, the differential verifier normally finds only the early-history offset described above; compile and compare the result against the MT5 chart anyway.
Pitfalls
- Writing your own RSI with
ta.emaorta.smafor the gain and loss averages. Onlyta.rmareproducesiRSI; the others shift every overbought and oversold reading. - Comparing the first bars of the chart. If TradingView loads 5,000 bars and MetaTrader loads 100,000, the seeds differ and so do the first few hundred RSI values; the difference decays geometrically, so compare later bars.
- Reading
RsiBuffer[rates_total - 1]in MT5 while thinking of it asrsi[1]. WithoutArraySetAsSeries, the last array element is the forming bar, which isrsi(index 0) in Pine. - Forgetting the
BarsCalculatedguard. The firstOnCalculatecall after attaching may run before the RSI handle has data;CopyBufferfails and the plot is blank until the next tick. Pine never has this state. - Assuming
iRSIon another timeframe needs no alignment. Pine'srequest.securityreturns the higher-timeframe RSI for every chart bar; MT5 requires you to map bar times yourself.
FAQ
Is iRSI exactly ta.rsi?
Yes in formula: both are Wilder's RSI seeded with a simple average of the first n changes. Values differ only while the two seeds are still influencing the average, which depends on where each platform's loaded history begins.
How do I port applied_price?
Replace it with the matching series: PRICE_CLOSE → close, PRICE_MEDIAN → hl2, PRICE_TYPICAL → hlc3, PRICE_WEIGHTED → hlcc4. An input ENUM_APPLIED_PRICE becomes input.source.
What about an RSI of another indicator?
In Pine just pass the series: ta.rsi(ta.sma(close, 10), 14). In MQL5 that needs iRSI on a custom indicator handle or a hand-written RSI over the buffer, which is where most porting work goes.
Convert the whole program
Paste an MQL4 or MQL5 indicator or Expert Advisor and get Pine Script v6 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.
Open the converter