Home » Candlestick Patterns » Detecting Three Inside Down Candlestick Pattern in Pine Script

Detecting Three Inside Down Candlestick Pattern in Pine Script

Photo of author
Published on

Overview of Three Inside Down Candlestick Pattern


The Three Inside Down candlestick pattern is a bearish reversal indicator commonly used in technical analysis of financial markets. It signifies a potential shift from a bullish to bearish market sentiment and is characterized by a specific sequence of three candlesticks:

  1. First Candle: This is a large bullish candle, reflecting the continuation of the current uptrend.
  2. Second Candle: A smaller bearish candle that is completely contained within the vertical range of the first candle, including the body and wicks. Its presence suggests a weakening of the ongoing bullish trend.
  3. Third Candle: Another bearish candle that closes below the close of the second candle. This is a crucial element of the pattern, indicating a potential reversal of the bullish trend and the beginning of a bearish movement.

This pattern typically emerges at the end of an uptrend and is considered a reliable signal for a bearish reversal. Traders and investors often use it to identify potential selling opportunities in anticipation of a downward price movement. However, as with all trading patterns, it is recommended to use the Three Inside Down pattern in conjunction with other technical analysis tools and market context for more reliable decision-making.

In technical analysis, the Three Inside Down candlestick pattern holds significant importance due to several reasons:

  1. Bearish Reversal Signal: It is primarily recognized as a bearish reversal signal. This pattern occurs at the end of an uptrend and suggests a potential shift in market momentum from bullish to bearish, indicating that buying pressure is waning and sellers are beginning to dominate.
  2. Reliability and Confirmation: The Three Inside Down pattern is more reliable than single-candle patterns because it is based on a sequence of three candles. This multi-candle structure requires confirmation over multiple trading periods, reducing the chances of a false signal.
  3. Strategic Trading Decisions: It helps traders in making strategic decisions like entering a short position, placing stop-loss orders, or exiting long positions. The formation of this pattern can serve as a trigger for these actions.
  4. Complementary with Other Indicators: The pattern’s effectiveness increases when combined with other technical analysis tools, such as trend lines, oscillators, or volume indicators. This combination helps in validating the bearish reversal signal provided by the pattern.
  5. Psychological Insight: The Three Inside Down pattern reflects a shift in investor sentiment from bullish to bearish. Understanding this shift can provide valuable insights into market psychology and future price movements.

Example From Trading View

Example From Trading View

Defining the Three Inside Down Candlestick Pattern Criteria 

The Three Inside Down candlestick pattern is a technical analysis tool used to signal a potential bearish reversal. This pattern is defined by specific criteria:

  1. Prevailing Uptrend: The pattern must occur within an uptrend in the market. This context is crucial as it indicates a potential shift in momentum.
  2. First Candle – Large Bullish Candle: The pattern begins with a large bullish candle. The size and upward direction of this candle are key indicators of the prevailing bullish sentiment.
  3. Second Candle – Smaller Bearish Candle: The second candle should be a smaller bearish (downward) candle, completely contained within the vertical range of the first candle (including both the body and the wicks). This candle suggests a loss of bullish momentum.
  4. Third Candle – Bearish Close Below Second Candle: The third candle is another bearish candle that closes below the close of the second candle. This is a critical confirmation of the pattern, signaling a bearish reversal and potential downtrend.

Code For Detecting Three Inside Down

//@version=5
indicator("Three inside candles", shorttitle="Three inside", overlay=true)

// Define an input for the EMA period with a default value of 15, minimum 5 and maximum 50
ema_input = input.int(15, title="Ema", minval=5, maxval=50)
// Calculate the Exponential Moving Average (EMA) of the close price with the specified period
prev_p_1 = ta.ema(close, ema_input)
// Plot the EMA on the chart in yellow color for visual reference
plot(prev_p_1, color = color.yellow)

// Calculate the minimum and maximum values between the open and close of the current bar
open_close_min = math.min(close, open)
open_close_max = math.max(close, open)
// Determine the range (high-low) of the current bar
bar = open_close_max - open_close_min

// Retrieve the close and open prices of the previous bar
close_1 = close[1]
open_1 = open[1]
// Calculate the min and max values between the open and close of the previous bar
open_close_min_1 = math.min(close[1], open[1])
open_close_max_1 = math.max(close[1], open[1])
// Determine the range of the previous bar
bar_1 = open_close_max_1 - open_close_min_1

// Retrieve the close and open prices of the bar before the previous bar
close_2 = close[2]
open_2 = open[2]
// Calculate the min and max values between the open and close of the bar before the previous bar
open_close_min_2 = math.min(close[2], open[2])
open_close_max_2 = math.max(close[2], open[2])
// Determine the range of the bar before the previous bar
bar_2 = open_close_max_2 - open_close_min_2

// Define the conditions for the 'Three Inside Down' pattern
triple_inside_down = open_close_max_2 == open_close_max_1 and bar_1 > bar_2 * 0.4 and bar_1 < bar_2 * 0.6 and close < open_2 and bar > bar_1 and bar + bar_1 < bar_2 * 2
// Plot a downward arrow above the bar if the 'Three Inside Down' pattern is identified
plotshape(triple_inside_down ? 1 : na, style=shape.arrowdown, color=color.rgb(255, 0, 0), location=location.abovebar, size=size.large, text="Three inside down")

Output

Output

Overview of the script 

1. Script Initialization:

//@version=5
indicator("Three inside candles", shorttitle="Three inside", overlay=true)
  • //@version=5: Specifies that the script uses version 5 of Pine Script.
  • indicator(...): Declares the script as a custom indicator.
  • "Three inside candles": The name of the indicator.
  • shorttitle="Three inside": A shorter title for the indicator.
  • overlay=true: The indicator will be overlaid on the main price chart.

2. EMA Input Configuration

ema_input = input.int(15, title="Ema", minval=5, maxval=50)
  • input.int(...): Creates an input field for users to select an integer value.
  • 15: The default value for the EMA period.
  • title="Ema": The title for the input field.
  • minval=5 and maxval=50: The minimum and maximum values allowed for the EMA period.

3. EMA Calculation and Plotting

prev_p_1 = ta.ema(close, ema_input)
plot(prev_p_1, color = color.yellow)
  • ta.ema(close, ema_input): Calculates the Exponential Moving Average (EMA) of the close price for the given period.
  • plot(...): Plots the EMA on the chart in yellow.

4. Current Bar Metrics Calculation

open_close_min = math.min(close, open)
open_close_max = math.max(close, open)
bar = open_close_max - open_close_min
  • Determines the minimum and maximum values between the open and close prices of the current bar.
  • bar: Calculates the range (difference) between open_close_max and open_close_min.

5. Previous Bar Metrics Calculation

close_1 = close[1]
open_1 = open[1]
open_close_min_1 = math.min(close[1], open[1])
open_close_max_1 = math.max(close[1], open[1])
bar_1 = open_close_max_1 - open_close_min_1
  • Retrieves close and open prices from the previous bar ([1]).
  • Calculates the min and max values and the range for the previous bar.

6. Metrics Calculation for Two Bars Ago

close_2 = close[2]
open_2 = open[2]
open_close_min_2 = math.min(close[2], open[2])
open_close_max_2 = math.max(close[2], open[2])
bar_2 = open_close_max_2 - open_close_min_2
  • Similar to step 5, but for the bar two periods ago ([2]).

7. Three Inside Down Pattern Logic

triple_inside_down = open_close_max_2 == open_close_max_1 and bar_1 > bar_2 * 0.4 and bar_1 < bar_2 * 0.6 and close < open_2 and bar > bar_1 and bar + bar_1 < bar_2 * 2
  • This line sets the criteria for the “Three Inside Down” pattern, comparing the current, previous, and the bar before the previous one.

8. Pattern Plotting

plotshape(triple_inside_down ? 1 : na, style=shape.arrowdown, color=color.rgb(255, 0, 0), location=location.abovebar, size=size.large, text="Three inside down")
  • plotshape(...): Plots a shape on the chart.
  • Uses a conditional (triple_inside_down ? 1 : na) to plot a red downward arrow (shape.arrowdown) above the bar where the “Three Inside Down” pattern is identified.

Frequently Asked Questions

What is the purpose of the ‘Three Inside Down’ Pine Script?

This script is designed to identify the ‘Three Inside Down’ candlestick pattern, a bearish reversal indicator, on financial charts. It helps traders spot potential trend reversals from bullish to bearish.

How does the Exponential Moving Average (EMA) feature in the script?

The script includes an EMA calculation as an additional trend analysis tool. The EMA provides a visual representation of the prevailing market trend, aiding in the confirmation of the bearish reversal indicated by the ‘Three Inside Down’ pattern.

Can the period of the EMA be adjusted in the script?

Yes, the script allows for adjustment of the EMA period. User input is set up to select any integer value between 5 and 50 as the EMA period, offering flexibility based on the user’s preference or trading strategy.

What criteria does the script use to identify the ‘Three Inside Down’ pattern?

The script identifies the pattern based on specific conditions involving the sizes and positions of three consecutive candlesticks (the current bar, the previous bar, and the bar before the previous one). These conditions align with the traditional definition of the ‘Three Inside Down’ pattern.

Is this script applicable to all financial markets and timeframes?

Yes, the script can be applied across different financial markets, including stocks, forex, and commodities. It is also versatile across various timeframes, making it suitable for different trading styles, such as day trading or swing trading. However, the effectiveness of candlestick patterns can vary by market and timeframe, so it’s recommended to use this tool in conjunction with other forms of analysis.

Conclusion

The Pine Script code for the “Three Inside Down” candlestick pattern identifies potential bearish reversals in the market, aiding in decision-making processes. The inclusion of the Exponential Moving Average (EMA) adds a layer of trend analysis, enhancing the reliability of the pattern’s identification. While the script offers flexibility through adjustable EMA parameters and is applicable across various markets and timeframes, it is important to remember that reliance on a single indicator or pattern is not advisable. The most effective trading strategies often involve a combination of different technical indicators, fundamental analysis, and a clear understanding of market conditions.

Leave a Comment