Here is the institutional-grade implementation. To make this production-ready for an algorithmic environment, the script is upgraded from a binary toggle into a Ternary State Machine (Chop, Transition, Trend) with vector directionality, signal smoothing to prevent micro-flicking, and tuple-optimized MTF fetching to reduce computational overhead.
Code: Select all
//@version=5
indicator("Institutional MTF ER Regime Filter", shorttitle="Inst. ER Regime", overlay=false)
// =========================================================================
// INPUTS & ARCHITECTURE
// =========================================================================
grp_er = "Efficiency Ratio Engine"
i_len = input.int(14, "ER Lookback", minval=1, group=grp_er)
i_smooth = input.int(3, "Signal Smoothing (EMA)", minval=1, group=grp_er, tooltip="Applies an EMA to the ER to prevent threshold flickering.")
grp_mtf = "Multi-Timeframe & Execution"
i_htf = input.timeframe("15", "HTF Vector", group=grp_mtf)
i_strict = input.bool(true, "Strict Non-Repainting", group=grp_mtf, tooltip="Locks evaluation to the last fully closed HTF bar.")
grp_regime = "Regime Thresholds"
i_th_chop = input.float(0.25, "Chop Ceiling (Max)", step=0.05, group=grp_regime)
i_th_trend = input.float(0.40, "Trend Floor (Min)", step=0.05, group=grp_regime)
grp_ui = "UI & Automation"
i_show_hud = input.bool(true, "Show Dashboard HUD", group=grp_ui)
// =========================================================================
// CORE ENGINE: TUPLE-BASED CALCULATION
// =========================================================================
// Calculating ER, Smoothed ER, and Direction simultaneously
f_regime_engine(len, smooth) =>
change = close - close[len]
vol = math.sum(math.abs(close - close[1]), len)
// Raw Efficiency
raw_er = vol == 0 ? 0 : math.abs(change) / vol
// Smoothed Signal (Prevents micro-whipsaws around the threshold)
sig_er = ta.ema(raw_er, smooth)
// Vector (1 = Bullish flow, -1 = Bearish flow)
dir = change > 0 ? 1 : change < 0 ? -1 : 0
[raw_er, sig_er, dir]
// Single request.security call returning a Tuple (Highly optimized for Pine VM)
[htf_raw, htf_sig, htf_dir] = request.security(
syminfo.tickerid,
i_htf,
i_strict ? f_regime_engine(i_len, i_smooth)[1] : f_regime_engine(i_len, i_smooth),
lookahead = i_strict ? barmerge.lookahead_on : barmerge.lookahead_off
)
// =========================================================================
// STATE MACHINE & ROUTING
// =========================================================================
// Define the 3 operational states
bool is_chop = htf_sig < i_th_chop
bool is_trend = htf_sig >= i_th_trend
bool is_trans = not is_chop and not is_trend
// Color routing based on Vector + State
color c_bull_strong = color.new(#00E676, 10)
color c_bear_strong = color.new(#FF5252, 10)
color c_trans = color.new(#FFB74D, 30) // Orange for transition/caution
color c_chop = color.new(#787B86, 60)
color state_color = is_chop ? c_chop : is_trans ? c_trans : (htf_dir == 1 ? c_bull_strong : c_bear_strong)
// =========================================================================
// VISUALIZATION
// =========================================================================
// Histogram mapping the smoothed ER
plot(htf_sig, "Regime Signal", color=state_color, style=plot.style_columns, linewidth=2)
plot(htf_raw, "Raw ER (Ghost)", color=color.new(state_color, 80), style=plot.style_line) // Faint underlying raw data
// Threshold barriers
hline(i_th_trend, "Trend Floor", color=color.new(#00E676, 50), linestyle=hline.style_dashed)
hline(i_th_chop, "Chop Ceiling", color=color.new(#FF5252, 50), linestyle=hline.style_dotted)
// Main chart background integration
bgcolor(is_trend ? color.new(state_color, 90) : na, title="Trend Lock Highlight")
// =========================================================================
// HEADS UP DISPLAY (HUD)
// =========================================================================
var table hud = table.new(position.top_right, 2, 2, bgcolor=color.new(#131722, 20), border_width=1, border_color=color.new(#363A45, 50))
if i_show_hud and barstate.islast
string regime_str = is_chop ? "STAND ASIDE (CHOP)" : is_trans ? "CAUTION (BUILDING)" : "ACTIVE (EFFICIENT)"
string dir_str = htf_dir == 1 ? "BULLISH" : htf_dir == -1 ? "BEARISH" : "FLAT"
table.cell(hud, 0, 0, "HTF REGIME", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(hud, 1, 0, regime_str, text_color=state_color, text_size=size.small, text_halign=text.align_right)
table.cell(hud, 0, 1, "ORDER FLOW", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(hud, 1, 1, is_chop ? "N/A" : dir_str, text_color=is_chop ? color.gray : state_color, text_size=size.small, text_halign=text.align_right)
// =========================================================================
// ALERTS (Webhook / Automated Execution Ready)
// =========================================================================
alertcondition(is_trend and not is_trend[1], title="Regime: Trend Started", message='{"Regime": "Trend", "Action": "Unlock Pullbacks"}')
alertcondition(is_chop and not is_chop[1], title="Regime: Chop Started", message='{"Regime": "Chop", "Action": "Lockdown"}')