Edgoro
Compile error · Syntax and indentation

Pine Script error: Syntax error at input (fix)

Why Pine Script reports "Syntax error at input" on a line that looks correct, and how to fix block indentation, statements on one line and functions in blocks.

Pine Editor console · line 4
Syntax error at input "upBars"

Cause

The parser hit a token it can't fit into a valid statement. In practice it is usually indentation: a block indented by 2 spaces instead of 4, or two statements on the same line.

Broken code

TradingView reports the error on line 4.

Pine Script v6
//@version=6
indicator("Up bars")
var int upBars = 0
if close > open
  upBars += 1
plot(upBars)

Fixed code

The same script, corrected for Pine Script v6. Paste it into the Pine Editor and save.

Pine Script v6
//@version=6
indicator("Up bars")
var int upBars = 0
if close > open
    upBars += 1
plot(upBars)

Why it works

Blocks in Pine Script are indented by exactly 4 spaces or one tab. Two spaces is the indentation of a wrapped line, so Pine tried to read upBars += 1 as the continuation of if close > open, which can't be parsed. That is why the error points at the end of line 4, where the two lines were joined, and quotes upBars. With 4 spaces the line becomes the body of the if.

Code pasted from a chat answer or a website often arrives with 2-space indentation. Re-indent each block level to 4 spaces.

Other causes

  • Two statements on one line. ma = ta.sma(close, 20) plot(ma) gives Syntax error at input "plot". Put each statement on its own line.
  • A function declared inside a block. User-defined functions can only be declared at the top level of the script; f(x) => x * 2 inside an if gives Syntax error at input "=>". Declare the function globally and call it from the block.
  • A stray space at the start of a top-level line. A line indented by one space after a complete statement is read as a continuation of that statement.

Questions

The line looks correct. Why does Pine Script report a syntax error?

The quoted token is where parsing failed, not necessarily where the mistake is. Check the indentation of that line and the end of the line above: a block indented by 2 spaces, a missing operator or an unclosed bracket on the previous line all surface on the next token.

Can I declare a function inside an if block in Pine Script?

No. User-defined functions must be declared at the top level of the script, above the first line that calls them. Declare the function globally and call it from inside the block.

The scripts on this page are minimal examples written to show the error and its fix, not trading systems. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Still getting an error?

Paste your script and the message into the free Pine Script error fixer: it explains what went wrong and returns a corrected v6 version. Or describe the indicator in plain English and let Edgoro write the code.