If Trend is Not UP or Down in Both Method, Then Describe that as Sideways: //@version=5
indicator("Trend Indicator with Early Trend Detection", overlay=true)

// User inputs
htf = input.timeframe('1D', 'Higher Time Frame')  // Default higher time frame is 1 Day
left = input.int(4, 'Pivot Left Bars', minval=1)  // Number of left bars for pivot detection
right = input.int(2, 'Pivot Right Bars', minval=1)  // Number of right bars for pivot detection

// Request higher time frame data
htf_high = request.security(syminfo.tickerid, htf, high)
htf_low = request.security(syminfo.tickerid, htf, low)
htf_close = request.security(syminfo.tickerid, htf, close)

// Calculate pivot highs and lows on the higher time frame
htf_pivotHigh = request.security(syminfo.tickerid, htf, ta.pivothigh(htf_high, left, right), lookahead=barmerge.lookahead_on)
htf_pivotLow = request.security(syminfo.tickerid, htf, ta.pivotlow(htf_low, left, right), lookahead=barmerge.lookahead_on)

// Plot the pivot points at the exact bars where they occurred
plotshape(not na(htf_pivotHigh), title="Pivot High", style=shape.triangledown,
     location=location.abovebar, offset=-right, color=color.red, size=size.tiny,
     text="PH", textcolor=color.white)
plotshape(not na(htf_pivotLow), title="Pivot Low", style=shape.triangleup,
     location=location.belowbar, offset=-right, color=color.green, size=size.tiny,
     text="PL", textcolor=color.white)

// Initialize variables for trend detection
var float[] pivotHighs = array.new_float()
var float[] pivotLows = array.new_float()
var string trend = 'Unknown'
var string earlyTrend = 'Unknown'

// Collect pivot highs
if not na(htf_pivotHigh)
    pivotHighTime = bar_index - right
    pivotHighValue = htf_pivotHigh
    array.unshift(pivotHighs, pivotHighValue)
    if array.size(pivotHighs) > 3
        array.pop(pivotHighs)

// Collect pivot lows
if not na(htf_pivotLow)
    pivotLowTime = bar_index - right
    pivotLowValue = htf_pivotLow
    array.unshift(pivotLows, pivotLowValue)
    if array.size(pivotLows) > 3
        array.pop(pivotLows)

// Early trend detection logic using last three pivots
if array.size(pivotHighs) >= 3 and array.size(pivotLows) >= 3
    recentHigh = array.get(pivotHighs, 0)
    recentLow = array.get(pivotLows, 0)
    
    // Check if price crosses recent higher high
    if htf_close > recentHigh
        earlyTrend := 'Uptrend'
    // Check if price crosses recent lower low
    else if htf_close < recentLow
        earlyTrend := 'Downtrend'
    else
        earlyTrend := earlyTrend  // Keep previous early trend

// Existing trend detection logic (4 pivots)
var float previousHigh = na
var float previousLow = na
var float currentHigh = na
var float currentLow = na

// Update pivot highs and lows
if not na(htf_pivotHigh)
    previousHigh := currentHigh
    currentHigh := htf_pivotHigh

if not na(htf_pivotLow)
    previousLow := currentLow
    currentLow := htf_pivotLow

// Shift the pivot values forward by 'right' bars to align with the pivot occurrence
currentHighShifted = currentHigh[right]
previousHighShifted = previousHigh[right]
currentLowShifted = currentLow[right]
previousLowShifted = previousLow[right]

// Determine the trend using the shifted pivot values
if not na(previousHighShifted) and not na(previousLowShifted) and not na(currentHighShifted) and not na(currentLowShifted)
    if currentHighShifted > previousHighShifted and currentLowShifted > previousLowShifted
        trend := 'Uptrend'
    else if currentHighShifted < previousHighShifted and currentLowShifted < previousLowShifted
        trend := 'Downtrend'
    else
        trend := trend  // Keep the previous trend if conditions are not met

// Combine trends for display
displayTrend = earlyTrend != 'Unknown' ? earlyTrend : trend

// Create a table to display the trend
var table trendTable = table.new(position.top_right, 1, 1)

// Determine the background color based on the trend
bgcolor = displayTrend == 'Uptrend' ? color.new(color.green, 0) :
          displayTrend == 'Downtrend' ? color.new(color.red, 0) :
          color.new(color.gray, 0)

// Update the table with the current trend
if barstate.islast
    table.cell(trendTable, 0, 0, displayTrend + ' (' + htf + ')',
         bgcolor=bgcolor, text_color=color.white)