Home » Candlestick Patterns » Detecting Piercing Line Candlestick Pattern in Pine Script

Detecting Piercing Line Candlestick Pattern in Pine Script

Photo of author
Published on

Overview of Piercing Line Candlestick Pattern

The Piercing Line candlestick pattern is a notable formation in technical analysis, often indicative of a potential bullish reversal, especially when observed in a downtrend context. This two-candlestick pattern is recognized for its ability to signal a shift in market momentum from bearish to bullish. Here’s an overview of its characteristics:

Piercing Line
  1. Occurrence in Downtrend:
    • The pattern typically appears during a downtrend, highlighting its potential as a reversal signal.
  2. First Candlestick:
    • This is a large bearish candle, aligning with the prevailing downtrend and indicating continued selling pressure.
  3. Second Candlestick:
    • The following day opens lower, often creating a gap down, but reverses to close significantly into the body of the first day’s bearish candle. The ideal scenario is for the second candle to close above the midpoint of the first candle’s body.

Importance in Technical Analysis

The Piercing Line candlestick pattern holds significant importance in technical analysis for several reasons, making it a valuable tool for traders and analysts:

  1. Bullish Reversal Signal: It is primarily recognized as a bullish reversal indicator, particularly useful in spotting potential shifts in trend from bearish to bullish, especially after a sustained downtrend.
  2. Market Sentiment Indicator: The pattern reflects a substantial change in market sentiment. The strong bullish close on the second day, after a gap-down opening, indicates a shift from bearish dominance to growing bullish momentum.
  3. Enhances Decision-Making: For traders who rely on candlestick patterns, the Piercing Line can inform entry points for long positions or exiting short positions, thereby aiding in strategic decision-making.
  4. Volume Confirmation: The significance of the pattern is often confirmed by increased trading volume on the second day, adding to its reliability as a reversal signal.
  5. Complements Other Analysis Tools: When combined with other technical indicators such as moving averages, RSI, or MACD, the Piercing Line pattern can provide a more comprehensive view of market conditions.

Example From Trading View

Example From Trading View

Defining the Piercing Line Candlestick Pattern Criteria

The Piercing Line candlestick pattern is a notable formation in technical analysis, particularly as a potential indicator of bullish reversals. Here are the defining criteria for the Piercing Line candlestick pattern:

  1. Prevailing Trend:
    • The pattern should occur within the context of a downtrend. Its significance is heightened when it appears after a notable decline in prices.
  2. First Candlestick:
    • Large Bearish Candle: The first candle in the pattern is a large bearish candlestick, signifying strong selling pressure and continuation of the downtrend.
  3. Second Candlestick:
    • Bullish Candle: The second candle is bullish (closing price higher than opening price).
    • Opening Gap Down: It opens lower than the close of the first candle, often creating a gap.
    • Substantial Close: The bullish candle should close significantly into the body of the first candle. Ideally, it should close above the midpoint of the first candle’s body, covering at least 50% of its range.

Code For Detecting Piercing Line

//@version=5
indicator("Piercing - Bullish", shorttitle = "Piercing - Bull", overlay=true)
// Trend detection variables
C_DownTrend = true
C_UpTrend = true
var trendRule1 = "SMA50"
var trendRule2 = "SMA50, SMA200"
// User input for trend detection method
var trendRule = input.string(trendRule1, "Detect Trend Based On", options=[trendRule1, trendRule2, "No detection"])
// SMA50 trend detection
if trendRule == trendRule1
	priceAvg = ta.sma(close, 50)
	C_DownTrend := close < priceAvg
	C_UpTrend := close > priceAvg
// SMA50 and SMA200 trend detection
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
// Candlestick analysis variables
C_Len = 14 // EMA depth for bodyAvg
C_ShadowPercent = 5.0 // Size of shadows as a percentage
C_ShadowEqualsPercent = 100.0 // Percentage to consider shadows equal
C_DojiBodyPercent = 5.0 // Body size as a percentage to consider a Doji
C_Factor = 2.0 // Factor for shadow dominance over the body
// Candlestick body and shadow calculations
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
// Positioning for labels
patternLabelPosLow = low - (ta.atr(30) * 0.6)
patternLabelPosHigh = high + (ta.atr(30) * 0.6)
// Color settings for labels
label_color_bullish = input(color.blue, "Label Color Bullish")
// Piercing Line pattern detection logic
C_PiercingBullishNumberOfCandles = 2
C_PiercingBullish = false
if (C_DownTrend[1] and C_BlackBody[1] and C_LongBody[1]) and (C_WhiteBody and open <= low[1] and close > C_BodyMiddle[1] and close < open[1])
	C_PiercingBullish := true
// Alert condition for the pattern
alertcondition(C_PiercingBullish, title = "New pattern detected", message = "New Piercing – Bullish pattern detected")
// Label plotting for detected pattern
if C_PiercingBullish
    var ttBullishPiercing = "Piercing\nPiercing is a two-candle bullish reversal candlestick pattern found in a downtrend. The first candle is red and has a larger than average body. The second candle is green and opens below the low of the prior candle, creating a gap, and then closes above the midpoint of the first candle. The pattern shows a possible shift in the momentum from the downside to the upside, indicating that a reversal might happen soon."
    label.new(bar_index, patternLabelPosLow, text="Piercing", style=label.style_label_up, color = label_color_bullish, textcolor=color.white, tooltip = ttBullishPiercing)
// Background color change for the pattern
bgcolor(ta.highest(C_PiercingBullish?1:0, C_PiercingBullishNumberOfCandles)!=0 ? color.new(#64b9ff, 90) : na, offset=-(C_PiercingBullishNumberOfCandles-1))

Output

Output

Overview of the script 

1. Indicator Setup

//@version=5
indicator("Piercing - Bullish", shorttitle = "Piercing - Bull", overlay=true)
  • Specifies Pine Script version 5.
  • Declares a new indicator named “Piercing – Bullish” that overlays on the price chart.

2. Trend Detection Variables

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"])
  • Initializes trend variables and sets options for trend detection methods (using SMA50, SMA50 and SMA200, or no trend detection).

3. Simple Moving Average (SMA) Trend Determination:

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

// For SMA50 and SMA200
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
  • If SMA50 is selected, the script checks if the price is below (downtrend) or above (uptrend) the 50-period SMA.
  • If both SMA50 and SMA200 are selected, the script determines the trend based on the position of the close relative to these SMAs.

4. Candlestick Analysis Configuration

C_Len = 14 // EMA depth for bodyAvg
C_ShadowPercent = 5.0 // Size of shadows as a percentage
C_ShadowEqualsPercent = 100.0 // Percentage to consider shadows equal
C_DojiBodyPercent = 5.0 // Body size as a percentage to consider a Doji
C_Factor = 2.0 // Factor for shadow dominance over the 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
  • Candlestick Body and Shadow Calculations:
    • C_BodyHi and C_BodyLo calculate the high and low of the candlestick’s body.
    • C_Body computes the size of the candlestick’s body.
    • C_BodyAvg calculates the average size of candlestick bodies over a specified period (14 days in this case, using EMA).
    • C_SmallBody and C_LongBody determine whether a candlestick has a small or long body relative to the average.
    • C_UpShadow and C_DnShadow calculate the size of the upper and lower shadows of the candlestick.
    • C_HasUpShadow and C_HasDnShadow determine whether the candlestick has significant upper or lower shadows.
    • C_WhiteBody and C_BlackBody are boolean flags identifying if the candlestick is bullish (white) or bearish (black).
  • Additional Candlestick Characteristics:
    • C_Range represents the total range of the candlestick (high to low).
    • C_IsInsideBar checks if the current candlestick is an inside bar relative to the previous candlestick.
    • C_BodyMiddle calculates the midpoint of the candlestick’s body.
    • C_ShadowEquals checks if the upper and lower shadows are approximately equal, a condition used in identifying Doji candlesticks.
    • C_IsDojiBody and C_Doji determine if the candlestick qualifies as a Doji, based on the relative size of its body and equality of shadows.

5. Label Positioning Calculation:

patternLabelPosLow = low - (ta.atr(30) * 0.6)
patternLabelPosHigh = high + (ta.atr(30) * 0.6)
  • Calculates the position for placing labels based on the Average True Range (ATR).

6. Label Color Setting:

label_color_bullish = input(color.blue, "Label Color Bullish")

Sets the color for bullish labels (in this case, blue).

7. Piercing Line Pattern Detection Logic:

C_PiercingBullishNumberOfCandles = 2
C_PiercingBullish = false
if (C_DownTrend[1] and C_BlackBody[1] and C_LongBody[1]) and (C_WhiteBody and open <= low[1] and close > C_BodyMiddle[1] and close < open[1])
    C_PiercingBullish := true
  • Defines the logic for detecting the Piercing Line pattern. It checks specific conditions over two candles to confirm the pattern, particularly focusing on the color and size of these candles and their relative opening and closing positions.

8. Pattern Alert Condition:

alertcondition(C_PiercingBullish, title = "New pattern detected", message = "New Piercing – Bullish pattern detected")
  • Creates an alert condition that triggers when the Piercing Line pattern is detected.

9. Label Plotting for Detected Pattern:

if C_PiercingBullish
    var ttBullishPiercing = "Piercing\nPiercing is a two-candle bullish reversal candlestick pattern found in a downtrend..."
    label.new(bar_index, patternLabelPosLow, text="Piercing", style=label.style_label_up, color = label_color_bullish, textcolor=color.white, tooltip = ttBullishPiercing)
  • If the Piercing Line pattern is detected, a label is plotted on the chart with a description of the pattern.

10. Background Color Change for Detected Pattern:

bgcolor(ta.highest(C_PiercingBullish?1:0, C_PiercingBullishNumberOfCandles)!=0 ? color.new(#64b9ff, 90) : na, offset=-(C_PiercingBullishNumberOfCandles-1))
  • Changes the background color of the chart to visually emphasize the detected pattern.

Frequently Asked Questions

What is the main purpose of this Pine Script?

This script is designed to identify and visually highlight Piercing Line candlestick patterns in a downtrend on TradingView charts, indicating potential bullish reversals.

Can the trend detection method in the script be customized?

Yes, the script allows users to choose between different trend detection methods – SMA50, SMA50 and SMA200 combined, or no trend detection – through the user input options.

How does the script determine if a candlestick is part of a downtrend or uptrend?

The script uses Simple Moving Averages (SMA50 and/or SMA200) to determine the trend. A candlestick is considered part of a downtrend if it closes below these averages and part of an uptrend if it closes above them.

What does the script do once it identifies a Piercing Line pattern?

When the script detects a Piercing Line pattern, it places a blue label on the chart at the low of the pattern’s second candlestick. It also triggers an alert and changes the background color for visual emphasis.

Is this script suitable for all types of traders?

Yes, both novice and experienced traders can use this script. It simplifies the process of identifying Piercing Line patterns, which is helpful for those learning technical analysis, while also serving as a quick visual aid for seasoned traders.

Conclusion

The “Piercing Line” is specifically designed to identify Piercing Line candlestick patterns. This script enhances technical analysis by automatically highlighting potential bullish reversal signals in downtrends, making it easier for traders to spot significant changes in market sentiment. With customizable trend detection options and visual alerts, it serves as a valuable asset for both novice and experienced traders looking to refine their trading strategies and decision-making processes.

Leave a Comment