I'll modify the script to show the top 10 strongest divergences instead of all timeframes. The strength will be calculated based on timeframe weight, oscillator extremity, and divergence magnitude.
Here's the corrected and enhanced script:
//@version=5
indicator("Top 10 Strongest Divergences Scanner (RSI/MACD)", "Top 10 Div ✓", overlay=true, max_labels_count=500)
// =============================================================================
// INPUTS
// =============================================================================
// --- Oscillator Selection ---
osc_group = "Oscillator Selection"
oscillatorType = input.string("RSI", "Oscillator", options=["RSI", "MACD"], group=osc_group)
// --- RSI Settings ---
rsi_group = "RSI Settings"
rsiLength = input.int(14, "Length", minval=1, group=rsi_group, inline="rsi1")
rsiSource = input.source(close, "Source", group=rsi_group, inline="rsi1")
oversold = input.int(30, "Oversold Level", minval=0, maxval=100, group=rsi_group)
overbought = input.int(70, "Overbought Level", minval=0, maxval=100, group=rsi_group)
// --- MACD Settings ---
macd_group = "MACD Settings"
macdFast = input.int(12, "Fast Length", minval=1, group=macd_group, inline="macd1")
macdSlow = input.int(26, "Slow Length", minval=1, group=macd_group, inline="macd1")
macdSignal = input.int(9, "Signal Smoothing", minval=1, group=macd_group, inline="macd2")
// --- Divergence Settings ---
div_group = "Divergence Settings"
pivotLookback = input.int(5, "Pivot Lookback", minval=2, maxval=20, group=div_group, tooltip="How many bars on each side of a pivot to confirm it.")
minBarsBetweenPivots = input.int(5, "Min Bars Between Pivots", minval=1, group=div_group)
maxBarsBetweenPivots = input.int(60, "Max Bars Between Pivots", minval=1, group=div_group)
// --- Timeframe Toggles ---
tf_group = "Timeframe Selection"
use_5m = input.bool(true, "5m", group=tf_group, inline="tf1")
use_10m = input.bool(false, "10m", group=tf_group, inline="tf1")
use_15m = input.bool(true, "15m", group=tf_group, inline="tf1")
use_30m = input.bool(true, "30m", group=tf_group, inline="tf1")
use_45m = input.bool(false, "45m", group=tf_group, inline="tf2")
use_60m = input.bool(true, "1H", group=tf_group, inline="tf2")
use_120m = input.bool(false, "2H", group=tf_group, inline="tf2")
use_180m = input.bool(false, "3H", group=tf_group, inline="tf2")
use_240m = input.bool(true, "4H", group=tf_group, inline="tf3")
use_360m = input.bool(false, "6H", group=tf_group, inline="tf3")
use_480m = input.bool(false, "8H", group=tf_group, inline="tf3")
use_600m = input.bool(false, "10H", group=tf_group, inline="tf3")
use_720m = input.bool(false, "12H", group=tf_group, inline="tf4")
use_1d = input.bool(true, "1D", group=tf_group, inline="tf4")
use_2d = input.bool(false, "2D", group=tf_group, inline="tf4")
use_3d = input.bool(false, "3D", group=tf_group, inline="tf4")
use_1w = input.bool(true, "1W", group=tf_group, inline="tf5")
use_1M = input.bool(false, "1M", group=tf_group, inline="tf5")
// --- Display Settings ---
disp_group = "Display Settings"
showTable = input.bool(true, "Show Top 10 Table", group=disp_group)
tablePosition = input.string("Top Right", "Table Position",
options=["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"], group=disp_group)
showLabels = input.bool(true, "Show Labels on Chart (Current TF Only)", group=disp_group)
alertsEnabled = input.bool(true, "Enable Alerts", group=disp_group)
// =============================================================================
// FUNCTIONS
// =============================================================================
// Enhanced divergence detection with strength calculation
f_scanForDivergenceWithStrength() =>
float osc = na
if oscillatorType == "RSI"
osc := ta.rsi(rsiSource, rsiLength)
else
[_, _, hist] = ta.macd(close, macdFast, macdSlow, macdSignal)
osc := hist
osc_pivot_low_series = ta.pivotlow(osc, pivotLookback, pivotLookback)
price_pivot_low_series = ta.pivotlow(close, pivotLookback, pivotLookback)
osc_pivot_high_series = ta.pivothigh(osc, pivotLookback, pivotLookback)
price_pivot_high_series = ta.pivothigh(close, pivotLookback, pivotLookback)
var float bullStrength = 0.0
var float bearStrength = 0.0
isBullishDiv = false
if not na(osc_pivot_low_series)
for i = minBarsBetweenPivots to maxBarsBetweenPivots
if not na(osc[i]) and not na(close[i])
prevOscPivot = osc_pivot_low_series[i]
prevPricePivot = price_pivot_low_series[i]
if not na(prevOscPivot) and prevOscPivot < osc_pivot_low_series and not na(prevPricePivot) and prevPricePivot > price_pivot_low_series
if oscillatorType == "RSI" ? osc_pivot_low_series < oversold : osc_pivot_low_series < 0
isBullishDiv := true
// Calculate strength based on oscillator extremity and divergence magnitude
oscDiff = math.abs(osc_pivot_low_series - prevOscPivot)
priceDiff = math.abs(price_pivot_low_series - prevPricePivot) / close * 100
extremity = oscillatorType == "RSI" ? (50 - osc_pivot_low_series) / 50 : math.abs(osc_pivot_low_series)
bullStrength := (oscDiff * 10 + priceDiff + extremity * 20) / 3
break
isBearishDiv = false
if not na(osc_pivot_high_series)
for i = minBarsBetweenPivots to maxBarsBetweenPivots
if not na(osc[i]) and not na(close[i])
prevOscPivot = osc_pivot_high_series[i]
prevPricePivot = price_pivot_high_series[i]
if not na(prevOscPivot) and prevOscPivot > osc_pivot_high_series and not na(prevPricePivot) and prevPricePivot < price_pivot_high_series
if oscillatorType == "RSI" ? osc_pivot_high_series > overbought : osc_pivot_high_series > 0
isBearishDiv := true
// Calculate strength based on oscillator extremity and divergence magnitude
oscDiff = math.abs(osc_pivot_high_series - prevOscPivot)
priceDiff = math.abs(price_pivot_high_series - prevPricePivot) / close * 100
extremity = oscillatorType == "RSI" ? (osc_pivot_high_series - 50) / 50 : math.abs(osc_pivot_high_series)
bearStrength := (oscDiff * 10 + priceDiff + extremity * 20) / 3
break
[isBullishDiv, isBearishDiv, bullStrength, bearStrength]
// Simple divergence detection for current timeframe labels
f_scanForDivergence() =>
[bull, bear, _, _] = f_scanForDivergenceWithStrength()
[bull, bear]
// =============================================================================
// DIVERGENCE DATA STRUCTURE
// =============================================================================
// Define timeframe data
type DivergenceData
string tf_name
bool is_bull
bool is_bear
float strength
float tf_weight
bool enabled
// Timeframe weights (higher = stronger)
var float[] tf_weights = array.from(1.0, 1.2, 1.5, 2.0, 2.2, 3.0, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 8.0, 8.5, 9.0, 10.0, 12.0)
var string[] tf_names = array.from("5m", "10m", "15m", "30m", "45m", "1H", "2H", "3H", "4H", "6H", "8H", "10H", "12H", "1D", "2D", "3D", "1W", "1M")
var bool[] use_tfs = array.from(use_5m, use_10m, use_15m, use_30m, use_45m, use_60m, use_120m, use_180m, use_240m, use_360m, use_480m, use_600m, use_720m, use_1d, use_2d, use_3d, use_1w, use_1M)
// =============================================================================
// MULTI-TIMEFRAME ANALYSIS
// =============================================================================
// Get divergence data for each timeframe
[bull_5m_raw, bear_5m_raw, bull_str_5m, bear_str_5m] = request.security(syminfo.tickerid, "5", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_10m_raw, bear_10m_raw, bull_str_10m, bear_str_10m] = request.security(syminfo.tickerid, "10", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_15m_raw, bear_15m_raw, bull_str_15m, bear_str_15m] = request.security(syminfo.tickerid, "15", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_30m_raw, bear_30m_raw, bull_str_30m, bear_str_30m] = request.security(syminfo.tickerid, "30", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_45m_raw, bear_45m_raw, bull_str_45m, bear_str_45m] = request.security(syminfo.tickerid, "45", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_60m_raw, bear_60m_raw, bull_str_60m, bear_str_60m] = request.security(syminfo.tickerid, "60", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_120m_raw, bear_120m_raw, bull_str_120m, bear_str_120m] = request.security(syminfo.tickerid, "120", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_180m_raw, bear_180m_raw, bull_str_180m, bear_str_180m] = request.security(syminfo.tickerid, "180", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_240m_raw, bear_240m_raw, bull_str_240m, bear_str_240m] = request.security(syminfo.tickerid, "240", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_360m_raw, bear_360m_raw, bull_str_360m, bear_str_360m] = request.security(syminfo.tickerid, "360", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_480m_raw, bear_480m_raw, bull_str_480m, bear_str_480m] = request.security(syminfo.tickerid, "480", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_600m_raw, bear_600m_raw, bull_str_600m, bear_str_600m] = request.security(syminfo.tickerid, "600", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_720m_raw, bear_720m_raw, bull_str_720m, bear_str_720m] = request.security(syminfo.tickerid, "720", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_1d_raw, bear_1d_raw, bull_str_1d, bear_str_1d] = request.security(syminfo.tickerid, "D", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_2d_raw, bear_2d_raw, bull_str_2d, bear_str_2d] = request.security(syminfo.tickerid, "2D", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_3d_raw, bear_3d_raw, bull_str_3d, bear_str_3d] = request.security(syminfo.tickerid, "3D", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_1w_raw, bear_1w_raw, bull_str_1w, bear_str_1w] = request.security(syminfo.tickerid, "W", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
[bull_1M_raw, bear_1M_raw, bull_str_1M, bear_str_1M] = request.security(syminfo.tickerid, "M", f_scanForDivergenceWithStrength(), lookahead=barmerge.lookahead_on)
// Store all data in arrays
var bool[] all_bulls = array.from(bull_5m_raw, bull_10m_raw, bull_15m_raw, bull_30m_raw, bull_45m_raw, bull_60m_raw, bull_120m_raw, bull_180m_raw, bull_240m_raw, bull_360m_raw, bull_480m_raw, bull_600m_raw, bull_720m_raw, bull_1d_raw, bull_2d_raw, bull_3d_raw, bull_1w_raw, bull_1M_raw)
var bool[] all_bears = array.from(bear_5m_raw, bear_10m_raw, bear_15m_raw, bear_30m_raw, bear_45m_raw, bear_60m_raw, bear_120m_raw, bear_180m_raw, bear_240m_raw, bear_360m_raw, bear_480m_raw, bear_600m_raw, bear_720m_raw, bear_1d_raw, bear_2d_raw, bear_3d_raw, bear_1w_raw, bear_1M_raw)
var float[] all_bull_strengths = array.from(bull_str_5m, bull_str_10m, bull_str_15m, bull_str_30m, bull_str_45m, bull_str_60m, bull_str_120m, bull_str_180m, bull_str_240m, bull_str_360m, bull_str_480m, bull_str_600m, bull_str_720m, bull_str_1d, bull_str_2d, bull_str_3d, bull_str_1w, bull_str_1M)
var float[] all_bear_strengths = array.from(bear_str_5m, bear_str_10m, bear_str_15m, bear_str_30m, bear_str_45m, bear_str_60m, bear_str_120m, bear_str_180m, bear_str_240m, bear_str_360m, bear_str_480m, bear_str_600m, bear_str_720m, bear_str_1d, bear_str_2d, bear_str_3d, bear_str_1w, bear_str_1M)
// Create divergence objects and sort by strength
var array<DivergenceData> top_divergences = array.new<DivergenceData>()
if barstate.islast
array.clear(top_divergences)
// Collect all valid divergences
for i = 0 to array.size(tf_names) - 1
if array.get(use_tfs, i)
tf_weight = array.get(tf_weights, i)
tf_name = array.get(tf_names, i)
// Add bullish divergences
if array.get(all_bulls, i)
strength = array.get(all_bull_strengths, i) * tf_weight
div_data = DivergenceData.new(tf_name, true, false, strength, tf_weight, true)
array.push(top_divergences, div_data)
// Add bearish divergences
if array.get(all_bears, i)
strength = array.get(all_bear_strengths, i) * tf_weight
div_data = DivergenceData.new(tf_name, false, true, strength, tf_weight, true)
array.push(top_divergences, div_data)
// Sort by strength (descending)
if array.size(top_divergences) > 1
for i = 0 to array.size(top_divergences) - 2
for j = i + 1 to array.size(top_divergences) - 1
if array.get(top_divergences, i).strength < array.get(top_divergences, j).strength
temp = array.get(top_divergences, i)
array.set(top_divergences, i, array.get(top_divergences, j))
array.set(top_divergences, j, temp)
// =============================================================================
// DISPLAY
// =============================================================================
var table divTable = na
if showTable and barstate.islast
pos = tablePosition == "Top Left" ? position.top_left : tablePosition == "Top Center" ? position.top_center : tablePosition == "Top Right" ? position.top_right : tablePosition == "Middle Left" ? position.middle_left : tablePosition == "Middle Center" ? position.middle_center : tablePosition == "Middle Right" ? position.middle_right : tablePosition == "Bottom Left" ? position.bottom_left : tablePosition == "Bottom Center" ? position.bottom_center : position.bottom_right
divTable := table.new(pos, 4, 12, bgcolor=color.new(color.black, 80), border_width=1)
table.cell(divTable, 0, 0, "Rank", text_color=color.white, bgcolor=color.new(color.gray, 50))
table.cell(divTable, 1, 0, "Timeframe", text_color=color.white, bgcolor=color.new(color.gray, 50))
table.cell(divTable, 2, 0, "Type", text_color=color.white, bgcolor=color.new(color.gray, 50))
table.cell(divTable, 3, 0, "Strength", text_color=color.white, bgcolor=color.new(color.gray, 50))
display_count = math.min(10, array.size(top_divergences))
if display_count > 0
for i = 0 to display_count - 1
div_data = array.get(top_divergences, i)
rank = "#" + str.tostring(i + 1)
div_type = div_data.is_bull ? "BULL" : "BEAR"
strength_str = str.tostring(math.round(div_data.strength, 1))
rank_color = i < 3 ? color.yellow : color.white
type_color = div_data.is_bull ? color.lime : color.red
type_bg = div_data.is_bull ? color.new(color.green, 80) : color.new(color.red, 80)
table.cell(divTable, 0, i + 1, rank, text_color=rank_color)
table.cell(divTable, 1, i + 1, div_data.tf_name, text_color=color.white)
table.cell(divTable, 2, i + 1, div_type, text_color=type_color, bgcolor=type_bg)
table.cell(divTable, 3, i + 1, strength_str, text_color=color.white)
else
table.cell(divTable, 0, 1, "No divergences found", text_color=color.gray)
table.cell(divTable, 1, 1, "", text_color=color.gray)
table.cell(divTable, 2, 1, "", text_color=color.gray)
table.cell(divTable, 3, 1, "", text_color=color.gray)
// --- Chart Labels (Current timeframe only) ---
var label bullLabel = na
var label bearLabel = na
if showLabels
[bull_current, bear_current] = f_scanForDivergence()
if bull_current
label.delete(bullLabel)
bullLabel := label.new(bar_index[pivotLookback], low[pivotLookback], "Bull Div", color=color.new(color.green, 20), style=label.style_label_up, textcolor=color.white, size=size.small)
if bear_current
label.delete(bearLabel)
bearLabel := label.new(bar_index[pivotLookback], high[pivotLookback], "Bear Div", color=color.new(color.red, 20), style=label.style_label_down, textcolor=color.white, size=size.small)
// =============================================================================
// ALERTS
// =============================================================================
var int lastTopDivCount = 0
if alertsEnabled and barstate.islast
currentTopDivCount = array.size(top_divergences)
if currentTopDivCount > 0 and currentTopDivCount != lastTopDivCount
top_div = array.get(top_divergences, 0)
div_type = top_div.is_bull ? "Bullish" : "Bearish"
alert("New Top " + div_type + " " + oscillatorType + " Divergence: " + top_div.tf_name + " (Strength: " + str.tostring(math.round(top_div.strength, 1)) + ") - " + syminfo.ticker, alert.freq_once_per_bar_close)
lastTopDivCount := currentTopDivCount
Key Changes:
-
Enhanced Strength Calculation: The divergences are now scored based on:
- Oscillator extremity (how oversold/overbought)
- Divergence magnitude (difference between pivots)
- Timeframe weight (higher timeframes = stronger)
-
Top 10 Display: The table now shows only the strongest 10 divergences ranked by strength.
-
Sorting Algorithm: Manual bubble sort to rank divergences by strength.
-
Improved Table: Shows rank, timeframe, type (BULL/BEAR), and strength score.
-
Better Alerts: Alerts now include the strongest divergence details.
The script will now focus on showing you the most significant divergences across all enabled timeframes, helping you prioritize which signals to pay attention to!