I see the issue. Pine Script doesn't support lambda functions (arrow functions). Here's the corrected code:
//@version=5
indicator("Multi-TF Divergence Scanner (RSI/MACD) [Final Fix]", "MTF Div Scan ✓", 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 Summary 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
// =============================================================================
f_scanForDivergence() =>
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)
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
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
break
[isBullishDiv, isBearishDiv]
// =============================================================================
// MULTI-TIMEFRAME ANALYSIS
// =============================================================================
// --- SYNTAX ERROR FIX: THIS IS THE CORRECTED SECTION ---
// We first get the raw tuple from the security call.
// Then, we check the 'use_tf' boolean to determine the final value.
[bull_5m_raw, bear_5m_raw] = request.security(syminfo.tickerid, "5", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_5m = use_5m and nz(bull_5m_raw, false)
bear_5m = use_5m and nz(bear_5m_raw, false)
[bull_10m_raw, bear_10m_raw] = request.security(syminfo.tickerid, "10", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_10m = use_10m and nz(bull_10m_raw, false)
bear_10m = use_10m and nz(bear_10m_raw, false)
[bull_15m_raw, bear_15m_raw] = request.security(syminfo.tickerid, "15", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_15m = use_15m and nz(bull_15m_raw, false)
bear_15m = use_15m and nz(bear_15m_raw, false)
[bull_30m_raw, bear_30m_raw] = request.security(syminfo.tickerid, "30", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_30m = use_30m and nz(bull_30m_raw, false)
bear_30m = use_30m and nz(bear_30m_raw, false)
[bull_45m_raw, bear_45m_raw] = request.security(syminfo.tickerid, "45", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_45m = use_45m and nz(bull_45m_raw, false)
bear_45m = use_45m and nz(bear_45m_raw, false)
[bull_60m_raw, bear_60m_raw] = request.security(syminfo.tickerid, "60", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_60m = use_60m and nz(bull_60m_raw, false)
bear_60m = use_60m and nz(bear_60m_raw, false)
[bull_120m_raw, bear_120m_raw] = request.security(syminfo.tickerid, "120", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_120m = use_120m and nz(bull_120m_raw, false)
bear_120m = use_120m and nz(bear_120m_raw, false)
[bull_180m_raw, bear_180m_raw] = request.security(syminfo.tickerid, "180", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_180m = use_180m and nz(bull_180m_raw, false)
bear_180m = use_180m and nz(bear_180m_raw, false)
[bull_240m_raw, bear_240m_raw] = request.security(syminfo.tickerid, "240", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_240m = use_240m and nz(bull_240m_raw, false)
bear_240m = use_240m and nz(bear_240m_raw, false)
[bull_360m_raw, bear_360m_raw] = request.security(syminfo.tickerid, "360", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_360m = use_360m and nz(bull_360m_raw, false)
bear_360m = use_360m and nz(bear_360m_raw, false)
[bull_480m_raw, bear_480m_raw] = request.security(syminfo.tickerid, "480", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_480m = use_480m and nz(bull_480m_raw, false)
bear_480m = use_480m and nz(bear_480m_raw, false)
[bull_600m_raw, bear_600m_raw] = request.security(syminfo.tickerid, "600", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_600m = use_600m and nz(bull_600m_raw, false)
bear_600m = use_600m and nz(bear_600m_raw, false)
[bull_720m_raw, bear_720m_raw] = request.security(syminfo.tickerid, "720", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_720m = use_720m and nz(bull_720m_raw, false)
bear_720m = use_720m and nz(bear_720m_raw, false)
[bull_1d_raw, bear_1d_raw] = request.security(syminfo.tickerid, "D", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_1d = use_1d and nz(bull_1d_raw, false)
bear_1d = use_1d and nz(bear_1d_raw, false)
[bull_2d_raw, bear_2d_raw] = request.security(syminfo.tickerid, "2D", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_2d = use_2d and nz(bull_2d_raw, false)
bear_2d = use_2d and nz(bear_2d_raw, false)
[bull_3d_raw, bear_3d_raw] = request.security(syminfo.tickerid, "3D", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_3d = use_3d and nz(bull_3d_raw, false)
bear_3d = use_3d and nz(bear_3d_raw, false)
[bull_1w_raw, bear_1w_raw] = request.security(syminfo.tickerid, "W", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_1w = use_1w and nz(bull_1w_raw, false)
bear_1w = use_1w and nz(bear_1w_raw, false)
[bull_1M_raw, bear_1M_raw] = request.security(syminfo.tickerid, "M", f_scanForDivergence(), lookahead=barmerge.lookahead_on)
bull_1M = use_1M and nz(bull_1M_raw, false)
bear_1M = use_1M and nz(bear_1M_raw, false)
// Data structures to hold settings and results for efficient looping in the display table
tf_names = array.from("5m", "10m", "15m", "30m", "45m", "1H", "2H", "3H", "4H", "6H", "8H", "10H", "12H", "1D", "2D", "3D", "1W", "1M")
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)
all_bull_divs = array.from(bull_5m, bull_10m, bull_15m, bull_30m, bull_45m, bull_60m, bull_120m, bull_180m, bull_240m, bull_360m, bull_480m, bull_600m, bull_720m, bull_1d, bull_2d, bull_3d, bull_1w, bull_1M)
all_bear_divs = array.from(bear_5m, bear_10m, bear_15m, bear_30m, bear_45m, bear_60m, bear_120m, bear_180m, bear_240m, bear_360m, bear_480m, bear_600m, bear_720m, bear_1d, bear_2d, bear_3d, bear_1w, bear_1M)
// Count divergences - Fixed syntax error (removed lambda functions)
bullishCount = 0.0
bearishCount = 0.0
for i = 0 to array.size(all_bull_divs) - 1
if array.get(all_bull_divs, i)
bullishCount += 1.0
if array.get(all_bear_divs, i)
bearishCount += 1.0
// =============================================================================
// 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, 3, 20, bgcolor=color.new(color.black, 80), border_width=1)
table.cell(divTable, 0, 0, "Timeframe", text_color=color.white, bgcolor=color.new(color.gray, 50))
table.cell(divTable, 1, 0, "Bullish", text_color=color.white, bgcolor=color.new(color.gray, 50))
table.cell(divTable, 2, 0, "Bearish", text_color=color.white, bgcolor=color.new(color.gray, 50))
rowIndex = 1
for i = 0 to array.size(tf_names) - 1
if array.get(use_tfs, i)
isBull = array.get(all_bull_divs, i)
isBear = array.get(all_bear_divs, i)
table.cell(divTable, 0, rowIndex, array.get(tf_names, i), text_color=color.white)
table.cell(divTable, 1, rowIndex, isBull ? "✓" : "-", text_color=isBull ? color.lime : color.gray, bgcolor=isBull ? color.new(color.green, 80) : na)
table.cell(divTable, 2, rowIndex, isBear ? "✓" : "-", text_color=isBear ? color.red : color.gray, bgcolor=isBear ? color.new(color.red, 80) : na)
rowIndex += 1
table.cell(divTable, 0, rowIndex, "TOTAL", text_color=color.white, bgcolor=color.new(color.blue, 50))
table.cell(divTable, 1, rowIndex, str.tostring(bullishCount), text_color=bullishCount > 0 ? color.lime : color.white, bgcolor=color.new(color.blue, 50))
table.cell(divTable, 2, rowIndex, str.tostring(bearishCount), text_color=bearishCount > 0 ? color.red : color.white, bgcolor=color.new(color.blue, 50))
// --- Chart Labels (Properly Managed) ---
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 lastBullCount = 0.0
var lastBearCount = 0.0
if alertsEnabled
if bullishCount > 0 and bullishCount != lastBullCount
alert("Bullish " + oscillatorType + " Divergence: " + str.tostring(bullishCount) + " TF(s) - " + syminfo.ticker, alert.freq_once_per_bar_close)
if bearishCount > 0 and bearishCount != lastBearCount
alert("Bearish " + oscillatorType + " Divergence: " + str.tostring(bearishCount) + " TF(s) - " + syminfo.ticker, alert.freq_once_per_bar_close)
lastBullCount := bullishCount
lastBearCount := bearishCount
The main fix was replacing the lambda functions (arrow functions) with a proper for loop to count the divergences:
// Old (incorrect for Pine Script):
// bullishCount = array.sum(array.map(all_bull_divs, b => b ? 1.0 : 0.0))
// New (correct):
bullishCount = 0.0
bearishCount = 0.0
for i = 0 to array.size(all_bull_divs) - 1
if array.get(all_bull_divs, i)
bullishCount += 1.0
if array.get(all_bear_divs, i)
bearishCount += 1.0
This should now compile without errors in TradingView.