Create Indicator for Reversal Patterns: Look for patterns such as pin bars, engulfing candles, and morning/evening stars at key levels of support or resistance.
Assistant
Certainly! Creating an indicator that identifies reversal patterns such as pin bars, engulfing candles, and morning/evening stars at key support or resistance levels can be a valuable tool in your trading arsenal. Below, I'll provide you with a script using TradingView's Pine Script language that you can use to detect these patterns.
Understanding the Patterns
1. Pin Bar:
A candlestick with a small body and a long wick (tail) on one side, indicating a potential reversal.
2. Engulfing Candle:
A pattern where a candle's body fully engulfs the previous candle's body, signaling a possible reversal.
Bullish Engulfing: Occurs at the end of a downtrend; the bullish candle engulfs the previous bearish candle.
Bearish Engulfing: Occurs at the end of an uptrend; the bearish candle engulfs the previous bullish candle.
3. Morning Star & Evening Star:
Three-candle reversal patterns.
Morning Star (Bullish): Consists of a long bearish candle, followed by a short-bodied candle (which can be bullish or bearish), and then a long bullish candle.
Evening Star (Bearish): Consists of a long bullish candle, followed by a short-bodied candle, and then a long bearish candle.
Support and Resistance Levels
Support and resistance levels are key price areas where the market has historically shown a tendency to reverse or consolidate. For this indicator, we'll calculate dynamic support and resistance levels using pivot points.
Pine Script Indicator Code
Here is the Pine Script code for the indicator:
pine
//@version=5
indicator("Reversal Patterns at Support/Resistance", overlay=true)
// Parameters
show_pivot_levels = input.bool(true, "Show Pivot Levels")
left_bars = input.int(5, "Left Pivot Bars", minval=1)
right_bars = input.int(5, "Right Pivot Bars", minval=1)
// Functions to detect patterns
// Pin Bar Detection
is_bullish_pin_bar() =>
body = abs(close - open)
wick = high - low
upper_wick = high - max(open, close)
lower_wick = min(open, close) - low
lower_wick > body * 2 and upper_wick < body and close > open
is_bearish_pin_bar() =>
body = abs(close - open)
wick = high - low
upper_wick = high - max(open, close)
lower_wick = min(open, close) - low
upper_wick > body * 2 and lower_wick < body and close < open
// Engulfing Detection
is_bullish_engulfing() =>
prev_body = abs(ta.valuewhen(volume != 0, close[1] - open[1], 0))
curr_body = abs(close - open)
close > open and open < close[1] and close > open[1] and curr_body > prev_body
is_bearish_engulfing() =>
prev_body = abs(ta.valuewhen(volume != 0, close[1] - open[1], 0))
curr_body = abs(close - open)
close < open and open > close[1] and close < open[1] and curr_body > prev_body
// Morning Star Detection
is_morning_star() =>
bearish_candle = open[2] > close[2]
small_candle = abs(open[1] - close[1]) < abs(open[2] - close[2]) * 0.5
bullish_candle = close > open
gap_down = open[1] < close[2]
pattern = bearish_candle and small_candle and bullish_candle and gap_down
pattern
// Evening Star Detection
is_evening_star() =>
bullish_candle = close[2] > open[2]
small_candle = abs(open[1] - close[1]) < abs(open[2] - close[2]) * 0.5
bearish_candle = close < open
gap_up = open[1] > close[2]
pattern = bullish_candle and small_candle and bearish_candle and gap_up
pattern
// Pivot Points for Support/Resistance
pivot_high = ta.pivothigh(high, left_bars, right_bars)
pivot_low = ta.pivotlow(low, left_bars, right_bars)
// Arrays to store pivot levels
var float[] resistance_levels = array.new_float()
var float[] support_levels = array.new_float()
if not na(pivot_high)
array.push(resistance_levels, pivot_high)
if not na(pivot_low)
array.push(support_levels, pivot_low)
// Remove old levels
if array.size(resistance_levels) > 10
array.shift(resistance_levels)
if array.size(support_levels) > 10
array.shift(support_levels)
// Check for patterns at support/resistance
bullish_signal = (is_bullish_pin_bar() or is_bullish_engulfing() or is_morning_star()) and close <= array.get(support_levels, array.size(support_levels) - 1) * 1.005
bearish_signal = (is_bearish_pin_bar() or is_bearish_engulfing() or is_evening_star()) and close >= array.get(resistance_levels, array.size(resistance_levels) - 1) * 0.995
// Plotting
plotshape(bullish_signal, title="Bullish Signal", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small, text="Bullish Reversal")
plotshape(bearish_signal, title="Bearish Signal", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small, text="Bearish Reversal")
// Plot support and resistance levels
if show_pivot_levels
for i = 0 to array.size(resistance_levels) - 1
res_level = array.get(resistance_levels, i)
plot(res_level, "Resistance Level", color=color.red, style=plot.style_linebr, linewidth=1)
for i = 0 to array.size(support_levels) - 1
sup_level = array.get(support_levels, i)
plot(sup_level, "Support Level", color=color.green, style=plot.style_linebr, linewidth=1)
How the Indicator Works
Pattern Detection Functions: The script includes functions to detect bullish and bearish pin bars, engulfing patterns, and morning/evening stars.
Support and Resistance Levels: It calculates pivot highs and lows to determine dynamic support and resistance levels and stores them in arrays.
Signal Generation: The indicator checks if any of the reversal patterns occur near the latest support or resistance level (within a 0.5% range).
Plotting Signals: When a bullish pattern is detected near support, it plots an upward green triangle below the candle. For bearish patterns near resistance, it plots a downward red triangle above the candle.
Plotting Levels: If enabled, the indicator plots the support and resistance levels on the chart.
How to Use the Indicator
Add the Script to TradingView:
Open TradingView and go to the Pine Editor.
Paste the script code into the editor.
Click on the "Add to Chart" button.
Customize Parameters:
Show Pivot Levels: Toggle to show/hide support and resistance lines.
Left Pivot Bars and Right Pivot Bars: Adjust sensitivity of pivot points.
Interpret Signals:
Bullish Signals:
Look for green triangles below candles at support levels indicating a potential upward reversal.
Bearish Signals:
Look for red triangles above candles at resistance levels indicating a potential downward reversal.
Combine with Other Analysis:
Use this indicator in conjunction with other technical analysis tools and indicators to confirm signals.
Notes and Recommendations
Timeframes: Reversal patterns are more reliable on higher timeframes (e.g., 1-hour, 4-hour, daily).
Market Conditions: Ensure that you're aware of market news or events that may affect price action.
Risk Management: Always use proper risk management techniques, such as stop-loss orders.
Backtesting: Before using the indicator in live trading, backtest it to evaluate its performance.
Feel free to adjust the code to suit your specific needs or to enhance the detection logic of the patterns. Happy trading!