PineScript – Lesson 1: Getting Started
Getting Started with PineScript
When you open the PineScript Editor for the first time, it will look like this:
This is the default template for a basic indicator script.
There are two different script types you can choose to use. One is called a ‘study’ (indicator), the other is called a ‘strategy’ (which essentially behaves the same as a study, except that it allows you to enter and exit mock trades through TradingView’s back-testing system).
For now, we will focus on indicators, as strategies require a basic understanding of indicators to implement and are far more complex.
The first line in the default template code represents a comment.
Comments are a common feature of most programming languages. By placing two forward-slashes before a sentence you are saying to the computer, ‘Ignore this text as it is for human eyes only’.
You can use comments to explain your thought process behind certain segments of code. This is helpful for when you write a particularly complex or sophisticated piece of code that might not make sense to you if you were to look back on it weeks or months from the time you wrote it.
As we are dealing with price action data (high, low, open, close, indicator values etc), complex scripts can often become very unreadable very fast. A well-written indicator often looks like pure gibberish to the untrained eye.
Using comments is the simplest way to include annotations in your code to explain what the code does—both for yourself, and for anyone else who you might want to read your code later.
It is generally considered best practice to heavily comment your code. Unless the intention of the code is obvious, then you should always include a comment that explains what it does in human terms.
The first line of code in this case is not a meaningless comment but is telling the compiler to target ‘Version 3’ of the PineScript syntax.
Syntax is just a fancy word for ‘programming grammar’, and a compiler is a tool that converts your script from human readable language to computer-speak (binary code). This is all done for you when you click ‘Add to Chart’.
The @ symbol flags this line of code as a special comment. These types of comments are not ignored by the compiler, but treated as a kind of meta-data. This comment is not a line of PineScript code but rather it tells the TradingView platform which version of PineScript to use.
The word ‘version’ before the ‘=’ sign simply means ‘assign the value 3 to the variable named version’. So @version=3 in English means ‘This script is using version 3 of the PineScript syntax, so when you compile my code into computer-speak, use PineScript v3.0’s rules to do it.’
This may be a little bit confusing, and you don’t need to understand this concept completely just yet. Just be aware that if you do not include this line in your script, then you will not have access to the latest features of the programming language and as a result, you may run into errors and limitations.
PineScript is a few years old, so it has a few different versions. Each different version has slightly different syntax rules and in-built features. The syntax version you tell the compiler to use is very important. When you are telling a computer what to do by writing code, you need to be VERY specific in your instructions or it won’t understand what you are requesting.
A single typo can ruin a script and cause it not to compile, because the computer doesn’t know what it means. The good news is that the compiler will often tell you where this error is so typos are typically easy to fix. But a single wrong character in your code will cause the compiler to fail.
The reason why PineScript allows you to tell it to use old versions is to allow for backwards-compatibility.
For example, if you wrote a script a few years ago using @version=2, then if you leave that line in your code you will retain access to the old syntax rules which you used to write the code and it will compile successfully even if there are differences between the latest version.
Generally speaking, you will never need to modify this line of code when making new scripts.
This line of code is called a ‘constructor’. You can think of it like a page header. When you are writing a set of instructions, you ought to have a title at the top of the page explaining what the instructions are for. That is what this line of code does for the computer.
When creating a strategy script, you would change this line to ‘strategy(“My Script”)’, which would change the capabilities of your script and the default features you can access (eg. allowing you to place mock trades). Most scripts never need to perform trading actions, so for now, we will ignore strategy scripts.
The brackets () are used to enclose the parameters (aka. inputs) of the script’s constructor. Programming borrows a lot of functionality from math, so you can think of brackets like you would in a simple algebra equation.
In this case, it simply encloses the information you want to pass to the ‘study’ instance. It is telling the computer ‘Create me a “study”, and call it My Script’.
When you create new scripts, you can rename them by changing this text to whatever you like.
Now this is where the fun begins.
In this line of code, we are telling PineScript to plot the close of the current candle onto our chart. The script will run on every tick for the current candle and every historic candle, so by default, this will plot a blue line on your chart connecting all the candle closes (essentially drawing a price line chart).
This is the best part of PineScript – how easy it is to paint information directly onto your charts. With just 3 simple lines of code, we already have the foundation of a simple indicator.
With just a few adjustments, you can make this paint the close of a different timeframe, the high or low of the past 50 candles, or any specific price action data you’d like to see to the screen. You can change the drawing color, the line thickness, and even draw certain symbols on your chart.
Once you have written your code and you want to test it out, click the button ‘Add to Chart’ and TradingView will save your script’s source code and then draw your script on the chart.
The default script template will look like this:
It is that simple to get started. Welcome to PineScript!
//@version=3 study("My Script") plot(close)