How to plot the Highest High and Lowest Low in the TradingView Chart using PineScript?
In this lesson we’ll learn how to plot the highest high and lowest low on your chart for the past 50/100 candles.
Drawing To The Chart
First, start a new script and name it whatever you want. I am calling mine “Episode 2”. Note, this is the same script we used in the previous episode as well.
Now change the second line of code by adding overlay.
study(“Lesson 2”, overlay=true)
Now Click ‘Add to Chart’ to add this to the chart.
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”.
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 100 candles using this line of code:
highestHigh = highest(high, 100)
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 100 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. So 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:
Now, to finish the script, let’s also draw the lowest low. Add these two lines to your script:
lowestLow = lowest(low, 100)
plot(lowestLow)
And we’re done! You now have a 100-period horizontal price channel indicator.
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.
Check Out Script Library to get script used for this episode:
https://marketsecrets.in/script-library/
To know more, checkout:
Related
amibroker tutorial videos how to backtest a trading strategy? how to do algo trading in india? how to do algo trading in Zerodha? how to start with algo trading in india? how to use amibroker afl for algo trading? how to use pine script for algo trading? how to write algo trading scripts? Pine script tutorial videos step-by-step approach to start with algo-trading in India