To Pine Script™ version 4
This is a guide to converting Pine Script™ code from @version=3
to
@version=4
.
Converter
The Pine Editor comes with a utility to automatically convert v3
indicators and strategies to v4. To access it, open a script with
//@version=3
in it and select the Convert to v4
option in the More
dropdown menu:
Not all scripts can be automatically converted from v3 to v4. If you want to convert the script manually or if your indicator returns a compilation error after conversion, consult the guide below for more information.
Renaming of built-in constants, variables, and functions
In Pine Script™ v4 the following built-in constants, variables, and functions were renamed:
- Color constants (e.g
red
) are moved to thecolor.*
namespace (e.g.color.red
). - The
color
function has been renamed tocolor.new
. - Constants for
input()
types (e.g.integer
) are moved to theinput.*
namespace (e.g.input.integer
). - The plot style constants (e.g.
histogram
style) are moved to theplot.style_*
namespace (e.g.plot.style_histogram
). - Style constants for the
hline
function (e.g. thedotted
style) are moved to thehline.style_*
namespace (e.g.hline.style_dotted
). - Constants of days of the week (e.g.
sunday
) are moved to thedayofweek.*
namespace (e.g.dayofweek.sunday
). - The variables of the current chart timeframe (e.g.
period
,isintraday
) are moved to thetimeframe.*
namespace (e.g.timeframe.period
,timeframe.isintraday
). - The
interval
variable was renamed totimeframe.multiplier
. - The
ticker
andtickerid
variables are renamed tosyminfo.ticker
andsyminfo.tickerid
respectively. - The
n
variable that contains the bar index value has been renamed tobar_index
.
The reason behind renaming all of the above was to structure the standard language tools and make working with code easier. New names are grouped according to assignments under common prefixes. For example, you will see a list with all available color constants if you type ‘color’ in the editor and press Ctrl + Space.
Explicit variable type declaration
In Pine Script™ v4 it’s no longer possible to create variables with an
unknown data type at the time of their declaration. This was done to
avoid a number of issues that arise when the variable type changes after
its initialization with the na value. From now on, you need to
explicitly specify their type using keywords or type functions (for
example, float
) when declaring variables with the na value: