Tom Demark has created many interesting indicators, among them is the TD Moving Average II, an overlay technique that aims to reduce lag by incorporating a rate-of-change calculation. This article presents the concept of moving averages and shows how to code the TD Moving Average II.
For a detailed and thorough collection of contrarian trading strategies, you can check out my book. The book features a huge number of classic and modern techniques as it dwelves into the realm of technical analysis with different trading strategies. The book comes with its own GitHub.
Contrarian Trading Strategies in Python
Amazon.com: Contrarian Trading Strategies in Python: 9798434008075: Kaabar, Sofien: Booksamzn.to
Moving Averages
Moving averages come in all shapes and types. The most basic type is the simple moving average which is simply the sum divided by the quantity. The next mathematical representation shows how to calculate a simple mean given a dataset:
Therefore, the simple moving average is the sum of the values divided by their number. In technical analysis, you generally use moving averages to understand the underlying trend and to find trading signals. Check the next Figure which shows a 60-period simple moving average applied on hourly values of Ethereum versus USD.
Assume you have an OHLC data array imported in Python (which I have shown how to do many times in previous articles). Write the below primal functions that allow you to better manipulate data:
def add_column(data, times):
for i in range(1, times + 1):
new = np.zeros((len(data), 1), dtype = float)
data = np.append(data, new, axis = 1)
return data
def delete_column(data, index, times):
for i in range(1, times + 1):
data = np.delete(data, index, axis = 1)
return data
def delete_row(data, number):
data = data[number:, ]
return data
Then, to code a simple moving average, write the below function:
def ma(data, lookback, close, position):
data = add_column(data, 1)
for i in range(len(data)):
try:
data[i, position] = (data[i - lookback + 1:i + 1, close].mean())
except IndexError:
pass
data = delete_row(data, lookback)
return data
The TD Moving Average II
First of all, this indicator has been created by Tom Demark, a renowned technical analyst. The main idea is to reduce lags. The TD Moving Average II is composed of two simple moving averages, the first one has a 3-period lookback and the second one has a 34-period lookback. Note how these periods are part of the Fibonacci sequence, a sequence that Tom Demark likes and uses frequently in his indicators.
A rate-of-change is then calculated on both moving averages but not exactly the same way. Here’s how:
The 3-period simple moving average is compared to its value from 2 periods ago.
The 34-period simple moving average is compared to its value from 1 period ago.
A rate-of-change calculation is done by dividing the current value by the value from a specified period in the past and then subtracting one. It is simply the return of the period.
The way to use the indicator is to consider a trend as established only if the following conditions are met:
Bullish: The market must be above both moving averages and the two rate-of-changes are positive.
Bearish: The market must be below both moving averages and the two rate-of-changes are negative.
The next Figure shows an example on the EURUSD. It is worth noting that the moving average is shown in green when the rate of change is positive and it is shown in red when the rate of change is negative.
The next code snippet shows how to calculate both moving averages and the rate-of-change on them.
my_data = ma(my_data, 3, 3, 4)
my_data = ma(my_data, 34, 3, 5)
my_data = add_column(my_data, 10)
for i in range(len(my_data)):
my_data[i, 6] = (my_data[i, 4] / my_data[i - 2, 4]) - 1
for i in range(len(my_data)):
my_data[i, 7] = (my_data[i, 5] / my_data[i - 1, 5]) - 1
The next Figure shows an example on the USDCHF.
If you want to see how to create all sorts of algorithms yourself, feel free to check out Lumiwealth. From algorithmic trading to blockchain and machine learning, they have hands-on detailed courses that I highly recommend.
Learn Algorithmic Trading with Python Lumiwealth
Learn how to create your own trading algorithms for stocks, options, crypto and more from the experts at Lumiwealth. Click to learn more
Summary
To sum up, what I am trying to do is to simply contribute to the world of objective technical analysis which is promoting more transparent techniques and strategies that need to be back-tested before being implemented. This way, technical analysis will get rid of the bad reputation of being subjective and scientifically unfounded.
I recommend you always follow the the below steps whenever you come across a trading technique or strategy:
Have a critical mindset and get rid of any emotions.
Back-test it using real life simulation and conditions.
If you find potential, try optimizing it and running a forward test.
Always include transaction costs and any slippage simulation in your tests.
Always include risk management and position sizing in your tests.
Finally, even after making sure of the above, stay careful and monitor the strategy because market dynamics may shift and make the strategy unprofitable.