Pine Script: Could not find function or function reference (fix)
"Could not find function or function reference" in Pine Script usually means old v4 code, such as sma() without ta. Here is how to find and fix it.
TradingView shows "Could not find function or function reference '<name>'" when a script calls a function name that Pine does not know. Most of the time the script was written for Pine version 4 and is now compiled as version 5 or 6, where functions such as sma live under a namespace (ta.sma). The fix is to use the current name, or let a converter update the whole script for you.
What causes it
The name in quotes tells you which call failed. There are three usual reasons.
Old version 4 code in a version 5 or 6 script
Version 4 had bare function names. Newer versions group them: the technical analysis functions moved under ta., security became request.security, and study() was replaced by indicator(). Code copied from an old post, with the version line changed to 6, FAILS on TradingView:
//@version=6
indicator("Old call")
plot(sma(close, 20))
TradingView reports that it cannot find sma. The same happens with rsi, security and study. A longer old script may use several of them.
iff() was removed
iff(condition, a, b) was the version 4 way to choose between two values. It no longer exists, so any iff( call triggers this error. The replacement is the ? : operator: condition ? a : b.
A misspelt name, or a library function you cannot reach
A typing slip such as ta.smaa gives the same message, because Pine looks for a function with exactly that name. Library functions add one more case: the function must be exported by the library, and your script must import that library. If either is missing, the name cannot be found.
How to fix it
For the first example, add the namespace. This is the tested fix:
//@version=6
indicator("SMA 20", overlay = true)
plot(ta.sma(close, 20), "SMA 20", color.blue)
For a script that mixes several old calls, update each one. Here is an old script with study, iff, security and rsi, after conversion to version 6:
//@version=6
indicator("Old")
x = (close > open ? high : low)
plot(x)
plot(request.security(syminfo.tickerid, "D", ta.rsi(close, 14)))
study became indicator, iff(close > open, high, low) became (close > open ? high : low), security became request.security and rsi became ta.rsi.
Ways to fix it faster:
- On TradingView, the Pine editor has an option to convert a script to a newer version. Use it on the original version 4 code before you start editing by hand.
- For a misspelling, compare the name in the error with the function list in the Pine reference, character by character.
- For a library function, check the
importline at the top of your script and that the library exports the function you call.
What SuperCharting's Script Editor shows
SuperCharting's engine is more lenient here and runs this code. The broken example with bare sma(close, 20) under //@version=6 compiles and runs in the Script Editor without an error. If the script must also work on TradingView, fix it the way shown above.
For genuinely old code, the built-in Auto-fix button modernises it. It does this when the script has no //@version line or a version below 6. It then:
- sets
//@version=6; - turns
study()intoindicator(); - adds the
ta.,math.,str.andrequest.namespaces to old function names; - rewrites
iff()as? :.
The converted script in the previous section is what Auto-fix produced from an unversioned script that used study, iff, security and rsi. It lists each change in the console, line by line. Because the conversion runs only when the version is missing or below 6, keep the original version line in place when you paste old code. If you have already changed it to 6 by hand, add the namespaces yourself.
To try it:
- Open the Script Editor with Ctrl + E.
- Paste the old script, version line and all.
- Read the console, press Auto-fix to convert it, then press Add to chart.
The Pine Script code checker page lists everything Auto-fix changes, and Pine Script v6 basics shows current syntax with examples that run.
Related errors
Old scripts rarely have only one problem. Old colour names such as red instead of color.red give a different message, undeclared identifier, because red on its own is a name Pine does not know. Once the names are fixed, a function such as ta.ema may complain that a series int was used where a simple int is expected if its length changes from bar to bar. And if the script reads far back in history inside an if or ? :, see the max_bars_back error.
Frequently asked questions
Why did my old script stop working?
It was most likely written for Pine version 4 and is now compiled as a newer version. Functions like sma, rsi and security need their ta. or request. prefix, and study() must be indicator().
What replaced iff() in Pine Script?
The ? : operator. iff(c, a, b) becomes c ? a : b.
Can SuperCharting convert version 4 code for me?
Yes. If the script has no //@version line or a version below 6, the Auto-fix button sets version 6, changes study() to indicator(), adds the missing namespaces and rewrites iff(). Check the list of changes it prints.
The error names a library function. What should I check?
That your script imports the library, and that the library exports the function. Without both, Pine cannot find it.
TradingView and Pine Script are trademarks of TradingView, Inc. SuperCharting is independent and not affiliated with it.