Wednesday, 16 April 2025

Introduction to the "3-Candle Pattern with Highlighted First Candle" Pine Script

  No comments
Image


This Pine Script is designed to identify and highlight specific candlestick patterns on a chart. Specifically, it detects 3 consecutive bullish or bearish candles and highlights the first candle of each valid 3-candle pattern. The key features of the script include:

  • Bullish Pattern Detection: The script identifies when there are 3 consecutive bullish candles, each with higher closing prices and rising lows.

  • Bearish Pattern Detection: It also identifies 3 consecutive bearish candles with falling highs and lower closing prices.

  • Highlighting: When a valid pattern is detected, only the first candle of the 3-candle pattern is highlighted using a colored rectangle. The first bullish candle is highlighted with a green rectangle, while the first bearish candle is highlighted with a red rectangle.

  • Reset Mechanism: The script resets after a pattern ends, ensuring that only the first candle of a new 3-candle sequence is highlighted.

  • Customizable for Trading: The script can be easily adapted to trigger trading signals (buy/sell) based on the detection of these candlestick patterns.

Buying and Selling Strategies

Based on the 3-candle pattern detection, the following buying and selling strategies can be applied:

Buy Strategy (Bullish Pattern)

  • Trigger: When the script identifies the first bullish candle of a valid 3-candle bullish pattern (i.e., 3 consecutive bullish candles with higher lows), it signals a potential buy opportunity.

  • Confirmation: A buy signal can be confirmed if the price is at or near the low of the first bullish candle. Traders might also wait for the price to pull back slightly and then bounce off the low before entering a position.

  • Take Profit: Set a target for a small profit based on a risk-to-reward ratio (e.g., 1:2), or use a trailing stop to capture larger trends.

  • Stop Loss: Place a stop loss slightly below the low of the first candle of the pattern to limit downside risk in case the pattern fails.

Sell Strategy (Bearish Pattern)

  • Trigger: When the script identifies the first bearish candle of a valid 3-candle bearish pattern (i.e., 3 consecutive bearish candles with falling highs), it signals a potential sell opportunity.

  • Confirmation: A sell signal can be confirmed if the price is at or near the high of the first bearish candle. Traders might wait for a retracement and a rejection at the high before entering a short position.

  • Take Profit: Set a target for a small profit based on a risk-to-reward ratio (e.g., 1:2), or use a trailing stop to capture larger downward trends.

  • Stop Loss: Place a stop loss slightly above the high of the first candle of the pattern to protect against the pattern failing.

Example Scenario:

  • Bullish Example: After detecting the first bullish candle of a 3-candle pattern, the script would highlight it with a green rectangle. A trader could then place a buy order near the low of this candle, looking for the price to rise further.

  • Bearish Example: After detecting the first bearish candle of a 3-candle pattern, the script would highlight it with a red rectangle. A trader could place a sell order near the high of this candle, looking for the price to decline further.

//@version=5
indicator("3-Candle Pattern with Highlighted First Candle", overlay=true)

// === Function: Detect 3 consecutive bullish candles with rising lows ===
isValidBullishPattern() =>
    close[2] > open[2] and close[1] > open[1] and close > open and low[1] > low[2] and low > low[1]

// === Function: Detect 3 consecutive bearish candles with falling highs ===
isValidBearishPattern() =>
    close[2] < open[2] and close[1] < open[1] and close < open and high[1] < high[2] and high < high[1]

// === Persistent variables to store box reference ===
var box bullishBox = na
var box bearishBox = na
var bool isBullishHighlighted = false
var bool isBearishHighlighted = false

// === Detect patterns ===
bullishPattern = isValidBullishPattern()
bearishPattern = isValidBearishPattern()

// === Create Rectangle for First Bullish Candle of the Pattern ===
if bullishPattern and not isBullishHighlighted
    // Highlight the first candle in the bullish pattern (candle at index 2)
    bullishBox := box.new(left=bar_index[2], top=high[2], right=bar_index[2] + 1, bottom=low[2], border_color=color.green, bgcolor=color.new(color.green, 90))
    isBullishHighlighted := true // Mark as highlighted

// === Create Rectangle for First Bearish Candle of the Pattern ===
if bearishPattern and not isBearishHighlighted
    // Highlight the first candle in the bearish pattern (candle at index 2)
    bearishBox := box.new(left=bar_index[2], top=high[2], right=bar_index[2] + 1, bottom=low[2], border_color=color.red, bgcolor=color.new(color.red, 90))
    isBearishHighlighted := true // Mark as highlighted

// === Reset Highlight Flags After 3-Candle Pattern Ends ===
if not bullishPattern and isBullishHighlighted
    isBullishHighlighted := false
if not bearishPattern and isBearishHighlighted
    isBearishHighlighted := false

// === Reset Boxes at the Start of New Day or New Trend ===
if (dayofweek != dayofweek[1])
    box.delete(bullishBox)
    box.delete(bearishBox)

No comments :

Post a Comment