Pine Script – Lesson 2: Plotting Data On The Chart

Pine Script Basics 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
23 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Anonymous
Anonymous
2 years ago

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

CX
CX
1 year 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
1 year 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
1 year ago

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

Jim Fredrickson
1 year 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
1 year ago

Great post thank you 🙏

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

Jim Fredrickson
1 year ago

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

Dhav
Dhav
1 year 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
1 year ago

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

Lapog K
Lapog K
1 year 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.

calum
calum
1 year ago

how could i find the highest high a set number of bars back.. and keep it as constant until it is breached by the close price? I would then want the highest high to move up with the close to find the new resistance (or reversal level) and then stay constant at that level until it is broken again? I don’t want the highest high to move like the donchain channel, I just want it to stay “constant” until it is breached. Similarly, i would do the same for the bottom band of support or reversal too. BUT, I want… Read more »

Antony
Antony
9 months ago

Hi i just need a small script for last day high low close open as a horizontal line can you help me with it in V4?

slowbutsure
slowbutsure
9 months ago

HI, im trying this out. How can I just set it to trade long only in the backtest?

Thanks.

Akshay
Akshay
6 months ago

Hey I was trying to plot, the Days High till one candle before the current candle Eg
if the current is number 5 then I want to plot the high till 4 the candle
just to check if the current close is higher than the previous 4 candles

I used a bar counter my custom function but I am not able to use that inside the highest function

simIlarly,2) I want to check if any closing is below close of 1 st candle
could you help me I am new to pine script
I am waiting for your response

Minh Tri
Minh Tri
4 months ago

Please help Me! How to draw a vertical line at candle number 20, candle number 50, candle number 100 in the script through TradingView?

Toddfx
Toddfx
1 month ago

Hi, is it able to plot highest price of a certain timeframe and view it on lower timeframe? For instance I wish to use 4h timeframe for trading and I’d like to view the highest price of last 5 days on it and I hope that switching to any other timeframe like 30min will still show the highest price of last 5 days.