OPEN-SOURCE SCRIPT

TTP Universal Divergence Detector

1โ€ฏ747
UNIVERSAL DIVERGENCE DETECTOR

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

OVERVIEW

The Universal Divergence Detector is a powerful and flexible indicator that identifies divergences between price action and ANY oscillator or indicator of your choice. Unlike traditional divergence indicators that only work with specific oscillators like RSI or MACD, this tool allows you to select any indicator on your chart as the source for divergence detection.

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

KEY FEATURES

UNIVERSAL COMPATIBILITY
โ†’ Works with ANY indicator - RSI, MACD, Stochastic, Volume, custom indicators, or any other source
โ†’ Flexible source selection - Choose your preferred indicator from a simple dropdown menu
โ†’ Multi-timeframe capable - Use on any timeframe from 1-minute to monthly charts

COMPLETE DIVERGENCE DETECTION
The indicator detects all four types of divergences:

Regular Divergences (Potential Trend Reversals):
โ†’ Regular Bullish: Price makes lower low, indicator makes higher low โ†’ Potential reversal UP
โ†’ Regular Bearish: Price makes higher high, indicator makes lower high โ†’ Potential reversal DOWN

Hidden Divergences (Trend Continuation):
โ†’ Hidden Bullish: Price makes higher low, indicator makes lower low โ†’ Continuation UP
โ†’ Hidden Bearish: Price makes lower high, indicator makes higher high โ†’ Continuation DOWN

DUAL DISPLAY MODES

1. Divergences Only (Default)
โ†’ Beautiful pattern lines connecting pivot points
โ†’ Shows the complete divergence structure
โ†’ WARNING: REPAINTS as new bars form - for analysis only, NOT for trading

2. Signals Only
โ†’ Clean, simple B/S markers (Buy/Sell signals)
โ†’ NON-REPAINTING - what you see is what you get
โ†’ Appears when divergence is confirmed after pivot bars
โ†’ Safe for trading and backtesting

3. Both
โ†’ See the complete picture
โ†’ Divergence lines show the historical pattern
โ†’ Signal markers show exact entry points

BACKTESTING SUPPORT
โ†’ Binary outputs (1/0) for each signal type
โ†’ Non-repainting signals ensure reliable backtest results
โ†’ Compatible with Pine Script strategies - reference the outputs in your own scripts
โ†’ Six separate streams: Regular Bullish, Regular Bearish, Hidden Bullish, Hidden Bearish, Any Bullish, Any Bearish

COMPREHENSIVE ALERTS
โ†’ Individual alerts for each divergence type
โ†’ Combined alerts for any bullish/bearish signals
โ†’ All alerts are NON-REPAINTING (signals only, not divergence patterns)
โ†’ No false alerts from unconfirmed patterns

CUSTOMIZABLE VISUALS
โ†’ Adjustable pivot sensitivity (left/right bars)
โ†’ Customizable lookback range (min/max bars between pivots)
โ†’ Color customization for each divergence type
โ†’ Line width and label size controls
โ†’ Toggle individual divergence types on/off

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

HOW TO USE

BASIC SETUP

1. Add the indicator to your chart
Click "Indicators" โ†’ Search "Universal Divergence Detector" โ†’ Add to chart

2. Select your indicator source
โ†’ Open indicator settings
โ†’ Under "Source Settings", click the "Indicator Source" dropdown
โ†’ Choose ANY indicator already on your chart (e.g., RSI, MACD, Stochastic, etc.)

3. Configure pivot settings (optional)
โ†’ Pivot Left Bars: Number of bars to the left of pivot (default: 5)
โ†’ Pivot Right Bars: Number of bars to the right of pivot (default: 5)
โ†’ Higher values = more reliable but slower signals
โ†’ Signals appear AFTER right bars for confirmation

4. Choose your display mode
โ†’ Divergences Only: Beautiful analysis view (repainting)
โ†’ Signals Only: Clean trade signals (non-repainting)
โ†’ Both: Complete picture

UNDERSTANDING THE DISPLAY

Divergence Lines (when enabled):
โ†’ Solid lines = Regular divergences (potential reversals)
โ†’ Dashed lines = Hidden divergences (trend continuation)
โ†’ Labels: "RB" (Regular Bullish), "HB" (Hidden Bullish), etc.

Signal Markers (when enabled):
โ†’ "B" = Buy signal (below bars)
โ†’ "S" = Sell signal (above bars)
โ†’ Colors:
โ€ข Green = Regular Bullish
โ€ข Red = Regular Bearish
โ€ข Blue = Hidden Bullish
โ€ข Orange = Hidden Bearish

TRADING STRATEGY TIPS

For Trend Reversals:
โ†’ Enable Regular Divergences only
โ†’ Use on higher timeframes (4H, Daily) for reliability
โ†’ Wait for signal confirmation (B/S markers)
โ†’ Combine with support/resistance levels

For Trend Continuation:
โ†’ Enable Hidden Divergences only
โ†’ Use in strong trending markets
โ†’ Enter on pullbacks when hidden divergence appears

For Backtesting:
โ†’ Enable "Backtesting Outputs" in settings
โ†’ Use "Signals Only" display mode
โ†’ Reference the binary outputs in your Pine Script strategy
โ†’ Available outputs:
โ€ข BT: Regular Bullish Signal
โ€ข BT: Regular Bearish Signal
โ€ข BT: Hidden Bullish Signal
โ€ข BT: Hidden Bearish Signal
โ€ข BT: Any Bullish Signal
โ€ข BT: Any Bearish Signal

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

SETTINGS GUIDE

SOURCE SETTINGS
โ†’ Indicator Source: Select which indicator to use for divergence detection

PIVOT SETTINGS
โ†’ Pivot Left Bars (1-โˆž): Bars to the left of pivot point (default: 5)
โ†’ Pivot Right Bars (1-โˆž): Bars to the right of pivot point (default: 5)

DIVERGENCE TYPES
Toggle each type individually:
โ†’ Regular Bullish
โ†’ Regular Bearish
โ†’ Hidden Bullish
โ†’ Hidden Bearish

DISPLAY SETTINGS
โ†’ Display Mode: Divergences Only / Signals Only / Both

DETECTION SETTINGS
โ†’ Max Lookback Bars (10-500): Maximum bars to search for divergence patterns (default: 60)
โ†’ Min Lookback Bars (1-100): Minimum bars between pivots (default: 5)

BACKTESTING
โ†’ Enable Backtesting Outputs: Turns on binary (1/0) plot streams for strategy development

VISUAL SETTINGS
โ†’ Line Width (1-5): Thickness of divergence lines
โ†’ Label Size: tiny / small / normal / large

COLORS
Customize colors for each divergence type:
โ†’ Regular Bullish (default: Green)
โ†’ Regular Bearish (default: Red)
โ†’ Hidden Bullish (default: Blue)
โ†’ Hidden Bearish (default: Orange)

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

IMPORTANT CONSIDERATIONS

REPAINTING VS NON-REPAINTING

DIVERGENCE LINES (Repainting):
โ†’ Show beautiful historical patterns
โ†’ Update as new bars form
โ†’ Great for learning and analysis
โ†’ NOT suitable for trading decisions
โ†’ NOT suitable for backtesting

SIGNALS (Non-Repainting):
โ†’ Appear on current bar when confirmed
โ†’ What you see is what you get
โ†’ Safe for trading
โ†’ Reliable for backtesting
โ†’ Appear AFTER pivot right bars (e.g., 5 bars delay with default settings)

This is why the indicator has two display modes - so you can see the pattern AND know exactly when you could have entered!

SIGNAL TIMING

With default settings (Pivot Right Bars = 5):
1. Price forms a pivot low at bar 100
2. Bars 101, 102, 103, 104, 105 form (5 bars of confirmation)
3. At bar 105, if divergence conditions are met, signal triggers
4. You enter your trade on bar 105 (the current bar)

The signal appears exactly when the divergence is mathematically confirmed - no earlier, no later.

BEST PRACTICES

DO:
โ†’ Use signals for actual trading decisions
โ†’ Combine with other analysis (support/resistance, trend lines)
โ†’ Test different pivot settings for your timeframe
โ†’ Use divergence lines for learning and pattern recognition
โ†’ Enable backtesting outputs when developing strategies

DO NOT:
โ†’ Trade based on divergence lines (they repaint)
โ†’ Use very small pivot values (increases false signals)
โ†’ Ignore the broader market context
โ†’ Rely solely on divergences without confirmation

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

EXAMPLES

Example 1: RSI Divergence on Bitcoin
1. Add RSI indicator to your BTC chart
2. Add Universal Divergence Detector
3. Set source to RSI
4. Switch to "Both" display mode
5. Watch for regular bullish divergences at support levels

Example 2: MACD Divergence on Stocks
1. Add MACD indicator to your stock chart
2. Add Universal Divergence Detector
3. Set source to MACD (usually the MACD line, not signal line)
4. Enable only Regular divergences
5. Use on daily timeframe for swing trades

Example 3: Custom Indicator Divergence
1. Add your custom indicator (e.g., volume-weighted RSI)
2. Add Universal Divergence Detector
3. Set source to your custom indicator
4. Now you have divergence detection on your unique indicator!

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

TECHNICAL DETAILS

โ†’ Pine Script Version: v6
โ†’ Overlay: Yes (plots on price chart)
โ†’ Max Labels: 500
โ†’ Max Lines: 500
โ†’ Calculation Method: Pivot-based using ta.pivothigh() and ta.pivotlow()

PERFORMANCE NOTES

โ†’ Memory Efficient: Arrays limited to 100 pivots maximum
โ†’ Calculation Speed: Optimized loops with configurable max lookback
โ†’ Visual Performance: Toggle off unused divergence types for cleaner charts

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

TROUBLESHOOTING

No divergences showing?
โ†’ Check that your selected source is actually plotted on the chart
โ†’ Reduce Min Lookback Bars or increase Max Lookback Bars
โ†’ Decrease Pivot Left/Right Bars for more sensitivity

Too many signals?
โ†’ Increase Pivot Left/Right Bars for more selective detection
โ†’ Increase Min Lookback Bars
โ†’ Disable divergence types you don't need

Signals appear too late?
โ†’ This is by design - signals need confirmation
โ†’ Reduce Pivot Right Bars (but this increases false signals)
โ†’ Remember: reliable signals require confirmation time

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

EDUCATIONAL RESOURCES

What is Divergence?
Divergence occurs when price action and an indicator move in opposite directions, suggesting potential momentum shifts.

Types of Divergences:

Regular Divergences suggest trend reversals:
โ†’ Price continues in one direction
โ†’ Indicator shows weakening momentum
โ†’ Often occurs at major support/resistance

Hidden Divergences suggest trend continuation:
โ†’ Price pulls back against the trend
โ†’ Indicator shows momentum still strong
โ†’ Often occurs during healthy pullbacks in trends

LEARNING MODE

To learn divergence patterns:
1. Set Display Mode to "Both"
2. Study historical charts
3. Notice how divergence lines show the pattern
4. See how signals appear after confirmation
5. Practice identifying divergences manually
6. Then switch to "Signals Only" for trading

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

FUTURE ENHANCEMENT IDEAS

Users can modify this open-source script to add:
โ†’ Divergence strength scoring
โ†’ Confluence with other indicators
โ†’ Auto entry/exit strategy development
โ†’ Multi-timeframe divergence detection
โ†’ Volume-weighted divergence signals

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

Happy Trading!

Remember: The best indicator is the one you understand. Take time to learn how divergences work before incorporating them into your trading strategy.

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•


Pernyataan Penyangkalan

Informasi dan publikasi ini tidak dimaksudkan, dan bukan merupakan, saran atau rekomendasi keuangan, investasi, trading, atau jenis lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Ketentuan Penggunaan.