Example output
Creating a custom trading indicator in Pine Script for TradingView that detects trend reversals involves a combination of technical indicators, visual cues, adjustable input settings, and alerts. Below is a comprehensive implementation that incorporates Moving Averages, RSI, and MACD to provide buy and sell signals, along with options for user customization.
### Pine Script Code
```pinescript
//@version=5
indicator("Trend Reversal Detector", overlay=true)
// User Inputs for Moving Averages
maType = input.string("SMA", title="Moving Average Type", options=["SMA", "EMA"])
maPeriod1 = input.int(50, title="Short MA Period")
maPeriod2 = input.int(200, title="Long MA Period")
// User Inputs for RSI
rsiPeriod = input.int(14, title="RSI Period")
rsiOverbought = input.int(70, title="RSI Overbought Level")
rsiOversold = input.int(30, title="RSI Oversold Level")
// User Inputs for MACD
macdFastLength = input.int(12, title="MACD Fast Length")
macdSlowLength = input.int(26, title="MACD Slow Length")
macdSignalLength = input.int(9, title="MACD Signal Length")
// Calculating Moving Averages
ma1 = maType == "SMA" ? ta.sma(close, maPeriod1) : ta.ema(close, maPeriod1)
ma2 = maType == "SMA" ? ta.sma(close, maPeriod2) : ta.ema(close, maPeriod2)
// Calculating RSI
rsi = ta.rsi(close, rsiPeriod)
// Calculating MACD
[macdLine, signalLine, _] = ta.macd(close, macdFastLength, macdSlowLength, macdSignalLength)
// Conditions for Buy and Sell Signals
buySignal = ta.crossover(ma1, ma2) and (rsi < rsiOversold) and (macdLine > signalLine)
sellSignal = ta.crossunder(ma1, ma2) and (rsi > rsiOverbought) and (macdLine < signalLine)
// Plotting Moving Averages
plot(ma1, color=color.blue, title="Short MA")
plot(ma2, color=color.red, title="Long MA")
// Visual cues for Buy and Sell signals
plotshape(buySignal, style=shape.labelup, location=location.belowbar, color=color.green, size=size.small, title="Buy Signal", text="BUY")
plotshape(sellSignal, style=shape.labeldown, location=location.abovebar, color=color.red, size=size.small, title="Sell Signal", text="SELL")
// Alerts
alertcondition(buySignal, title="Buy Alert", message="Buy Signal: Price crossed above MA and RSI is oversold.")
alertcondition(sellSignal, title="Sell Alert", message="Sell Signal: Price crossed below MA and RSI is overbought.")
// Background color for overbought/oversold regions
bgcolor(rsi > rsiOverbought ? color.new(color.red, 90) : na, title="Overbought")
bgcolor(rsi < rsiOversold ? color.new(color.green, 90) : na, title="Oversold")
// Display RSI in a separate pane
rsiPlot = plot(rsi, color=color.purple, title="RSI")
hline(rsiOverbought, "Overbought", color=color.red)
hline(rsiOversold, "Oversold", color=color.green)
```
### Explanation of Key Components
1. **Technical Indicators**:
- **Moving Averages**: The script allows users to choose between Simple Moving Average (SMA) and Exponential Moving Average (EMA). Two moving averages (short and long) are used to determine trend direction.
- **RSI (Relative Strength Index)**: This indicator measures the speed and change of price movements to identify overbought or oversold conditions.
- **MACD (Moving Average Convergence Divergence)**: This is used to identify potential buy/sell signals based on the convergence and divergence of moving averages.
2. **Buy and Sell Signals**:
- A buy signal is generated when the short moving average crosses above the long moving average, RSI is below the oversold level, and the MACD line is above the signal line.
- A sell signal is triggered when the short moving average crosses below the long moving average, RSI is above the overbought level, and the MACD line is below the signal line.
3. **Visual Cues**:
- The script plots buy and sell signals on the chart using label shapes (green for buy, red for sell).
- Moving averages are plotted in different colors for clarity.
4. **Adjustable Inputs**:
- Users can customize the period lengths for moving averages, RSI, and MACD through the input parameters at the top of the script.
5. **Alerts**:
- Alerts are set up to notify the trader when buy or sell conditions are met. The alerts can be configured within TradingView for real-time notifications.
6. **Performance Optimization**:
- The use of built-in functions like `ta.crossover` and `ta.crossunder` ensures the script runs efficiently without excessive lag on different timeframes.
### Conclusion
This Pine Script indicator provides a robust framework for detecting trend reversals in Bitcoin trading by leveraging multiple technical indicators. Users can easily customize the parameters to fit their trading strategy, and visual cues, along with alerts, ensure that they can act quickly on trading signals.