A program written in Pine is composed of functions and variables. Functions contain instructions that describe required calculations and variables that save the values used in the process of those calculations. The source code line should not start with spaces (there is an exception, see syntax of a multiline function).
A script should contain a study
function which specifies the script
name and some other script properties. Script body contains functions
and variables necessary to calculate the result which will be rendered
as a chart with a plot
(or some other function that produces the output)
function call.
As a first example, we’ll examine the implementation of the MACD indicator:
1 2 3 4 5 6 7 8 9 | //@version=3 study("MACD") fast = 12, slow = 26 fastMA = ema(close, fast) slowMA = ema(close, slow) macd = fastMA - slowMA signal = sma(macd, 9) plot(macd, color=blue) plot(signal, color=orange) |
//@version=3
study("MACD")
fast = 12, slow = 26
fast
and slow
.fastMA = ema(close, fast)
fastMA
, containing the result of the
EMA calculation (Exponential Moving Average) with the length equal
to fast
(12) on close
series (closing prices of bars).slowMA = ema(close, slow)
slowMA
, containing the result of the
EMA calculation with the length equal to slow
(26) from close
.macd = fastMA - slowMA
macd
, which is being calculated as a
difference between two EMAs with different length inputs.signal = sma(macd, 9)
signal
, calculated as a smooth value of the
variable macd
by the SMA algorithm (Simple Moving Average) with
length equal to 9.plot(macd, color=blue)
plot
function, which would draw a chart based on the values
saved in the variable macd
(the color of the line is blue).plot(signal, color=orange)
plot
function, which would draw a chart for the variable
signal
with an orange color.After adding the indicator “MACD” to the chart we would see the following:
Pine contains a variety of built-in functions for the most popular algorithms (SMA, EMA, WMA, etc.) as well as making it possible to create your custom functions. You can find a description of all available built-in functions here. In the following sections the document will describe in full all the Pine Script capabilities.