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!
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!
//@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)
Instead of a line how can I plot the price on a specific candle ? (version 3)
Hi! You can try this:
This will plot the closing price of the candle next to the indicator title when you hover your mouse over a candle, but will not draw any visuals on your chart. Is that what you meant?
Otherwise, if you want to draw prices visually onto your chart then you will have to change your script to the new @version=4 and use the new label drawing feature:
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 ?
Unfortunately this is not possible to do within a single script. You would have to place an RSI script on your chart and then add a Stoch script. Once you add them both, you can move the windows around to how you like them by clicking on the . . . symbol at the end of the indicator’s title and choosing the “Move To” option.
Let me know if you need help working out how to do this and I’ll send you a video or a gif :) good luck!
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??? imagine a horizontal bar that moves up or down as price moves.
but offset by a calculated amount… thanks
Hey Jim! Thanks for the great question, that’s something I’ve never investigated before. I found a solution that works, but it draws the horizontal line across the entire chart. I’ll play around with it when I get some time and see if I can get it to work exactly how you suggested, but in the meantime this should do the trick:
study(“Previous Close”, overlay=true)
plot(close, trackprice=true, offset=-9999)
So cool :) I actually think across the whole chart works well. I truly appreciate your feedback. Thank you.
You’re welcome Jim! Also don’t forget that with a little creativity you can do all kinds of cool things with that line of code, such as draw the previous Day’s close across your chart which could definitely be helpful for intraday trading:
study(“Previous Close”, overlay=true)
prevCloseHTF = security(syminfo.tickerid, “D”, close, lookahead=true)
plot(prevCloseHTF, trackprice=true, offset=-9999)
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 »
Great post thank you 🙏
Would it be possible to plot say the last bearish candle in a bullish move??
Hey Gaz! The easiest way I can think of to achieve that is to do this:
study(“Detect Last Reversal”)
drawBearish = input(title=”Draw Bearish Signals?”, type=input.bool, defval=true)
drawBullish = input(title=”Draw Bullish Signals?”, type=input.bool, defval=false)
plotshape(drawBearish and close < open and close > open, title=”Bearish Candle”, location=location.abovebar, color=color.red, transp=0, style=shape.xcross, text=””)
plotshape(drawBullish and close > open and close < open, title="Bearish Candle", location=location.abovebar, color=color.green, transp=0, style=shape.xcross, text="") plot(close)
with your idea of using offset = -9999 I was able to get my script working – thanks!
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.
I plan to cover that problem in a future lesson I’m working on now, but in the meantime here’s a script I made that does exactly that: https://pastebin.com/d5ngv6XV
Feel free to subscribe to the Pine Script Email for announcements of new lessons: https://mailchi.mp/f29afd740719/zenandtheartoftrading
Fuck… I have to scroll all the way back up…
If that’s too much for you then you’re probably not gonna make it as a trader or a coder lol. Try using the links on the right sidebar next time
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.
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 »
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?
HI, im trying this out. How can I just set it to trade long only in the backtest?
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
Please help Me! How to draw a vertical line at candle number 20, candle number 50, candle number 100 in the script through TradingView?
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.