Tradingview: Line Wrapping

In last week’s post, we looked at creating functions. In it, I noted that line wrapping a long line in a function can be a little tricky at first. The official documentation is not bad, but if you don’t read it carefully, you can end up in a battle with the pine script compiler.

So what is line wrapping?

If you have not read any of the previous articles and are new to programming, you might not know what line wrapping is. In a nutshell, line wrapping allows us to break up a really long line of code and spread it over multiple lines. This helps to make the code more readable and be a little better organized. It is not absolutely required but it is worth understanding so that you are not caught off guard when reviewing someone else’s code.

On the Tradingview wiki there are three examples of when you might choose to wrap some lines of code. These are:

  • Long single line statements: Good candidates for wrapping include long complex calculations and Ternary conditional operators.
  • Function calls: Some functions accept a lot of keywords, which can make calling it turn into quite a long line. As such pine script allows you to break up the call onto multiple lines.
  • Declaring Functions: Line wrapping a self-created function gives you the opportunity to segment the code into related chunks. Not to mention you may still want to wrap long single line statements inside a function too! We briefly covered line wrapping a function declaration in the tutorial for creating functions.

Below we will look at the 3 main examples in a bit more detail. To try and avoid just regurgitating the wiki, we will look at cases which work, some which don’t and some alternative valid wrapping styles.

Long Statements

If we look at the Tradingview wiki explanation:

Any statement that is too long in Pine Script can be placed on a few lines. Syntactically, a statement must begin at the beginning of the line. If it wraps to the next line then the continuation of the statement must begin with one or several (different from multiple of 4) spaces.

For beginners, this explanation might be a bit intimidating. Also, since the author is not writing in their native language, the most important part of this statement is easy to get lost in translation. Let’s break it down:

  • Syntactically, a statement must begin at the beginning of the line: This just is telling you that normally when you write a line of code, you need to start writing it at the beginning of the line. (Without any indentation or whitespaces)
  • If it wraps to the next line then the continuation of the statement must begin with one or several (different from multiple of 4) spaces: Ok, part is easier to misunderstand. It is saying “In order to wrap a line, the next line you write (which is a continuation of the statement / long line) must start with one or more blank spaces/whitespaces”. This is how the compiler/interpreter knows that the next line is part of the same long line and not a new one.  The important part not to miss (which I did the first time around) is “different from multiple of 4“. This means you cannot wrap a line using 4 blank spaces or any multiple of 4. (So 8, 12, 16 and so on). That indentation level is reserved for functions and it will confuse the compiler/interpreter.

You will be able to easily see when you are using a multiple of 4 spaces in the pine script editor. A handy little line is drawn to mark it. By the way, if you hit “tab” on the keyboard, this is considered 4 spaces!

Line wrapping - indentation markers

Examples

First, let’s flesh out the official example into a super simple indicator.

No prizes for guessing this one works well! However, this is not the only way. The following gallery shows some alternative ways to write the same line with different indentation levels.

For me, the most jarring example it that I can have a line that ends with an = operator. Having said that, I guess it is only because I am coming from a Python background. Indentation is the key to line continuation.

Finally, we should show one quick example of what does not work. In the example below you will see that the open and low lines start on a multple of 4 blank spaces. This will cause the following compile error.

Add to Chart operation failed, reason: line 5: syntax error at input 'end of line without line continuation'

Invalid Line Wrapping

Function Calls

Let’s extend the example above and do some line wrapping on the plot function call. In this section, I shall only provide one example as the alternatives are the same as above.

I personally prefer to wrap the lines to the same indentation level but it is a case of “each to their own”. Go with what you find easier to read.

Something to watch out for

When wrapping a function call, take special care to remember to include the comma, between arguments. It is easy to forget when wrapping lines and will result in an error that looks something like this:

Add to Chart operation failed, reason: line 11: mismatched input 'color' expecting ')'

It is not the most helpful of errors and may leave you scratching your head!

Declaring Functions

The last example covers declaring/creating functions. Again, the methodology is the same but you might want to use a number of different indentation levels to break a large function into smaller readable chunks. The official documentation does just that:

Full credit: https://www.tradingview.com/wiki/Lines_Wrapping

The only issues I see here for beginners is that the function is really complex and there is little instruction as to why the author decided to use the indentation levels selected. The following example is taken from the Tradingview: Creating Functions post.

The function is used to return a data source (Open, High, Low, Close etc) depending on what string (str) is fed into the function. It is used in conjunction with the built-in input()function to allow a user to change the source of an indicators data. I hope this example shall be a little easier to understand. There are a couple of things to pay attention to:

  • The first linex = str == 'Open' ? is not wrapped. It is the first line of the function. Therefore, it MUST be indented to 4 white spaces (or one tab).
  • The final x is the value returned from the function and not part of the wrapped line. It is also indented to 4 white spaces.
  • The indentation levels selected for the ternary conditional operator, switch between two levels. My preference was to have one level for performing the test (i.e does the string equal ‘high’?). Then a second indentation level for the result if the test returns true. I find this quite easy to read.

Comment Issue

The Tradingview wiki highlights an issue with commenting on a wrapped line. Essentially, you cannot do it (for now). If you put a comment at the end of a wrapped line, you will receive an error. Hopefully, this will be resolved in the future. However, in the short term avoid comments on wrapped lines.