Suggestion: //@version=5
indicator("Multi Time Frame Trend Detector", overlay=true)

// User Inputs
higher_tf = input.timeframe("D", "Higher Time Frame")
intermediate_tf = input.timeframe("60", "Intermediate Time Frame")
pivot_left = input.int(5, "Pivot Left Bars", minval=1)
pivot_right = input.int(5, "Pivot Right Bars", minval=1)
ma_length = input.int(21, "MA Length")

// === Higher Time Frame Data ===
// Get Higher Time Frame Data
htf_high = request.security(syminfo.tickerid, higher_tf, high)
htf_low = request.security(syminfo.tickerid, higher_tf, low)
htf_close = request.security(syminfo.tickerid, higher_tf, close)

// Detect Pivots in Higher Time Frame
htf_pivot_high = ta.pivothigh(htf_high, pivot_left, pivot_right)
htf_pivot_low = ta.pivotlow(htf_low, pivot_left, pivot_right)

// Shift pivots and data back by pivot_right bars to reduce lag
htf_pivot_high_shifted = htf_pivot_high[pivot_right]
htf_pivot_low_shifted = htf_pivot_low[pivot_right]
htf_close_shifted = htf_close[pivot_right]
htf_ma = ta.sma(htf_close, ma_length)
htf_ma_shifted = htf_ma[pivot_right]

// Initialize Higher Time Frame Trend Variable
var string htf_trend = na

// Determine Trend Based on Shifted Pivots for Higher Time Frame
if not na(htf_pivot_high_shifted)
    htf_trend := "Downtrend"
else if not na(htf_pivot_low_shifted)
    htf_trend := "Uptrend"
else
    htf_trend := na(htf_trend[1]) ? "Sideways" : htf_trend[1]

// Use Moving Average to Supplement Trend Detection for Higher Time Frame
if htf_close_shifted > htf_ma_shifted
    htf_trend := htf_trend == "Downtrend" ? "Sideways" : "Uptrend"
else
    htf_trend := htf_trend == "Uptrend" ? "Sideways" : "Downtrend"

// === Intermediate Time Frame Data ===
// Get Intermediate Time Frame Data
itf_high = request.security(syminfo.tickerid, intermediate_tf, high)
itf_low = request.security(syminfo.tickerid, intermediate_tf, low)
itf_close = request.security(syminfo.tickerid, intermediate_tf, close)

// Detect Pivots in Intermediate Time Frame
itf_pivot_high = ta.pivothigh(itf_high, pivot_left, pivot_right)
itf_pivot_low = ta.pivotlow(itf_low, pivot_left, pivot_right)

// Shift pivots and data back by pivot_right bars to reduce lag
itf_pivot_high_shifted = itf_pivot_high[pivot_right]
itf_pivot_low_shifted = itf_pivot_low[pivot_right]
itf_close_shifted = itf_close[pivot_right]
itf_ma = ta.sma(itf_close, ma_length)
itf_ma_shifted = itf_ma[pivot_right]

// Initialize Intermediate Time Frame Trend Variable
var string itf_trend = na

// Determine Trend Based on Shifted Pivots for Intermediate Time Frame
if not na(itf_pivot_high_shifted)
    itf_trend := "Downtrend"
else if not na(itf_pivot_low_shifted)
    itf_trend := "Uptrend"
else
    itf_trend := na(itf_trend[1]) ? "Sideways" : itf_trend[1]

// Use Moving Average to Supplement Trend Detection for Intermediate Time Frame
if itf_close_shifted > itf_ma_shifted
    itf_trend := itf_trend == "Downtrend" ? "Sideways" : "Uptrend"
else
    itf_trend := itf_trend == "Uptrend" ? "Sideways" : "Downtrend"

// === Display Trend Conditions Using Table ===
// Create Table with 2 columns for HTF and ITF trends
var table trend_table = table.new(position.top_right, 1, 2, border_width=1)

// Background colors for trends
bgcolor_htf = htf_trend == "Uptrend" ? color.new(color.green, 70) : htf_trend == "Downtrend" ? color.new(color.red, 70) : color.new(color.orange, 70)
bgcolor_itf = itf_trend == "Uptrend" ? color.new(color.green, 70) : itf_trend == "Downtrend" ? color.new(color.red, 70) : color.new(color.orange, 70)

// Update the table cells on every bar
table.cell(trend_table, 0, 0, "HTF Trend: " + htf_trend, text_color=color.white, bgcolor=bgcolor_htf)
table.cell(trend_table, 0, 1, "ITF Trend: " + itf_trend, text_color=color.white, bgcolor=bgcolor_itf)

// === Plotting for Visualization ===
// Plot Higher Time Frame Pivots and Moving Average
plot(not na(htf_pivot_high_shifted) ? htf_high[pivot_right * 2] : na, "HTF Pivot High", color=color.red, style=plot.style_circles, linewidth=2)
plot(not na(htf_pivot_low_shifted) ? htf_low[pivot_right * 2] : na, "HTF Pivot Low", color=color.lime, style=plot.style_circles, linewidth=2)
plot(htf_ma, "HTF MA", color=color.blue)

// Plot Intermediate Time Frame Pivots and Moving Average
plot(not na(itf_pivot_high_shifted) ? itf_high[pivot_right * 2] : na, "ITF Pivot High", color=color.purple, style=plot.style_circles, linewidth=2)
plot(not na(itf_pivot_low_shifted) ? itf_low[pivot_right * 2] : na, "ITF Pivot Low", color=color.yellow, style=plot.style_circles, linewidth=2)
plot(itf_ma, "ITF MA", color=color.orange)