Use Plot Instead of Label: //@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)

// 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 Labels Management ===

// Initialize label arrays
var label[] htf_pivot_high_labels = array.new<label>()
var label[] htf_pivot_low_labels = array.new<label>()
var label[] itf_pivot_high_labels = array.new<label>()
var label[] itf_pivot_low_labels = array.new<label>()

// Default pivot label 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_label_size_default = size.tiny

// Function to create and manage pivot labels
create_pivot_label(_labelsArray, _numPivotsToShow, _barIndex, _price, _color, _size) =>
    label l = label.new(_barIndex, _price, style=label.style_circle, color=_color, size=_size)
    array.unshift(_labelsArray, l)
    if array.size(_labelsArray) > _numPivotsToShow
        label.delete(array.pop(_labelsArray))

// Plot and manage pivot labels for HTF
if not na(htf_pivot_high_shifted)
    create_pivot_label(htf_pivot_high_labels, num_pivots_to_show, bar_index - pivot_right * 2, htf_high[pivot_right * 2], htf_pivot_high_color_default, pivot_label_size_default)

if not na(htf_pivot_low_shifted)
    create_pivot_label(htf_pivot_low_labels, num_pivots_to_show, bar_index - pivot_right * 2, htf_low[pivot_right * 2], htf_pivot_low_color_default, pivot_label_size_default)

// Plot and manage pivot labels for ITF
if not na(itf_pivot_high_shifted)
    create_pivot_label(itf_pivot_high_labels, num_pivots_to_show, bar_index - pivot_right * 2, itf_high[pivot_right * 2], itf_pivot_high_color_default, pivot_label_size_default)

if not na(itf_pivot_low_shifted)
    create_pivot_label(itf_pivot_low_labels, num_pivots_to_show, bar_index - pivot_right * 2, itf_low[pivot_right * 2], itf_pivot_low_color_default, pivot_label_size_default)

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