Example output
Below is a Pine Script code snippet for TradingView to create a custom indicator that highlights overbought and oversold zones using RSI and moving average crossovers. The script includes user-configurable parameters, visual features, and alert conditions.
```pinescript
//@version=5
indicator("RSI & SMA Overbought/Oversold Zones", overlay=true)
// === User Inputs ===
// RSI Settings
rsiPeriod = input.int(14, title="RSI Period", minval=1)
rsiOverbought = input.int(70, title="RSI Overbought Level", minval=50, maxval=100)
rsiOversold = input.int(30, title="RSI Oversold Level", minval=0, maxval=50)
// SMA Settings
smaPeriod = input.int(50, title="SMA Period", minval=1)
// Alert Conditions
enableAlerts = input.bool(true, title="Enable Alerts")
// Visual Settings
highlightOverbought = input.bool(true, title="Highlight Overbought Zone")
highlightOversold = input.bool(true, title="Highlight Oversold Zone")
// === Calculations ===
// Calculate RSI and SMA
rsiValue = ta.rsi(close, rsiPeriod)
smaValue = ta.sma(close, smaPeriod)
// Define Overbought and Oversold Zones
isOverbought = rsiValue > rsiOverbought and close > smaValue
isOversold = rsiValue < rsiOversold and close < smaValue
// === Plotting ===
// Plot RSI on a separate window
hline(rsiOverbought, "Overbought", color=color.red, linewidth=1)
hline(rsiOversold, "Oversold", color=color.green, linewidth=1)
plot(rsiValue, "RSI", color=color.blue, linewidth=2)
// Highlight Overbought Zone
if highlightOverbought and isOverbought
label.new(bar_index, high, "Overbought", style=label.style_label_down, color=color.red, textcolor=color.white)
// Highlight Oversold Zone
if highlightOversold and isOversold
label.new(bar_index, low, "Oversold", style=label.style_label_up, color=color.green, textcolor=color.white)
// Overlay SMA on the main chart
plot(smaValue, "SMA", color=color.orange, linewidth=2)
// === Alerts ===
if enableAlerts
if isOverbought
alert("RSI is overbought, and price is above SMA!", alert.freq_once_per_bar)
if isOversold
alert("RSI is oversold, and price is below SMA!", alert.freq_once_per_bar)
// === Comments ===
/*
1. This script highlights overbought and oversold zones based on RSI and price-SMA crossover.
2. RSI thresholds and SMA periods are configurable via user inputs.
3. Overbought zones are identified when RSI exceeds the set threshold and price is above the SMA.
4. Oversold zones are identified when RSI falls below the set threshold and price is below the SMA.
5. Visual enhancements include labeled overbought/oversold zones and plotted SMA and RSI levels.
6. Alerts notify traders when these conditions occur in real time.
*/
```
### Features of the Indicator:
1. **Configurable Inputs**:
- RSI period, overbought/oversold levels, and SMA period.
- Option to enable/disable visual highlights and alerts.
2. **Visual Indicators**:
- RSI plotted in a separate window with overbought/oversold levels marked.
- Overbought/oversold zones labeled on the chart.
- SMA plotted on the main chart for better context.
3. **Alerts**:
- Notifications for overbought/oversold conditions based on RSI and price relationship with SMA.
You can copy and paste this code into TradingView's Pine Script editor, and it will create the described indicator.