In version 4 of pine script, Tradingview added support for drawing lines and objects on the chart. Hooray! This small addition makes a huge difference to the visual quality of custom scripts. To be fair, version 3 was not bereft of eye-candy and many people managed to use the tools available to craft visually impressive charts. But with version 4, new features such a being able to print dynamic prices on charts or draw trend lines into the future will mean we are able to create better charts than ever before. So let’s take a look at one of the new tools in our toolbox.
As always we will keep things as simple as possible. We will start by simply using labels to print prices on the chart. This is a feature that was not available before and will really help traders highlight key levels of interest. The example code will use the
label.new()function to flag pivots (swing highs/lows) and print the level of the swing, enabling traders to easily monitor these levels or support/resistance.
study("Pivot Prices", overlay=true)
leftbars = input(10, minval=1, title='Bars to the left')
rightbars = input(2, minval=1, title='Bars to the right')
phigh = pivothigh(high, leftbars,rightbars)
plow = pivotlow(low, leftbars, rightbars)
label1 = label.new(bar_index[rightbars], high[rightbars], text=tostring(high[rightbars]), style=label.style_labeldown, color=color.orange)
label2 = label.new(bar_index[rightbars], low[rightbars], text=tostring(low[rightbars]), style=label.style_labelup, color=color.green)
Before we talk about labels, let’s spend a paragraph discussing the swing detection as, without it, we wouldn’t have a script to work with. Fortunately, detecting swings is really easy thanks to Tradingview’s built-in
pivotlowfunctions. We just need to tell those functions how many candles before and after a potential swing to analyze. We do this using the
rightbarsvariables. To understand how these work, imagine we are looking at the last 7 prices where
leftbars = 5 and
rightbars = 2. Then this means that the code analyze the candle 2 bars back (
rightbars) to see if it is the swing/pivot point. If it is, it must be the highest or lowest value between the current candle and then. Next, we take look at the 5 bars before the candle we are analyzing (
leftbars). Again we check the candle we are analyzing is the highest or lowest value compared to all 5 candles before it. If it is, we have a pivot.
Given that we are always looking for a swing on a candle
xbars behind us (because we use
rightbars), that means we cannot detect a swing until after it has happened. In other words, the indicator will lag by however many bars you set
The new bits
Next, let’s dive into the new bits.
Tradingview has a whole host of new plotting options and at first glance, they can seem a little scary. Seeing a plethora of functions such as
label.delete() along with similar sounding arguments such as
ylocmight make you worry there is a lot to learn. However, plotting a simple label is easier than you might think. It is for this reason, we have attempted to keep the example code in this article as minimal as possible.
label.new() as you might guess, creates a new label. It will do this every time you call it and since scripts are re-run on every bar, you can end up plotting a new label on every bar until you reach a label limit. It is for this reason that we place the
label.new()call behind an
if statement in the example code. To see an example of adding a new label on every bar, you can run the following code snippet below:
study("My Script", overlay=true)
label.new(bar_index, high, text='A', style=label.style_triangledown)
The new label function has several arguments that we can specify. Let’s take a look at the main arguments we have used.
x: This is simply the coordinate for where the label should appear along the x-axis. The difference between this and the
xlocargument (which we don’t use in this example) is that
xexpects you to provide it a
xlocexpects a string, which is a single value. To understand more about
seriesvariables and indexing, see this article.
y: Is similar to above but places the label on the y-axis. In our example, we provide two series variables (
low) so we can place our labels at the swing high and swing low points
text: Is the text you want to display. It needs to be a string but fortunately, it does not need to be a constant string like the
plotshape()in version 3. A constant string is a string that never changes. Price values change each bar and so we could never plot a value from a price series in version 3. By contrast, in version 4, we are able to use the
to_string()function and convert our
highprices to a string.
Finally, as you might notice, on the
y arguments, we index our series with our
rightbars value. This ensures that the labels point nicely to the swing. However, as mentioned earlier, they will only appear a couple of bars after the swing has happened!
On the Charts
Finally, before we wrap up, let’s just take a quick look at how the indicator should appear when placed on the charts.
Support the site
If you like this article and are thinking to become a Tradingview subscriber, you can support the site by clicking on the affiliate link below before signing up!