Building a comprehensive set of Technical Indicators in Python for quantitative trading

Customizable, comprehensive indicators for Machine-learning and statistical algorithms

Modishubham
Towards Data Science

--

Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our Reader Terms for details.

Overview

Predicting asset price movements has been a widely researched area aimed at developing alpha-generating trading strategies that capture these asset price movements “accurately”. I say accurately with a pinch of salt given the stochastic nature of most asset prices which, by definition, is random in nature. The idea thus focuses on performing some sort of analysis to capture, with some degree of confidence, the movement of this stochastic element. Among the multitude of methods used to predict this movement, technical indicators have been around for quite some time (reportedly used since the 1800s) as one of the methods used in forming an opinion of a potential move.

Until the widespread of algorithmic trading, technical indicators were primarily used by traders who would look up at these indicators on their trading screen to make a buy/sell decision. Even though this is still very prevalent, technical analysis has made its way into automated trading given the ability of Machine-Learning and other statistical tools to analyze this data in a fraction of time and the computational ability of computers to back-test with multiple decades of data.

Even though this article does not argue for or against use of Technical analysis, the technical indicators below can be used to perform various back-tests and come up with an opinion on their prediction power.

Technical Indicators

This article will focus on a comprehensive list of technical indicators that are widely used by professionals and scholars, and those that I believe are most beneficial in automated trading. The list of indicators are:

1. Simple Moving Average (Fast and Slow)

2. Average True Range

3. Average Directional Index (Fast and Slow)

4. Stochastic Oscillators (Fast and Slow)

5. Relative Strength Index (Fast and Slow)

6. Moving Average Convergence Divergence

7. Bollinger Bands

8. Rate of Change

Data

Using pandas datareader for Yahoo finance database, I extract daily Apple and Netflix stock data from January 1990 to today. Most of the indicators are created using Close rather than Adjusted Close in this article. It is left to the reader for choice of price.

Since technical indicators work best in short term, I will use 5 days and 15 days as my fast and slow signal respectively. This however may be changed based on the investment horizon. The following indicators are customizable to any duration with a single parameter change.

Image by author

Simple Moving Average (SMA)

Simple Moving Average is one of the most common technical indicators. SMA calculates the average of prices over a given interval of time and is used to determine the trend of the stock. As defined above, I will create a slow SMA (SMA_15) and a fast SMA (SMA_5). To provide Machine Learning algorithms with already engineered factors, one can also use (SMA_15/SMA_5) or (SMA_15 - SMA_5) as a factor to capture the relationship between these two moving averages.

Image by author

Simple Moving Average Volume

Similar to Simple Moving Average of price, a simple moving average of volume provides insights into the strength of signal that the stock displays.

Wilder’s Smoothing

Before moving on to the next indicator, I would like to mention another type of smoothening or moving average that is commonly used with other indicators. Although SMA is quite common, it contains a bias of giving equal weight to each value in the past. To solve this, Wells Wilder introduced a new version of smoothening that places more weight on the recent events. We will use Wilder’s Smoothing for most of our following indicators, and below is the function that can be generally used to obtain this Smoothing.

Average True Range (ATR)

Average True Range is a common technical indicator used to measure volatility in the market, measured as a moving average of True Ranges. A higher ATR of a company implied higher volatility of the stock. ATR however is primarily used in identifying when to exit or enter a trade rather than the direction in which to trade the stock.

As defined above, a slow ATR represents 5 days moving average and fast ATR represents 15 days moving average.

True Range is defined as maximum of:

a. High - Low

b. abs(High - Previous Close)

c. abs(Low - Previous Close)

Image by author

Average Directional Index (ADX)

Average Directional Index was developed by Wilder to assess the strength of a trend in stock prices. Two of its main components, +DI and -DI helps in identifying the direction of the trend. In general, an ADX of 25 or above indicates a strong trend and an ADX of less than 20 indicates a weak trend. The calculation of ADX is quite complex and requires certain steps.

I will calculate ADX for 5 and 15 days as well.

Image by author

Stochastic Oscillators

Stochastic oscillator is a momentum indicator aiming at identifying overbought and oversold securities and is commonly used in technical analysis.

Image by author

Relative Strength Index (RSI)

RSI is one of the most common momentum indicator aimed at quantifies price changes and the speed of such change.

Image by author

Moving Average Convergence Divergence (MACD)

MACD uses two exponentially moving averages and creates a trend analysis based on their convergence or divergence. Although most commonly used MACD slow and fast signals are based on 26 days and 12 days respectively, I have used 15 days and 5 days to be consistent with other indicators.

Image by author

Bollinger Bands

Bollinger bands capture the volatility of a stock and are used to identify overbought and oversold stocks. Bollinger bands consists of three main elements: The simple moving average line, an upper bound which is 2 standard deviations above moving average and a lower bound which is 2 standard deviations below moving average.

Image by author

Rate of Change

Rate of change is a momentum indicator that explains a price momentum relative to a price fixed period before.

Image by author

Conclusion

These technical indicators are highly customizable with regards to the time horizon captured along with allowing various Feature Engineering that would help create a better model. These values can either directly fit into a Machine Learning model or form a subset of factors for a bigger model.

There are numerous other indicators that can be considered, even if with not much importance. The indicators listed in the article are in no way an exhaustive list of indicators however a list of those that I have used in my models.

In my next article, I will explain the implementation of these indicators into a Machine Learning model and dive deeper into creating and carefully back testing the strategy.

Do leave your comments and suggestion below.

--

--