Mastering Coding and Back-testing the TD Waldo #2 Trading Pattern
Written on
Chapter 1: Introduction to Pattern Recognition
Pattern recognition involves identifying and locating recurring trends that yield similar results. When a trader identifies a pattern, they anticipate a specific outcome to act upon in their trading decisions. A well-known example is the head and shoulders formation, which signifies an impending trend reversal. While opinions vary on the predictive power of this configuration, this article will focus on some unconventional objective patterns. These patterns have distinct rules, unlike traditional ones like the head and shoulders or double top/bottom patterns. Our primary focus will be the TD Waldo #2, a timing and reversal pattern introduced by Tom Demark.
I recently published a new book, building upon the success of my earlier work. This new edition includes advanced trend-following indicators and strategies, complete with a dedicated GitHub page for ongoing code updates. Additionally, the book features original colors optimized for printing cost efficiency. If this piques your interest, check out the Amazon link below or reach out to me on LinkedIn for the PDF version.
Chapter 2: Understanding the TD Waldo Patterns
Tom Demark, a distinguished technical analyst, has developed numerous indicators and patterns throughout his career. Among his many contributions are seven distinct price patterns known as "Waldo patterns," inspired by the cartoon "Where's Waldo?". These patterns are designed to help traders identify short-term market tops and bottoms. While traders may feel a thrill discovering these patterns, it's essential to remain vigilant against hindsight bias. Often, individuals will scrutinize charts for patterns that confirm their beliefs while ignoring those that do not align with their expectations. The solution lies in creating a systematic trading strategy based on these patterns, which we will outline here.
To identify a potential short-term bottom and initiate a buying opportunity, we look for a scenario where a new low is established, yet the closing price exceeds the last four closing prices. Conversely, to pinpoint a potential short-term top and a selling opportunity, we search for a new high that concludes lower than the last four closing prices.
The first video titled "Basics of CODING in 10 minutes" provides a quick introduction to coding concepts that are essential for understanding the mechanics of the TD Waldo #2 pattern.
Chapter 3: Coding the Indicator
To implement this indicator, we first need to define the essential manipulation functions:
# Function to add a specified number of columns
def adder(Data, times):
for i in range(1, times + 1):
z = np.zeros((len(Data), 1), dtype=float)
Data = np.append(Data, z, axis=1)
return Data
# Function to delete a specified number of columns
def deleter(Data, index, times):
for i in range(1, times + 1):
Data = np.delete(Data, index, axis=1)return Data
# Function to remove a specified number of rows from the beginning
def jump(Data, jump):
Data = Data[jump:,]
return Data
Assuming we have an OHLC array, we can use the following function to search for the TD Waldo #2 pattern:
def td_waldo_2(Data, high, low, close, buy, sell):
# Adding extra columns
Data = adder(Data, 10)
for i in range(len(Data)):
# Identifying a short-term bottom
if Data[i, 2] < Data[i - 1, 2] and Data[i, 2] < Data[i - 2, 2] and Data[i, 2] < Data[i - 3, 2] and Data[i, 2] < Data[i - 4, 2] and Data[i, 3] > Data[i - 1, 3] and Data[i, 3] > Data[i - 2, 3] and Data[i, 3] > Data[i - 3, 3] and Data[i, 3] > Data[i - 4, 3]:
Data[i, buy] = 1
# Identifying a short-term top
if Data[i, 1] > Data[i - 1, 1] and Data[i, 1] > Data[i - 2, 1] and Data[i, 1] > Data[i - 3, 1] and Data[i, 1] > Data[i - 4, 1] and Data[i, 3] < Data[i - 1, 3] and Data[i, 3] < Data[i - 2, 3] and Data[i, 3] < Data[i - 3, 3] and Data[i, 3] < Data[i - 4, 3]:
Data[i, sell] = -1return Data
Chapter 4: Back-testing Without Risk Management
The initial back-testing of the pattern will be conducted without risk management. This involves opening and closing positions based solely on closing prices and the subsequent signals. The second approach incorporates a theoretical 2:1 risk-reward ratio, where the stop loss is set at half the distance from the entry point, compared to the target profit level.
The following conditions will apply for the back-test:
- The time frame is hourly, starting from 2010, with a simulated 0.2 pip spread to mimic an institutional algorithm.
- Positions will open based on signals, and exits will occur upon receiving either the same or a contrary signal.
The second video titled "Programming vs Coding - What's the difference?" clarifies the distinction between programming and coding, which is crucial for understanding the nuances of implementing trading strategies.
Chapter 5: Back-testing with a 2:1 Risk-Reward Ratio
To align with sound risk management principles, we need to ensure that our potential risks are less than our expected returns. By utilizing a theoretical 2:1 risk-reward ratio, we adhere to this fundamental principle. We will establish this ratio using the Average True Range (ATR) indicator.
The back-test conditions will remain consistent with previous sections, but we will incorporate the following:
- Stop-loss orders will activate when the current price reaches 1x the ATR value at entry.
- Profit orders will trigger when the current price reaches 2x the ATR value at entry.
The performance of this strategy appears suboptimal and remains challenging to analyze.
Chapter 6: Final Thoughts
I advocate for the importance of conducting thorough back-tests. It’s crucial to maintain a healthy skepticism; what works for one trader may not work for another. I believe in the principle of independent learning through experimentation rather than imitation. It’s essential to grasp the concepts, functions, and conditions of a strategy before refining it and conducting your own back-tests.
Lastly, I have launched an NFT collection aimed at supporting various humanitarian and medical initiatives. "The Society of Light" features limited collectibles where a portion of each sale is donated to associated charities. Benefits of purchasing these NFTs include potential appreciation in value, portfolio diversification, and contributions to your chosen causes, along with a complimentary PDF copy of my latest book.
Consider supporting this cause to aid in eradicating polio through the link provided.