# 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.

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.

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:

# 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.

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.

# 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?

Written by