Trend Indicator Does not Changes Label Immediately as Trend Changes so fix that, it is…
- User
Trend Indicator Does not Changes Label Immediately as Trend Changes so fix that, it is lagging while replay: //@version=5 indicator("Higher Time Frame Trend Detector", overlay=true)
// User Inputs higher_tf = input.timeframe("D", "Higher Time Frame") pivot_left = input.int(5, "Pivot Left Bars", minval=1) pivot_right = input.int(5, "Pivot Right Bars", minval=1)
// 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 pivot_high = ta.pivothigh(htf_high, pivot_left, pivot_right) pivot_low = ta.pivotlow(htf_low, pivot_left, pivot_right)
// Initialize Trend Variable var string trend = na
// Determine Trend Based on Pivots if not na(pivot_high) trend := "Downtrend" else if not na(pivot_low) trend := "Uptrend" else trend := na(trend[1]) ? "Sideways" : trend[1]
// Optional: Use Moving Average to Supplement Trend Detection ma_length = input.int(21, "MA Length") htf_ma = ta.sma(htf_close, ma_length) if htf_close > htf_ma trend := trend == "Downtrend" ? "Sideways" : "Uptrend" else trend := trend == "Uptrend" ? "Sideways" : "Downtrend"
// Display Trend Condition using Table var table trend_table = table.new(position.top_right, 1, 1)
bgcolor = trend == "Uptrend" ? color.new(color.green, 70) : trend == "Downtrend" ? color.new(color.red, 70) : color.new(color.orange, 70)
if barstate.islastconfirmedhistory table.cell(trend_table, 0, 0, "Trend: " + trend, text_color=color.white, bgcolor=bgcolor)
// Plotting for Visualization (Optional) plot(not na(pivot_high) ? htf_high[pivot_right] : na, "Pivot High", color=color.red, style=plot.style_circles, linewidth=2) plot(not na(pivot_low) ? htf_low[pivot_right] : na, "Pivot Low", color=color.lime, style=plot.style_circles, linewidth=2) plot(htf_ma, "Higher TF MA", color=color.blue)
- Assistant