Let’s Create a Technical Indicator for Trading.
By: Sofien Kaabar
Tags: Machine Learning, Data Science, Writing, Finance, Trading

Technical indicators are all around us. Many are famous like the Relative Strength Index and the MACD while others are less known such as the Relative Vigor Index and the Keltner Channel. These indicators have been developed to aid in trading and sometimes they can be useful during certain market states. For example, the RSI works well when markets are ranging. Technical indicators are certainly not intended to be the protagonists of a profitable trading strategy.

Our aim is to see whether we could think of an idea for a technical indicator and if so, how do we come up with its formula. The struggle doesn’t stop there, we must also back-test its effectiveness, after all, we can easily develop any formula and say we have an indicator then market it as the holy grail.

We will try to compare our new indicator’s back-testing results with those of the RSI, hence giving us a relative view of our work. If you like to see more trading strategies relating to the RSI before you start, here’s an article that presents it from a different and interesting view:

Brainstorming & Formulating the idea

The first step in creating an indicator is to choose which type will it be? Is it a trend-following indicator? Maybe a contrarian one? Does it relate to timing or volatility? Will it be bounded or unlimited?

To simplify our signal generation process, let’s say we will choose a contrarian indicator. This means that we will try to create an indicator that oscillates around recurring values and is either stationary or almost-stationary (although this term does not exist in statistics). One of my favourite methods is to simple start by taking differences of values. This is a huge leap towards stationarity and getting an idea on the magnitudes of change over time. But, to make things more interesting, we will not subtract the current value from the last value. As I am a fan of Fibonacci numbers, how about we subtract the current value (i.e. today’s closing price or this hour’s closing price) minus the value 8 periods ago. So, the first step in this indicator is a simple spread that can be mathematically defined as follows with delta (Δ) as the spread:

The next step can be a combination of a weighting adjustment or an addition of a volatility measure such as the Average True Range or the historical standard deviation. Let’s stick to the simple method and choose to divide our spread by the rolling 8-period standard deviation of the price. This gives a volatility adjustment with regards to the momentum force we’re trying to measure. Hence, we will calculate a rolling standard-deviation calculation on the closing price; this will serve as the denominator in our formula. Remember, we said that we will divide the spread by the rolling standard-deviation. Let’s update our mathematical formula. Knowing that the equation for the standard deviation is the below:

We can consider X as the result we have so far (The indicator that is being built). The result is the spread divided by the standard deviation as represented below:

One last thing to do now is to choose whether to smooth out our values or not. Sometimes, we can get choppy and extreme values from certain calculations. Luckily, we can smooth those values using moving averages. As we want to be consistent, how about we make a rolling 8-period average of what we have so far? This means we will simply calculate the moving average of X.

For more about moving averages, consider this article that shows how to code them:

Now, we can say that we have an indicator ready to be visualized, interpreted, and back-tested. Before we do that, let’s see how we can code this indicator in python assuming we have an OHLC array.

for i in range(len(Asset)): # Calculating the spread

Asset[i, 4] = Asset[i, 3] - Asset[i - 8, 3]

# Calculating the Standard Deviation

Asset[i, 5] = Asset[i - 8:i + 1, 3].std()

# Volatility Adjustment of the spread

Asset[i, 6] = Asset[i, 4] / Asset[i, 5]

# Smoothing out and getting the indicator's values

Asset[i, 7] = Asset[i - 7:i + 1, 6].mean()

Visualizing the indicator

Visual interpretation is one of the first key elements of a good indicator. Below is our indicator versus a number of FX pairs.