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 works
The 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
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)
Since 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 Charts
Loading 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.
Of course, this indicator is not perfect. No indicators are! If you scroll back in time, you will find significant areas of support and resistance that happened more than 3 pivots/swings ago. Additionally, it may flag some swings which turn out not to be all that significant in the end. Use your own judgment when utilizing this indicator as to whether or not to rely on the levels it produces!
Expanding the code
Once 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!