Creating a New Technical Indicator From Scratch in TradingView.
Presenting & Coding a Technical Indicator Using Pine Script.
The Stochastic Oscillator is one of the most common indicators in Technical Analysis. It allows us to have a quick glance as to whether the market is overbought or oversold. In this article we will code a variation of the oscillator called the Stochastic Smoothing Oscillator in TradingView coding language.
I have just published a new book after the success of my previous one “New Technical Indicators in Python”. It features a more complete description and addition of structured trading strategies with a GitHub page dedicated to the continuously updated code. If you feel that this interests you, feel free to visit the below link, or if you prefer to buy the PDF version, you could contact me on LinkedIn.
The Stochastic Smoothing Oscillator
A familiar oscillator in the world of technical analysis goes by the name of Stochastic. There is another one similar to it called the Stochastic Smoothing Oscillator based on the concept of more smoothing. First, let us take a quick look at the Stochastic Oscillator.
The normalization technique allows us to trap the values between 0 and 1 (or 0 and 100 if we wish to multiply by 100). The concept revolves around subtracting the minimum value in a certain lookback period from the current value and dividing by the maximum value in the same lookback period minus the minimum value (the same in the nominator).
The Stochastic Oscillator seeks to find oversold and overbought zones by incorporating the highs and lows using the normalization formula as shown below:
An overbought level is an area where the market is perceived to be extremely bullish and is bound to consolidate. An oversold level is an area where market is perceived to be extremely bearish and is bound to bounce. Hence, the Stochastic Oscillator is a contrarian indicator that seeks to signal reactions of extreme movements. Now, our aim is to go to TradingView and code the Stochastic Smoothing Oscillator ourselves. Why should we do this knowing that it already exists in their proposed indicators?
The answer to this is that this is the first step towards creating our own technical indicators in a reputable charting platform. If we start by understanding the fundamentals of the Pine Script coding language, then by time, we will acquire the necessary skills to become fluent in it and code our own indicators and strategies.
Creating the Stochastic Smoothing Oscillator in TradingView
We want to create and plot the Stochastic Smoothing Oscillator on the EURGBP values loaded on the TradingView platform. Note that you must create an account to be able to view the charts, the good news is that it is free. Locate the Chart button on the home screen and then choose any asset you would like to calculate the indicator on. Now, on the bottom of the screen, locate Pine Editor and warm up your fingers to do some coding.
The first step is to specify the version of Pine Script. In our case it is 4.
//@version = 4
The next step is to specify the name of the indicator (Script) by using the following syntax
study("Stochastic Smoothing Oscillator")
Having done the introductory part, we can proceed by defining the parameters of the oscillator. The Stochastic Smoothing Oscillator is composed of exponential moving averages applied on the highs, lows, and closing prices which have the stochastic normalization function applied onto them. We know that we have two lookback periods we need to define, 2, and 13. The 2-period lookback period refers to the exponential moving average applied on the HLC data and the 13-period lookback period refers to the normalization window.
lookback = 13, ema_lookback = 2
Now, we are set to transform and smooth the HLC data. The ema() function is a built-in function that calculates the exponential moving average given a source which is our closing price and a lookback period which are the ones we have already defined.
SSO_low = ema(low, ema_lookback)
SSO_high = ema(high, ema_lookback)
SSO_close = ema(close, ema_lookback)
Now, all we need is to to apply the SSO formula which is the same as the Stochastic Oscillator’s formula. We can use the highest() and lowest() built-in functions to find the highest and lowest values given a certain lookback period, making our job much easier.
SSO = (SSO_close - lowest(SSO_low, lookback)) / (highest(SSO_high, lookback) - lowest(SSO_low, lookback)) * 100
And finally, we are set to plot the indicator alongside the chart. This is done using the plot() function. It takes a source and a color property as shown below.
plot(SSO, color = color.red)
hline(10, color=color.gray, linestyle=hline.style_dashed)
hline(90, color=color.gray, linestyle=hline.style_dashed)
The full code can be found below.
//@version = 4
study("Stochastic Smoothing Oscillator")
lookback = 13
ema_lookback = 2
SSO_low = ema(low, ema_lookback)
SSO_high = ema(high, ema_lookback)
SSO_close = ema(close, ema_lookback)
SSO = (SSO_close - lowest(SSO_low, lookback)) / (highest(SSO_high, lookback) - lowest(SSO_low, lookback)) * 100
plot(SSO, color = color.red)
hline(10, color=color.gray, linestyle=hline.style_dashed)
hline(90, color=color.gray, linestyle=hline.style_dashed)
The SSO is traded the same way as the Stochastic Oscillator. We have a bullish bias whenever the SSO is at the lower bottom and a bearish bias whenever the SSO is at the upper part.
If you are also interested by more technical indicators and using Python to create strategies, then my best-selling book on Technical Indicators may interest you:
Conclusion
Remember to always do your back-tests. You should always believe that other people are wrong. My indicators and style of trading may work for me but maybe not for you.
I am a firm believer of not spoon-feeding. I have learnt by doing and not by copying. You should get the idea, the function, the intuition, the conditions of the strategy, and then elaborate (an even better) one yourself so that you back-test and improve it before deciding to take it live or to eliminate it. My choice of not providing specific Back-testing results should lead the reader to explore more herself the strategy and work on it more.
To sum up, are the strategies I provide realistic? Yes, but only by optimizing the environment (robust algorithm, low costs, honest broker, proper risk management, and order management). Are the strategies provided only for the sole use of trading? No, it is to stimulate brainstorming and getting more trading ideas as we are all sick of hearing about an oversold RSI as a reason to go short or a resistance being surpassed as a reason to go long. I am trying to introduce a new field called Objective Technical Analysis where we use hard data to judge our techniques rather than rely on outdated classical methods.