iMFI in Pine Script: ta.mfi(hlc3, n) and the volume feed
Short answer
Use ta.mfi(hlc3, length): the formula is the same. The numbers agree only when both platforms see the same volumes, which for forex and CFDs is often not the case.
iMFI returns a handle to MetaTrader's Money Flow Index, a volume-weighted RSI computed on the typical price. Pine Script's equivalent is ta.mfi(hlc3, length). The formulas agree; the numbers agree when the two platforms see the same volumes, which for forex and CFDs is often not the case.
What iMFI computes
The signature is iMFI(symbol, timeframe, ma_period, applied_volume). There is no applied price: the stock indicator (Indicators\Examples\MFI.mq5) always uses the typical price TP = (high + low + close) / 3. For each bar it takes the raw money flow TP · volume and classifies it as positive if TP rose against the previous bar, negative if it fell, and neither if it was unchanged. Over the last n bars it sums positive and negative flow and sets MFI = 100 − 100 / (1 + positive / negative); when the negative sum is zero the value is 100. The window is finite, so there is no seed: the first value appears on bar n, and before that the line is not drawn. applied_volume chooses tick volume (the default) or real exchange volume.
The Pine Script equivalent
ta.mfi(source, length) sums volume · source on bars where the source rose and on bars where it fell over length bars and applies the same ratio. Pass hlc3 to match MetaTrader. The script also writes the calculation out with math.sum, including MetaTrader's rule of returning 100 when there is no negative flow, which makes it easy to inspect a single bar.
//@version=6
indicator("iMFI port", overlay = false)
len = input.int(14, "Period", minval = 1)
mfi = ta.mfi(hlc3, len) // typical price, as MetaTrader uses; volume is the chart's volume
// The same calculation written out, for checking against MetaTrader's buffer
flow = hlc3 * volume
up = math.sum(ta.change(hlc3) > 0 ? flow : 0.0, len)
down = math.sum(ta.change(hlc3) < 0 ? flow : 0.0, len)
mfiMt = down == 0 ? 100.0 : 100 - 100 / (1 + up / down)
plot(mfi, "ta.mfi", color.rgb(30, 144, 255))
plot(mfiMt, "MFI (iMFI rules)", color.gray)
hline(80, "Upper", color.gray)
hline(20, "Lower", color.gray)The MQL5 indicator this replaces:
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1 20
#property indicator_level2 80
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
input int InpPeriod = 14; // ma_period
input ENUM_APPLIED_VOLUME InpVolume = VOLUME_TICK; // applied_volume
double MfiBuffer[];
int mfiHandle;
int OnInit()
{
SetIndexBuffer(0, MfiBuffer, INDICATOR_DATA);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpPeriod);
mfiHandle = iMFI(_Symbol, _Period, InpPeriod, InpVolume);
if(mfiHandle == 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(mfiHandle) < rates_total)
return(0);
int to_copy = (prev_calculated == 0) ? rates_total : rates_total - prev_calculated + 1;
if(CopyBuffer(mfiHandle, 0, 0, to_copy, MfiBuffer) <= 0)
return(0);
return(rates_total);
}Where the two differ
| Topic | MQL5 iMFI | Pine ta.mfi |
|---|---|---|
| Price | Typical price, fixed | Any source; pass hlc3 |
| Flow classification | TP up → positive, down → negative, equal → neither | Identical |
| No negative flow | 100 | Division by zero; handle it explicitly if it can occur |
| Volume | VOLUME_TICK (default) or VOLUME_REAL from the broker | The feed's volume |
| Warm-up | First value on bar n | First value on bar n |
| History start | Irrelevant: a finite window with no recursion | |
Because volume enters both the numerator and the denominator, a feed that is uniformly larger does not change the MFI. What changes it is a different distribution of volume across bars, which is exactly what you get between a broker's tick counts and another provider's.
What the Stratlate converter emits
The MQL5 → Pine engine maps iMFI to ta.mfi(hlc3, int(period)) (PineCatalog, entry iMFI) and substitutes that series for the handle and its CopyBuffer. Our probe of the indicator above emitted MfiBuffer := ta.mfi(hlc3, int(InpPeriod)), carried the 20/80 levels across as hline calls and hid the first n bars as the PLOT_DRAW_BEGIN did. The applied_volume argument is not modelled: the probe logged VOLUME_TICK as an identifier with no Pine mapping (MQLW0512), and the Pine uses the chart's volume. Compile and compare the converted line on the same symbol.
Pitfalls
- Passing
closetota.mfi. MetaTrader always uses the typical price; withclosethe flow direction changes on many bars. - Expecting identical values on forex. MetaTrader's tick volume is broker-specific, and TradingView's forex volume depends on the data provider; the MFI shape is similar, the readings differ.
- Comparing against
nawhen there is no negative flow. MetaTrader shows 100; in Pine, guard the division as in the written-out version. - Using
VOLUME_REALin MT5 on a symbol without exchange volume. The values are zero and the indicator shows 100 or nothing; there is nothing comparable to port. - Porting with an applied-price input.
iMFIhas none, so an MQL5 input that looks like one is probably used elsewhere in the program.
FAQ
Is iMFI the same as ta.mfi?
The formula is the same when ta.mfi is given hlc3. Both sum typical-price money flow over a fixed window, so there is no seed or history-start effect; any remaining difference comes from the volume data.
Does the volume type matter for the port?
Only through the data. Pine has a single volume series, so VOLUME_TICK and VOLUME_REAL both become volume, which is whatever the TradingView feed supplies for that symbol.
Why is MFI called a volume-weighted RSI?
It has RSI's structure, 100 − 100/(1 + up/down), but the up and down amounts are price times volume and they are summed over a window instead of smoothed with Wilder's average.
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