Line 9 Syntax Error at Input end of Line Without Line Continuation
With a clever use of indentation and whitespace the code of our TradingView indicator and strategy scripts becomes easier to read. But indentation is a precise matter: a single space can make our script error with 'end of line without continuation'. Let's see what that error is about.
IN THIS ARTICLE:
# Examining the 'end of line without continuation' error
Whitespace is an important feature in TradingView. With Enter, Tab, and Space we not only format how our code looks like, but also which statements belong together. For instance, when we indent all statements of a custom function TradingView knows which lines belongs to the function and which not.
TradingView is quite strict when it comes to whitespace: a single space here or there can make all the difference. When we mess up our script's indentation, chances are we run into the 'end of line without continuation' error. With that error two things can happen. When our script already runs on the chart, we'll see the general 'cannot compile script' error there:
This doesn't say much since most TradingView errors show the exact same message. A more informative error message is the one we find in TradingView's Pine Editor.
There in the console window we see something like:
Processing script... line 4: mismatched input 'end of line without line continuation' expecting ')' Script 'Error example' has been saved
Now this error information is something we can use to fix a mistake. Let's learn how.
# Fixing TradingView's 'end of line without continuation' error
The 'end of line without continuation' error message sounds a bit cryptic and abstract. But luckily solving the error is easier than its name suggests:
- Read the error message carefully. Note the line number.
- Go to that line number in the Pine Editor. There look for any whitespace or indentation mistakes and fix them.
The line number that TradingView errors mention is where a statement starts. When we have a multi-line statement on lines 5-7 with an error in line 6, the error still mentions line 5. That's because on that line the erroneous statement started.
This means that sometimes we'll have to look one or two lines lower than the line number mentioned by the error message.
To make fixing this error more concrete, we'll explore several error examples below. But first a quick look at TradingView's whitespace rules.
# Understanding TradingView's indentation and whitespace rules
How we indent our code is a big deal in TradingView. That's because indentation determines if a piece of code is a new statement, some part of a previous line, or belongs to a custom function, loop, or if/else statement. While other languages use braces ({
and }
) to make those decisions, TradingView use Tab and Space.
These are the indentation rules that TradingView Pine has (TradingView Wiki, 2018a, 2018b, 2018c):
- When we take one statement and spread it out over multiple lines, we'll have to indent those extra lines as follows:
- Additional lines of a multi-line statement are indented with 5 spaces or the equivalent 1 tab plus a space.
- When we make a multi-line function, each line of code inside that function's body has to be indented with 4 spaces or 1 tab.
- Should we make a multi-line statement inside a multi-line function, then we indent each additional line of that statement with at least 9 spaces or 2 tabs plus a space.
- We indent code of an if/else statement with 4 spaces or 1 tab.
- Should our if statement contain another if or if/else statement, then we indent that additional if statement with 8 spaces or 2 tabs.
- We indent code of a loop with 4 spaces or 1 tab.
While it helps to know these rules, you don't have to be able to recall them. In the Pine Editor we can quickly spot how much whitespace an indentation uses thanks to a handy feature.
# Tip: quickly see indentation in TradingView's Pine Editor
To see how much whitespace a particular line of code uses, we only need to look for small vertical bars in the Pine Editor:
Those little vertical bars appear at each 4 spaces or 1 tab. So when our line is indented with Tab, we don't see the vertical bar (because the first letter of the line of code appears there). But if our code is indented with say 5 spaces, the vertical bar is visible.
In other words, here's how the vertical bar should appear in our script code:
- We should see the vertical bar with multi-line statements since these are indented with at least 5 spaces (1 tab + space).
- We should not see the vertical bar with multi-line functions, if statements, and loops. That's because these are all indented with one tab (4 spaces).
- When our multi-line function, if statement, or loop contains another multi-line function, if statement, or loop, then we should see one vertical bar. That's because we'll need 2 tabs (8 spaces) of indentation, and the vertical bar appears at the 1 tab (4 spaces) level.
Now let's explore some example scripts that trigger the 'end of line without continuation' error and how we'd fix them.
# Error example: multi-line statement indented with Tab (4 spaces)
When we got a lengthly line of code, we can spread that statement out over multiple lines. For that we'll need to indent each extra line with 5 spaces or 1 tab plus a space.
But when a multi-line statement is indented with 4 spaces or 1 tab (or a multiple thereof), our code triggers the 'end of line without continuation' TradingView error. Here's an example of that:
| |
Processing script... line 2: syntax error at input 'end of line without line continuation' Script 'Error example' has been saved
Here in line 2 we start a multi-line statement for the study()
function. What's lacking, however, is proper indentation: the overlay
argument is indented with a single Tab (4 spaces).
But a multi-line statement needs at least 5 spaces (1 tab plus space). So all that's needed to solve the 'end of line without continuation' error is to add one space to the start of line 3:
study(title= "Error example" , overlay= true)
# Error example: indent multi-line statement with multiple tabs
A statement that we spread out over multiple lines should be indented, but not with a multiple of 4 spaces or 1 tab (TradingView Wiki, 2018a). The minimum indentation for those extra lines is 5 spaces (1 tab plus a space).
In other words, a multi-line statement cannot have 4, 8, 12, or 16 spaces nor 1, 2, 3, or 4 tabs before it. When we do use one of these indentations, we'll get TradingView's 'end of line without continuation' error.
For example, this faulty indicator script uses 2 tabs to indent a multi-line statement:
| |
Processing script... line 4: syntax error at input 'end of line without line continuation' Script 'Error example' has been saved
The problem here is the 4th line. There we set the value of the plotValue
variable with a multi-line statement. But we didn't indented those extra lines properly: they use 2 tabs (8 spaces), while 5, 6, 7, 9, 10 spaces would do. (This is where the tip from earlier comes in handy: with the vertical bar we can quickly see a line's level of indentation.)
To fix the 'end of line without continuation' error here, all we have to do is add one extra space before each indented line, like so:
plotValue = (open != close [ 1 ]) ? close : low - (. 1 * close)
# Error example: indenting a standalone line that shouldn't be indented
One helpful feature of TradingView's Pine Editor is that it remembers the current line's indentation. Then when we press Enter the new lines at the same level of indentation. This makes it easier to write long multi-line functions and complex loops and if statements.
But we shouldn't start a new, standalone statement with the same indentation that previous lines have. The same indentation should only be keeps when the new code actually belongs to previous lines. When we don't reset the level of indentation, we'll run in the 'end of line without continuation' error message sooner or later.
An example strategy that incorrectly keeps the same amount of tabs and spaces is:
| |
Processing script... line 16: mismatched input 'plot' expecting 'end of line without line continuation' Script 'Error example strategy' has been saved
Line 16 of this example uses the plot()
function to show a 10-bar simple moving average on the chart. But for that line we kept the same indentation as line 12. There we made the plotColour
variable and gave it a colour with the iff()
function.
But the plot()
statement is a new line, separate from that plotColour
line. And so we should reset the indentation, not continue with it.
And so we solve the 'end of line without continuation' error here by removing the whitespace before plot()
:
plotColour = iff(strategy.position_size > 0 , red , iff(strategy.position_size < 0 , green , yellow)) plot(series= sma(close , 10), color=plotColour)
# Error example: indent a function line with a space too many
When we create a custom function that has multiple lines of code, we'll need to indent each statement with 4 spaces or 1 tab (or a multiple hereof) (TradingView Wiki, 2018b).
Even if that indentation is off by a single space, our code doesn't compile and TradingView generates the 'end of line without continuation' error message.
For instance, this example script uses incorrect indentation: one statement in the custom function uses a tab plus a space:
| |
Processing script... line 6: mismatched input 'sum' expecting 'end of line without line continuation' Script 'Error example' has been saved
Since not all statements in the plotValue()
custom function are indented with 1 tab or 4 spaces (or a multiple thereof), TradingView errors with 'end of line without continuation'.
While fixing the error here is simple, spotting it is a bit more challenging. But that becomes easier when we look for the small vertical bar in the Pine Editor (like we discussed above). When the first line of a statement in a custom function begins with that bar, we know the indentation is incorrect.
To fix the error in the above example, we only have to remove one space of indentation. That makes the function look like:
plotValue(a, b) = > c = a + 0.1 * b sum = a + b + c sum / 3.21
# Error example: forget to indent a multi-line statement
When we spread out a statement over multiple lines, its extra lines should be indented with at least 5 spaces (or 1 tab plus a space) (TradingView Wiki, 2018a).
But sometimes indentation isn't copied over when paste code in our script. Or perhaps the source from which we copied had no indentation to begin with. Then when we save our script, TradingView generates the 'end of line without continuation' error.
An example where a multi-line statement lacks (proper) indentation is:
| |
Processing script... line 6: syntax error at input 'end of line without line continuation' Script 'Error example' has been saved
Here line 6, the alertcondition()
statement, triggered the error. The cause is pretty clear when we look at that code: that statement spans across three lines, but has no indentation at all.
To fix the 'end of line without continuation' error we simply indent the two extra lines of that statement with 5 spaces (or 1 tab plus a space). Like so:
alertcondition(close > emaValue, "Example alert" , "Close is above emaValue")
# Error example: incorrect indentation with if/else
When we code an if/else statement, we need to indent the statements below if
and else
with 1 tab (or 4 spaces) each (or a multiple thereof). When that indentation is off by just one space, we'll end up with a faulty script and the 'end of line without continuation' error message.
An example indicator with that mistake is:
| |
Processing script... line 6: mismatched input 'min' expecting 'end of line without line continuation' Script 'Error example' has been saved
The statement below the else
keyword is indented here with 5 spaces (the equivalent of 1 tab plus a space). But that isn't allowed: code blocks of an if/else statement are indented with 4 spaces or 1 tab (TradingView Wiki, 2018c).
We fix this example script when we indent both if/else statement lines with one tab (or 4 spaces). So like this:
value = if close > open max(close , min(low , high)) else min(close , max(low , high))
# Summary
Indentation and whitespace are a serious matter in TradingView. Tab and Space not only format how our code looks, but also which lines are considered part of the same statement.
When we make a mistake with indentation, chances are we'll trip over TradingView's 'end of line without continuation' error message. While the error sounds complex, fixing it isn't hard. First read the error message carefully and note the line number. Then navigate to that line in the Pine Editor. There look for any indentation problems and fix them.
TradingView's indentation rules are quite precise. Statements inside a multi-line function, loop, or if statement are all indented with 4 spaces or 1 tab. When a statement spans multiple lines, we'll need to indent those extra lines with at least 5 spaces (1 tab plus a space).
To quickly see a line's indentation, look for a small vertical bar in TradingView's Pine Editor. That bar shows for every 4 spaces (1 tab). With statements inside a multi-line function, loop, or if statement we should not see the vertical bar. For multi-line statements that bar does need to show.
References
TradingView Wiki (2018a, January 28). Lines Wrapping. Retrieved on July 19, 2018, from https://www.tradingview.com/wiki/Lines_Wrapping
TradingView Wiki (2018b, January 28). Declaring Functions. Retrieved on July 19, 2018, from https://www.tradingview.com/wiki/Declaring_Functions
TradingView Wiki (2018c, March 25). Expressions, Declarations and Statements. Retrieved on July 20, 2018, from https://www.tradingview.com/wiki/Expressions,_Declarations_and_Statements
« All TradingView errors articles
Source: https://kodify.net/tradingview/errors/end-line-without-continuation/
0 Response to "Line 9 Syntax Error at Input end of Line Without Line Continuation"
Post a Comment