Script structure
A Pine script follows this general structure:
<version><declaration_statement><code>Version
A compiler annotation in the following form tells the compiler which of the versions of Pine Script® the script is written in:
- The version number is a number from 1 to 6.
- The compiler annotation is not mandatory. When omitted, version 1 is assumed. It is strongly recommended to always use the latest version of the language.
- While it is synctactically correct to place the version compiler annotation anywhere in the script, it is much more useful to readers when it appears at the top of the script.
Notable changes to the current version of Pine Script are documented in the Release notes.
Declaration statement
All Pine scripts must contain one declaration statement, which is a call to one of these functions:
The declaration statement:
- Identifies the type of the script, which in turn dictates which content is allowed in it, and how it can be used and executed.
- Sets key properties of the script such as its name, where it will appear when it is added to a chart, the precision and format of the values it displays, and certain values that govern its runtime behavior, such as the maximum number of drawing objects it will display on the chart. With strategies, the properties include parameters that control backtesting, such as initial capital, commission, slippage, etc.
Each script type has distinct basic requirements. Scripts that do not meet these criteria cause a compilation error:
- Indicators must call at least one function that creates a script output, such as plot(), plotshape(), barcolor(), line.new(), log.info(), alert(), etc.
- Strategies must call at least one order placement command or other output function.
- Libraries must export at least one user-defined function, method, type, or enum.
Code
Lines in a script that are not comments or compiler annotations are statements, which implement the script’s algorithm. A statement can be one of these:
- variable declaration
- variable reassignment
- function definition
- built-in function call, user-defined function call or a library function call
- if, for, while, switch, type, or enum structure.
Statements can be arranged in multiple ways:
- Some statements can be expressed in one line, like most variable declarations, lines containing only a function call or single-line function declarations. Lines can also be wrapped (continued on multiple lines). Multiple one-line statements can be concatenated on a single line by using the comma as a separator.
- Others statements such as structures or multiline function definitions always require multiple lines because they require a local block. A local block must be indented by a tab or four spaces. Each local block defines a distinct local scope.
- Statements in the global scope of the script (i.e., which are not part of local blocks) cannot begin with white space (a space or a tab). Their first character must also be the line’s first character. Lines beginning in a line’s first position become by definition part of the script’s global scope.
A simple valid Pine Script indicator can be generated in the Pine Script Editor by using the “Open” button and choosing “New blank indicator”:
This indicator includes three local blocks, one in the barIsUp() function
declaration, and two in the variable declaration using an
if
structure:
You can bring up a simple Pine Script strategy by selecting “New blank strategy” instead:
Comments
Double slashes (//) define comments in Pine Script. Comments can
begin anywhere on the line. They can also follow Pine Script code on
the same line:
The Pine Editor has a keyboard shortcut to comment/uncomment lines:
ctrl + /. You can use it on multiple lines by highlighting them
first.
Line wrapping
Scripts can use line wrapping to define a long single line of code across multiple lines. Generally, each wrapped line after the first can use any indentation length except multiples of four, because Pine uses four-space or tab indentations to define local code blocks.
For example, consider the following line of code:
We can distribute any part of this single line of code across two or more lines. Within a wrapped statement or expression, the subsequent lines can use different indentation lengths, and can also include comments without disrupting the code:
If parts of a wrapped expression are enclosed in parentheses ( ), such as function calls or parameter declarations, the wrapped lines within the parentheses do not have any restriction on their indentation lengths. Therefore, those wrapped lines can use any indentation length including multiples of four.
For example, this script demonstrates various ways that expressions enclosed in parentheses can wrap across multiple lines:
Expressions inside local code blocks can also use line wrapping. A local block requires indenting each line that belongs to its scope by four spaces or a tab relative to the local block’s header. Therefore, we recommend indenting any wrapped lines inside local blocks by a larger indentation than that of the block’s scope for readability. For example:
Compiler annotations
Compiler annotations are comments that issue special instructions for a script:
//@version=specifies the PineScript version that the compiler will use. The number in this annotation should not be confused with the script’s version number, which updates on every saved change to the code.//@descriptionsets a custom description for scripts that use the library() declaration statement.//@function,//@paramand//@returnsadd custom descriptions for a user-defined function or method, its parameters, and its result when placed above the function declaration.//@typeadds a custom description for a user-defined type (UDT) when placed above the type declaration.//@enumadds a custom description for an enum types when placed above the enum declaration.//@fieldadds a custom description for the field of a user-defined type (UDT) or an enum types when placed above the type or enum declaration.//@variableadds a custom description for a variable when placed above its declaration.//@strategy_alert_messageprovides a default message for strategy scripts to pre-fill the “Message” field in the alert creation dialog.
The Pine Editor also features two specialized annotations, //#region and //#endregion, that create collapsible code regions.
Clicking the dropdown arrow next to a //#region line collapses all the code between that line and the nearest //#endregion annotation
below it.
This example draws a triangle using three interactively selected points on the chart. The script illustrates how one can use compiler and Editor annotations to document code and make it easier to navigate:
