Pine script: lets get started

in #trading4 years ago

Let's start making contact with the TradingView Pine Script code. Let's start from scratch, that is, from the basics.

The first thing you encounter in the code is the declaration of the version to use. It may seem bizarre but it is done with a comment that in the case of the code is similar to Javascript and is triggered with two bars "//" (the alternative symbol to the number 7 key on the Italian keyboard). The complete declaration will be followed by '@ version' and the version number, as in the example below:

//@version=4

Version 4 at the time of this writing is the latest and we will refer to it. If nothing is declared the system will take Pine Script version 1 as a basis.

However, we learned how to write comments on the code, they can also be placed after valid code as long as they are on the same line and at the end of it.

The next statement will be to indicate whether we are going to indicate a code that is related to "Study" or "Strategy". Here we obviously stop for a moment because it is necessary to understand the differences well.

Study vs Strategy

Study actually allows you to apply objects, calculations and even activate alarms but it is not able to compute sales or purchase actions (simulated or not).

This can be done by the Strategy, which is actually also able to check the losses and gains of this strategy. We can see it as a prototype of a trade-bot since based on certain choices on the code it can potentially place orders and sales.

So to go on the practical Study or Strategy apply in this way:

study("Il mio Script") // dichiaro uno study
strategy("La mia Strategia") // dichiaro una strategy

As you can see, the syntax is simple, you just have to be careful to give a name to the strategy or study.

Invisible loop

A second note to consider is the type of application. In computer programs, when a code is executed, that is, it has a beginning, and on the basis of the instructions it arrives at a result, that is, an end. It is possible to re-iterate this code in a loop, called a “loop”. In the case of Pine Script the loop is invisible, since we don't have to worry about instructing the system to loop, this is already guaranteed by the system itself. So we talk about “invisible loop” and the application of the code will most likely be applied at the end of a candle's execution.

Obviously, in the case of a strategy that includes a back-test in the past, this will also apply to the data existing in the system.

Commands

So after the declaration of Study or Strategy, the code will have some differences that we will discover with the continuation, in this phase it is not important to know or have to remember this thing.

Let's start with a rather important command that is obviously ambivalent, namely Plot. This command is used to draw something on the graph, let's see the basic code it proposes:

//@version=4
study("Il mio Script")
plot(close)

in this case the closing value of the candle is drawn on the chart, creating a new linear chart, which shows the trend of all closings.

To apply the above, once the code was written, it was sufficient to press the "add to graph" button located in the editor header.

To eliminate this indicator, it will be sufficient to move the mouse to the title of our object "my script" in this case, where a pop-up menu will appear with a whole series of icons, including "delete".

It is interesting to note that the graph we created is shown in a new section, generally on the bottom and that from the point of view of the "space" on the screen, it reduces the size of the screen. In fact, the study command brings with it some default options which, if not explicitly declared, are inserted by the system automatically.

If we write the above code like this we will have the same result:

//@version=4
study("Il mio Script", overlay=false)
plot(close)


As you can see, we have added overlay = false which indicates not to overlay the closing chart above the price chart. If instead of false we indicate the value true, this will be superimposed on the price

Coin Marketplace

STEEM 0.20
TRX 0.26
JST 0.039
BTC 100104.47
ETH 3619.58
USDT 1.00
SBD 3.10