Home » Candlestick Patterns » Detecting Morning Doji Star Candlestick Pattern in Pine Script

Detecting Morning Doji Star Candlestick Pattern in Pine Script

Photo of author
Published on

Overview of Morning Doji Star Candlestick Pattern

The Morning Doji Star Candlestick Pattern is a bullish reversal pattern typically found at the bottom of a downtrend. It is a three-candlestick formation that signals a potential turnaround from a bearish to a bullish market sentiment. Here’s a breakdown of its structure:

Morning Doji Star Candlestick Pattern
  1. First Candlestick: This is a long bearish candlestick that continues the existing downtrend. It represents a strong selling day where the close is significantly lower than the open.
  2. Second Candlestick: The middle candle is a Doji, which is a candlestick with a very small or virtually non-existent body. This candle opens and closes at approximately the same price, indicating indecision in the market. The Doji can gap down from the first candlestick, adding to the pattern’s significance.
  3. Third Candlestick: The final candle is a bullish candlestick that closes well into the body of the first day’s bearish candle, often covering at least half of the body of the first candlestick. This candle indicates a strong buying pressure overtaking the prior bearish sentiment.

Importance in Technical Analysis

The Morning Doji Star Candlestick Pattern is important in technical analysis due to several key reasons:

  1. Strong Bullish Reversal Signal: It’s considered a reliable indicator of a shift from a bearish to a bullish trend, particularly in a downtrend.
  2. Indicates Market Indecision and Shift: The presence of a Doji suggests a point of indecision in the market, often marking the transition point in sentiment.
  3. Confirmation of Trend Change: When followed by additional bullish signals, it can confirm a trend reversal, aiding in decision-making for buying or closing short positions.
  4. Widely Recognized: As a well-known pattern, it is closely watched by many traders, potentially increasing its effectiveness due to collective response.
  5. Helps in Risk Management: Identifying this pattern can assist traders in placing stop-loss orders more effectively, managing risk in a downtrend.

Example From Trading View

Example From Trading View

Defining the Morning Doji Star Candlestick Pattern Criteria 

The Morning Doji Star Candlestick Pattern is defined by specific criteria:

  1. Preceding Downtrend: It occurs after a noticeable downtrend in the market, indicating bearish momentum.
  2. First Candlestick: A long bearish (red or black) candlestick, reinforcing the existing downtrend.
  3. Second Candlestick: A Doji, characterized by a small or almost nonexistent body, indicating market indecision. It usually gaps down from the first candlestick.
  4. Third Candlestick: A bullish (green or white) candlestick that closes well into the body of the first candle, typically covering at least half of its length. This suggests a strong shift to bullish sentiment.

Code For Detecting Morning Doji Star

//@version=5
indicator("Morning Doji Star - Bullish", shorttitle = "Morning Doji Star", overlay=true)

C_DownTrend = true
C_UpTrend = true
var trendRule1 = "SMA50"
var trendRule2 = "SMA50, SMA200"
var trendRule = input.string(trendRule1, "Detect Trend Based On", options=[trendRule1, trendRule2, "No detection"])

if trendRule == trendRule1
	priceAvg = ta.sma(close, 50)
	C_DownTrend := close < priceAvg
	C_UpTrend := close > priceAvg

if trendRule == trendRule2
	sma200 = ta.sma(close, 200)
	sma50 = ta.sma(close, 50)
	C_DownTrend := close < sma50 and sma50 < sma200
	C_UpTrend := close > sma50 and sma50 > sma200
C_Len = 14 // ta.ema depth for bodyAvg
C_ShadowPercent = 5.0 // size of shadows
C_ShadowEqualsPercent = 100.0
C_DojiBodyPercent = 5.0
C_Factor = 2.0 // shows the number of times the shadow dominates the candlestick body

C_BodyHi = math.max(close, open)
C_BodyLo = math.min(close, open)
C_Body = C_BodyHi - C_BodyLo
C_BodyAvg = ta.ema(C_Body, C_Len)
C_SmallBody = C_Body < C_BodyAvg
C_LongBody = C_Body > C_BodyAvg
C_UpShadow = high - C_BodyHi
C_DnShadow = C_BodyLo - low
C_HasUpShadow = C_UpShadow > C_ShadowPercent / 100 * C_Body
C_HasDnShadow = C_DnShadow > C_ShadowPercent / 100 * C_Body
C_WhiteBody = open < close
C_BlackBody = open > close
C_Range = high-low
C_IsInsideBar = C_BodyHi[1] > C_BodyHi and C_BodyLo[1] < C_BodyLo
C_BodyMiddle = C_Body / 2 + C_BodyLo
C_ShadowEquals = C_UpShadow == C_DnShadow or (math.abs(C_UpShadow - C_DnShadow) / C_DnShadow * 100) < C_ShadowEqualsPercent and (math.abs(C_DnShadow - C_UpShadow) / C_UpShadow * 100) < C_ShadowEqualsPercent
C_IsDojiBody = C_Range > 0 and C_Body <= C_Range * C_DojiBodyPercent / 100
C_Doji = C_IsDojiBody and C_ShadowEquals

patternLabelPosLow = low - (ta.atr(30) * 0.6)
patternLabelPosHigh = high + (ta.atr(30) * 0.6)

label_color_bullish = input(color.rgb(0, 255, 64), "Label Color Bullish")
C_MorningDojiStarBullishNumberOfCandles = 3
C_MorningDojiStarBullish = false
if C_LongBody[2] and C_IsDojiBody[1] and C_LongBody and C_DownTrend and C_BlackBody[2] and C_BodyHi[1] < C_BodyLo[2] and C_WhiteBody and C_BodyHi >= C_BodyMiddle[2] and C_BodyHi < C_BodyHi[2] and C_BodyHi[1] < C_BodyLo
	C_MorningDojiStarBullish := true
alertcondition(C_MorningDojiStarBullish, title = "New pattern detected", message = "New Morning Doji Star – Bullish pattern detected")
if C_MorningDojiStarBullish
    var ttBullishMorningDojiStar = "Morning Doji Star\nThis candlestick pattern is a variation of the Morning Star pattern. A three-day bullish reversal pattern, which consists of three candlesticks will look something like this: The first being a long-bodied red candle that extends the current downtrend. Next comes a Doji that gaps down on the open. After that comes a long-bodied green candle, which gaps up on the open and closes above the midpoint of the body of the first day. It is more bullish than the regular morning star pattern because of the existence of the Doji."
    label.new(bar_index, patternLabelPosLow, text="Morning Doji Star", style=label.style_label_up, color = label_color_bullish, textcolor=color.rgb(0, 0, 0), tooltip = ttBullishMorningDojiStar)
bgcolor(ta.highest(C_MorningDojiStarBullish?1:0, C_MorningDojiStarBullishNumberOfCandles)!=0 ? color.new(#25fc41, 90) : na, offset=-(C_MorningDojiStarBullishNumberOfCandles-1))

Output

Overview of the script 

1. Script Header

//@version=5
indicator("Morning Doji Star - Bullish", shorttitle = "Morning Doji Star", overlay=true)
  • The script uses version 5 of Pine Script.
  • It defines a custom indicator called “Morning Doji Star – Bullish”, with a shorter title for display purposes, and it will overlay on the price chart.

2. Trend Detection Settings

C_DownTrend = true
C_UpTrend = true
var trendRule1 = "SMA50"
var trendRule2 = "SMA50, SMA200"
var trendRule = input.string(trendRule1, "Detect Trend Based On", options=[trendRule1, trendRule2, "No detection"])

if trendRule == trendRule1
    priceAvg = ta.sma(close, 50)
    C_DownTrend := close < priceAvg
    C_UpTrend := close > priceAvg

if trendRule == trendRule2
    sma200 = ta.sma(close, 200)
    sma50 = ta.sma(close, 50)
    C_DownTrend := close < sma50 and sma50 < sma200
    C_UpTrend := close > sma50 and sma50 > sma200
  • The script initializes two variables, C_DownTrend and C_UpTrend, to detect the market trend.
  • Users can choose the trend detection method: either based on a 50-period Simple Moving Average (SMA) or a combination of 50-period and 200-period SMAs.
  • The trend is defined as downward (C_DownTrend) or upward (C_UpTrend) based on the closing price relative to the SMA(s).

3. Candlestick Properties

C_Len = 14 // ta.ema depth for bodyAvg
C_ShadowPercent = 5.0 // size of shadows
C_ShadowEqualsPercent = 100.0
C_DojiBodyPercent = 5.0
C_Factor = 2.0 // shows the number of times the shadow dominates the candlestick body

C_BodyHi = math.max(close, open)
C_BodyLo = math.min(close, open)
C_Body = C_BodyHi - C_BodyLo
C_BodyAvg = ta.ema(C_Body, C_Len)
C_SmallBody = C_Body < C_BodyAvg
C_LongBody = C_Body > C_BodyAvg
C_UpShadow = high - C_BodyHi
C_DnShadow = C_BodyLo - low
C_HasUpShadow = C_UpShadow > C_ShadowPercent / 100 * C_Body
C_HasDnShadow = C_DnShadow > C_ShadowPercent / 100 * C_Body
C_WhiteBody = open < close
C_BlackBody = open > close
C_Range = high-low
C_IsInsideBar = C_BodyHi[1] > C_BodyHi and C_BodyLo[1] < C_BodyLo
C_BodyMiddle = C_Body / 2 + C_BodyLo
C_ShadowEquals = C_UpShadow == C_DnShadow or (math.abs(C_UpShadow - C_DnShadow) / C_DnShadow * 100) < C_ShadowEqualsPercent and (math.abs(C_DnShadow - C_UpShadow) / C_UpShadow * 100) < C_ShadowEqualsPercent
C_IsDojiBody = C_Range > 0 and C_Body <= C_Range * C_DojiBodyPercent / 100
C_Doji = C_IsDojiBody and C_ShadowEquals
  • C_Len = 14: Sets the length for calculating the Exponential Moving Average (EMA) of the candlestick body sizes. This EMA is used for average size comparison.
  • C_ShadowPercent = 5.0: Defines the minimum size of a candle’s shadow as a percentage of its body to consider it significant.
  • C_ShadowEqualsPercent = 100.0: Used in defining a Doji candlestick (where the upper and lower shadows are approximately equal).
  • C_DojiBodyPercent = 5.0: Defines the maximum body size of a Doji candlestick as a percentage of the total candlestick range.
  • C_Factor = 2.0: Indicates the number of times the shadow length must exceed the candlestick body to be considered dominant.
  • C_BodyHi and C_BodyLo: Determine the high and low points of the candlestick body.
  • C_Body: Calculates the size of the candlestick body.
  • C_BodyAvg: Calculates the EMA of the candlestick body size over C_Len periods.
  • C_SmallBody and C_LongBody: Determine if the candlestick has a small or long body compared to the average.
  • C_UpShadow and C_DnShadow: Calculate the lengths of the upper and lower shadows of the candlestick.
  • C_HasUpShadow and C_HasDnShadow: Determine if the candlestick has significant upper or lower shadows based on C_ShadowPercent.
  • C_WhiteBody and C_BlackBody: Identify if the candlestick is bullish (white/green) or bearish (black/red).
  • C_Range: Calculates the total range of the candlestick (from high to low).
  • C_ShadowEquals: Determines if the upper and lower shadows are approximately equal in length, used for identifying Doji candles.
  • C_IsDojiBody: Checks if the candlestick qualifies as a Doji based on the body size and total range.
  • C_Doji: Final condition to identify if a candlestick is a Doji, combining the body size and shadow equality conditions.

4. Pattern Detection Logic

C_MorningDojiStarBullishNumberOfCandles = 3
C_MorningDojiStarBullish = false
if C_LongBody[2] and C_IsDojiBody[1] and C_LongBody and C_DownTrend and C_BlackBody[2] and C_BodyHi[1] < C_BodyLo[2] and C_WhiteBody and C_BodyHi >= C_BodyMiddle[2] and C_BodyHi < C_BodyHi[2] and C_BodyHi[1] < C_BodyLo
    C_MorningDojiStarBullish := true
  • This section defines the logic for detecting the Morning Doji Star pattern: a long bearish body followed by a Doji, and then a long bullish body, all under a downtrend condition.

5. Alert Condition

alertcondition(C_MorningDojiStarBullish, title = "New pattern detected", message = "New Morning Doji Star – Bullish pattern detected")
  • This line sets up an alert condition in TradingView. When the Morning Doji Star pattern is detected, it triggers an alert with the specified title and message. This feature is useful for traders who want to be notified in real-time when the pattern occurs.

6. Labeling the Pattern





  • When the Morning Doji Star pattern is identified (C_MorningDojiStarBullish is true), the script creates a new label on the chart.
  • var ttBullishMorningDojiStar stores a descriptive tooltip for the label, explaining the pattern.
  • label.new(...): This function creates the label at the specified position with the set style, color, and tooltip. The label indicates the occurrence of the Morning Doji Star pattern, providing a visual cue on the chart

7. Background Coloring

bgcolor(ta.highest(C_MorningDojiStarBullish?1:0, C_MorningDojiStarBullishNumberOfCandles)!=0 ? color.new(#25fc41, 90) : na, offset=-(C_MorningDojiStarBullishNumberOfCandles-1))
  • The bgcolor function is used to change the background color of the chart for the duration of the pattern. This visual cue makes it easier to spot the pattern.
  • ta.highest(...) checks if the Morning Doji Star pattern has occurred within the last few candles (as defined by C_MorningDojiStarBullishNumberOfCandles).
  • The background color is set to a semi-transparent green if the pattern is detected; otherwise, it remains unchanged (na).

Frequently Asked Questions

What does this Pine Script specifically identify?

It identifies the Morning Doji Star Candlestick Pattern, a bullish reversal indicator following a downtrend.

Can this script be applied to any financial market or timeframe?

Yes, it’s versatile and can be used across various markets, including stocks and forex, and in different timeframes.

Does the script include any alert system for pattern detection?

Yes, it has an alert condition that notifies users when the Morning Doji Star pattern is detected on the chart.

Is it possible to adjust the criteria for pattern recognition in the script?

Yes, the script includes parameters like shadow percent and body length that can be customized to alter pattern sensitivity.

How does the script visually indicate the presence of the pattern on the chart?

It marks the pattern with a label and changes the background color, providing clear visual cues on the chart.

Conclusion


This Pine Script code identifies the Morning Doji Star Candlestick Pattern, a crucial indicator for bullish reversals in financial markets. By utilizing a combination of candlestick analysis and trend recognition, the script offers a reliable tool for traders and analysts. Its customizable parameters and real-time alert system enhance its utility, making it adaptable to various trading styles and strategies. The script not only aids in accurate pattern recognition but also serves as an educational resource for those learning technical analysis and Pine Script programming.

Leave a Comment