Home » Candlestick Patterns » Detecting Bullish Abandoned Baby Candlestick Pattern in Pine Script

Detecting Bullish Abandoned Baby Candlestick Pattern in Pine Script

Photo of author
Published on

Overview of Bullish Abandoned Baby Candlestick Pattern

The Bullish Abandoned Baby is a rare but significant candlestick pattern used in technical analysis to predict a potential reversal in a downtrend. It consists of three key components:

  1. Strong Downward Candle: The pattern begins with a strong bearish candle, indicating the continuation of the existing downtrend.
  2. Gapped Down Doji: The second candle is a doji—a candle with a small or virtually nonexistent body—indicating indecision. This candle gaps down from the first candle, meaning it opens lower than the previous close.
  3. Strong Bullish Candle: The final candle is a strong bullish one, opening with a gap up from the doji. This suggests a sudden shift in market sentiment from bearish to bullish.

Importance in Technical Analysis

In technical analysis, the Bullish Abandoned Baby candlestick pattern holds significant importance for several reasons:

  1. Reversal Indicator: It is considered a reliable indicator of a potential reversal in market trends. After a period of downward movement, this pattern suggests a strong likelihood of an upcoming bullish phase.
  2. Market Sentiment Shift: The pattern reflects a dramatic shift in market sentiment. Initially, the market is bearish, as indicated by the strong downward candle. However, the appearance of a doji signifies uncertainty and a pause in selling pressure. Finally, the strong bullish candle indicates a shift towards buying interest, suggesting that bulls have taken control.
  3. Confirmation of Trend Change: The gaps on either side of the doji reinforce the strength of the reversal signal. These gaps indicate a significant change in market dynamics and are considered more reliable than patterns without gaps.
  4. Trading Decisions: Traders often use this pattern to make informed decisions about entering long positions. The Bullish Abandoned Baby can serve as a cue to buy or close short positions, especially when complemented by other technical indicators.
  5. Risk Management: Recognizing this pattern helps in risk management, as it provides a clear signal of a changing trend. Traders can adjust their stop-loss orders accordingly to minimize potential losses.

Example From Trading View

Example From Trading View

Defining the Bullish Abandoned Baby Candlestick Pattern Criteria 

The Bullish Abandoned Baby candlestick pattern is a specific formation in technical analysis that signifies a potential bullish reversal. To accurately identify this pattern, several criteria must be met:

  1. Preceding Downtrend: The pattern appears during a downtrend in the market. It’s essential that the pattern follows a period of bearish sentiment, as it is a reversal pattern.
  2. First Candle – Bearish: The first candle of the pattern is a large bearish (downward) candle. This candle is an indication of strong selling pressure and confirms the ongoing downtrend.
  3. Second Candle – Doji: The middle candle is a doji, a candlestick with a very small or almost nonexistent body. The doji represents a state of indecision in the market. It should gap down from the first candle, meaning its opening price is lower than the closing price of the first candle, creating a gap.
  4. Third Candle – Bullish: The final candle is a large bullish (upward) candle. It’s essential that this candle gaps up from the doji, meaning its opening price is higher than the closing price of the doji. This gap up is a key element, as it suggests a sudden shift in market sentiment.

Code For Detecting Bullish Abandoned Baby

//@version=5
indicator("Abandoned Baby - Bullish", shorttitle = "Abandoned Baby - Bull", 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.blue, "Label Color Bullish")
C_AbandonedBabyBullishNumberOfCandles = 3
C_AbandonedBabyBullish = C_DownTrend[2] and C_BlackBody[2] and C_IsDojiBody[1] and low[2] > high[1] and C_WhiteBody and high[1] < low
alertcondition(C_AbandonedBabyBullish, title = "New pattern detected", message = "New Abandoned Baby – Bullish pattern detected")
if C_AbandonedBabyBullish
    var ttBullishAbandonedBaby = "Abandoned Baby\nThis candlestick pattern is quite rare as far as reversal patterns go. The first of the pattern is a large down candle. Next comes a doji candle that gaps below the candle before it. The doji candle is then followed by another candle that opens even higher and swiftly moves to the upside."
    label.new(bar_index, patternLabelPosHigh, text="Abandonrd Baby", style=label.style_label_down, color = color.green, textcolor=color.white, tooltip = ttBullishAbandonedBaby)
bgcolor(ta.highest(C_AbandonedBabyBullish?1:0, C_AbandonedBabyBullishNumberOfCandles)!=0 ? color.new(#1ff81f, 90) : na, offset=-(C_AbandonedBabyBullishNumberOfCandles-1))

Output

Output

Overview of the script 

1. Indicator Declaration

//@version=5
indicator("Abandoned Baby - Bullish", shorttitle = "Abandoned Baby - Bull", overlay=true)
  • //@version=5: Specifies that this script uses version 5 of Pine Script.
  • indicator(...): Declares a new indicator. “Abandoned Baby – Bullish” is the name, with a short title for display. overlay=true ensures that this indicator is plotted directly on the price chart.

2. Trend Detection Configuration

var trendRule1 = "SMA50"
var trendRule2 = "SMA50, SMA200"
var trendRule = input.string(trendRule1, "Detect Trend Based On", options=[trendRule1, trendRule2, "No detection"])
  • Here, two trend rules (trendRule1, trendRule2) are defined using Simple Moving Averages (SMA). The user can choose between these rules or opt for no trend detection. This choice is made through a dropdown menu created by input.string.

3. Simple Moving Average (SMA) Calculations for Trend

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
  • Depending on the chosen trend rule, the script calculates the trend. For trendRule1, it uses a 50-day SMA. For trendRule2, it uses both 50-day and 200-day SMAs. The downtrend and uptrend conditions are set based on these calculations.

4. Candlestick Feature Definitions

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
  1. EMA and Candle Body Size Calculation:
    • C_Len = 14: Sets the length for calculating the Exponential Moving Average (EMA) of the candle body sizes.
    • C_BodyHi, C_BodyLo, and C_Body: Calculate the high, low, and size of the candle body.
  2. Average Body Size:
    • C_BodyAvg = ta.ema(C_Body, C_Len): Calculates the average body size over the specified period (14 candles). This average is used to determine what constitutes a “small” or “long” body.
  3. Shadow (Wick) Analysis:
    • C_UpShadow and C_DnShadow: Calculate the size of the upper and lower shadows (wicks) of the candle.
    • C_HasUpShadow and C_HasDnShadow: Check whether the candle has significant upper or lower shadows based on the C_ShadowPercent threshold.
  4. Candle Color:
    • C_WhiteBody and C_BlackBody: Determine if the candle is bullish (white) or bearish (black) based on the opening and closing prices.
  5. Doji Identification:
    • C_IsDojiBody: Determines if the body of the candle is small enough to be considered a doji, a key component in the Bullish Abandoned Baby pattern.
    • C_Doji: Final check to confirm if a candle is a doji, considering both the body size and the equality of the shadows.

5. Bullish Abandoned Baby Pattern Detection

C_AbandonedBabyBullish = C_DownTrend[2] and C_BlackBody[2] and C_IsDojiBody[1] and low[2] > high[1] and C_WhiteBody and high[1] < low

This is the heart of the script. It checks if the current and previous two candles satisfy the criteria for a Bullish Abandoned Baby:

  • C_DownTrend[2]: The first candle (two periods ago) is in a downtrend.
  • C_BlackBody[2]: The first candle is bearish.
  • C_IsDojiBody[1]: The second candle is a doji.
  • low[2] > high[1]: There’s a gap down between the first and the doji candle.
  • C_WhiteBody: The third candle is bullish.
  • high[1] < low: There’s a gap up between the doji and the third candle.

6. Alert Setup and Chart Labeling

alertcondition(C_AbandonedBabyBullish, title = "New pattern detected", message = "New Abandoned Baby – Bullish pattern detected")
if C_AbandonedBabyBullish
    label.new(bar_index, patternLabelPosHigh, text="Abandonrd Baby", style=label.style_label_down, color = color.green, textcolor=color.white, tooltip = ttBullishAbandonedBaby)
  • Sets up an alert condition when the pattern is detected. If the pattern is present, it creates a new label on the chart at the high position of the current bar.

7. Background Coloring

bgcolor(ta.highest(C_AbandonedBabyBullish?1:0, C_AbandonedBabyBullishNumberOfCandles)!=0 ? color.new(#1ff81f, 90) : na, offset=-(C_AbandonedBabyBullishNumberOfCandles-1))
  • This line changes the background color of the chart for the duration of the pattern. It highlights when the pattern is present, making it visually easier to spot.

Frequently Asked Questions

What is the purpose of this Pine Script code?

This code is designed to identify the Bullish Abandoned Baby candlestick pattern in financial trading charts. It automatically detects this pattern, which is often used by traders as a potential signal of a trend reversal from bearish to bullish.

How does the code determine the trend before identifying the pattern?

The code allows users to select from different methods to determine the trend, using either a single 50-day Simple Moving Average (SMA) or a combination of 50-day and 200-day SMAs. The trend is crucial for the pattern to be valid, as the Bullish Abandoned Baby must occur during a downtrend.

What are the key conditions for the Bullish Abandoned Baby pattern in this script?

The script checks for a sequence of three specific candles: a large bearish candle, followed by a doji that gaps down from the first candle, and finally a bullish candle that gaps up from the doji. These candles should appear in a downtrend (as defined by the user’s selection of trend determination).

Does the script provide any visual indicators or alerts when the pattern is identified?

Yes, the script creates a label on the chart to indicate the presence of the Bullish Abandoned Baby pattern and changes the background color of the chart for visual emphasis. It also includes an alert condition, allowing traders to receive notifications when the pattern is detected.

Can this Pine Script be customized for different trading strategies or preferences?

Absolutely. Traders can modify the length of the moving averages used for trend detection, adjust the thresholds for determining candlestick features like body size and shadows, and change label colors. This flexibility allows the script to be adapted to various trading styles and preferences.

Conclusion

The Pine Script code presented is specifically designed to detect the Bullish Abandoned Baby candlestick pattern, a notable pattern in technical analysis signaling a potential bullish reversal after a downtrend. The script’s effectiveness lies in its meticulous approach to identifying the distinct elements of this pattern: a bearish candle followed by a gapped-down doji, and then a bullish candle gapping up. This sequence is indicative of a shift in market sentiment from bearish to bullish. Key strengths of the script include its capability to adjust trend detection methods, allowing traders to align the pattern recognition with their trading strategies and market conditions. Additionally, the code’s detailed criteria for candlestick analysis ensure a high degree of accuracy in pattern identification.

Leave a Comment