Creating a Swing Indicator in TradingView: A Comprehensive Guide
Written on
Chapter 1: Introduction to the Swing Indicator
Momentum in trading can be assessed in various ways, particularly when we identify signs of potential exhaustion. Techniques such as divergence analysis, recognizing overbought or oversold conditions, and utilizing timing indicators are effective for this purpose. This guide will walk you through the process of creating a swing indicator, which utilizes highs and lows to determine empirical mean-reversion levels, suitable for both discretionary and systematic trading.
I've recently published a new book following the success of my prior one, "The Book of Trading Strategies." This new volume includes advanced trend-following indicators and strategies, along with a dedicated GitHub page for ongoing code updates. The book features optimized colors for print. If this piques your interest, feel free to check it out on Amazon, or contact me on LinkedIn for a PDF version.
The Fundamentals of the Swing Indicator
The swing indicator is designed to identify when a trend begins to lose strength as it nears a specific threshold. This can be quantified using the Momentum Gauge, which will be explained below. The initial scoring system is as follows:
- Score 0: Current high is less than the previous high and current close is below current open.
- Score 1: Either the current high exceeds the previous high or the current close is above the current open.
- Score 2: Both the current high exceeds the previous high and the current close surpasses the current open.
- Score 0: Current low exceeds the previous low and current close is above current open.
- Score 1: Either the current low is below the previous low or the current close is below the current open.
- Score 2: Both the current low is below the previous low and the current close is below the current open.
Next, we will calculate the difference between the Upside Momentum Gauge and the Downside Momentum Gauge to derive the net momentum effect. Ultimately, we will sum the most recent values of the Raw Swing over a lookback period set to 8.
If you're interested in more insights, consider subscribing to my DAILY Newsletter (a free plan is available) via the link below. Subscribers will receive my Medium articles, additional trading strategies, coding tutorials related to research and analysis, and a complimentary PDF copy of my first book. Expect 5–7 articles per week with a paid subscription, and 1–2 articles per week for free plan subscribers. Your support allows me to continue sharing my research. Thank you!
Creating the Indicator in TradingView
To implement the swing indicator on GBPUSD values in TradingView, you'll first need to set up a free account to access the charts. Begin by clicking the Chart button on the homepage and select any asset on which you wish to compute the indicator. Then, find the Pine Editor at the bottom of the screen and prepare to code.
The initial step is to define the version of Pine Script, which in our case is version 5. Start with this line:
Copy//@version = 5
Next, declare the indicator name using the following syntax:
Copyindicator("Swing Indicator")
After setting up the basics, let's define the conditions for both upside and downside momentum gauges:
Copyupside_momentum_gauge = 0
if high > high[1] and close > open
upside_momentum_gauge := 2
if high > high[1] and close < open
upside_momentum_gauge := 1
if high < high[1] and close > open
upside_momentum_gauge := 1
if high < high[1] and close < open
upside_momentum_gauge := 0
downside_momentum_gauge = 0
if low > low[1] and close > open
downside_momentum_gauge := 0
if low > low[1] and close < open
downside_momentum_gauge := 1
if low < low[1] and close > open
downside_momentum_gauge := 1
if low < low[1] and close < open
downside_momentum_gauge := 2
Now we can calculate the raw swing, which is the difference between the upside and downside momentum gauges:
Copyraw_swing = upside_momentum_gauge - downside_momentum_gauge
Finally, to compute the swing indicator, sum the last eight raw swing calculations:
Copyswing_indicator = raw_swing + raw_swing[1] + raw_swing[2] + raw_swing[3] + raw_swing[4] + raw_swing[5] + raw_swing[6] + raw_swing[7]
plot(swing_indicator)
hline(10)
hline(-10)
This setup will display GBPUSD in the first panel alongside the swing indicator in a second panel. The default levels for the 8-period Swing Indicator are set at 10 for bearish signals and -10 for bullish signals. The trading conditions are as follows:
- Go long (Buy) when the Swing Indicator reaches or dips below -10, provided the previous four readings are above -10.
- Go short (Sell) when the Swing Indicator reaches or exceeds 10, provided the previous four readings are below 10.
// © Sofien-Kaabar
indicator("Swing Indicator")
upside_momentum_gauge = 0
if high > high[1] and close > open
upside_momentum_gauge := 2
if high > high[1] and close < open
upside_momentum_gauge := 1
if high < high[1] and close > open
upside_momentum_gauge := 1
if high < high[1] and close < open
upside_momentum_gauge := 0
downside_momentum_gauge = 0
if low > low[1] and close > open
downside_momentum_gauge := 0
if low > low[1] and close < open
downside_momentum_gauge := 1
if low < low[1] and close > open
downside_momentum_gauge := 1
if low < low[1] and close < open
downside_momentum_gauge := 2
raw_swing = upside_momentum_gauge - downside_momentum_gauge
swing_indicator = raw_swing + raw_swing[1] + raw_swing[2] + raw_swing[3] + raw_swing[4] + raw_swing[5] + raw_swing[6] + raw_swing[7]
plot(swing_indicator)
hline(10)
hline(-10)
Depending on the asset being analyzed, the indicator may yield either frequent or infrequent contrarian signals. It’s essential to understand that this indicator primarily provides consolidation or correction signals rather than extreme reversals. Notably, the indicator has a correlation of 0.60–0.70 with the same-period Relative Strength Index (RSI), indicating limited diversification of signals, yet it still adds value.
If you're interested in exploring more technical indicators and strategies, my book may be of interest to you.
Chapter 2: Conclusion
Always conduct your back-tests. It’s vital to maintain skepticism and remember that while my indicators and trading style may work for me, they might not suit everyone. I firmly believe in self-directed learning; I have grown by experimenting rather than merely imitating. It's important to grasp the concept, function, intuition, and conditions of a strategy and then develop an improved version that you can back-test and refine before implementing it.
Medium serves as a platform for many intriguing reads; I consumed numerous articles before I began writing. Consider joining Medium through my referral link!
To summarize, are the strategies I provide realistic? Yes, but they require optimizing the environment (a robust algorithm, low costs, a trustworthy broker, effective risk management, and order management). These strategies are not solely for trading; they also aim to inspire brainstorming and generate new trading ideas, moving beyond outdated methods like relying on an oversold RSI to go short or a surpassed resistance level to go long.
One Final Note
I have also launched an NFT collection aimed at supporting various humanitarian and medical causes. The Society of Light features limited collectibles, with a portion of each sale donated to the associated charity. Here are some key benefits of purchasing these NFTs:
- High-potential gain: By directing remaining sales proceeds towards marketing and promoting The Society of Light, I aim to maximize their secondary market value.
- Art collection and portfolio diversification: Owning a collection of avatars representing good deeds is fulfilling. Investing can transcend selfish motives, allowing for profit while supporting others and collecting art.
- Donating to your preferred causes: This offers flexibility in allocating funds to various charities.
- Free copy of my book in PDF: Every NFT buyer will receive a complimentary PDF copy of my latest book, as mentioned in the article link.
Click here to purchase an NFT and support the fight against discrimination.