Create Indicator that can Detect Trend in Higher Time Frame as Write that on Chart (Top…
- User
Create Indicator that can Detect Trend in Higher Time Frame as Write that on Chart (Top Right Side)
- Assistant
Create Indicator that can Detect Trend in Higher Time Frame as Write that on Chart (Top Right Side)
Certainly! I'd be happy to help you create an indicator that detects the trend on a higher time frame and displays it on your chart in the top right corner.
I'll provide a script using TradingView's Pine Script (version 5) as it's a popular platform for technical analysis. This script will:
//@version=5
indicator("HTF Trend Indicator", overlay=true)
// — Inputs —
htf = input.timeframe(title="Higher Time Frame", defval="D")
ma_length = input.int(title="Moving Average Length", defval=50, minval=1)
ma_type = input.string(title="Moving Average Type", defval="SMA", options=["SMA", "EMA"])
show_label = input.bool(title="Show Trend Label", defval=true)
// — Functions —
// Get higher time frame data
get_htf_data(_source) =>
request.security(syminfo.tickerid, htf, _source)
// Calculate moving average on higher time frame
htf_close = get_htf_data(close)
htf_ma = 0.0
if ma_type == "SMA"
htf_ma := get_htf_data(ta.sma(close, ma_length))
else
htf_ma := get_htf_data(ta.ema(close, ma_length))
// Determine trend direction
trend_up = htf_close > htf_ma
trend_down = htf_close < htf_ma
trend = trend_up ? "Uptrend" : trend_down ? "Downtrend" : "Sideways"
// — Plotting —
// Plot moving average (optional, can be enabled if needed)
// plot(htf_ma, title="HTF MA", color=color.orange, linewidth=2)
// Display trend label on the chart
if show_label and barstate.islast
label.new(
x=bar_index,
y=na,
text="HTF Trend: " + trend,
xloc=xloc.viewport_right,
yloc=yloc.price,
color=color.new(color.black, 0),
style=label.style_label_upper_right,
textcolor=trend_up ? color.lime : trend_down ? color.red : color.yellow,
size=size.normal,
textalign=text.align_right)
//alertcondition(trend_up, title="HTF Uptrend", message="Higher Time Frame is in Uptrend")
//alertcondition(trend_down, title="HTF Downtrend", message="Higher Time Frame is in Downtrend")
Inputs:
htf: The higher time frame to analyze (default is Daily). You can set this to any valid time frame like "W" for weekly, "60" for hourly, etc.ma_length: Length of the moving average used to determine the trend (default is 50 periods).ma_type: Type of moving average ("SMA" for Simple Moving Average or "EMA" for Exponential Moving Average).show_label: Option to show or hide the trend label on the chart.Functions:
get_htf_data(_source): A custom function to retrieve data from the higher time frame using request.security.Calculations:
htf_close: Fetches the close price from the higher time frame.htf_ma: Calculates the moving average (SMA or EMA) on the higher time frame.Plotting:
xloc=xloc.viewport_right: Positions the label at the rightmost part of the viewport.yloc=yloc.price: Aligns the label vertically based on price.style=label.style_label_upper_right: Styles the label to appear at the upper right.Alerts (Optional):
alertcondition lines if you wish to set up alerts when the trend changes.Add the Indicator to Your Chart:
Customize Settings:
Interpreting the Indicator:
Optional Alerts:
alertcondition lines in the script (remove the // at the beginning).If you want to enhance the indicator, here are some ideas:
Add Multiple Time Frame Trends:
Incorporate Different Trend Detection Methods:
// Example using RSI
htf_rsi = get_htf_data(ta.rsi(close, 14))
trend = htf_rsi > 50 ? "Uptrend" : htf_rsi < 50 ? "Downtrend" : "Sideways"
Display Trend Strength:
Visual Enhancements:
Change the background color of the chart based on the trend.
bgcolor(trend_up ? color.new(color.lime, 90) : trend_down ? color.new(color.red, 90) : na)
Add icons or shapes to represent the trend visually.
Combine with Lower Time Frame Analysis:
Feel free to ask if you have any questions or need help customizing the indicator further. I'm here to help you make the most out of your trading analysis!
Not yet included in a published release.