This post is part of the T4p Series.
In the previous post, I briefly introduced candlesticks and how to detect a bearish and bullish candle in Python.
In this post, we are going deeper, discussing candlestick patterns, what they are all about, and how to detect and represent them in Python
Introduction
As you learned in the previous post, there are mainly two types of candles: Bearish and Bullish. The size of the wick and the body determines what kind of bearish and bullish candle it is. Candlestick patterns visually help traders learn about the price movement to interpret the market sentiments.
History
Candlestick patterns were developed centuries ago by Japanese rice traders looking to predict price shifts in the market. Today, traders worldwide use these same patterns to spot potential trends and reversals, blending ancient wisdom with modern trading techniques.
Types of Candlestick Patterns
In a broader sense, we can divide patterns into two categories:
- Single Candlestick Patterns: Doji, Hammer, and Shooting Star.
- Multiple Candlestick Patterns: Engulfing, Morning Star, and Evening Star.
Some of the patterns represent either a bearish or bullish market while other patterns emerge in both.
There are many candlestick patterns, such as Doji, Engulfing, Hammer, Morning Star, Evening Star, etc. I can’t cover all those in a single post, so I will try to cover them in separate posts. For now, I am discussing the Doji and Engulfing patterns here.
Doji Pattern
It is a pattern that occurs when the opening and closing prices are virtually equal, creating a cross or a plus sign shape. The name Doji comes from Japanese and means blunder or mistake, referring to the rarity of having the same opening and closing prices.
Usage
Doji is used as a reversal pattern.
After an Uptrend:
- When you spot a Doji after a strong upward move
- Wait for confirmation (next candle closing lower)
- Consider taking profit on long positions or entering short positions
- Place stop loss above the Doji’s high
After a Downtrend:
- When you spot a Doji after a strong downward move
- Wait for confirmation (next candle closing higher)
- Consider entering long positions or covering shorts
- Place stop loss below the Doji’s low
Types of Doji
- Standard Doji:
- Open and Close should be the same/very close
- Equal shadows above and below
- Dragonfly Doji:
- Open and Close at/near the high
- Long lower shadow
- Little to no upper shadow
- Gravestone Doji:
- Open and Close at/near the low
- Long upper shadow
- Little to no lower shadow
Visual Representation
Now let’s write some code in Python to learn how you can represent Doji patterns
import mplfinance as mpf
import pandas as pd
def plot_candlestick_from_dict(data,title="CandleStick Chart"):
df = pd.DataFrame(data)
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
mpf.plot(df, type='candle', style='charles', title=title, ylabel='Price')
Standard Doji
ohlc_data = [
{"Date":"2024-09-01", "Open":100, "High":105, "Low":95, "Close":100},
]
plot_candlestick_from_dict(ohlc_data,"Standard Doji")
Dragonfly Doji
ohlc_data = [
{"Date":"2024-09-01", "Open":100, "High":100.5, "Low":95, "Close":100},
]
plot_candlestick_from_dict(ohlc_data,"Dragonfly Doji")
Gravestone Doji
ohlc_data = [
{"Date":"2024-09-01", "Open":100, "High":105, "Low":99.5, "Close":100},
]
plot_candlestick_from_dict(ohlc_data,"Gravestone Doji")
I have embedded the entire notebook. Visual representation is not enough. Now let’s write some code to detect Doji patterns in the trading OHLC data.
def is_doji(open_price, high, low, close, doji_size=0.1): """ Check if a candlestick is a Doji pattern Parameters: open_price: Opening price high: High price low: Low price close: Closing price doji_size: maximum body size ratio threshold (default 0.1 or 10% of range) """ body_size = abs(close - open_price) candle_range = high - low # Avoid division by zero if candle_range == 0: return False # Calculate body size as a ratio of the candle range body_ratio = body_size / candle_range return body_ratio <= doji_size
I have written functions for all Doji types but here I am explaining the core concept that will be helpful to figure out different Doji types.
Here are a few concepts:
- Body Size: It should be very small for Doji. We are computing it by taking the difference between closing and opening prices.
- Candle Range: Total range of price movement.
- Body Ratio: Compares the body to the total range.
- Doji Size: Threshold (default 0.1 or 10%).
The visual representation would be something like the below:
Normal Candle: Doji Candle: High 105 ─┬─ High 105 ─┬─ │ │ │ │ Open 100 ─┤ Open 100 ─┼─ Close 100.2 │ │ │ │ Close 95 ─┴─ Low 95 ─┴─ Body Ratio: 30% Body Ratio: 2% (Not a Doji) (Is a Doji!)
You can see the entire code in the Jupyter Notebook.
Engulfing Pattern
It is formed by two candles where the body of the first candle is engulfed by the body of the second candle. Trading volume often increases on the engulfing candle. There are two types of engulfing patterns: Bearish and Bullish.
- Bullish Engulfing: A two-candle pattern where a smaller bearish candle is completely “engulfed” by a larger bullish candle, suggesting a potential upward reversal.
- Bearish Engulfing: The opposite, where a smaller bullish candle is engulfed by a larger bearish candle, hinting at a possible downward trend.
Visual Representation
Below is the exported Jupyter Notebook for the visual representation of the engulfing pattern in Python: