Here's a Pine Script v6 code that plots triangles for bullish and bearish engulfing patterns:
//@version=6
indicator("Engulfing Pattern Triangles", overlay=true)
// Function to detect bullish engulfing pattern
isBullishEngulfing() =>
// Previous candle is bearish
prevBearish = close[1] < open[1]
// Current candle is bullish
currBullish = close > open
// Current candle engulfs previous candle's body
engulfs = open <= close[1] and close >= open[1]
// Return true if all conditions are met
prevBearish and currBullish and engulfs
// Function to detect bearish engulfing pattern
isBearishEngulfing() =>
// Previous candle is bullish
prevBullish = close[1] > open[1]
// Current candle is bearish
currBearish = close < open
// Current candle engulfs previous candle's body
engulfs = open >= close[1] and close <= open[1]
// Return true if all conditions are met
prevBullish and currBearish and engulfs
// Detect patterns
bullishEngulfing = isBullishEngulfing()
bearishEngulfing = isBearishEngulfing()
// Plot triangles
// Green triangle below bullish engulfing
plotshape(bullishEngulfing, title="Bullish Engulfing", style=shape.triangleup,
location=location.belowbar, color=color.green, size=size.tiny)
// Red triangle above bearish engulfing
plotshape(bearishEngulfing, title="Bearish Engulfing", style=shape.triangledown,
location=location.abovebar, color=color.red, size=size.tiny)
// Optional: Add alerts
alertcondition(bullishEngulfing, title="Bullish Engulfing Alert",
message="Bullish Engulfing pattern detected")
alertcondition(bearishEngulfing, title="Bearish Engulfing Alert",
message="Bearish Engulfing pattern detected")
This script:
-
Detects Bullish Engulfing: Checks if the previous candle is bearish and the current candle is bullish, with the current candle's body completely engulfing the previous candle's body.
-
Detects Bearish Engulfing: Checks if the previous candle is bullish and the current candle is bearish, with the current candle's body completely engulfing the previous candle's body.
-
Plots Triangles:
- Green upward-pointing triangle below bullish engulfing candles
- Red downward-pointing triangle above bearish engulfing candles
- Uses
size.tiny to make the triangles small as requested
-
Alert Conditions: Includes optional alert conditions that you can use to get notifications when these patterns occur.
To use this script:
- Open TradingView's Pine Editor
- Copy and paste this code
- Click "Add to Chart"
- The triangles will appear automatically when engulfing patterns are detected
You can customize the colors, sizes, or add additional conditions to filter the patterns if needed.