Skip to content
GetProfitable
Search
Trend

Higher Timeframe Trend

A higher timeframe exponential moving average drawn on the current chart with the background tinted by its direction, so an intraday chart is read in the context of the daily one.

indicator
//@version=6
indicator("Higher Timeframe Trend", "HTF Trend", overlay = true)

htf = input.timeframe("1D", "Higher timeframe", group = "Settings")
length = input.int(50, "EMA length", minval = 1, group = "Settings")
bull = input.color(#26A69A, "Rising", group = "Style")
bear = input.color(#EF5350, "Falling", group = "Style")
shade = input.int(92, "Background transparency", minval = 50, maxval = 100, group = "Style")

basis = ta.ema(close, length)
htfBasis = request.security(syminfo.tickerid, htf, basis, lookahead = barmerge.lookahead_off)
up = htfBasis > htfBasis[1]
tone = up ? bull : bear

plot(htfBasis, "Higher timeframe EMA", color = tone, linewidth = 2)
bgcolor(color.new(tone, shade), title = "Higher timeframe direction")

turnedUp = up and not up[1]
turnedDown = not up and up[1]
plotshape(turnedUp, "Turned up", location = location.belowbar, style = shape.triangleup, color = bull, size = size.tiny)
plotshape(turnedDown, "Turned down", location = location.abovebar, style = shape.triangledown, color = bear, size = size.tiny)
alertcondition(turnedUp, "Higher timeframe turned up", "The higher timeframe EMA started rising")
alertcondition(turnedDown, "Higher timeframe turned down", "The higher timeframe EMA started falling")
This runs in TradingView, not here
Pine Script only executes inside TradingView. Paste the source into the Pine Editor and add it to a chart to see it plotted.

What it will not do

  • The current higher timeframe bar is unfinished, so the line and the tint can change until that bar closes. Only closed higher timeframe bars are settled.
  • Direction here is one bar of slope on the higher timeframe, which flips on small moves whenever that timeframe is ranging.
  • Asking for a timeframe below the chart's own returns something meaningless. Keep the setting above the chart timeframe.

Written from this description

Pull an EMA from a higher timeframe with request.security, plot it on the current chart, tint the background by whether that EMA is rising or falling, and alert when the direction changes.

Educational only, not financial advice. The maths is simple arithmetic on the numbers you enter; it knows nothing about your broker, fees, slippage or the market. Something wrong with it? Say so in Site Feedback.