Pine Script – Lesson 2: Plotting Data On The Chart
Plotting the Highest High/Lowest Low to the Chart
In this lesson we’ll get our hands dirty writing some code. I’ll start out simple and show you how to plot the highest high and lowest low on your chart for the past 50 candles.
If you prefer to learn in a visual/audio manner, then here’s a video version of this lesson:
Drawing To The Chart
First, start a new script and name it whatever you want. I am calling mine “Lesson 2”. Once you have named it, change the second line of code to say this:
study("Lesson 2", overlay=true)
Click ‘Add to Chart’, save your script, and then it should look like this:
Notice that the blue line is no longer drawn in its own indicator box, but is now drawn directly onto the price chart.
This is because we set the script parameter “overlay” to “true”. When working with computer code, we must tell the computer what type of data we want it to work with. There are several different datatypes, but I won’t go into detail about them here.
The basic datatypes are numbers such as integers (whole numbers) and floats (decimal numbers), text (called “Strings” in programming), Booleans (True/False or Yes/No) and objects (eg. other scripts or indicators – which contain datatypes).
If you are brand new to programming then I bet this is all very confusing, but don’t worry. It will make sense once we begin using them.
The overlay setting is useful because some indicators interact with visual price action (candles etc.), while others work on pure price data (eg. oscillators like the RSI or MACD).
You typically want oscillator indicators like the RSI to draw in their own box, while, for example, a moving average indicator may need to be drawn directly over the top of price action.
By default the “overlay” setting is set to false and you must overwrite it on every script you make if you wish to draw directly to the chart.
Getting the Highest High and Lowest Low
Ok. Now that we have made the script draw to the screen, let’s paint our highs and lows. First we will obtain the highest high of the last 50 candles using this line of code:
highestHigh = highest(high, 50)
This line of code is telling Pine Script “Create me a variable named ‘highestHigh’. Then use the built-in function ‘highest()’ to search through the past 50 candles to find the highest candle high and assign that value to my variable.”
Now we can do whatever we like with this variable. We can compare it to other variables, we can check if the next candle closes above it (for breakout alerts), or we can draw it to our screen to create a price channel indicator.
We are going to do the last option for this lesson.
Once you add this line of code to your script, change the plot(close) code to this:
And then save your script. The chart will refresh itself, and look like this:
Now, to finish the script, let’s also draw the lowest low. Add these two lines to your script:
lowestLow = lowest(low, 50) plot(lowestLow)
And we’re done! Voila. You now have a 50-period horizontal price channel indicator.
But wait! There’s more.
Maybe you also want to change the color of the line, or the thickness? Then simply add these parameters to your plot() code:
plot(highestHigh, color=color.red, linewidth=2) plot(lowestLow, color=color.blue, linewidth=2)
This code should be pretty self-explanatory. You are telling Pine Script to plot the highs and lows with the given color setting, and the given linewidth setting.
The result should look like this:
All from six lines of code!
I’m currently working on a very structured, very advanced Pine Script course for those who want to really take their coding skills to the next level. If you’re interested in that opportunity, click here to sign up to be notified when the course is released.
Those who sign up to be notified before the course is released will receive a 50% discount off the opening price!
//@version=4 study("Lesson 2", overlay=true) highestHigh = highest(high, 50) lowestLow = lowest(low, 50) plot(highestHigh, color=color.red, linewidth=2) plot(lowestLow, color=color.blue, linewidth=2)