OPEN-SOURCE SCRIPT
Mis à jour

RTI Trend State + MacroCover

242
RTI Trend State + MacroCover

A trend-state oscillator built on the Relative Trend Index (RTI) — a stochastic-position measure of where price sits inside its recent high/low range, then EMA-smoothed:

Pine Script®
raw = 100 * (close - lowest(low, n)) / (highest(high, n) - lowest(low, n)) RTI = EMA(raw, smooth) -> a 0-100 line


How the state works
The 0-100 RTI is converted into a persistent LONG / FLAT / SHORT state through a hysteresis band, so it does not flip on noise around the mid-line:
  • RTI above the upper band -> LONG
  • RTI below the lower band -> SHORT
  • in between -> holds the last state

The gap between the two bands is the hysteresis — widen it for fewer, steadier flips; narrow it for a more reactive read.

MacroCover (optional long bias)
When the state would be SHORT but price trades above a slow EMA (default 200), the short is covered to FLAT — i.e. it won't fight the higher-timeframe uptrend. Turn MacroCover off for a symmetric long/short reading.

On the chart
  • The RTI line (0-100) colored by state: green long, red short, gray flat.
  • Dashed upper/lower bands and a dotted mid-line.
  • State background tint and flip markers where the state changes.
  • A compact table with the current RTI value, state and macro side.
  • The RTI value plus [code]state_-1_0_1[/code] and [code]ema_macro[/code] are available in the Data Window for tooltips and cross-checks.


Inputs
  • RTI engine — Lookback n (30), EMA smoothing (3), upper/lower bands (55 / 45).
  • MacroCover — on/off and macro EMA length (200).
  • Timeframe — use the chart timeframe, or lock the calculation to a fixed resolution.
  • Visual — toggle the state background, flip markers and table.


Notes
  • Works on any symbol and any timeframe.
  • Non-repainting: values are computed on confirmed bars, and the optional higher-timeframe request uses lookahead_off.
  • This is a discretionary / confluence tool, not financial advice or a complete trading system.
Notes de version
Two additions since the first publication.

1) Laguerre confluence — a 4-pole Ehlers Laguerre filter provides a second, independent trend read (the slope of the filter). When the RTI state and the Laguerre slope AGREE, the read has higher conviction, shown as a dot on the line and in the table. The two are correlated but not identical: their agreement is the useful part, not either one on its own.

2) Top-down 1D gate (optional, ON by default) — when the chart-timeframe state contradicts the AlphaTrend of the previous completed day, it is forced FLAT. It never opens positions, it only removes the ones fighting the daily trend. Toggle it off for the raw signal.
Honest note: it cuts a large share of the signals and, in my testing, consistently lowered drawdown — but it gives up upside during clean trends, and I could not establish it as a statistically significant edge. Treat it as a risk overlay, not as alpha, and test it on your own market.

Non-repainting: the daily read latches yesterday's FINAL value at each day change. (Requesting state[1] inside request.security() repaints on the live bar — it returns the forming day instead of yesterday.)

Also: confluence dots and a marker on bars vetoed by the gate; table rows for the Laguerre read, the confluence, the daily state and the gate status; alerts for confluence and for when the gate starts vetoing; and Data Window outputs for the gated and ungated state, the Laguerre read, the daily read and the veto flag.
Notes de version
Graph Update
Notes de version
Maintenance update — repaint fix, warm-up fix, and a cleaner veto marker. The signal itself (RTI hysteresis state, MacroCover, Laguerre confluence and the top-down 1D gate) is unchanged; nothing about how it trades has moved.

FIXES

1) Removed "Force a fixed resolution" (the useHT / Resolution inputs).
That option requested the signal from a fixed higher timeframe, which returns the FORMING value on the realtime bar — so it repainted, and it was the only part of the script that did. It was off by default, so default behaviour is unchanged. Everything is now computed on the chart timeframe; to get the old behaviour, just open the chart on that timeframe. This makes the non-repainting guarantee fully categorical: the only request.security left is the gate's, which latches yesterday's final daily state at each day change.

2) Laguerre filter now seeded with price instead of 0.
Starting the 4-pole filter at 0 made it climb toward price over the first ~20 bars, forcing a spurious LONG slope read at the very start of the history. The poles are now seeded with close. Only the warm-up is affected; confirmed-bar behaviour on live charts is otherwise identical.

3) 1D-gate veto marker moved onto the RTI line.
The veto is a STATE (it persists across consecutive bars), not a one-off event, so it now shows as dots on the line — next to the confluence dots — instead of a circle on the bottom row. This frees the bottom/top rows for the one-off flip markers, which previously collided with the veto circle on the bar where the gate first fires (that bar is both a flip-to-flat and a veto).

Also: the non-repainting section of the description was rewritten to state plainly that the signal has no higher-timeframe request and therefore nothing there to repaint.

Clause de non-responsabilité

Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.