Harami Pattern Recognition in TradingView
Coding a Harami Candlestick Pattern Scanner in TradingView
Candlestick patterns are a great addition to market analysis. Some may even consider them vital in research and trading. This article presents the Harami pattern and shows how to code a scanner in TradingView that detects these patterns.
For the complete collection of candlestick patterns in detail with back-tests and technical strategies, you can check out my newest book with O’Reilly Media. The book features a huge number of classic and modern candlestick patterns as it dwelves into the realm of technical analysis with different trading strategies. The book comes with its own GitHub and is dynamic in nature as it is continuously updated and questions are answered on the O’Reilly platform promptly.
Mastering Financial Pattern Recognition
Amazon.com: Mastering Financial Pattern Recognition eBook : Kaabar, Sofien: Kindle Storeamzn.to
The Harami Pattern
Candlestick charts are among the most famous ways to analyze the time series visually. They contain more information than a simple line chart and have more visual interpretability than bar charts.
The Harami pattern is a two-candlestick contrarian configuration. It can be segmented into two types, the strict Harami and the flexible Harami. We will discuss and code both in the scanner. The Harami’s general shape is a candlestick that fully englobes the candlestick that follows it.
The strict bullish Harami is composed of a bullish candlestick with the high price lower than the open price of the previous candlestick and the low price higher than the close price of the previous candlestick. The following Figure shows a theoretical illustration of the strict bullish Harami.
The strict bearish Harami is composed of a bearish candlestick with the high price lower than the close price of the previous candlestick and the low price higher than the open price of the previous candlestick. The following Figure shows a theoretical illustration of the strict bearish Harami.
The flexible Harami pattern relaxes the condition that deals with the highs and lows.
The flexible bullish Harami is composed of a bullish candlestick with the high price lower than the high price of the previous candlestick and the low price higher than the low price of the previous candlestick. The following Figure shows a theoretical illustration of the flexible bullish Harami.
The flexible bearish Harami is composed of a bearish candlestick with the high price lower than the high price of the previous candlestick and the low price higher than the low price of the previous candlestick. The following Figure shows a theoretical illustration of the flexible bearish Harami.
Coding the Scanner in TradingView
The conditions of the pattern are relatively easy to code especially in a straightforward and simple coding language such as Pine Script, TradingView’s native language.
The aim of the scanner is to detect the Harami patterns using the following indications:
A green arrow for flexible bullish Harami.
A red arrow for flexible bearish Harami.
A blue arrow for strict bullish Harami.
An orange arrow for strict bearish Harami.
It is reasonable to assume that flexible signals will be much more common than strict signals.
// 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("Harami Finder", overlay = true)
strict_bullish_harami = high <= open[1] and low >= close[1] and close > open and close[1] < open[1] and close[2] < open[2]
strict_bearish_harami = high <= close[1] and low >= open[1] and close < open and close[1] > open[1] and close[2] > open[2]
flexible_bullish_harami = close < open[1] and open >= close[1] and high < high[1] and low > low[1] and close > open and close[1] < open[1] and close[2] < open[2]
flexible_bearish_harami = close > open[1] and open <= close[1] and high < high[1] and low > low[1] and close < open and close[1] > open[1] and close[2] > open[2]
plotshape(flexible_bullish_harami, style = shape.triangleup, color = color.green, location = location.belowbar, size = size.small)
plotshape(flexible_bearish_harami, style = shape.triangledown, color = color.red, location = location.abovebar, size = size.small)
plotshape(strict_bullish_harami, style = shape.triangleup, color = color.blue, location = location.belowbar, size = size.normal)
plotshape(strict_bearish_harami, style = shape.triangledown, color = color.orange, location = location.abovebar, size = size.normal)
The following Figure shows a signal chart after the code has been applied and executed.
The following Figure shows another signal chart.
If you want to see how to create all sorts of algorithms yourself, feel free to check out Lumiwealth. From algorithmic trading to blockchain and machine learning, they have hands-on detailed courses that I highly recommend.
Learn Algorithmic Trading with Python Lumiwealth
Learn how to create your own trading algorithms for stocks, options, crypto and more from the experts at Lumiwealth. Click to learn more
Summary
To sum up, what I am trying to do is to simply contribute to the world of objective technical analysis which is promoting more transparent techniques and strategies that need to be back-tested before being implemented. This way, technical analysis will get rid of the bad reputation of being subjective and scientifically unfounded.
I recommend you always follow the the below steps whenever you come across a trading technique or strategy:
Have a critical mindset and get rid of any emotions.
Back-test it using real life simulation and conditions.
If you find potential, try optimizing it and running a forward test.
Always include transaction costs and any slippage simulation in your tests.
Always include risk management and position sizing in your tests.
Finally, even after making sure of the above, stay careful and monitor the strategy because market dynamics may shift and make the strategy unprofitable.
Building tech is slow and expensive. Bubble is the most powerful no-code platform for creating digital products. Build better and faster.