Apply this indicator to a standard time-based chart and choose a higher requested timeframe. The example requests both values in one tuple, carries the last available confirmed values between updates, and distinguishes missing and equal values. The histogram is a price difference in the symbol’s price units, not a percentage or probability.
Confirmed HTF close relative to its EMA; display only, no orderspinescript
//@version=6
indicator("Confirmed HTF Trend State [Pineify]", overlay=false)
string higherTf = input.timeframe("60", "Higher timeframe")
int maLength = input.int(50, "Higher-timeframe EMA length", minval=1)
if timeframe.in_seconds(higherTf) <= timeframe.in_seconds()
runtime.error("Choose a timeframe strictly above the chart timeframe.")
// Both expressions refer to the preceding bar in the requested context.
[confirmedClose, confirmedEma] = request.security(syminfo.tickerid, higherTf, [close[1], ta.ema(close, maLength)[1]], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
bool ready = not na(confirmedClose) and not na(confirmedEma)
float spread = ready ? confirmedClose - confirmedEma : na
string state = not ready ? "NO DATA" : spread > 0 ? "ABOVE EMA" : spread < 0 ? "BELOW EMA" : "AT EMA"
color stateColor = not ready ? color.gray : spread > 0 ? color.green : spread < 0 ? color.red : color.gray
plot(spread, "Previous HTF close minus previous HTF EMA", color=stateColor, style=plot.style_columns)
hline(0, "EMA reference", color=color.gray)
var table status = table.new(position.top_right, 2, 1, bgcolor=color.new(color.black, 20))
if barstate.islast
table.cell(status, 0, 0, "Requested TF: " + higherTf, text_color=color.white)
table.cell(status, 1, 0, state, text_color=stateColor)