OPEN-SOURCE SCRIPT
RSI Divergence + Hidden

RSI Divergence + Hidden (TV-like pairing, final)
What it does
This indicator plots RSI and automatically detects both regular and hidden divergences by pairing RSI pivots with price pivots. It supports a TradingView-like loose pairing (within a user-defined bar tolerance) and a strict same-bar pairing. Detected signals are drawn with lines and optional labels on the RSI pane for quick visual verification.
Divergence logic
Regular Bullish (label: Bull)
Price makes a lower low while RSI makes a higher low → potential upward reversal.
Regular Bearish (label: Bear)
Price makes a higher high while RSI makes a lower high → potential downward reversal.
Hidden Bullish (label: H_Bull)
Price makes a higher low while RSI makes a lower low → trend-continuation bias upward.
Hidden Bearish (label: H_Bear)
Price makes a lower high while RSI makes a higher high → trend-continuation bias downward.
All conditions use pivot-to-pivot comparisons with optional equality tolerance for price and RSI to reduce false “equal” mismatches.
Pairing modes
TV-like
Pairs the latest price and RSI pivots if their pivot bars occur within ±tolBars.
A lightweight “pending” buffer allows pairing a newly detected pivot with a recent opposite pivot that arrived a few bars earlier/later (within tolerance).
Same Bar
Price and RSI pivots must occur on the exact same bar to form a pair.
Key inputs
RSI Source & Length: srcRsi, rsiLen (default 14). RSI line and reference levels (70/50/30) can be shown/hidden.
Pivot Window: leftBars, rightBars for both price and RSI pivots.
Pairing: pairMode = TV-like or Same Bar; tolBars for bar tolerance (TV-like only).
Price Pivot Basis: priceMode = High/Low (default) or Close.
Equality Tolerance:
allowEqual (use >=/<=),
priceEpsTks (ticks) for price equality slack,
rsiEps (points) for RSI equality slack.
Visibility: showRSI, showRegular, showHidden, showLabels.
Visuals
Lines (on RSI):
Regular Bearish: red
Regular Bullish: lime
Hidden Bearish: orange
Hidden Bullish: teal
Labels (optional): "Bear", "Bull", "H_Bear", "H_Bull" placed on the RSI series at the second pivot.
Alerts
Four alert conditions are provided and fire when the corresponding divergence is confirmed:
Bear (Regular)
Bull (Regular)
H_Bear (Hidden)
H_Bull (Hidden)
Notes & tips
Divergences are evaluated only when both price and RSI pivots exist and can be paired under the selected mode.
Pivot sensitivity: smaller leftBars/rightBars → earlier but noisier signals; larger values → fewer, more stable pivots.
Tolerance: If you miss valid setups because pivots land a few bars apart, use TV-like with a small tolBars (e.g., 1–2). If you prefer stricter confirmation, use Same Bar.
Equality slack: Use priceEpsTks and rsiEps to avoid rejecting near-equal highs/lows due to tiny differences.
Works on any symbol/timeframe; as with all divergence tools, treat signals as context—combine with trend, structure, and risk management.
What it does
This indicator plots RSI and automatically detects both regular and hidden divergences by pairing RSI pivots with price pivots. It supports a TradingView-like loose pairing (within a user-defined bar tolerance) and a strict same-bar pairing. Detected signals are drawn with lines and optional labels on the RSI pane for quick visual verification.
Divergence logic
Regular Bullish (label: Bull)
Price makes a lower low while RSI makes a higher low → potential upward reversal.
Regular Bearish (label: Bear)
Price makes a higher high while RSI makes a lower high → potential downward reversal.
Hidden Bullish (label: H_Bull)
Price makes a higher low while RSI makes a lower low → trend-continuation bias upward.
Hidden Bearish (label: H_Bear)
Price makes a lower high while RSI makes a higher high → trend-continuation bias downward.
All conditions use pivot-to-pivot comparisons with optional equality tolerance for price and RSI to reduce false “equal” mismatches.
Pairing modes
TV-like
Pairs the latest price and RSI pivots if their pivot bars occur within ±tolBars.
A lightweight “pending” buffer allows pairing a newly detected pivot with a recent opposite pivot that arrived a few bars earlier/later (within tolerance).
Same Bar
Price and RSI pivots must occur on the exact same bar to form a pair.
Key inputs
RSI Source & Length: srcRsi, rsiLen (default 14). RSI line and reference levels (70/50/30) can be shown/hidden.
Pivot Window: leftBars, rightBars for both price and RSI pivots.
Pairing: pairMode = TV-like or Same Bar; tolBars for bar tolerance (TV-like only).
Price Pivot Basis: priceMode = High/Low (default) or Close.
Equality Tolerance:
allowEqual (use >=/<=),
priceEpsTks (ticks) for price equality slack,
rsiEps (points) for RSI equality slack.
Visibility: showRSI, showRegular, showHidden, showLabels.
Visuals
Lines (on RSI):
Regular Bearish: red
Regular Bullish: lime
Hidden Bearish: orange
Hidden Bullish: teal
Labels (optional): "Bear", "Bull", "H_Bear", "H_Bull" placed on the RSI series at the second pivot.
Alerts
Four alert conditions are provided and fire when the corresponding divergence is confirmed:
Bear (Regular)
Bull (Regular)
H_Bear (Hidden)
H_Bull (Hidden)
Notes & tips
Divergences are evaluated only when both price and RSI pivots exist and can be paired under the selected mode.
Pivot sensitivity: smaller leftBars/rightBars → earlier but noisier signals; larger values → fewer, more stable pivots.
Tolerance: If you miss valid setups because pivots land a few bars apart, use TV-like with a small tolBars (e.g., 1–2). If you prefer stricter confirmation, use Same Bar.
Equality slack: Use priceEpsTks and rsiEps to avoid rejecting near-equal highs/lows due to tiny differences.
Works on any symbol/timeframe; as with all divergence tools, treat signals as context—combine with trend, structure, and risk management.
Mã nguồn mở
Theo đúng tinh thần TradingView, người tạo ra tập lệnh này đã biến tập lệnh thành mã nguồn mở để các nhà giao dịch có thể xem xét và xác minh công năng. Xin dành lời khen tặng cho tác giả! Mặc dù bạn có thể sử dụng miễn phí, nhưng lưu ý nếu đăng lại mã, bạn phải tuân theo Quy tắc nội bộ của chúng tôi.
Thông báo miễn trừ trách nhiệm
Thông tin và ấn phẩm không có nghĩa là và không cấu thành, tài chính, đầu tư, kinh doanh, hoặc các loại lời khuyên hoặc khuyến nghị khác được cung cấp hoặc xác nhận bởi TradingView. Đọc thêm trong Điều khoản sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, người tạo ra tập lệnh này đã biến tập lệnh thành mã nguồn mở để các nhà giao dịch có thể xem xét và xác minh công năng. Xin dành lời khen tặng cho tác giả! Mặc dù bạn có thể sử dụng miễn phí, nhưng lưu ý nếu đăng lại mã, bạn phải tuân theo Quy tắc nội bộ của chúng tôi.
Thông báo miễn trừ trách nhiệm
Thông tin và ấn phẩm không có nghĩa là và không cấu thành, tài chính, đầu tư, kinh doanh, hoặc các loại lời khuyên hoặc khuyến nghị khác được cung cấp hoặc xác nhận bởi TradingView. Đọc thêm trong Điều khoản sử dụng.