Technical Pattern Recognition for Trading in Python.

Original article was published by Sofien Kaabar on Artificial Intelligence on Medium


TD Differential Pattern

This pattern seeks to find short-term trend reversals; therefore, it can be seen as a predictor of small corrections and consolidations. Below is an example on a candlestick chart of the TD Differential pattern.

Example of the Pattern. Notice the Short-term tendancy of the Reaction. (Image by Author)

If we want to code the conditions in Python, we may have a function similar to the below:

Buy (Go long) trigger:

  • Two closes each less than the prior.
  • Current buying pressure > previous buying pressure.
  • Current selling pressure < previous selling pressure.

Sell (Go short) trigger:

  • Two closes each greater than the prior.
  • Current buying pressure < previous buying pressure.
  • Current selling pressure > previous selling pressure.
def TD_differential(Data, true_low, true_high, buy, sell):
for i in range(len(Data)):

# True low
Data[i, true_low] = min(Data[i, 2], Data[i - 1, 3])
Data[i, true_low] = Data[i, 3] - Data[i, true_low]

# True high
Data[i, true_high] = max(Data[i, 1], Data[i - 1, 3])
Data[i, true_high] = Data[i, 3] - Data[i, true_high]

# TD Differential
if Data[i, 3] < Data[i - 1, 3] and Data[i - 1, 3] < Data[i - 2, 3] and \
Data[i, true_low] > Data[i - 1, true_low] and Data[i, true_high] < Data[i - 1, true_high]:
Data[i, buy] = 1
if Data[i, 3] > Data[i - 1, 3] and Data[i - 1, 3] > Data[i - 2, 3] and \
Data[i, true_low] < Data[i - 1, true_low] and Data[i, true_high] > Data[i - 1, true_high]:
Data[i, sell] = -1

Now, let us back-test this strategy all while respecting a risk management system that uses the ATR to place objective stop and profit orders. I have found that by using a stop of 4x the ATR and a target of 1x the ATR, the algorithm is optimized for the profit it generates (be that positive or negative). It is clear that this is a clear violation of the basic risk-reward ratio rule, however, remember that this is a systematic strategy that seeks to maximize the hit ratio on the expense of the risk-reward ratio.

Reminder: The risk-reward ratio (or reward-risk ratio) measures on average how much reward do you expect for every risk you are willing to take. For example, you want to buy a stock at $100, you have a target at $110, and you place your stop-loss order at $95. What is your risk reward ratio? Clearly, you are risking $5 to gain $10 and thus 10/5 = 2.0. Your risk reward ratio is therefore 2. It is generally recommended to always have a ratio that is higher than 1.0 with 2.0 as being optimal. In this case, if you trade equal quantities (size) and risking half of what you expect to earn, you will only need a hit ratio of 33.33% to breakeven. A good risk-reward ratio will take the stress out of pursuing a high hit ratio.

Example of a Signal Chart on the EURUSD — TD Differential Pattern. (Image by Author)
Equity Curves of the TD Differential Strategy on 10 Major Currency Pairs. (Image by Author)

It looks like it works well on AUDCAD and EURCAD with some intermediate periods where it underperforms. Also, the general tendency of the equity curves is upwards with the exception of AUDUSD, GBPUSD, and USDCAD. For a strategy based on only one pattern, it does show some potential if we add other elements. If we take a look at an honorable mention, the performance metrics of the AUDCAD were not bad, topping at 69.72% hit ratio and an expectancy of $0.44 per trade.