Example output
Below is a Pine Script code snippet for TradingView that creates an overlay indicator. It highlights trend reversals using candlestick patterns (e.g., bullish engulfing and bearish engulfing) and moving average (MA) crossovers. It includes configurable inputs, visual elements, alerts, and tooltips/labels.
### Pine Script Code
```pinescript
//@version=5
indicator("Trend Reversal Indicator", overlay=true)
// === Configurable Inputs ===
lengthFastEMA = input.int(9, "Fast EMA Length", minval=1)
lengthSlowEMA = input.int(50, "Slow EMA Length", minval=1)
showBullishEngulfing = input.bool(true, "Highlight Bullish Engulfing")
showBearishEngulfing = input.bool(true, "Highlight Bearish Engulfing")
labelStyle = input.string("Circle", options=["Circle", "Arrow"], "Label Style")
alertEnabled = input.bool(true, "Enable Alerts")
// === Moving Averages ===
fastEMA = ta.ema(close, lengthFastEMA)
slowEMA = ta.ema(close, lengthSlowEMA)
// === Candlestick Pattern Detection ===
// Bullish Engulfing Pattern
bullishEngulfing = close > open[1] and open < close[1] and close > open and close[1] < open[1]
// Bearish Engulfing Pattern
bearishEngulfing = close < open[1] and open > close[1] and close < open and close[1] > open[1]
// === Trend Reversal Signals ===
bullishSignal = showBullishEngulfing and bullishEngulfing and ta.crossover(fastEMA, slowEMA)
bearishSignal = showBearishEngulfing and bearishEngulfing and ta.crossunder(fastEMA, slowEMA)
// === Plot Signals on Chart ===
bullishLabel = bullishSignal ? (labelStyle == "Arrow" ? label.new(bar_index, low, "BUY ▲", style=label.style_label_up, color=color.green)
: label.new(bar_index, low, "●", style=label.style_circle, color=color.green)) : na
bearishLabel = bearishSignal ? (labelStyle == "Arrow" ? label.new(bar_index, high, "SELL ▼", style=label.style_label_down, color=color.red)
: label.new(bar_index, high, "●", style=label.style_circle, color=color.red)) : na
// === Tooltips ===
if bullishSignal
label.set_tooltip(bullishLabel, "Bullish Reversal: Bullish Engulfing + EMA Crossover")
if bearishSignal
label.set_tooltip(bearishLabel, "Bearish Reversal: Bearish Engulfing + EMA Crossover")
// === Alerts ===
if bullishSignal and alertEnabled
alert("Bullish reversal detected: Bullish Engulfing + EMA crossover", alert.freq_once_per_bar)
if bearishSignal and alertEnabled
alert("Bearish reversal detected: Bearish Engulfing + EMA crossover", alert.freq_once_per_bar)
// === Comments/Logic Explanation ===
// 1. The script calculates fast and slow exponential moving averages (EMA) based on user-defined lengths.
// 2. Detects bullish and bearish engulfing patterns using price action logic:
// - Bullish engulfing: Current candle's body fully engulfs the previous candle's body in an upward direction.
// - Bearish engulfing: Current candle's body fully engulfs the previous candle's body in a downward direction.
// 3. Combines candlestick patterns with EMA crossovers for trend reversal signals.
// 4. Displays arrows or circles above/below candles to mark buy/sell signals, customizable via input settings.
// 5. Includes alerts for each signal type, with detailed tooltips explaining the signal conditions.
```
### Key Features of This Script:
1. **Configurable Inputs**:
- Fast and Slow EMA lengths.
- Option to toggle detection of bullish/bearish engulfing patterns.
- Customizable visual style for signals (arrows or circles).
- Alert toggling.
2. **Visual Elements**:
- Arrows or circles appear directly on the price chart at reversal points.
- Arrows indicate BUY (green) or SELL (red).
3. **Tooltips**:
- Clear descriptions of why a signal was generated.
4. **Alerts**:
- Alerts trigger when specific conditions are met.
### How to Use:
1. Copy the script into the Pine Editor on TradingView.
2. Add the indicator to your chart.
3. Customize settings through the input panel to suit your trading strategy.
4. Enable alerts to receive notifications for trend reversals.
Let me know if you need further customization!