Example output
Below is a Pine Script for TradingView that detects potential trend reversals using the Relative Strength Index (RSI) and Double Top/Bottom price action patterns. This script includes adjustable parameters, visualization on the chart, real-time alerts, and comments explaining the logic behind the trend reversal detection. Additionally, it has backtesting capability to evaluate the performance over the last year.
```pinescript
//@version=5
indicator("Trend Reversal Detector with RSI and Double Top/Bottom", overlay=true)
// --- Parameters ---
// RSI settings
rsi_length = input.int(14, title="RSI Length")
rsi_overbought = input.int(70, title="RSI Overbought Level")
rsi_oversold = input.int(30, title="RSI Oversold Level")
// Double Top/Bottom settings
double_pattern_lookback = input.int(5, title="Double Top/Bottom Lookback Period")
double_pattern_tolerance = input.float(0.01, title="Double Top/Bottom Tolerance (%)") / 100
// Backtesting settings
test_period = input.time(timestamp("2023-09-04 00:00"), title="Backtest Start Date")
// --- RSI Calculation ---
rsi = ta.rsi(close, rsi_length)
// --- Double Top Detection ---
double_top = (ta.highest(high, double_pattern_lookback) == ta.highest(high[1], double_pattern_lookback)) and
(high > ta.highest(high[1], double_pattern_lookback) * (1 - double_pattern_tolerance))
// --- Double Bottom Detection ---
double_bottom = (ta.lowest(low, double_pattern_lookback) == ta.lowest(low[1], double_pattern_lookback)) and
(low < ta.lowest(low[1], double_pattern_lookback) * (1 + double_pattern_tolerance))
// --- Trend Reversal Signals ---
bullish_reversal = double_bottom and rsi < rsi_oversold
bearish_reversal = double_top and rsi > rsi_overbought
// --- Visualization ---
plotshape(series=bullish_reversal, title="Bullish Reversal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=bearish_reversal, title="Bearish Reversal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")
// --- Alerts ---
alertcondition(bullish_reversal, title="Bullish Reversal Alert", message="Bullish trend reversal detected!")
alertcondition(bearish_reversal, title="Bearish Reversal Alert", message="Bearish trend reversal detected!")
// --- Backtesting ---
// Entry and Exit points
strategy.entry("Long", strategy.long, when=bullish_reversal and time >= test_period)
strategy.exit("Exit Long", from_entry="Long", when=bearish_reversal and time >= test_period)
strategy.entry("Short", strategy.short, when=bearish_reversal and time >= test_period)
strategy.exit("Exit Short", from_entry="Short", when=bullish_reversal and time >= test_period)
// --- Comments ---
// The script detects potential trend reversals using the RSI and Double Top/Bottom patterns.
// A Bullish reversal signal is generated when a Double Bottom pattern coincides with RSI being oversold.
// A Bearish reversal signal is generated when a Double Top pattern coincides with RSI being overbought.
// The strategy backtests these signals starting from the specified test period.
```
### Explanation:
1. **RSI Calculation**: The script calculates the RSI to determine overbought and oversold conditions, which are key indicators of potential trend reversals.
2. **Double Top/Bottom Detection**: The script detects double top (potential bearish reversal) and double bottom (potential bullish reversal) patterns by comparing the highest highs and lowest lows over a specified lookback period.
3. **Trend Reversal Signals**: The script generates a bullish reversal signal when a double bottom pattern coincides with the RSI being in the oversold region. Conversely, it generates a bearish reversal signal when a double top pattern coincides with the RSI being in the overbought region.
4. **Visualization**: The script uses green and red markers on the chart to indicate potential buy and sell signals, respectively.
5. **Alerts**: Real-time alerts are triggered when the script detects a potential trend reversal.
6. **Backtesting**: The script includes a backtesting mechanism that allows you to evaluate the strategy's performance over the last year. The test period is adjustable, so you can refine the backtest as needed.
You can copy and paste this script into TradingView's Pine Editor to start using it. Make sure to adjust the parameters as per your preferences and the specific asset you are analyzing.