Code Snippet: Tradingview 52 Week High / Low Indicator

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.

Search results for a 52 Week high/low indicator

 

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/

On the Chart

When the code is applied to a chart, this is what you can expect to see:

52 Week High/Low indicator on the chart

Commentary

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_closeand referenceweekly_close[52] 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/

If you plot the two together on a lower time frame and the difference will become more clear:

Actual 52 week high (orange) versus the weekly high from 52 periods (bars) ago. (blue)

 

The solution to this is problem is to move the highest()and 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.

Line Offset

Usingtrackprice=trueandoffset=-9999inside the 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!