This code snippet is a Tradingview Indicator intended for use with equities. It simply plots the current 52 week high and low values on the chart in an unobtrusive manner. At the time of writing, I was surprised to see that there were not already a plethora of such indicators in the public repository. In fact, there was only one other indicator that tracks both the 52-week high and low values.
As such, it seemed like a nice gap to fill in the indicator space. Afterall, there are a lot of people who closely watch the relationship of equities to their 52 week high and low values. My intention for this indicator was to create a simple, unobtrusive indicator that can be used on any timeframe to display the 52 week high/low values.
52 Week High/Low Code
A copy of the code can be found on the Tradingview website here: https://www.tradingview.com/script/kSeWecuE-52-Week-High-Low/
study("52 Week High/Low",shorttitle="52W",overlay=true)
high_low_close = input(defval="Highs/Lows", title="Base 52 week values on candle:", options=["Highs/Lows", "Close"] )
weekly_hh = security(tickerid,"W", highest(high,52), lookahead=barmerge.lookahead_on)
weekly_ll = security(tickerid,"W", lowest(low,52), lookahead=barmerge.lookahead_on)
weekly_hc = security(tickerid,"W", highest(close,52), lookahead=barmerge.lookahead_on)
weekly_lc = security(tickerid,"W", lowest(close,52), lookahead=barmerge.lookahead_on)
high_plot = high_low_close == "Highs/Lows" ? weekly_hh : weekly_hc
low_plot = high_low_close == "Highs/Lows" ? weekly_ll : weekly_lc
plot(high_plot, title='52 Week High', trackprice=true, color=orange, offset=-9999)
plot(low_plot, title='52 Week Low', trackprice=true, color=orange, offset=-9999)
On the Chart
When the code is applied to a chart, this is what you can expect to see:
As touched upon in the introduction, the code has the following features:
- Works on any timeframe.
- Unobtrusive: Simple horizontal lines showing only the current 52-week values.
- Allows the user to select whether to calculate the 52-week values from candle close values or the respective highs/lows.
And that is it. Overall it is fairly simple. Having said that, there are a few things that are worth digging deeper into.
Working on any timeframe
Since Pine-Script is a series based scripting language, we need to be careful when indexing values. For example, if you are viewing daily data, then import weekly close data as
weekly_close it will not reference the close 52 weeks ago. It will reference the weekly close from 52 days ago which is only about 7 weeks ago. This is a problem the other 52-week high/low indicator suffers from.
Code Credit: https://www.tradingview.com/u/Shizaru/
study("JD 52 weeks HighLow",shorttitle="52wHL_SH",overlay=true)
tf = input("W",type=resolution)
a = security(tickerid,tf,close)
len = input(52,title="Length")
h = highest(a,len)
l = lowest(a,len)
p1 = plot(h)
If you plot the two together on a lower time frame and the difference will become more clear:
The solution to this is problem is to move the
lowest()calls into the
symbol()call. This will then pull the correct 52-week data for each bar at the bars own moment in time.
plot()call allows us to draw a nice horizontal dotted line across the top of the screen. This is a matter of personal preference but I find that it is less cluttered than tracking all the historical levels.
That is it. I believe the rest of the code is quite straightforward. If not or you are new to pine script, there are a series of introductory posts that may provide additional guidance and help. Please see here: Getting Started. If there is anything specific you would like to see, feel free to get in touch!
Find This Post Useful?
If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny.