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

Detecting Evening Doji Star Candlestick Pattern in Pine Script

Photo of author
Published on

Overview of Evening Doji Star Candlestick Pattern

The Evening Doji Star is a bearish reversal candlestick pattern that typically appears at the top of an uptrend, signaling a potential reversal to a downtrend. It’s a three-part formation characterized by the following:

Evening Doji Star Candlestick Pattern
  1. Strong Uptrend Start: The pattern starts with a large bullish candle, indicating the presence of strong buying pressure and continuation of the uptrend.
  2. Appearance of Uncertainty: The second candle is a Doji, a candle with a small body, indicating indecision in the market. This Doji opens at or above the close of the previous candle, representing a gap in an upward direction, which is a sign of weakening upward momentum.
  3. Bearish Confirmation: The third candle is a large bearish one that opens below the Doji and closes well into the body of the first candle. This suggests a strong shift from buying to selling pressure, confirming the potential for a trend reversal.

Importance in Technical Analysis

In technical analysis, the Evening Doji Star candlestick pattern holds significant importance due to its ability to signal a potential reversal from an uptrend to a downtrend. Its relevance in technical analysis can be outlined as follows:

  1. Reversal Signal: The Evening Doji Star is a powerful indicator of a trend reversal. This pattern alerts traders to a possible shift from bullish to bearish sentiment, enabling them to prepare for a change in market direction.
  2. High Reliability: Among various candlestick patterns, the Evening Doji Star is considered highly reliable, especially when it occurs after a strong uptrend and is accompanied by high trading volume. Its distinct formation makes it easier to identify and act upon.
  3. Decision-Making Tool: Traders use this pattern to make informed decisions, such as closing long positions or initiating short positions. It provides a visual representation of market sentiment shifting from optimism to uncertainty and then to pessimism.
  4. Risk Management: Identifying the Evening Doji Star pattern helps traders in risk management. It serves as a warning sign to tighten stop losses or avoid entering new long positions, thereby protecting from potential market downturns.
  5. Confirmation with Other Indicators: While the Evening Doji Star is a strong signal on its own, its effectiveness increases when used in conjunction with other technical analysis tools, such as support/resistance levels, moving averages, or momentum indicators. This helps in confirming the trend reversal and filtering out false signals.

Example From Trading View

Example From Trading View

Defining the Evening Doji Star Candlestick Pattern Criteria 


The Evening Doji Star candlestick pattern is a bearish reversal pattern with specific criteria that define its formation. It typically occurs at the end of an uptrend and signals a potential shift to a downtrend. The criteria for this pattern are as follows:

  1. Uptrend Precedence: The pattern must form during a prevailing uptrend. This is crucial as it represents a potential shift in market sentiment from bullish to bearish.
  2. First Candle: The pattern begins with a large bullish candlestick. This candlestick should be a clear representation of the ongoing uptrend, characterized by a long body that indicates strong buying pressure.
  3. Second Candle – The Doji: The second candle is a Doji, which is a candlestick with a very small body. The Doji shows that the opening and closing prices are nearly the same, signifying market indecision. This candle should gap up from the first candle, emphasizing the uncertainty after a strong bullish move.
  4. Third Candle – Bearish Shift: The third candle is a bearish one with a long body. It should open below the Doji (second candle) and close well into the body of the first candle. This suggests a shift in control from the bulls to the bears.

Code For Detecting Evening Doji Star

//@version=5
indicator("Evening Doji Star - Bearish", shorttitle = "Evening 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_bearish = input(color.red, "Label Color Bearish")
C_EveningDojiStarBearishNumberOfCandles = 3
C_EveningDojiStarBearish = false
if C_LongBody[2] and C_IsDojiBody[1] and C_LongBody and C_UpTrend and C_WhiteBody[2] and C_BodyLo[1] > C_BodyHi[2] and C_BlackBody and C_BodyLo <= C_BodyMiddle[2] and C_BodyLo > C_BodyLo[2] and C_BodyLo[1] > C_BodyHi
	C_EveningDojiStarBearish := true
alertcondition(C_EveningDojiStarBearish, title = "New pattern detected", message = "New Evening Doji Star – Bearish pattern detected")
if C_EveningDojiStarBearish
    var ttBearishEveningDojiStar = "Evening Doji Star\nThis candlestick pattern is a variation of the Evening Star pattern. It is bearish and continues an uptrend with a long-bodied, green candle day. It is then followed by a gap and a Doji candle and concludes with a downward close. The close would be below the first day’s midpoint. It is more bearish than the regular evening star pattern because of the existence of the Doji."
    label.new(bar_index, patternLabelPosHigh, text="Evening Doji Star", style=label.style_label_down, color = label_color_bearish, textcolor=color.white, tooltip = ttBearishEveningDojiStar)
bgcolor(ta.highest(C_EveningDojiStarBearish?1:0, C_EveningDojiStarBearishNumberOfCandles)!=0 ? color.new(color.red, 90) : na, offset=-(C_EveningDojiStarBearishNumberOfCandles-1))

Output

Overview of the script 

1. Indicator Declaration

//@version=5
indicator("Evening Doji Star - Bearish", shorttitle = "Evening Doji Star", overlay=true)
  • //@version=5: Specifies the version of Pine Script (version 5).
  • indicator(...): Declares a new indicator named “Evening Doji Star – Bearish”. overlay=true means the indicator is drawn over the price chart.

2. Trend Determination

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"])
  • Sets initial trend values and defines trend detection rules based on moving averages (SMA50 and SMA50, SMA200).

3. Trend Calculation Based on Moving Averages

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
  • This section calculates the trend based on Simple Moving Averages (SMA). The user selects one of two trend detection methods via the trendRule input.
  • trendRule1 ("SMA50"): Considers the 50-period SMA. If the close price is below the SMA, it’s a downtrend (C_DownTrend), and if above, it’s an uptrend (C_UpTrend).
  • trendRule2 ("SMA50, SMA200"): Uses both 50-period and 200-period SMAs. A downtrend is indicated when the close is below the 50-period SMA and the 50-period SMA is below the 200-period SMA. Conversely, an uptrend is indicated when the close is above the 50-period SMA and the 50-period SMA is above the 200-period SMA.

4. Candlestick Pattern Criteria Variables

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
  • This section sets up various parameters and thresholds for analyzing the candlestick pattern.
  • C_Len: Defines the length for calculating the Exponential Moving Average (EMA) of the candlestick body.
  • C_ShadowPercent: Determines the minimum size of a candle’s shadow as a percentage of its body to be considered significant.
  • C_ShadowEqualsPercent: Used to determine if the upper and lower shadows of a candle are approximately equal.
  • C_DojiBodyPercent: Defines the maximum body size relative to the entire candle range for a candle to be considered a Doji.
  • C_Factor: Represents a threshold for how much larger the shadow needs to be compared to the body to signify a specific candlestick feature.

5. Evening Doji Star Pattern Logic

C_EveningDojiStarBearish = false
if C_LongBody[2] and C_IsDojiBody[1] and C_LongBody and C_UpTrend and...
  • This section sets the logic for identifying the Evening Doji Star pattern. It checks the conditions of the last three candles to match the pattern’s criteria.

6. Alert Condition

alertcondition(C_EveningDojiStarBearish, title = "New pattern detected", message = "New Evening Doji Star – Bearish pattern detected")
  • This line of code sets an alert condition based on the C_EveningDojiStarBearish variable.
  • When the C_EveningDojiStarBearish condition becomes true (i.e., when the Evening Doji Star pattern is detected), an alert is triggered.
  • The title and message parameters define the title and the content of the alert, respectively. In this case, the alert notifies the user of the detection of a new “Evening Doji Star – Bearish” pattern.

7. Label Creation and Background Coloring

if C_EveningDojiStarBearish
    var ttBearishEveningDojiStar = "Evening Doji Star\nThis candlestick pattern is a variation of the Evening Star pattern. It is bearish and continues an uptrend with a long-bodied, green candle day. It is then followed by a gap and a Doji candle and concludes with a downward close. The close would be below the first day’s midpoint. It is more bearish than the regular evening star pattern because of the existence of the Doji."
    label.new(bar_index, patternLabelPosHigh, text="Evening Doji Star", style=label.style_label_down, color = label_color_bearish, textcolor=color.white, tooltip = ttBearishEveningDojiStar)
bgcolor(ta.highest(C_EveningDojiStarBearish?1:0, C_EveningDojiStarBearishNumberOfCandles)!=0 ? color.new(color.red, 90) : na, offset=-(C_EveningDojiStarBearishNumberOfCandles-1))
  • This section executes when the C_EveningDojiStarBearish condition is true.
  • label.new(...): Creates a new label on the chart at the position of the detected pattern. The bar_index and patternLabelPosHigh specify the location of the label. The label displays “Evening Doji Star” with a specified style, color (label_color_bearish), and text color (white). The tooltip parameter provides additional information about the pattern when hovered over.
  • bgcolor(...): Changes the background color of the chart to visually indicate the detection of the pattern. This color change applies to the bars where the pattern is found. color.new(color.red, 90)creates a new color (red in this case, with 90% transparency) for the background. The bgcolor function is conditioned on whether the Evening Doji Star pattern is detected within the last C_EveningDojiStarBearishNumberOfCandles candles. If the pattern is detected, the background color is set; otherwise, it remains unchanged (na for no color).

Frequently Asked Questions

What is the Purpose of This Pine Script Code?

This code is designed to identify the Evening Doji Star candlestick pattern on financial market charts. It’s a technical analysis tool used in TradingView to visually signal a potential bearish reversal in an uptrend, aiding traders in decision-making.

How Does the Script Determine the Market Trend?

The script uses Simple Moving Averages (SMA) to determine the market trend. Users can select between a single 50-period SMA or a combination of 50-period and 200-period SMAs. The trend is identified as bullish or bearish based on the relationship between the closing prices and these moving averages.

What Makes a Candlestick Pattern an Evening Doji Star?

An Evening Doji Star pattern is identified by three specific candlesticks: a large bullish candle, followed by a Doji candle that gaps up, and then a large bearish candle that opens below the Doji and closes into the body of the first candle. This pattern typically indicates a potential reversal from an uptrend to a downtrend.

Does the Script Provide Alerts for the Pattern?

Yes, the script is programmed to provide alerts when the Evening Doji Star pattern is detected. This alert includes a title and a message informing the user of the pattern’s occurrence, aiding in timely decision-making.

How Does the Script Enhance Chart Visualization?

The script enhances chart visualization by creating labels and changing the background color of the bars where the pattern is detected. This visual representation makes it easier to spot the pattern at a glance, improving the user’s ability to quickly interpret chart data.

Conclusion

This script is designed for detecting the Evening Doji Star candlestick pattern. Its primary function is to automate the identification of this key pattern, which is often indicative of potential bearish reversals in an uptrend. Such automation is crucial in modern trading environments where efficiency and speed are paramount. The script offers several advantages, including automated pattern recognition, which eliminates the need for manual scanning of charts, thereby enhancing trading efficiency. It also provides customizable trend analysis options, allowing users to select from different trend determination methods (such as SMA50 or a combination of SMA50 and SMA200). This flexibility is vital for tailoring the script to individual analysis styles and preferences.

Leave a Comment