Tradingview: Swing Indicator

Swings in price action can provide logical entry points for both trend reversal and trend following strategies. They signal that price has reversed from a significant movement or retracement and that price might continue to move in the opposite direction. If you are looking to enter at the end of a retracement or trend, a swing can give you a little confidence that you are trading in the right direction.

It is not all rosy though, detecting a swing is only possible well after the fact. Furthermore, the lag in swing detection is far greater than most lagging indicators. The more confidence you want in confirming a swing, the more the detection of it will lag price action and therefore, the later you are able to enter a trade.

In this post, we are going to build a swing indicator for Tradingview, take a look at situations where it can be utilized well and then just as importantly, look at some examples of when it might get you into trouble.

The Swing Indicator

The indicator shall allow users to “analyse” a recent historical candle to detect whether it was a swing point. This will work by inputting a number to select which historical candle you want to check. The indicator will then check all candles after it until the current candle. It shall also check the same amount of candles before it. If the candle analysed has the lowest low or highest high in the complete range, then we know it was a swing point.

Swing Indicator Code

Code Commentary

All the swing detection magic is performed in one large swing_dectection() function. This function simply loops through the full range of candles both before and after the analysed candle. If at any point, another candle has a higher high or lower low, the loop is broken and falseis returned. Conversely, if the loop makes it to the end without a break, we have a swing and true is returned.

For more information on how to write loops and functions see these two tutorials:

  1. Tradingview: How to create a For Loop
  2. Tradingview: Creating Functions

Next, when plotting the swing, we need to use the offsetparameter. This is because the script evaluates trueon the current candle but we are actually analysing a historical candle. As such, we need to offset the plot by the barsbackinput value so the swing is marked correctly.

Finally, we optionally plot a marker on the current bar. The reason for this is that the current bar is the first bar when the signal is confirmed and as such, we could not act on the swing before it. This marker can be used for signalling entries and alerts.

About that lag

In the opening section, we mentioned the lag. The further we look back, the greater the lag. This can be a blessing as well as a curse depending on your goals. On one hand, the more lag we allow, the more confidence we have that the swing was real. Then, on the other hand, we can have so much lag that the move as already completed by the time we are ready to act. Let’s take a look.

First up we have a chart showing the indicator using default settings.

Swing Indicator - Default Settings

At first glance, it looks great. It nicely identifies the each of the swing highs and lows in the range. However, if you turn the signal marker on, it looks like this:

Swing Indicator with Signals

As you can see, the move is often nearly over before we know about it. Therefore we know the default settings are not suitable for ranging markets with small swings.

If we then reduce the lag (by reducing the number of bars back), we can enter earlier but the problem here is we start generating lots of conflicting signals:

Less lag, more signals

Again this is not ideal but that is the price we must pay to be early! In this scenario, it is better not to act on every swing. Instead, focus on swings that are close to known support/resistance levels or confirming another indicators’ bias.

Trending Markets

If we are running a long-only strategy and trying to “buy the dip”, we can turn off plotting of the short swings and reduce the barsback to find some nice long entries on a trending stock.

Short side turned off. Trending stock

Technical Analysis

In the final example, the indicator is used to assist with other technical analysis. Here we are easily able to spot divergence between an RSI and the price action.

Swing Indicator - Divergence

Improvement and Extension Ideas

One extension that immediately springs to mind would be to add a second input that allows you to specify how many candles to check prior to the analysed candle. This would allow a lopsided detection where we check fewer candles after the swing than before it. The hypothesis is that this would allow us to still enter early whilst maintaining some confidence that a swing is forming.

To finish off, it is worth pointing out that swings can also help you identify support/resistance levels and form the basis for trend line detection. Therefore, even if you see no value in using the code in its current form, it could still form an important building block for other more complex indicators.