Suggestion: //@version=5 indicator("Higher Time Frame Trend Detector", overlay=true) //…
- User
Suggestion: //@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) ma_length = input.int(21, "MA Length")
// 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)
// Shift pivots and data back by pivot_right bars to reduce lag pivot_high_shifted = pivot_high[pivot_right] pivot_low_shifted = pivot_low[pivot_right] htf_close_shifted = htf_close[pivot_right] htf_ma = ta.sma(htf_close, ma_length) htf_ma_shifted = htf_ma[pivot_right]
// Initialize Trend Variable var string trend = na
// Determine Trend Based on Shifted Pivots if not na(pivot_high_shifted) trend := "Downtrend" else if not na(pivot_low_shifted) trend := "Uptrend" else trend := na(trend[1]) ? "Sideways" : trend[1]
// Use Moving Average to Supplement Trend Detection with shifted data if htf_close_shifted > htf_ma_shifted 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)
// Update the table cell on every bar (removed the conditional) table.cell(trend_table, 0, 0, "Trend: " + trend, text_color=color.white, bgcolor=bgcolor)
// Plotting for Visualization plot(not na(pivot_high_shifted) ? htf_high[pivot_right * 2] : na, "Pivot High", color=color.red, style=plot.style_circles, linewidth=2) plot(not na(pivot_low_shifted) ? htf_low[pivot_right * 2] : na, "Pivot Low", color=color.lime, style=plot.style_circles, linewidth=2) plot(htf_ma, "Higher TF MA", color=color.blue)
- Assistant