This is a guide to converting Pine Script® code from @version=3
to @version=4
.
The Pine Script® 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.
In Pine Script® v4 the following built-in constants, variables, and functions were renamed:
red
) are moved to the color.*
namespace (e.g. color.red
).color
function has been renamed to color.new
.input()
types (e.g. integer
) are moved to the input.*
namespace (e.g. input.integer
).histogram
style) are moved to the plot.style_*
namespace (e.g. plot.style_histogram
).hline
function (e.g. the dotted
style) are moved to the hline.style_*
namespace (e.g. hline.style_dotted
).sunday
) are moved to the dayofweek.*
namespace (e.g. dayofweek.sunday
).period
, isintraday
) are moved to the timeframe.*
namespace (e.g. timeframe.period
, timeframe.isintraday
).interval
variable was renamed to timeframe.multiplier
.ticker
and tickerid
variables are renamed to syminfo.ticker
and syminfo.tickerid
respectively.n
variable that contains the bar index value has been renamed to bar_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.
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:
//@version=4
study("Green Candle Close")
// We expect `src` to hold float values, so we declare in with the `float` keyword
float src = na
if close > open
src := close
plot(src)