Getting Started With Pine Script

avatar

pinescript.png

Pine Script is a scripting language built for developing trading chart indicators and backtesting trading ideas on Tradingview. If you are not familiar with the Tradingview platform you may want to read Getting Started with Tradingview first. In short, Tradingview is a social networking platform where users can share their trading ideas. It provides users with powerful charting tools such as real-time charts for various financial assets including cryptocurrencies, advanced drawing tools, programming tools, etc.

Pine Script is developed by Tradingview and only works on their platform. It is not a general-purpose programming language. It only works on Tradingview. It has only one purpose - to provide Tradingview users with programming capabilities for developing indicators and backtesting their strategies on the platform. The platform has an easy-to-use editor to write the scripts, and all scripts are run in the cloud. Because it is cloud-based, and computing is provided mainly for free they may impose certain limits to ensure a smooth experience to all users. I have been experimenting with it a little bit, and so far have not experienced any limitations.

Why learn Pine Script?

Those who already use the platform for various reasons like looking up prices, drawing on charts, following trading ideas will be able to take advantage of the platform to the fullest. Even if you don't write your own scripts, just being able to run publicly shared scripts will beneficial. Ability to read and be able to interpret the scripts, and the logic of what they are doing may help to understand commonly used indicators like RSI, MACD, moving averages, and more. It is up to the user to decide to what extent they would like to learn and implement their knowledge Pine Script. There sure are benefits for those who already use the platform.

Those who don't use the Tradingview platform yet but are crypto enthusiasts or traders may find it to be familiar as well. Tradingview supports third-party apps and websites, and there are many who utilized their charting tools on their website. Of course, Tradingview charts are not limited to cryptocurrencies, and the provide data/charts for many financial assets like stocks and futures markets. Normally those charts on third-party apps and websites are limited to charts and main indicators and do not provide the ability to run scripts.

Compared to other programming languages Pine Script easier to learn. There are many freelancing opportunities for Pine Script programmers. You may even have some friends who have trading ideas and strategies but don't know how to code it. Unlike other programming languages, Pine Script programmers do not need to learn several libraries and other technologies to become proficient in it. Just knowing the core language syntax, rules, logic, and getting familiar with built-in functions might be sufficient enough.

If you were to write code to build indicators on general programming languages like python or javascript, two of the main things you will have to learn would be how to get historic and realtime price data and how to plot them. While this may give more flexibility, it will also require a lot of time and effort. In Pine Script all the necessary price data is already available and plotting the final results is super easy with the use of the plot() function. Built-in variables like close, open, high, low, time, and volume will return the relevant data.

Oh and Pine Script is fun.

Basics of the language to start with

They actively continue developing and improving Pine Script. Over the years they have developed four versions. These versions are not inter-compatible with each other. The code written in version 4 may not run on 3 or vice-versa. A good practice is to use the latest version. However, from time to time you may see scripts shared by community members in the older versions. As long as the version is stated at the beginning of the code, older version codes will run just fine. If the version is not specified, it defaults to version 1. To declare the version of your code you would simply write the following line.

//@version=4

The next thing you will need to specify is if the code is a study or a strategy. This would go in the following line.

study("My First Script")

or

strategy("My First Strategy")

The difference between study and strategy is that study is used for building and plotting indicators, while strategy is used for backtesting. I personally have not used strategies yet. If you are a beginner you will probably start with study. Just remember every Pine Script code starts with either study() or strategy() function.

Pine Script codes will mainly consist of variables and functions. There are no classes, complex data structures, and object-oriented notions like in other programming languages. All you need to master is how to use variables and functions.

Variables will contain our data to be computed. Among primitives data types are integers, floats, booleans, strings. These are all easy to grasp. If you have done any programming chances are you already know them. The core data type on Pine Script is series. You may want to spend some extra time and effort to understand what they are and how they work. These are lists of values. However, they do not behave the same way as lists and arrays do in other programming languages.

For example, close is a built-in variable that holds a list of closing prices. When applied to a certain asset at a certain timeframe it will return the closing prices for each bar. In other programming languages to access individual values in the list we do something like this:

close[1] or close[5]

You would think it would return the value at the provided index. It doesn't. Using [x] will return another series with prices being shifted in the index and some turned into a special value na (not available).

Let's say our close has 5 prices inside, (10, 10.5, 11, 9, 9.5). close[1] will have (na, 10, 10.5, 11, 9).

If it seems complicated, don't worry, it kinda is. But after experimenting a little bit, it starts making sense.

Then we have functions. There are many built-in functions already available so we don't have to re-invent the wheel. At times though we want to create our own custom function. A simple example of a function may look like this:

f(a,b) => a + b

Here, our f function has two arguments a, b. Our function will return the sum of a and b. Functions are straightforward and are easy to understand and use.

An important thing to know about functions in Pine Script that they are distinguished in two categories: functions and annotations. While they both are still considered functions, the difference is functions are used to calculate values and often return a new value. Annotations are usually used to output the end result of our code, get input from users, and assign global properties. All annotations are built-in, we don't get to code them ourselves, we just use them. Functions can be built-in as well. We create our own functions for different calculations within our code.

I hope to write about various aspects of Pine Script more in more detail in the future as I continue learning and experimenting. This is not an educational material, but more of a documentation of my journey in learning the language for future reference. If you have experience with Pine Script and would like to share some tips and tricks I would appreciate it.



0
0
0.000
8 comments
avatar

To listen to the audio version of this article click on the play image.

Brought to you by @tts. If you find it useful please consider upvoting this reply.

0
0
0.000
avatar

According to the Bible, Will animals and pets go to heaven? (Part 4 of 4)

(Sorry for sending this comment. We are not looking for our self profit, our intentions is to preach the words of God in any means possible.)



Comment what you understand of our Youtube Video to receive our full votes. We have 30,000 #SteemPower. It's our little way to Thank you, our beloved friend.
Check our Discord Chat
Join our Official Community: https://beta.steemit.com/trending/hive-182074

0
0
0.000
avatar

I don't program, but this seems like great information and ike it would be interesting to many in this space!

Diversity in content is important.

0
0
0.000
avatar

coffeea You need to own more COFFEEA (5 COFFEEA in your wallet allows you to send 1 TOKEN per day)

0
0
0.000