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.
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:
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
//@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 Commentary
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, aquick_right
variable 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 trackprice
to draw the line.
On the Charts
Loading up the indicator, you should see something that looks a little like this:- We highlight an area of potentially increased resistance as two levels are close together.
quick_right
has 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 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 theleft
,right
andquick_right
variables 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.
Brave
Backtest Rookies is a registered with Brave publisher!
Brave users can drop us a tip.
Alternatively, support us by switching to Brave using this referral link and we will receive some BAT!
Tradingview
Referral Link
Enjoying the content and thinking of subscribing to Tradingview? Support this site by clicking the referral link before you sign up!
PayPal
BTC
3HxNVyh5729ieTfPnTybrtK7J7QxJD9gjD
ETH
0x9a2f88198224d59e5749bacfc23d79507da3d431
LTC
M8iUBnb8KamHR18gpgi2sSG7jopddFJi8S
Nice….i was looking for something like this………..how to make it for 5 or 7 swings ?
Hey Amit,
You just need to copy and paste the levels. Levels 7 and 8 are for the 3rd swings. So do a 9th and 10th level for a fourth swing and so on.
Cheers
how do i install this on tradingview,plis help
Just copy and paste the code into the pine script editor. If you don’t know how to do that, see this article:
https://backtest-rookies.com/2017/06/07/trading-view-first-script/
How do you create swings from a different timeframe? Like have Daily and Weekly swings on a 4HR chart?
hi,
Very nice concept
is it possible to backtest it as a strategy ?
Hi ,
Thanks for the pine script.
Can you please upgrade this script if we can use this for back testing
Regards,
Gunasindhu