Pine Script mismatched input error: causes and fixes
Short answer
The parser met a token where a statement should have ended; the quoted token is where it gave up, not always where the mistake is. In code ported from MQL5 it is usually a brace, a semicolon, a for(;;) header, two statements on one line, or indentation that isn't a multiple of four spaces.
TradingView reports Mismatched input 'x' expecting 'end of line without line continuation' (or a close relative such as Syntax error at input 'x', depending on the editor version) when the parser meets a token where a statement should have ended. The quoted token is where the parser gave up, not necessarily where the mistake is. In scripts ported from MQL5 the cause is almost always a C habit: a brace, a semicolon, a for(;;) header, two statements on one line, or an indentation that is not a multiple of four spaces.
The messages in this page's table were produced locally with the Stratlate Pine parser, which reports them in ANTLR's wording (mismatched input, extraneous input, missing INDENT) with the line and column. TradingView's editor words them differently but points at the same token; the sentence above is TradingView's usual phrasing for the most common case.
Why it happens when converting MQL5
Pine's grammar is line-based, like Python's. A statement ends at the end of the line. A block (the body of if, for, while, switch or a function) is the run of following lines indented by exactly four spaces (or one tab) more than the header. There are no braces, no semicolons and no statement terminators, and a line can only be continued onto the next one if the next line is indented by an amount that is not a multiple of four.
MQL5 is C: whitespace means nothing, braces make blocks, semicolons end statements and for has a three-part header in parentheses. Ported line by line, nearly every MQL5 construct carries something Pine's parser cannot place, and because the parser stops at the first one, fixing a script is a sequence of single errors.
Fix
Each row is a minimal Pine v6 script with one MQL habit in it, and the message the parser printed for it:
| Line as written | Parser message (line:column) | Pine v6 |
|---|---|---|
len = input.int(20, "Length"); | 3:29 extraneous input ';' expecting {NEWLINE, ','} | Drop the semicolon. |
if close > ma { | 4:14 extraneous input '{' expecting NEWLINE | if close > ma, body indented four spaces, no closing brace. |
if close > ma then | 5:14 extraneous input 'then' expecting NEWLINE | No then; the newline ends the condition. |
for (int i = 0; i < 10; i++) | 4:4 extraneous input '(' expecting {..., Identifier} | for i = 0 to 9 (the upper bound is inclusive). |
ma = ta.sma(close, 20) plot(ma) | 3:23 mismatched input 'plot' expecting {NEWLINE, ','} | One statement per line. (A comma can join simple declarations, but do not rely on it.) |
Body indented three spaces under if | 6:3 mismatched input 'c' expecting {NEWLINE, 'and', 'or', ...} | Indent by four spaces. Three spaces made the body a continuation of the if line. |
else indented two spaces | 7:2 extraneous input 'else' expecting {NEWLINE, ','} | else at the same column as its if. |
Body not indented under if | 7:0 missing INDENT at 'bull' | Indent the body four spaces. |
The second-to-last row shows why "mismatched" often points at a strange place. Three spaces is not a block indent, so the parser read the next line as a continuation of if close > ma, producing if close > ma c := color.green, and complained about c because it expected the condition to keep going with and, or or an operator.
A small MQL5 fragment, ported with all of those habits removed:
// MQL5
color c = clrGray;
if(close[i] > ma) { c = clrGreen; } else { c = clrRed; }
bool cond = close[i] > ma &&
close[i] > open[i];
double sum = 0.0;
for(int k = 0; k < 10; k++) sum += close[i - k];//@version=6
indicator("Fixed syntax", overlay=true)
len = input.int(20, "Length")
ma = ta.sma(close, len)
color c = color.gray
if close > ma
c := color.green
else
c := color.red
cond = close > ma and
close > open
float sum = 0.0
for i = 0 to 9
sum += close[i]
plot(ma, color=c)
plot(cond ? sum / 10 : na)The Pine script converts with status CLEAN through the Stratlate parser. The points to copy:
if/elsewithout parentheses or braces; each body indented exactly four spaces;elselined up withif.:=to assign to an existing variable;=declares a new one. Writingc = color.greeninside theifdeclares a second, localcand the outer one never changes.- A wrapped expression continues on a line indented by a number of spaces that is not a multiple of four (five here). The operator stays at the end of the first line.
&&,||and!areand,orandnot.for i = 0 to 9counts 0 through 9 inclusive. The loop body is indented like any other block.
Other causes of the same message
- Tabs mixed with spaces. One tab counts as one block indent. A line indented with a tab under a header indented with spaces, or the reverse, can be read as a continuation. Use spaces only.
- A wrapped line indented by exactly four spaces. TradingView treats a four-space indent after a top-level statement as the start of a block, not a continuation, and reports a mismatched or syntax error at its first token. Five spaces, or two, are safe. (Stratlate's parser accepts this case, so test wrapped lines on TradingView.)
- A missing closing parenthesis on the line before. The parser keeps reading into the next line and fails on its first token. The error then points one line below the real mistake.
- Older-version syntax in a v6 script.
study()instead ofindicator(),transp=arguments, orsecurity()without therequest.namespace usually give "could not find" errors rather than mismatched input, and a//@version=4header kept on top of newer code makes TradingView apply v4 rules, whereswitch, methods and several declaration forms do not exist. Check the version line first. The version differences guide lists the renames. - A C type keyword. Pine's types are
int,float,bool,color,stringand the drawing types.double x = 0.0is not Pine; writefloat x = 0.0. - Converted code. The Stratlate MQL5 → Pine converter writes its own Pine from a parsed program rather than editing your text, so it emits no braces, semicolons or C-style loops. The Pine → MQL5 direction stops at a parse error: the report carries a
PM900note with the parser's first message and its position, and no MQL5 is produced until the script parses.
Pitfalls
- Fixing the token in the message instead of the line before. When the quoted token is the first word of a line, look at the end of the previous line: an unclosed parenthesis, a trailing operator, or a continuation indent.
- Letting the editor re-indent. Pasting MQL5-formatted code into some editors converts four spaces to a tab or to three; the code looks right and fails. Turn on visible whitespace while porting.
- Writing
=where Pine needs:=. It parses, so there is no error, but the assignment inside the block creates a new variable and the plotted value never changes. A silent version of the same porting habit. - Translating
i++and+=by habit.+=is valid Pine;++is not a Pine operator. Writen += 1. - Expecting the first error to be the only one. The parser reports one error per compile. A ported file of 200 lines with braces throughout will show the next one as soon as the first is fixed; remove each habit across the whole file before compiling again.
FAQ
What does "mismatched input expecting end of line without line continuation" mean?
The parser expected the current statement to end at that point, but found another token on the same logical line. Usually a statement continues where it should not: two statements on one line, a brace or keyword after an if condition, or a line indented so that Pine reads it as a continuation of the line above.
How do I continue a long line in Pine Script v6?
Break it after an operator or a comma and indent the next line by a number of spaces that is not a multiple of four, for example five or six. A four-space indent starts a block instead.
Can Pine Script use braces or semicolons like MQL5?
No. Blocks are made by indentation of four spaces or one tab, and a statement ends at the end of its line. A semicolon at the end of a line or a brace after an if condition is a syntax error, and removing them is the first step of every manual MQL5 to Pine port.
Let the converter do the mechanical part
Paste the original script and get a conversion that compiles, with every construct that could not be carried over reported instead of hidden. Review, compile and backtest the result before trading.
Open the converterRelated
- How to Convert an MQL4 or MQL5 Indicator to Pine Script
- Pine Script vs MQL5: Differences That Matter for Conversion
- Pine Script v4, v5 and v6: What Changes When You Convert
- OnCalculate in Pine Script: the bar loop becomes one line
- Convert MQL5 to Pine Script (v6) — online MT5 to TradingView converter