Pine Script error: Mismatched input ... expecting ... (how to fix)
Fix "Mismatched input expecting" in Pine Script: check the line named and the line above for a stray indent, misaligned if/else blocks or a bracket.
"Mismatched input '<x>' expecting '<y>'" means Pine was reading your code, expected one thing next, and found something else. The word after "input" is what it found; the word after "expecting" is what would have fitted. In most scripts the cause is indentation, often a single stray space at the start of a line, so the fix is to check the line named and the line above it.
How to read the message
The two quoted parts are the clue. Say the message names 'plot' on line 3. Pine got as far as plot and could not fit it into the statement it thought it was reading. That statement may have started on line 2, which is why the line above matters as much as the line named. A missing bracket or a block that ended in the wrong place on line 2 often only shows up when Pine reaches line 3.
What causes it
A statement indented when it should start at column 0
Top-level statements start at the left margin. One space in front is enough to break the script, and it is hard to see. This FAILS on TradingView:
//@version=6
indicator("Mismatched")
plot(close)
Line 3 starts with one space. Pine treats the indent as meaningful and does not accept plot there.
if, else if and else blocks that do not line up
The keywords of one if chain must start in the same column, and each body must be indented the same way under its keyword. This FAILS on TradingView, because the else is two spaces further in than its if:
if close > open
level := high
else
level := low
A stray or missing bracket
An extra ) or a ( that is never closed can also produce this message. Both of these FAIL on TradingView:
plot(close))
plot(ta.sma(close, 20)
How to fix it
Work through the causes in this order.
- Check the start of the line named. Remove any spaces before a top-level statement. The tested fix for the first example is simply the same line moved back to column 0:
//@version=6
indicator("No stray indent")
plot(close)
- Check the line above. Look for an unfinished expression, an opening bracket without its partner, or an
ifwhose body ends one line too early or late.
- Line up if, else if and else. Put the keywords of one chain in the same column and indent each body by 4 spaces. This version compiles:
//@version=6
indicator("Aligned blocks")
float level = na
if close > open
level := high
else if close < open
level := low
else
level := close
plot(level)
- Count the brackets. On the line named and the one above, every
(needs one), and the same for[and].
If the script came from somewhere else, it is worth selecting all of it and checking the left edge of every line for spaces that should not be there.
What SuperCharting's Script Editor shows
SuperCharting stops on this mistake while you type. With the first broken example, the console shows:
✗ line 3: unexpected 'plot'
The wording differs from TradingView's. It names the line and the word that did not fit, so look at the start of line 3, then at line 2, just as you would with the TradingView message.
In our test the built-in Auto-fix did not repair this example, so remove the stray space yourself. On accounts where the site owner enables it, the deeper AI check inside Auto-fix can attempt repairs that simple rules cannot, such as a block indented wrongly. Read the list of changes it makes before you rely on the script.
To check your version:
- Open the Script Editor with Ctrl + E.
- Paste the script into the code area. The compile check runs as you type.
- Read the console. When it shows ✓ and the script name, press Add to chart.
The Pine Script code checker page explains the console messages in more detail, and the Script Editor page covers the rest of the editor.
Related errors
Indentation is behind another common message: when a long line is wrapped and the next line is indented by exactly 4 spaces, TradingView reports end of line without line continuation. If you fix the indent by moving a statement out of an if block and a variable stops being visible, you will see undeclared identifier. And moving a plot() into an indented block to line it up gives cannot use 'plot' in local scope.
Frequently asked questions
What does "expecting" mean in the error?
It is what Pine would have accepted at that point, while the word after "input" is what it found instead. Compare the two with what you wrote on that line and the line above.
The line named looks correct. Where else should I look?
At the line above it. A statement that started earlier, or a bracket left open there, often only causes the error on the next line.
Can one space really break a Pine script?
Yes. A top-level statement such as plot(close) must start at column 0. A single space before it is enough to give this error.
Does SuperCharting fix this automatically?
Not in our test: the built-in Auto-fix left the stray indent in place. The console still shows the line number, so the fix is quick to make by hand.
TradingView and Pine Script are trademarks of TradingView, Inc. SuperCharting is independent and not affiliated with it.