With a brutal bear market in 2022 I decided to take a dive into the world of coding to prevent myself from over trading. I’ve come to really enjoy coding and Pine Script has allowed me to combine my passion for the markets with my new found love of coding.
My goal with this section of my Substack is to provide simple and basic Pine Script lessons that anyone can understand, as well as continue to improve my own coding ability.
Lesson Summary
How to open the Pine editor
Different types of scripts you can create
How to use comments
What is the annotation function and compiler directive
What is Pine Script?
Pine Script is a programming language created by TradingView that allows you to create custom indicators and strategies.
Pine Script is a very basic, but powerful language for creating indicators, alerts, and strategies.
There are three different types of scripts you can create with Pine Script:
Indicator – an indicator is a technical analysis tool used to analyze markets and make trading decisions, a moving average for example
Strategy – a strategy is similar to an indicator but it also includes a set of rules for buy and sell decisions, that can be used for back testing
Library – a library is used to share pieces of code and not have to rewrite them multiple times in different scripts. You can create custom libraries or use one of TradingView’s many built in libraries.
Before we dive into how to get started with Pine Script make sure you are subscribed so that you don’t miss any future updates!
Getting Started
Getting started with Pine Script is very simple. All you need is a free TradingView account and you are able to start. You don’t need to download or install anything on your computer.
To open up the Pine Script editor log in to TradingView and open up a chart. Then click “Pine Editor” from the lower menu.
Once you open up the Pine Editor it should look something like this. This is the basic blank indicator template.
Let’s go through this line by line.
This line is simply a comment. All comments in Pine Script start with two forward slashes “ // “. Anything written after that will be ignored by the editor. You can use comments to leave yourself notes throughout your script. Comments can be written on any line and do not need to be at the beginning of the line.
This however, is an important comment as it has a link to terms and protects you, saying that the performance of the script falls on the script user not the author.
The next line is another comment that is automatically generated with your TradingView username, this acts as your copyright.
The third line is the only “comment” that isn’t actually a comment. This line is known as the compiler directive, and it’s used to tell the Pine Editor which version of Pine Script to use. If you delete this you will not be able to create a script.
Next is the annotation function, which determines which type of script you are writing. This can be an indicator, strategy, or library. This line is also used to give your script a title, define chart parameters and define overlay type (draw on the chart, or a new pane below). I’ll cover these in a future post.
Lastly, we have a plot function. Again, I will cover the details of the plot function in a future post, but just know a script must have at least one type of plot function or your script won’t compile, and you will receive an error like shown below.
Thanks for reading! I plan on putting out a lot more content on Pine Script very soon. If you enjoyed this article please help support my work by doing the following:
Follow me on Twitter @amphtrading
Share this post using the button below
The content presented is for informational and educational purposes only. Nothing contained in this newsletter should be construed as financial advice or a recommendation to buy or sell any security. Please do your own due diligence or contact a licensed financial advisor as participating in the financial markets involves risk.