A trend indicator that reads the market as a regime: bull, bear, or stand aside. Three EMAs form a ribbon, and the ribbon’s width, measured in ATR so it means the same thing on every instrument, tells how strong the trend is. A signal is printed only when the ribbon, the momentum and the volatility agree, and only in the direction of the daily trend.
This code is written in ProRealCode Script and runs with ProRealCode Studio on ProRealTime Web.
Paste the code into a new Studio tab and run it on an intraday chart (5 or 15 minutes). Every setting is a field of the Variables form. Try the trend gate at 0.1 (a signal on every wobble) and at 1.5 (only the moves that ran) and watch the signal log fill and empty. If the regime strip falls off the bottom of your chart, lower its distance.
The code also shows several additions of ProRealCode Script in one place: functions returning two values, input(), GRADIENTCOLOR inside COLORBETWEEN, BACKGROUNDCOLOR and TABLEBG, tables with DRAWTABLE, TABLECELL, TABLEMERGE and TABLEBG, ValueWhen and arrays.
//@ProRealCodeScript@version=1.0.0@
// ============================================================================
// Ribbon Regime
// An ATR trend ribbon with a daily filter, a signal log and a dashboard
// ----------------------------------------------------------------------------
// Author: Nicolas — ProRealCode
// Runs with ProRealCode Studio: https://www.prorealcode.com/prorealcode-studio/
// ----------------------------------------------------------------------------
// Three EMAs make a ribbon. Its width, measured in ATR so it reads the same on
// every instrument, is the trend reading. A signal needs three things to agree:
// the ribbon wider than the trend gate, the RSI on the same side of 50, and
// enough volatility. The daily trend then has a veto: a signal against it is
// not taken, and the indicator stands aside until the next move with it.
//
// On the chart: the ribbon filled with a colour gradient, a regime strip under
// the price, a light tint of the background, ▲ / ▼ on every signal, a
// dashboard (top right) and the log of the last five signals (bottom right).
//
// Use it on an intraday chart: the daily filter needs a chart finer than daily.
//
// Try "Trend gate" at 0.1 (a signal on every wobble) and at 1.5 (only the
// moves that ran), and watch the signal log fill and empty.
// ============================================================================
input(fastLen, 21, "Fast ribbon EMA", int)
input(slowLen, 55, "Slow ribbon EMA", int)
input(rsiLen, 14, "Momentum RSI", int)
input(atrLen, 14, "ATR length", int)
input(sepGate, 0.4, "Trend gate — ribbon width in ATR", float)
input(volFloor, 0.7, "Volatility floor — ATR vs its 200-bar average", float)
input(stripGap, 0.9, "Regime strip — distance under price, in ATR", float)
input(tintPane, 1, "Tint the pane with the trend (0/1)", int)
input(dFastLen, 8, "Daily filter — fast EMA (days)", int)
input(dSlowLen, 21, "Daily filter — slow EMA (days)", int)
// ── Helpers ─────────────────────────────────────────────────────────────────
FUNCTION dp2(x)
round(x * 100) / 100
ENDFUNCTION
// Prices rounded to the instrument's own number of decimals
FUNCTION price(x)
d = pow(10, decimals)
round(x * d) / d
ENDFUNCTION
// The regime: +1 bull, -1 bear, 0 chop, and how far past the gate the ribbon is.
// Below the volatility floor the ribbon means nothing, so the answer is chop.
FUNCTION classify(sep, mom, vp, gate, minVol)
IF vp < minVol THEN
RETURN 0, 0
ENDIF
strength = abs(sep) / gate
IF sep > gate AND mom > 0 THEN
RETURN 1, strength
ENDIF
IF sep < 0 - gate AND mom < 0 THEN
RETURN 0 - 1, strength
ENDIF
RETURN 0, strength
ENDFUNCTION
// ── The ribbon ──────────────────────────────────────────────────────────────
emaFast = ExponentialAverage[fastLen](close)
emaMid = ExponentialAverage[(fastLen + slowLen) / 2](close)
emaSlow = ExponentialAverage[slowLen](close)
atr = AverageTrueRange[atrLen](close)
// Volatility against its own recent level, so one floor fits every timeframe:
// 1 = usual activity, 0.7 = 30% quieter than usual
atrAvg = Average[200](atr)
IF atrAvg > 0 THEN
vp = atr / atrAvg
ELSE
vp = 1
ENDIF
irsi = RSI[rsiLen](close)
mom = irsi - 50
// Ribbon width in ATR
IF atr > 0 THEN
sep = (emaFast - emaSlow) / atr
ELSE
sep = 0
ENDIF
(state, strength) = classify(sep, mom, vp, sepGate, volFloor)
// ── The daily filter ────────────────────────────────────────────────────────
// The slow EMA needs that many daily candles. Until then, there is no veto.
TIMEFRAME(daily, updateonclose)
dFast = ExponentialAverage[dFastLen](close)
dSlow = ExponentialAverage[dSlowLen](close)
TIMEFRAME(default)
// Days of history on the chart, shown in the dashboard while the filter waits
once dayCount = 0
IF date <> date[1] THEN
dayCount = dayCount + 1
ENDIF
htfKnown = isset(dFast) AND isset(dSlow) AND dSlow > 0
htfDir = 0
IF htfKnown THEN
IF dFast > dSlow THEN
htfDir = 1
ELSIF dFast < dSlow THEN
htfDir = 0 - 1
ENDIF
ENDIF
// A reading against the daily trend is not taken
conf = 0
IF state <> 0 THEN
IF htfKnown = 0 OR htfDir = state THEN
conf = state
ENDIF
ENDIF
// ── The held trend and the signals ──────────────────────────────────────────
// A pause keeps the last direction. A reversal the daily filter refuses ends
// it: the indicator stands aside (0) until the next move with the daily trend.
once st = 0
IF conf <> 0 THEN
st = conf
ELSIF state <> 0 AND state <> st THEN
st = 0
ENDIF
// A signal is a new confirmed direction
flip = st <> st[1] AND st <> 0
buySig = flip AND st > 0
sellSig = flip AND st < 0
// ── The ribbon fills ────────────────────────────────────────────────────────
COLORBETWEEN(emaFast, emaMid, GRADIENTCOLOR(sep, 0 - 2.5, 2.5, "#ff4d4dcc", "#8899aa44", "#3fd18acc"))
COLORBETWEEN(emaMid, emaSlow, GRADIENTCOLOR(sep, 0 - 2.5, 2.5, "#ff4d4d77", "#8899aa26", "#3fd18a77"))
// ── The regime strip under the price ───────────────────────────────────────
// If it falls off the bottom of your chart, lower "Regime strip".
stripHi = Lowest[20](low) - stripGap * atr
stripLo = stripHi - 0.45 * atr
COLORBETWEEN(stripHi, stripLo, GRADIENTCOLOR(st, 0 - 1, 1, "#ff4d4daa", "#8899aa55", "#3fd18aaa"))
// ── The background tint ─────────────────────────────────────────────────────
IF tintPane = 1 THEN
BACKGROUNDCOLOR(GRADIENTCOLOR(st, 0 - 1, 1, "#ff4d4d14", "#00000000", "#3fd18a14"))
ENDIF
// ── The signal markers ──────────────────────────────────────────────────────
IF buySig THEN
DRAWTEXT("▲", barindex, low - 0.35 * atr, Dialog, Bold, 13) COLOURED(63, 209, 138)
DRAWPOINT(barindex, close, 4) COLOURED(63, 209, 138) BORDERCOLOR(10, 14, 20)
ENDIF
IF sellSig THEN
DRAWTEXT("▼", barindex, high + 0.35 * atr, Dialog, Bold, 13) COLOURED(255, 92, 92)
DRAWPOINT(barindex, close, 4) COLOURED(255, 92, 92) BORDERCOLOR(10, 14, 20)
ENDIF
// ── The last five signals ───────────────────────────────────────────────────
// ValueWhen(flip, x, n) = the value of x at the n-th most recent signal
$sigBar[0] = ValueWhen(flip, barindex, 0)
$sigBar[1] = ValueWhen(flip, barindex, 1)
$sigBar[2] = ValueWhen(flip, barindex, 2)
$sigBar[3] = ValueWhen(flip, barindex, 3)
$sigBar[4] = ValueWhen(flip, barindex, 4)
$sigPx[0] = ValueWhen(flip, close, 0)
$sigPx[1] = ValueWhen(flip, close, 1)
$sigPx[2] = ValueWhen(flip, close, 2)
$sigPx[3] = ValueWhen(flip, close, 3)
$sigPx[4] = ValueWhen(flip, close, 4)
$sigDir[0] = ValueWhen(flip, st, 0)
$sigDir[1] = ValueWhen(flip, st, 1)
$sigDir[2] = ValueWhen(flip, st, 2)
$sigDir[3] = ValueWhen(flip, st, 3)
$sigDir[4] = ValueWhen(flip, st, 4)
// ── Dashboard and signal log, on the last bar ───────────────────────────────
IF islastbarupdate THEN
hud = DRAWTABLE(10, 2, topright) FILLCOLOR(12, 16, 22, 232) BORDERCOLOR(58, 68, 86)
TABLEMERGE(hud, 0, 0, 1, 2)
TABLECELL(hud, 0, 0, "RIBBON REGIME", bold, center, 11) COLOURED(232, 238, 248) FILLCOLOR(30, 38, 52)
// The held trend, and whether it is still confirmed right now
TABLECELL(hud, 1, 0, "State", 11) COLOURED(150, 162, 180)
IF st > 0 THEN
IF conf > 0 THEN
TABLECELL(hud, 1, 1, "▲ BULL", bold, right, 11) COLOURED(90, 220, 155)
ELSE
TABLECELL(hud, 1, 1, "▲ bull · holding", right, 11) COLOURED(135, 185, 162)
ENDIF
ELSIF st < 0 THEN
IF conf < 0 THEN
TABLECELL(hud, 1, 1, "▼ BEAR", bold, right, 11) COLOURED(255, 110, 110)
ELSE
TABLECELL(hud, 1, 1, "▼ bear · holding", right, 11) COLOURED(206, 140, 140)
ENDIF
ELSE
TABLECELL(hud, 1, 1, "■ STAND ASIDE", bold, right, 11) COLOURED(170, 180, 198)
ENDIF
TABLEBG(hud, 1, 1, GRADIENTCOLOR(st, 0 - 1, 1, "#5c1c24", "#232a38", "#17553c"))
TABLECELL(hud, 2, 0, "Strength", 11) COLOURED(150, 162, 180)
IF strength >= 2 THEN
TABLECELL(hud, 2, 1, "████", right, monospaced, 11) COLOURED(255, 214, 120)
ELSIF strength >= 1 THEN
TABLECELL(hud, 2, 1, "███░", right, monospaced, 11) COLOURED(214, 222, 236)
ELSIF strength >= 0.5 THEN
TABLECELL(hud, 2, 1, "██░░", right, monospaced, 11) COLOURED(160, 172, 192)
ELSE
TABLECELL(hud, 2, 1, "█░░░", right, monospaced, 11) COLOURED(110, 122, 142)
ENDIF
TABLECELL(hud, 3, 0, "EMA #fastLen#", 11) COLOURED(150, 162, 180)
TABLECELL(hud, 3, 1, "#price(emaFast)#", right, monospaced, 11) COLOURED(236, 240, 248)
TABLECELL(hud, 4, 0, "EMA #slowLen#", 11) COLOURED(150, 162, 180)
TABLECELL(hud, 4, 1, "#price(emaSlow)#", right, monospaced, 11) COLOURED(236, 240, 248)
TABLECELL(hud, 5, 0, "Ribbon (ATR)", 11) COLOURED(150, 162, 180)
TABLECELL(hud, 5, 1, "#dp2(sep)#", right, monospaced, 11) COLOURED(236, 240, 248)
TABLEBG(hud, 5, 1, GRADIENTCOLOR(sep, 0 - 2.5, 2.5, "#6e2029", "#232a38", "#1d6242"))
// RSI extremes mean "stretched": warm at both ends, neutral in the middle
TABLECELL(hud, 6, 0, "RSI(#rsiLen#)", 11) COLOURED(150, 162, 180)
TABLECELL(hud, 6, 1, "#round(irsi)#", right, monospaced, 11) COLOURED(236, 240, 248)
TABLEBG(hud, 6, 1, GRADIENTCOLOR(irsi, 30, 70, "#1d6242", "#232a38", "#6e2029"))
TABLECELL(hud, 7, 0, "Volatility", 11) COLOURED(150, 162, 180)
IF vp < volFloor THEN
TABLECELL(hud, 7, 1, "#dp2(vp)# ✕", right, monospaced, 11) COLOURED(255, 190, 120)
ELSE
TABLECELL(hud, 7, 1, "#dp2(vp)# ✓", right, monospaced, 11) COLOURED(236, 240, 248)
ENDIF
TABLEBG(hud, 7, 1, GRADIENTCOLOR(vp, volFloor, volFloor * 2, "#4a3320", "#232a38", "#1d4a62"))
TABLECELL(hud, 8, 0, "Daily trend", 11) COLOURED(150, 162, 180)
IF htfKnown = 0 THEN
TABLECELL(hud, 8, 1, "n/a · #dayCount#/#dSlowLen# days", right, monospaced, 11) COLOURED(105, 116, 134)
ELSIF htfDir > 0 THEN
TABLECELL(hud, 8, 1, "▲ up", right, monospaced, 11) COLOURED(90, 210, 150)
ELSIF htfDir < 0 THEN
TABLECELL(hud, 8, 1, "▼ down", right, monospaced, 11) COLOURED(240, 110, 110)
ELSE
TABLECELL(hud, 8, 1, "flat", right, monospaced, 11) COLOURED(170, 180, 198)
ENDIF
TABLECELL(hud, 9, 0, "Last signal", 11) COLOURED(150, 162, 180)
IF isset($sigBar[0]) THEN
IF $sigDir[0] > 0 THEN
TABLECELL(hud, 9, 1, "▲ #round(barindex - $sigBar[0])# bars", bold, right, monospaced, 11) COLOURED(90, 220, 155)
ELSE
TABLECELL(hud, 9, 1, "▼ #round(barindex - $sigBar[0])# bars", bold, right, monospaced, 11) COLOURED(255, 110, 110)
ENDIF
ELSE
TABLECELL(hud, 9, 1, "none yet", right, monospaced, 11) COLOURED(105, 116, 134)
ENDIF
// Signal log, newest first
ilog = DRAWTABLE(0, 4, bottomright) FILLCOLOR(11, 15, 21, 232) BORDERCOLOR(56, 66, 84)
TABLEMERGE(ilog, 0, 0, 1, 4)
TABLECELL(ilog, 0, 0, "SIGNAL LOG", bold, center, 10) COLOURED(230, 236, 246) FILLCOLOR(28, 36, 50)
TABLECELL(ilog, 1, 0, "#", bold, 10) COLOURED(140, 152, 172)
TABLECELL(ilog, 1, 1, "age", bold, right, 10) COLOURED(140, 152, 172)
TABLECELL(ilog, 1, 2, "type", bold, center, 10) COLOURED(140, 152, 172)
TABLECELL(ilog, 1, 3, "price", bold, right, 10) COLOURED(140, 152, 172)
FOR i = 0 TO 4 DO
row = i + 2
TABLECELL(ilog, row, 0, "#i + 1#", 10) COLOURED(120, 132, 152)
IF isset($sigBar[i]) THEN
TABLECELL(ilog, row, 1, "#round(barindex - $sigBar[i])#", right, monospaced, 10) COLOURED(210, 218, 232)
IF $sigDir[i] > 0 THEN
TABLECELL(ilog, row, 2, "▲ BUY", bold, center, monospaced, 10) COLOURED(90, 220, 155)
ELSE
TABLECELL(ilog, row, 2, "▼ SELL", bold, center, monospaced, 10) COLOURED(255, 110, 110)
ENDIF
TABLECELL(ilog, row, 3, "#price($sigPx[i])#", right, monospaced, 10) COLOURED(236, 240, 248)
TABLEBG(ilog, row, 2, GRADIENTCOLOR($sigDir[i], 0 - 1, 1, "#4a1c22", "#232a38", "#17452f"))
ELSE
TABLECELL(ilog, row, 1, "—", right, monospaced, 10) COLOURED(95, 106, 124)
TABLECELL(ilog, row, 2, "—", center, monospaced, 10) COLOURED(95, 106, 124)
TABLECELL(ilog, row, 3, "—", right, monospaced, 10) COLOURED(95, 106, 124)
ENDIF
NEXT
ENDIF
RETURN emaMid AS "Ribbon mid" GRADIENTCOLOR(sep, 0 - 2.5, 2.5, "#ff4d4d", "#9fb0c4", "#3fd18a") STYLE(line, 3), emaFast AS "Fast" COLOURED(150, 165, 185, 170) STYLE(line, 1), emaSlow AS "Slow" COLOURED(150, 165, 185, 170) STYLE(line, 1)