Pine Script – Lesson 2: Plotting Data On The Chart

Pine Script Basics Course
Pine Script Mastery Course

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.


Video Lesson

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:

Pine Script Lesson 2 Example 1

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:

plot(highestHigh)


And then save your script. The chart will refresh itself, and look like this:

Pine Script Lesson 2 Example 2

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.

Pine Script Lesson 2 Example 3

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:

Pine Script Lesson 2 Example

All from six lines of code!

Advanced Course

If you want to take your Pine Script coding to the next level, then I think you’ll be interested in my Pine Script Mastery Course.

If you liked this free content then I promise that you’ll love my premium content where I am able to go into much greater detail and help answer students’ questions!



Source Code

//@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)

OVERVIEW


Free Premium Charts!

4.3 3 votes
Article Rating
Subscribe
Notify of
guest
17 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Anonymous
Anonymous
1 year ago

Instead of a line how can I plot the price on a specific candle ? (version 3)

CX
CX
9 months ago

Can we have multiple indicators in one chart ,say, RSI and Stoch. But i wanted the RSI and Stoch to be in seperate boxes.Is that possible ?
Thanks

Jim Fredrickson
8 months ago

any thoughts on how to get the close (last price) on the chart, to draw a horizontal line offset from the current price? every time i try it i get a result that gets the close from every bar. i just want the price off the current bar only?? like bar_index[0]??? imagine a horizontal bar that moves up or down as price moves.
but offset by a calculated amount… thanks

Jim Fredrickson
8 months ago

So cool :) I actually think across the whole chart works well. I truly appreciate your feedback. Thank you.

Jim Fredrickson
8 months ago

I don’t mean to impose. But if you have time and appreciate a challenge, here is the script I am trying to get work. (I am a long-time trader but noob scripter). ‘If you don’t have time to look at this, no worries… : //@version=4 study(“144 Lines”, shorttitle=”144 Lines”, overlay=true) int p1=na int p2=na int s1=na int s2=na int lprice=na base = input(title=”Base Support Price”, defval=10300) goUp = input(title=”Go up?”, type=input.bool, defval=false) //I want to plot horizontal lines on either side of current price, offset by multiples of 144 from a pivot hi/lo defined here as ‘base’ // For example… Read more »

Gaz
Gaz
8 months ago

Great post thank you 🙏

Would it be possible to plot say the last bearish candle in a bullish move??

Jim Fredrickson
8 months ago

with your idea of using offset = -9999 I was able to get my script working – thanks!

Dhav
Dhav
5 months ago

Thanks for the post! How can I plot the high and low for a given time period? For example, the high and low between 00:00 and 08:00.

Thanks

Someone Annoyed
Someone Annoyed
30 days ago

Fuck… I have to scroll all the way back up…

Lapog K
Lapog K
11 days ago

Hi,

Your teaching method is really good and your perspective from eyes of the learner is phenomenal. As a beginner trader, I am learning both. I will take the mastery course some time in the future when affordable. Thank you and Best wishes.