Posts

Tradingview Pine Script: Functions and Annotations

avatar of @geekgirl
25
@geekgirl
·
·
0 views
·
3 min read

If you haven't heard or used Tradingview yet, you may want to read m post about Getting Started with Tradingview. It is a social media platform with powerful charting tools. Users can post their trading ideas, develop and share various indicators, or simply just use the charting tool for their trading research.

Tradingview has its own programming language Pine Script that helps users to develop their own indicators and do backtesting and forwardtesting as needed. If you have no experience with Pine Script yet, consider reading my introduction post Getting Started with Pine Script.

Tradingview is a very helpful platform for all traders in traditional markets and crypto traders. Not only it provides historic price and volume data but also many built in indicators, community shared indicators, for pay indicators, and also programming tools with Pine Script for users to develop their own indicators and trading strategies.

Today I would like to talk about functions and annotations. While Pine Script is a powerful programming language, it is not too complex as other general purpose programming languages. It is has one purpose, to help users to develop indicators and strategies for financial assets and it only works on Tradingview platform.

Usually a Pine Script code is comprised of variable or primitive type like integers, floating point numbers, strings, booleans, and unique series. While series resemble lists in python or arrays in other languages, they are behave differently as they usually represent the value for prices or volumes for given timeframes. In addition to these variable, Pine Script utilizes functions and annotations. Syntacticly function and annotations are the same. Annotations are functions, but as the manual describes have side-affects. These side affects are shown in the form of output in charts, input data from users, etc.

So, basically annotations are functions but with input/output features. They are also called annotation functions, but shortly we can refer to them as annotations. All annotations are built in functions. Key difference of annotations from functions is that annotations take in key/value arguments while normal function don't.

Among commonly used annotations are study(), strategy(), and plot(). Every Pine Script code will require either study() or strategy() annotation functions to start the code. Former is for developing indicators, while latter is for testing strategies.
Also, most of the functions will require a plot() annotations, which plots the calculations on top of a chart or a separate pane. While main plotting annotation is plot(), there are some more plotting functions like plotshape(), plotchar(), hline(), etc. Another useful annotation that takes inputs from users for the code is input().

Users can easily find what each built in functions or annotations do by going to Pine Script Reference Manual to look up the details and arguments taken.

Now when it come to other functions, they can be separated into two: user defined and built in. Since Pine Script is specializes on developing indicators and strategies for financial assets, Tradingview has done a good job of proving many built-in functions for widely known tasks and indicators. At the same time Pine Script provides ability for users to write their own functions to make their proper computations in developing their own indicators and strategies.

As an example let's take a look at the code below:

Pine script provides multiple built-in function to calculate moving averages. One of them is a simple moving average, one can easily get with sma() function, which takes two arguments. First series like 'close' or 'open' and the period. In the example above it is sma(close, 15).

Users can make their own function to calculate that too as shown with pine_sma(x.y) function. Most likely there would be no need to reinvent the wheel for already existing built-in functions, but is a good example how users can define functions on Pine Script.

I hope I was able to explain the difference between built-in functions, user defined functions and annotations in Pine Script. In the future I hope to share more on developing indicators, hopefully useful ones as I continue learning myself.

Posted Using LeoFinance