Keep Early Trend, High Or Low as it, Don't Convert it to Sideways until It Break 2nd Pivot: //@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 = 'Sideways'
var string earlyTrend = 'Sideways'
var string newTrend = 'Sideways'  // For the new logic
var int consecutivePivotHighs = 0  // Counter for consecutive pivot highs
var int consecutivePivotLows = 0   // Counter for consecutive pivot lows

// 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)
    consecutivePivotHighs += 1
    consecutivePivotLows := 0  // Reset low counter

// 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)
    consecutivePivotLows += 1
    consecutivePivotHighs := 0  // Reset high counter

// New trend detection logic based on consecutive pivots
if consecutivePivotHighs >= 3
    newTrend := 'Uptrend'
else if consecutivePivotLows >= 3
    newTrend := 'Downtrend'
else
    newTrend := 'Sideways'  // Set to 'Sideways' if conditions are not met

// 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 := 'Sideways'  // Set to 'Sideways' if conditions are not met
else
    earlyTrend := 'Sideways'  // Set to 'Sideways' if insufficient data

// 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 := 'Sideways'  // Set to 'Sideways' if conditions are not met
else
    trend := 'Sideways'  // Set to 'Sideways' if insufficient data

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

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

// Determine the background color based on the trends
bgcolor_earlyTrend = earlyTrend == 'Uptrend' ? color.new(color.green, 0) :
                     earlyTrend == 'Downtrend' ? color.new(color.red, 0) :
                     color.new(color.gray, 0)

bgcolor_newTrend = newTrend == 'Uptrend' ? color.new(color.green, 0) :
                   newTrend == 'Downtrend' ? color.new(color.red, 0) :
                   color.new(color.gray, 0)

bgcolor_oldTrend = trend == 'Uptrend' ? color.new(color.green, 0) :
                   trend == 'Downtrend' ? color.new(color.red, 0) :
                   color.new(color.gray, 0)

// Update the table with the current trends
if barstate.islast
    table.cell(trendTable, 0, 0, 'Early Trend: ' + earlyTrend + ' (' + htf + ')',
         bgcolor=bgcolor_earlyTrend, text_color=color.white)
    table.cell(trendTable, 0, 1, 'New Trend: ' + newTrend + ' (' + htf + ')',
         bgcolor=bgcolor_newTrend, text_color=color.white)
    table.cell(trendTable, 0, 2, 'Old Trend: ' + trend + ' (' + htf + ')',
         bgcolor=bgcolor_oldTrend, text_color=color.white)