Pine Script – Lesson 1: Getting Started
Getting Started with Pine Script
Welcome to my Pine Script tutorial section!
In these lessons I’m going to walk you through the basic knowledge required in order to begin creating your own custom TradingView indicators, trading tools and trading alerts.
Throughout the following lessons (and especially through the Advanced section) I will take you step by step through the recreation of several of my most popular indicators.
But first things first – let’s start at the very beginning. I’m going to assume you have zero experience with Pine Script, but if you know the basics then feel free to skip ahead.
If you prefer to learn in a visual/audio manner, then here’s a video version of this lesson:
(Your First Script)
When you open the Pine Script 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 create. 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 backtesting system).
We will focus on indicators for now, 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 4” of the Pine Script syntax.
Syntax is just a fancy word for “programming grammar”, and a compiler is a translation 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’ so unlike most other programming languages, you don’t need to understand all of this in order to use Pine Script.
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 Pine Script code, but rather it tells the TradingView platform which version of Pine Script to use.
The word “version” before the “=” sign simply means “assign the value 4 to the variable named version”. So @version=4 in English means “Hey Compiler! Before you start, this script is using version 4 of the Pine Script syntax, so when you compile my code into computer-speak, use Pine Script v4.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.
Pine Script is a few years old, so it has a few different versions. Each different version has slightly different syntax rules and built-in 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 Pine Script 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 in the latest version.
Generally speaking, you will never need to modify this line of code when making new scripts. And as of the latest release of Version 4 of Pine Script, you can now click the button ‘Convert to v4’ next to the ‘Save’ button to convert Version 3 scripts into Version 4 without having to touch a line of code.
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 Pine Script 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 Pine Script – how easy it is to paint information directly onto your charts. With just three 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 on the screen. You can change the drawing color, the line thickness, and even draw certain symbols on your chart.
It’s also worth mentioning that if you ever get stuck, or you forget what values a function requires, or you’re just curious about your potential options – pressing CTRL + Spacebar will bring up the Autocomplete box which will suggest relevant coding options to you. This feature is a programmer’s best friend (kind of like a Thesaurus and Dictionary for writers), so you should get used to practicing with it.
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’s that simple to get started. Welcome to Pine Script!
I’m currently working on a very structured, very advanced Pine Script course for those who want to really take their coding skills to the next level. If you’re interested in that opportunity, click here to sign up to be notified when the course is released.
Those who sign up to be notified before the course is released will receive a 50% discount off the opening price!
//@version=4 study("My Script") plot(close)