What’s new in the new v4 version of Pine Script?

Just recently TradingView announced new v4 version of Pinescript.

From features, they announced one of the most interesting is line.new function. Now you can plot a line, save it as an object and then later interact with it. Here is an example of usage:

//@version=4
study("High/Low Levels", overlay=true)

length = input(20)

h = highest(high, length)
l = lowest(low,   length)

lh = line.new(bar_index[length], h, bar_index, h, width = 2, color = color.red, extend = extend.right)
line.delete(lh[1])
          
ll = line.new(bar_index[length], l, bar_index, l, width = 2, color = color.green, extend = extend.right)
line.delete(ll[1])

Another interesting function is label.new. It has the same functionality with object as line.new. Also now you can use custom labels created on fly in scripts. Here is a script where for pivot points I plot labels with price level of pivot point:

//@version=4
study("Pivot Reversal Strategy", overlay=true)

leftBars = input(4)
rightBars = input(2)

swh = pivothigh(leftBars, rightBars)
swl = pivotlow(leftBars, rightBars)

swh_cond = not na(swh)
swl_cond = not na(swl)

if (swh_cond)
    lab_h = label.new(bar_index - rightBars, na, 'PH ('+tostring(high[rightBars]) + ')', 
          color=color.green, 
          textcolor=color.white,
          style=label.style_labeldown, yloc=yloc.abovebar)
          
if (swl_cond)
    lab_l = label.new(bar_index - rightBars, na, 'PL ('+tostring(low[rightBars]) + ')', 
          color=color.red, 
          textcolor=color.white,
          style=label.style_labelup, yloc=yloc.belowbar)

All the other minor changes you can find in this script, majority of these changes are renaming of some prebuilt variables:

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

/////////////////
// VAR Keyword //

// v3
s = 0.0
s := nz(s[1]) + 1

// v4
var s = 0.0
s := s + 1

////////////////////////////////////////
// Explicit Variable Type Declaration //

a = na // v3
float a = na // v4

// Colors 
red     -> color.red
color() -> color.new()

// Days of the week
sunday -> dayofweek.sunday
 
// input type
i = input(title="Offset", type=integer) // v3 
i = input(title="Offset", type=input.integer) // v4
 
// plot style 
plot(close, style =  histogram) // v3 
plot(close, style =  plot.style_histogram) // v4

// hline style
hline(1000, linestyle = dotted) // v3
hline(1000, linestyle = hline.style_dotted) // v4

// Other renamed constants
ticker     -> syminfo.ticker
tickerid   -> syminfo.tickerid
n          -> bar_index
period     -> timeframe.period
interval   -> timeframe.multiplier
isintraday -> timeframe.isintraday

Here is the video where I show all these changes in action:

Leave a Reply

Your email address will not be published. Required fields are marked *