RSI Divergence Pro+ Volume

Finntech1 · study · 130 行 · 点赞 1,378 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=5
indicator(title="RSI Divergence Pro+ Volume", shorttitle="RDP v1.3", overlay=true, max_bars_back=5000, max_labels_count=500)
// © 2025 Finntech1. All Rights Reserved. 
// Licensed under the Mozilla Public License 2.0
 
// ===========================================================================
// DESCRIPTION:
// This indicator identifies potential trend reversals by detecting bullish and bearish
// divergences between price and the Relative Strength Index (RSI). A bullish
// divergence occurs when the price makes a lower low while the RSI makes a higher low.
// A bearish divergence occurs when the price makes a higher high while the RSI makes a lower high.
//
// This script includes volume confirmation and customizable visual alerts to
// highlight these potential trading signals.
//
// Release Notes v1.3:
// - FIX: Replaced `assert()` statements with the more compatible `runtime.error()`
//   function to resolve the "Could not find function" error.
//
// Release Notes v1.2:
// - FIX: Renamed "Predictive" components to "Divergence" for accuracy.
// - OPTIMIZATION: Divergence conditions are now calculated only once per bar.
// - FIX: Combined `bgcolor` calls to prevent them from overriding each other.
// ===========================================================================
 
// ------------------------------
// INPUT SECTION (Grouped Settings)
// ------------------------------
divSettings = "Divergence Configuration"
rsi_len = input.int(13, "RSI Length", group=divSettings, minval=1, maxval=50, inline="rsi")
overbought = input.int(65, "→ Overbought", group=divSettings, minval=50, maxval=90, inline="rsi")
oversold = input.int(35, "→ Oversold", group=divSettings, minval=10, maxval=50, inline="rsi")
 
divLookbackSettings = "Divergence Lookback"
// NOTE: This setting determines how many bars back to establish the 'first point' of the divergence.
divergence_lookback = input.int(4, "Divergence Lookback", group=divLookbackSettings, minval=1, maxval=20, tooltip="How many bars back to look for the start of the divergence. This is NOT a forward prediction.")
comparison_window = input.int(5, "Comparison Window", group=divLookbackSettings, minval=3, maxval=20, tooltip="The length of the period used to find the pivot high/low at the start of the divergence.")
 
volSettings = "Volume Confirmation"
vol_mult = input.float(1.8, "Volume Multiplier", group=volSettings, step=0.1, minval=1.0, maxval=5.0)
vol_ma_len = input.int(21, "Volume MA Length", group=volSettings, minval=5, maxval=50)
 
visualSettings = "Visual Options"
show_labels = input.bool(true, "Show Divergence Labels", group=visualSettings)
highlight_bars = input.bool(true, "Highlight Signal Bars", group=visualSettings)
use_gradient = input.bool(true, "→ Use Color Gradient", group=visualSettings, inline="grad")
gradient_strength = input.int(70, "→ Gradient Opacity", group=visualSettings, minval=20, maxval=90, inline="grad")
 
// ------------------------------
// CORE LOGIC (Modular & Efficient)
// ------------------------------
// Calculate base indicators
rsi = ta.rsi(close, rsi_len)
mom = ta.mom(rsi, 3)
vol_ma = ta.sma(volume, vol_ma_len)
 
// Anti-repainting guard (only signals on closed bars)
confirmed_bar = barstate.isconfirmed
 
// --- Divergence Functions ---
// Bullish Divergence Condition: Price makes a lower low, but RSI makes a higher low.
bullish_div_condition() =>
    price_low_prior = ta.lowest(low, comparison_window)[divergence_lookback]
    rsi_low_prior = ta.lowest(rsi, comparison_window)[divergence_lookback]
    is_rsi_rising = mom > mom[divergence_lookback]
    low < price_low_prior and
      rsi > rsi_low_prior and
      is_rsi_rising and
      volume > vol_ma * vol_mult and
      rsi < oversold and
      confirmed_bar
 
// Bearish Divergence Condition: Price makes a higher high, but RSI makes a lower high.
bearish_div_condition() =>
    price_high_prior = ta.highest(high, comparison_window)[divergence_lookback]
    rsi_high_prior = ta.highest(rsi, comparison_window)[divergence_lookback]
    is_rsi_falling = mom < mom[divergence_lookback]
    high > price_high_prior and
      rsi < rsi_high_prior and
      is_rsi_falling and
      volume > vol_ma * vol_mult and
      rsi > overbought and
      confirmed_bar
 
// --- Execute Calculations Once Per Bar for Efficiency ---
is_bull_div = bullish_div_condition()
is_bear_div = bearish_div_condition()
 
// ------------------------------
// VISUAL ENHANCEMENTS
// ------------------------------
bull_color = use_gradient ? color.new(color.green, gradient_strength) : color.green
bear_color = use_gradient ? color.new(color.red, gradient_strength) : color.red
vol_color = color.new(#2962FF, 85)
 
// Divergence markers
if show_labels and is_bull_div
    label.new(bar_index, low, "BULL↑",
      color=bull_color, textcolor=color.white,
      style=label.style_label_up, yloc=yloc.belowbar)
 
if show_labels and is_bear_div
    label.new(bar_index, high, "BEAR↓",
      color=bear_color, textcolor=color.white,
      style=label.style_label_down, yloc=yloc.abovebar)
 
// Background highlights for any divergence signal
bgcolor(highlight_bars and (is_bull_div or is_bear_div) ? vol_color : na)
 
// ------------------------------
// ALERT CONDITIONS
// ------------------------------
alertcondition(is_bull_div, "Bullish Divergence",
  "Bullish RSI Divergence detected on {{ticker}}")
alertcondition(is_bear_div, "Bearish Divergence",
  "Bearish RSI Divergence detected on {{ticker}}")
 
// ------------------------------
// QUALITY ASSURANCE TESTS
// ------------------------------
if barstate.islast
    // Validation checks to ensure inputs are within logical constraints.
    if overbought <= oversold
        runtime.error("The 'Overbought' level must be set higher than the 'Oversold' level.")
    if na(rsi)
        runtime.error("RSI calculation failed and returned an invalid value (na). Check the RSI length or the symbol's data.")
 
// ===========================================================================
// END OF SCRIPT
// ===========================================================================

← 返回列表