Quick Answer
Could not find function or function reference 'ta.cmf' means the Pine Script compiler looked in the ta namespace and found no function with that name. For ta.cmf the reason is simple: Pine Script has no built-in ta.cmf() function. Chaikin Money Flow (CMF) is an indicator you can add to a chart, but it is not a function you can call from your own script. You have to calculate it yourself from high, low, close and volume.
The same message appears for other reasons too: an old v4 name without the ta. prefix, a wrong //@version line, a typo, or a library function that was never imported. The checks below take about two minutes.
Key Facts
| Cause | Example | Fix |
|---|---|---|
| The function does not exist | ta.cmf(20) | Calculate it with math.sum and volume (see below) |
| v4 name used in v5 or v6 | sma(close, 20) | ta.sma(close, 20) |
| Wrong version line | //@version=4 with ta.ema(...) | Use //@version=5 or //@version=6 |
| Typo or wrong case | ta.Ema(...), ta.crossOver(...) | Names are case-sensitive: ta.ema, ta.crossover |
| Library function not imported | myLib.cmf(...) | import user/library/1 as myLib first |
Check the Cause in This Order
- Read the exact name in the error. The text between the quotes is what Pine could not find. Compare it letter by letter with the name in the Pine Script reference.
- Check the version line.
ta.*functions need version 5 or 6. In version 4 they have no prefix (sma,ema,rsi,crossover). Mixing the two styles in one script gives this error. - Check that the function exists. Not every indicator you know has a
ta.*function. If the name is not in the reference, there is nothing to call. - Check imports. A function from a library only works after the
importline.
Why ta.cmf Does Not Exist
The ta namespace holds a fixed list of functions and built-in variables such as ta.sma, ta.ema, ta.rsi, ta.atr, ta.mfi and ta.obv. Chaikin Money Flow is not in that list. TradingView ships CMF as a ready-made indicator and many users share their own versions, and that is where the idea of a ta.cmf call comes from. When an AI tool or a converter writes ta.cmf(...), it is guessing a name that follows the pattern of the other functions.
Calculate Chaikin Money Flow in Pine Script
CMF takes the close position inside the bar's range, multiplies it by volume, and divides the sum over length bars by the sum of volume over the same bars:
//@version=5
indicator("Chaikin Money Flow", overlay = false)
length = input.int(20, "Length", minval = 1)
// Money flow multiplier: +1 when the close is at the high, -1 when it is at the low
mfm = high == low ? 0.0 : ((close - low) - (high - close)) / (high - low)
mfv = mfm * volume
cmf = math.sum(mfv, length) / math.sum(volume, length)
plot(cmf, "CMF")
hline(0)
The high == low check avoids a division by zero on a bar with no range. The result moves between -1 and +1. The default length of 20 is common, but it is only a convention.
Port Chaikin Money Flow to MQL5
MQL5 has an accumulation/distribution indicator and a Chaikin oscillator, but no CMF with this exact formula. Write the same calculation as a function:
// Chaikin Money Flow over 'length' bars, ending at the bar at 'shift'.
// Like math.sum(mfv, length) / math.sum(volume, length) in Pine Script.
// Returns EMPTY_VALUE when the data is not ready.
double ChaikinMoneyFlow(int length, int shift)
{
double sumMfv = 0.0;
double sumVol = 0.0;
for(int i = shift; i < shift + length; i++)
{
double high = iHigh(_Symbol, _Period, i);
double low = iLow(_Symbol, _Period, i);
double close = iClose(_Symbol, _Period, i);
double vol = (double)iVolume(_Symbol, _Period, i);
if(high == 0.0 || low == 0.0 || close == 0.0)
return EMPTY_VALUE; // history not ready
double range = high - low;
double mfm = (range == 0.0) ? 0.0 : ((close - low) - (high - close)) / range;
sumMfv += mfm * vol;
sumVol += vol;
}
if(sumVol == 0.0)
return EMPTY_VALUE;
return sumMfv / sumVol;
}
Two details matter here. First, iVolume returns tick volume on most forex symbols, while TradingView may show real volume for the same instrument, so the values can differ. Second, the function reads closed bars when you call it with shift 1, which is the same rule as in Pine Script close[1] in MQL5. The check for EMPTY_VALUE must come before any comparison, because EMPTY_VALUE is a very large number.
Full MQL5 Example
This EA calculates CMF once per closed bar and prints it. It does not place trades.
input int CmfLength = 20;
datetime g_lastBarTime = 0;
int OnInit()
{
g_lastBarTime = iTime(_Symbol, _Period, 0);
return INIT_SUCCEEDED;
}
bool IsNewBar()
{
datetime currentBarTime = iTime(_Symbol, _Period, 0);
if(currentBarTime == 0 || currentBarTime == g_lastBarTime)
return false;
g_lastBarTime = currentBarTime;
return true;
}
double ChaikinMoneyFlow(int length, int shift)
{
double sumMfv = 0.0;
double sumVol = 0.0;
for(int i = shift; i < shift + length; i++)
{
double high = iHigh(_Symbol, _Period, i);
double low = iLow(_Symbol, _Period, i);
double close = iClose(_Symbol, _Period, i);
double vol = (double)iVolume(_Symbol, _Period, i);
if(high == 0.0 || low == 0.0 || close == 0.0)
return EMPTY_VALUE;
double range = high - low;
double mfm = (range == 0.0) ? 0.0 : ((close - low) - (high - close)) / range;
sumMfv += mfm * vol;
sumVol += vol;
}
if(sumVol == 0.0)
return EMPTY_VALUE;
return sumMfv / sumVol;
}
void OnTick()
{
if(!IsNewBar())
return;
double cmf = ChaikinMoneyFlow(CmfLength, 1); // last closed bar
if(cmf == EMPTY_VALUE)
return;
PrintFormat("CMF(%d) on the last closed bar: %.4f", CmfLength, cmf);
}
The new-bar check is explained in Pine Script barstate.isnew in MQL5. If your strategy uses a cross of CMF with the zero line, the cross rule is the same as in Pine Script ta.macd in MQL5, with 0 instead of the signal line. A zero in the divisor is also a risk in the Pine code and in the MQL5 code, and the MQL5 side is explained in MQL5 Zero Divide Error.
Common Mistakes
- Guessing a
ta.*name. If a function is not in the reference, it does not exist. Do not rely on a name an AI tool wrote. - Mixing v4 and v5 names. Use the
ta.prefix in every call, or none in v4, but not both. - Using
ta.sum. Summing over a window ismath.sumin v5 and v6. - Dividing by zero. A bar with
high == lowneeds the check shown above. - Comparing volume from two sources. Tick volume in MT5 and volume on TradingView are not always the same, so CMF values can differ.
- Evaluating on the live bar. CMF on shift 0 changes on every tick. Use shift 1 for signals.
FAQ
Related Guides
- Pine Script Error: 'series int' Used but 'simple int' Is Expected: another Pine compile error, caused by a length that changes from bar to bar.
- Pine Script ta.rsi and ta.ema vs MQL5: why indicator values differ between platforms.
- Pine Script v6 to MQL5: version differences and how to convert them.
- Pine Script ta.atr, ta.highest and ta.lowest in MQL5: more
ta.*functions and bar indexing. - Pine Script ta.macd in MQL5: iMACD and the signal line difference.
- MQL5 CopyBuffer guide: read indicator buffers safely.
- Why a Pine Script Strategy Gives Different Results in MQL5: indicator differences are one of the causes.
Convert a Script That Uses Missing Functions
A function that does not exist in Pine Script has no direct MQL5 equivalent either, so a converter has to rebuild the formula. CodeFlowOS translates Pine Script into MQL5 and lets you review the generated code before you test it in the MT5 Strategy Tester. The platform compile check is optional, and outputs are not guaranteed, so always review the result. For the full process, see the Pine Script to MQL5 guide, or try the converter page. If the code does not compile, the Error Fixer can suggest a fix, and the free early-bird Code Review checks an EA for common mistakes.
This article is educational. It does not describe trading performance and is not financial advice. Always compile and test code in MetaEditor and the Strategy Tester before using it on a live account.