Pattern recognition is the search and identification of recurring patterns with approximately similar outcomes. This means that when we manage to find a pattern, we have an expected outcome that we want to see and act on through our trading. For example, a head and shoulders pattern is a classic technical pattern that signals an imminent trend reversal. The literature differs on the predictive ability of this famous configuration.
I have just released a new book after the success of my previous one “The Book of Trading Strategies”. It features advanced trend-following indicators and strategies with a GitHub page dedicated to the continuously updated code. Also, this book features the original colors after having optimized for printing costs. If you feel that this interests you, feel free to visit the below Amazon link, or if you prefer to buy the PDF version, you could contact me on LinkedIn.
Introduction to Pattern Recognition
As stated in the introductory section, patterns are recurring configurations with a pre-defined outcome or expectancy. In finance, we like to detect pattern as it makes us more comfortable with predicting the next likely direction or reaction on the condition that the past success ratio of the pattern is statistically different from randomness.
The idea of this article is not to present a pattern and back-test it, rather it is to show how to brainstorm and find patterns then code them and analyze them visually. This means that we will pick some conditions that we make up and then simulate a pattern. Among my favorite types of configurations are the ones that rely on both the price and the time. For example, we can say that whenever we get a close higher than the opening price for four consecutive periods, a bullish continuation pattern has emerged. This is of course just a random hypothesis but it is to show how we can rely on the price through the close to open condition and on time through the four consecutive periods condition.
Brainstorming & Coding a Pattern in TradingView
Remaining in the price and time concept, let us create (or discover?) a pattern based on the highs and lows instead of just the close price. What if we also include the Fibonacci sequence into our time variable? Here is what I am suggesting:
A bullish (Buy) pattern appears whenever the current low is lower than the low 5 periods ago and lower than the low 13 periods ago and lower than the low 21 periods ago. Also, as an element of confirmation, the current close must be higher than the close 3 periods ago.
A bearish (Sell) pattern appears whenever the current high is higher than the high 5 periods ago and higher than the high 13 periods ago and higher than the high 21 periods ago. Also, as an element of confirmation, the current close must be lower than the close 3 periods ago.
Remember, this is not a pattern to be traded due to the fact that I have just come up with it as an example. I have tried to include the position of the current lows and highs relative to the past and then I have added a confirmation element using the close price relative to a near-term close price. This serves as a confirmation.
We want to create and plot the pattern values on the TradingView platform. Note that you must create an account to be able to view the charts, the good news is that it is free. Locate the Chart button on the home screen and then choose any asset you would like to calculate the indicator on. Now, on the bottom of the screen, locate Pine Editor and warm up your fingers to do some coding.
The first step is to specify the version of Pine Script. In our case it is 5 which is the latest version. Also, let us give it an arbitrary name; Alpha pattern.
//@version=5
indicator("Alpha Pattern", overlay = true)
Now, we will code the conditions as Boolean types. The buy pattern equals true if the low is less than the low from five periods ago (Hence, the number 5 inside the parentheses) and then the rest of the conditions follow.
buy_pattern = low < low[5] and low < low[13] and low > low[21] and close > close[3]
sell_pattern = high > low[5] and high > high[13] and high < high[21] and close < close[3]
Having defined the bulk of the pattern above, we now have the buy and sell conditions ready. All we need is to visualize them in the chart. This can be done by using the plotshape() function which plots arrows on the chart to highlight where the signals are.
plotshape(buy_pattern, style = shape.triangleup, color = color.green, location = location.belowbar, size = size.small)
plotshape(sell_pattern, style = shape.triangledown, color = color.red, location = location.abovebar, size = size.small)
One more thing before visualizing the pattern is by adding the conditions of the invalidation of the pattern. This helps us answer to the question: When is the pattern void?
We can simply state the following:
The bullish pattern is invalidated whenever the market breaks the low of the alpha bar (The candlestick where the pattern has been validated).
The bearish pattern is invalidated whenever the market surpasses the high of the alpha bar (The candlestick where the pattern has been validated).
bar_size = timeframe.multiplier * (timeframe.isdaily?1440:timeframe.isweekly?7*1440:timeframe.ismonthly?30*1440:1) * 60*1000
if buy_pattern == true
line.new(time, low, time + bar_size * 10, low, xloc.bar_time, color = color.green, width = 2)
if sell_pattern == true
line.new(time, high, time + bar_size * 10, high, xloc.bar_time, color = color.red, width = 2)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Sofien-Kaabar
//@version=5
indicator("Alpha Pattern", overlay = true)
buy_pattern = low < low[5] and low < low[13] and low > low[21] and close > close[3]
sell_pattern = high > low[5] and high > high[13] and high < high[21] and close < close[3]
plotshape(buy_pattern, style = shape.triangleup, color = color.green, location = location.belowbar, size = size.small)
plotshape(sell_pattern, style = shape.triangledown, color = color.red, location = location.abovebar, size = size.small)
bar_size = timeframe.multiplier * (timeframe.isdaily?1440:timeframe.isweekly?7*1440:timeframe.ismonthly?30*1440:1) * 60*1000
if buy_pattern == true
line.new(time, low, time + bar_size * 10, low, xloc.bar_time, color = color.green, width = 2)
if sell_pattern == true
line.new(time, high, time + bar_size * 10, high, xloc.bar_time, color = color.red, width = 2)
It is intuitive to see that red arrows denote short selling signals and green arrows denote buy signals while the horizontal lines are the invalidation points that need to be respected in order for the pattern to remain valid.
If you are also interested by more technical indicators and strategies, then my book might interest you:
Conclusion
Remember to always do your back-tests. You should always believe that other people are wrong. My indicators and style of trading may work for me but maybe not for you.
I am a firm believer of not spoon-feeding. I have learnt by doing and not by copying. You should get the idea, the function, the intuition, the conditions of the strategy, and then elaborate (an even better) one yourself so that you back-test and improve it before deciding to take it live or to eliminate it. My choice of not providing specific Back-testing results should lead the reader to explore more herself the strategy and work on it more.
To sum up, are the strategies I provide realistic? Yes, but only by optimizing the environment (robust algorithm, low costs, honest broker, proper risk management, and order management). Are the strategies provided only for the sole use of trading? No, it is to stimulate brainstorming and getting more trading ideas as we are all sick of hearing about an oversold RSI as a reason to go short or a resistance being surpassed as a reason to go long. I am trying to introduce a new field called Objective Technical Analysis where we use hard data to judge our techniques rather than rely on outdated classical methods.
One Last Word
I have recently started an NFT collection that aims to support different humanitarian and medical causes. The Society of Light is a set of limited collectibles which will help make the world slightly better as each sale will see a percentage of it sent directly to the charity attributed to the avatar. As I always say, nothing better than a bullet list to outline the benefits of buying these NFT’s:
High-potential gain: By concentrating the remaining sales proceedings on marketing and promoting The Society of Light, I am aiming to maximize their value as much as possible in the secondary market. Remember that trading in the secondary market also means that a portion of royalties will be donated to the same charity.
Art collection and portfolio diversification: Having a collection of avatars that symbolize good deeds is truly satisfying. Investing does not need to only have selfish needs even though there is nothing wrong with investing to make money. But what about investing to make money, help others, and collect art?
Donating to your preferred cause(s): This is a flexible way of allocating different funds to your charities.
A free copy of my book in PDF: Any buyer of any NFT will receive a free copy of my latest book shown in the link of the article.