Discover more from All About Trading!
The TD Moving Average II in Python
Coding the TD Moving Average II in Python
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.
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.
You can also check out my other newsletter The Weekly Market Analysis Report that sends tactical directional views every weekend to highlight the important trading opportunities using technical analysis that stem from modern indicators. The newsletter is free.
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
You can also check out my other newsletter The Weekly Market Sentiment Report that sends tactical directional views every weekend to highlight the important trading opportunities using a mix between sentiment analysis (COT reports, Put-Call ratio, Gamma exposure index, etc.) and technical analysis.
The next Figure shows an example on the USDCHF.
If you liked this article, do not hesitate to like and comment, to further the discussion!