Where’s Waldo? An example of pattern recognition. Back-testing and optimizing with Python.

Tom Demark, the renowned famous technical analyst has created many successful indicators and patterns. Among his many discoveries, he came up with 7 different price patterns that he dubbed as “Waldo patterns”.

Inspired by the cartoon Where’s Waldo? Tom has developed these patterns to find short-term tops and bottoms. Any trader or even a receiver of such information would be excited knowing about this, but with hindsight and confirmation bias, the person seeing this, will test the patterns on the chart and focus on the ones that work while disregarding the ones that do not. The only way to remedy this problem is by developing a systematic trading strategy based on these patterns, and that is precisely what we are going to do.

In this study we will limit our test to two of the 7 patterns, Waldo #5 and Waldo #8. (The numbering of the patterns starts at 2 which is why Waldo #8 is the seventh pattern).

The below definitions and conditions of the patterns are taken almost word for word from a great book written by Jason Perl: DeMark Indicators (Bloomberg Market Essentials: Technical Analysis)

TD WALDO #5

To find a potential short-term top:

  • The current bar’s close should be equal to the close of the previous price bar.
  • The close of the previous bar should be higher than the close of the bar before it.

To find a potential short-term bottom:

  • The current bar’s close should be equal to the close of the previous price bar.
  • The close of the previous bar should be lower than the close of the bar before it.
Image for post
Image for post
TD Waldo #5 pattern with a coincidental doji to reinforce the conviction.

The code function of the pattern is the below. Note that I have made condition1 and condition2 as variables so as to make it easier to optimize them as we will later see. Naturally, condition1 = 1 while condition2 = 2. As usual, we assume an OHLC data structure, thus the fifth column (index = 4) is the start of the signals. We also assume, buying/selling on close rather than on the next open (as we have done in other back-tests).

def waldo5(Data, condition1, condition2, trigger):

for i in range(len(Data)):
try:
if Data[i, 3] == Data[i - condition1, 3] and Data[i - condition1, 3] < Data[i - condition2, 3]:
Data[i, 4] = 1 # Long signal
else:
continue
except:
IndexError

for i in range(len(Data)):
try:
if Data[i, 3] == Data[i - condition1, 3] and Data[i - condition1, 3] > Data[i - condition2, 3]:
Data[i, 5] = -1 # Short signal
else:
continue
except:
IndexError

TD WALDO #8

To find a potential short-term top:

  • The close of the current bar must be lower than the close of five bars earlier.
  • The close of the current bar must be higher than all seven prior highs.

To find a potential short-term bottom:

  • The close of the current bar must be higher than the close of five bars earlier.
  • The close of the current bar must be lower than all seven prior lows.
Image for post
Image for post
TD Waldo #8 sell pattern in action.

The code function of the pattern is the below. Naturally, condition1 = 5.

def waldo8(Data, condition1, trigger):

for i in range(len(Data)):
try:
if Data[i, 3] > Data[i - condition1, 3] and \
Data[i, 3] < Data[i - condition1 - 1, 2] and \
Data[i, 3] < Data[i - condition1 - 2, 2] and \
Data[i, 3] < Data[i - condition1 - 3, 2] and \
Data[i, 3] < Data[i - condition1 - 4, 2] and \
Data[i, 3] < Data[i - condition1 - 5, 2] and \
Data[i, 3] < Data[i - condition1 - 6, 2] and \
Data[i, 3] < Data[i - condition1 - 7, 2]:
Data[i, 4] = 1
else:
continue
except:
IndexError

for i in range(len(Data)):
try:
if Data[i, 3] < Data[i - condition1, 3] and \
Data[i, 3] > Data[i - condition1 - 1, 1] and \
Data[i, 3] > Data[i - condition1 - 2, 1] and\
Data[i, 3] > Data[i - condition1 - 3, 1] and \
Data[i, 3] > Data[i - condition1 - 4, 1] and \
Data[i, 3] > Data[i - condition1 - 5, 1] and \
Data[i, 3] > Data[i - condition1 - 6, 1] and \
Data[i, 3] > Data[i - condition1 - 7, 1]:
Data[i, 5] = -1
else:
continue
except:
IndexError

Back-testing results

If we back-test the patterns through these conditions (Note, that this is a sample as I have back-tested these two patterns on most FX pairs):

  • Test asset: EURUSD
  • Time frame: 3H data.
  • Holding period: Variable (closing out when getting a contrarian signal)
  • Span: 01/01/2000 (around 42,525 3H data).
  • Risk management: None.
  • Starting balance = $1,000.
  • Lot = 0.1 of a standard $100,000 lot.

We get the following results:

Image for post
Image for post
Equity curve for TD Waldo #5: 49.88% hit ratio, 0.98 profit factor.
Image for post
Image for post
Signal chart of TD Waldo #5 (of the last 1000 trading periods).
Image for post
Image for post
Equity curve for TD Waldo #8: 45.26% hit ratio, 0.78 profit factor.
Image for post
Image for post
Signal chart of TD Waldo #8 (of the last 1000 trading periods).

Optimization attempt

If we run an optimization algorithm, we will get the below results. Remember, we are using 3H data with a 1 pip transaction cost. We will not use any risk optimization as we want to test the strategy in its purest form. The optimization algorithm is extremely simple and it is simply a loop between the two conditions and the holding period (a sort of grid search) to determine the optimal one.

Optimized Waldo #5

To find a potential short-term top:

  • The current bar’s close should be equal to the close of the previous price bar.
  • The close of the previous bar should be higher than the close of the 49th bar before it.

To find a potential short-term bottom:

  • The current bar’s close should be equal to the close of the previous price bar.
  • The close of the previous bar should be lower than the close of the 49th bar before it.
Image for post
Image for post
Optimized TD Waldo #5 equity curve
Image for post
Image for post
Optimized TD Waldo #5 Signal chart

Optimized Waldo #8

To find a potential short-term top:

  • The close of the current bar must be lower than the close of 42 bars earlier.
  • The close of the current bar must be higher than all 7 prior highs.

To find a potential short-term bottom:

  • The close of the current bar must be higher than the close of 42 bars earlier.
  • The close of the current bar must be lower than all 7 prior lows.
Image for post
Image for post
Optimized TD Waldo #8 equity curve
Image for post
Image for post
Optimized TD Waldo #8 Signal chart

Conclusion

The results show no added value whatsoever from Waldo #5 and Waldo #8 (especially Waldo #8) on the tested pair only with the given conditions above. Other conditions on multiple pairs have been tested but the results were as disappointing as the above. It is evident that the default parameters do not work and the optimized values are way too sensitive (i.e. a minor change in an optimized waldo pattern would drastically change the results), and this is how you get a bad strategy that is overfit.

The key point from this study is that theoretical patterns are not holy grail and should not be used without personally making sure they work.

One might wonder how these patterns were ever included in a textbook. Other Waldo patterns have been tested and showed no big difference from the above, making the Waldo group not great for any trading algorithm. Nevertheless, other external patterns do add value (some come from the great Demark himself). I guess the best answer to this study is by wondering whether we should be asking:

Where’s alpha rather than where’s Waldo?

Image for post
Image for post

Written by

Institutional FOREX Strategist | Trader | Data Science Enthusiast. Author of the Book of Back-tests: https://www.amazon.com/dp/B089CWQWF8

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store