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

// === User Inputs ===

// Time Frame Inputs
higher_tf = input.timeframe("D", "Higher Time Frame")
intermediate_tf = input.timeframe("60", "Intermediate Time Frame")

// Pivot Parameters
pivot_left = input.int(5, "Pivot Left Bars", minval=1)
pivot_right = input.int(5, "Pivot Right Bars", minval=1)
num_pivots_to_show = input.int(5, "Number of Recent Pivots to Show", minval=1)  // Note: Not used anymore

// Moving Average Length
ma_length = input.int(21, "MA Length")

// === Time Frame Data Retrieval ===

// 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)

// 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)

// === Pivot Point Detection ===

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

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

// Shift pivots back by pivot_right bars
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]

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]

// === Moving Averages ===

htf_ma = ta.sma(htf_close, ma_length)
htf_ma_shifted = htf_ma[pivot_right]

itf_ma = ta.sma(itf_close, ma_length)
itf_ma_shifted = itf_ma[pivot_right]

// === Trend Determination ===

// Higher Time Frame Trend
var string htf_trend = na
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]

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 Trend
var string itf_trend = na
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]

if itf_close_shifted > itf_ma_shifted
    itf_trend := itf_trend == "Downtrend" ? "Sideways" : "Uptrend"
else
    itf_trend := itf_trend == "Uptrend" ? "Sideways" : "Downtrend"

// === Trend Table Display ===

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

// Default Trend Table Colors
htf_trend_up_color_default = color.new(color.green, 70)
htf_trend_down_color_default = color.new(color.red, 70)
htf_trend_sideways_color_default = color.new(color.orange, 70)

itf_trend_up_color_default = color.new(color.green, 70)
itf_trend_down_color_default = color.new(color.red, 70)
itf_trend_sideways_color_default = color.new(color.orange, 70)

// Apply colors to the trend backgrounds
htf_bgcolor = htf_trend == "Uptrend" ? htf_trend_up_color_default :
              htf_trend == "Downtrend" ? htf_trend_down_color_default :
              htf_trend_sideways_color_default

itf_bgcolor = itf_trend == "Uptrend" ? itf_trend_up_color_default :
              itf_trend == "Downtrend" ? itf_trend_down_color_default :
              itf_trend_sideways_color_default

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

// === Pivot Plotting Management ===

// Default pivot plot styles
htf_pivot_high_color_default = color.red
htf_pivot_low_color_default = color.lime
itf_pivot_high_color_default = color.purple
itf_pivot_low_color_default = color.yellow
pivot_shape_size = size.tiny

// Plot pivot points for HTF
plotshape(not na(htf_pivot_high_shifted) ? htf_high[pivot_right * 2] : na, title="HTF Pivot High", style=shape.triangledown, location=location.absolute, color=htf_pivot_high_color_default, size=pivot_shape_size, offset=-pivot_right * 2)
plotshape(not na(htf_pivot_low_shifted) ? htf_low[pivot_right * 2] : na, title="HTF Pivot Low", style=shape.triangleup, location=location.absolute, color=htf_pivot_low_color_default, size=pivot_shape_size, offset=-pivot_right * 2)

// Plot pivot points for ITF
plotshape(not na(itf_pivot_high_shifted) ? itf_high[pivot_right * 2] : na, title="ITF Pivot High", style=shape.triangledown, location=location.absolute, color=itf_pivot_high_color_default, size=pivot_shape_size, offset=-pivot_right * 2)
plotshape(not na(itf_pivot_low_shifted) ? itf_low[pivot_right * 2] : na, title="ITF Pivot Low", style=shape.triangleup, location=location.absolute, color=itf_pivot_low_color_default, size=pivot_shape_size, offset=-pivot_right * 2)

// === Moving Averages Plotting ===

// Plot the moving averages (properties adjustable from the Style tab)
plot(htf_ma, title="HTF MA")
plot(itf_ma, title="ITF MA")