This post contains a simple automated support and resistance indicator for use on Tradingview. Identifying support and resistance zones is an important and popular technical analysis technique. Using historical data, humans can easily identify price zones where a lot of buyers (or sellers) came into the market. We can then use this information as a guide to indicate where demand might increase again in the future.
How it worksThe indicator attempts to automate detection of support and resistance levels by identifying large swings/pivots in historical price action. These tops and bottoms in price action show where lots of buyers or sellers came into the market and might act as future levels of support or resistance. By default, the code detects the last 3 significant swing highs and the last 3 swing lows. It then places lines on the chart to highlight those levels. In support and resistance theory we often hear that support becomes resistance and vice versa. Because of this, the indicator does not simply categorize swings lows as “support” and swing highs as “resistance”. Instead, we look at the swing position relative to the current close price. If we are above the level, we consider it support. If we are below the level, we consider it to be resistance. With this in mind, the lines are automatically colored according to whether they are above or below the current close price. When any of the levels are below the close price, the lines are colored green. Conversely, whenever they are above the close, they are colored red.
Support and Resistance Indicator Code
//@version=3 study("Support & Resistance", overlay=true) left = 50 right = 25 quick_right = 5 // Used to try and detect a more recent significant swing. pivot_high = pivothigh(high,left,right) pivot_lows = pivotlow(low, left,right) quick_pivot_high = pivothigh(high,left,quick_right) quick_pivot_lows = pivotlow(low, left,quick_right) level1 = valuewhen(quick_pivot_high, high[quick_right], 0) level2 = valuewhen(quick_pivot_lows, low[quick_right], 0) level3 = valuewhen(pivot_high, high[right], 0) level4 = valuewhen(pivot_lows, low[right], 0) level5 = valuewhen(pivot_high, high[right], 1) level6 = valuewhen(pivot_lows, low[right], 1) level7 = valuewhen(pivot_high, high[right], 2) level8 = valuewhen(pivot_lows, low[right], 2) level1_col = close >= level1 ? green : red level2_col = close >= level2 ? green : red level3_col = close >= level3 ? green : red level4_col = close >= level4 ? green : red level5_col = close >= level5 ? green : red level6_col = close >= level6 ? green : red level7_col = close >= level7 ? green : red level8_col = close >= level8 ? green : red plot(level1, style=circles, color=level1_col, show_last=1, linewidth=1, trackprice=true) plot(level2, style=circles, color=level2_col, show_last=1, linewidth=1, trackprice=true) plot(level3, style=circles, color=level3_col, show_last=1, linewidth=1, trackprice=true) plot(level4, style=circles, color=level4_col, show_last=1, linewidth=1, trackprice=true) plot(level5, style=circles, color=level5_col, show_last=1, linewidth=1, trackprice=true) plot(level6, style=circles, color=level6_col, show_last=1, linewidth=1, trackprice=true) plot(level7, style=circles, color=level7_col, show_last=1, linewidth=1, trackprice=true) plot(level8, style=circles, color=level8_col, show_last=1, linewidth=1, trackprice=true)
Code CommentarySince significant levels are detected using pivots with large numbers of left and right bars, this can cause quite a delay in receiving a recent significant swing. As such, a
quick_rightvariable is used to detect the first and second levels. This will help to reduce the lag and highlight a possible support level after only 5 bars as opposed to the usual 25. Of course, this speed comes at a cost of accuracy. Line coloring is set using a simple ternary conditional operator to detect whether the current close price is above/below the line.. Finally, a plotting trick is used so that we don’t end up with messy lines on the chart as new swings are detected. We simply plot the last value on the chart only and then use
trackpriceto draw the line.
On the ChartsLoading up the indicator, you should see something that looks a little like this: Note The annotations have been written on the chart after adding the support and resistance indicator! In the image above we can see a couple of interesting things going on:
- We highlight an area of potentially increased resistance as two levels are close together.
quick_righthas identified a recent swing low which looks like it could form the basis for future support.
- We successfully capture the highest high and lowest low of the trading period on the chart.
Expanding the codeOnce you are comfortable with the code base and how the indicator operates, you may wish to expand upon it. One obvious addition to make would be inputs for the
quick_rightvariables so that you can tune the indicator according to your own taste. Other users may wish to add more lines or perhaps swings from other time-frames. How far you take this, is up to you. I just hope the base code provides a good enough example for readers to build upon!
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.