OPEN-SOURCE SCRIPT

EMA Regime Filter

316
EMA Regime Filter is a trade/no-trade filter built on the spread between a fast
and a slow EMA. Instead of treating every EMA cross as a signal, it asks two
questions on every bar: *is the spread big enough to matter?* and *is it still
expanding?* Only when both answers are yes does it mark the market as tradeable
in that direction. Everything else is flagged as chop.

It's meant to sit underneath your existing entry method and keep you out of
the flat, whipsaw stretches where EMA-based systems give most of their losses
back.

**How it works**

1. **EMA spread.** The main line is Fast EMA − Slow EMA (9 and 21 by default).
Above zero the fast EMA leads, below zero it lags.
2. **Dead zone.** A band around zero (the dashed yellow lines) marks spreads
too small to trust. You can size the band three ways:
- **% of Price** (default, 0.04%): scales with the instrument, so one setting
works on ES, NQ, CL, stocks and crypto.
- **Absolute**: a fixed point distance.
- **ATR**: a fraction of ATR, so the band widens in volatile markets and
tightens in quiet ones.
3. **Momentum confirmation.** The spread has to widen bar over bar by at least
the *Min Spread Growth %* (10% by default) for *Lookback Bars* consecutive
bars (3 by default). One stalled or shrinking bar resets the count. That
separates a trend that's building from one that's rolling over, even when
the spread is still outside the dead zone.

**Regime output**

- **OK Long (1)**: spread above the upper band *and* expanding upward for the
required bars. The line turns green and the background shades green.
- **OK Short (−1)**: spread below the lower band *and* expanding downward. The
line turns red and the background shades red.
- **No Trade (0)**: anything else, whether that's inside the dead zone, a
spread that has stopped widening, or a direction that disagrees. The line
turns gray.

The regime value is shown in the Data Window, so you can read it at a glance or
reference it in your own workflow.

**Markers**

- ◆ Diamond: an OK Long or OK Short regime begins
- ■ Square: an OK Long or OK Short regime ends
- ▲ / ▼ Triangle on the zero line: the fast EMA crosses above or below the
slow EMA

**Alerts**

OK Long start/end, OK Short start/end, entered No-Trade, and zero-line crosses
up/down. Set alerts to *Once Per Bar Close* to match the indicator's bar-close
logic.

**Tips**

- The regime is strict on purpose. It usually confirms a few bars into a move
and drops out as soon as expansion stalls. Use it as a gate ("am I allowed to
take longs right now?") rather than as a standalone entry or exit.
- If it's too restrictive for your timeframe, lower *Lookback Bars* or *Min
Spread Growth %* first. If it lets through too much chop, raise the dead
zone.
- % of Price is the easiest mode for switching between symbols. ATR mode suits
instruments whose volatility changes a lot from session to session.

**Settings**

- EMA Settings: Fast / Slow EMA length
- Dead Zone: mode, %, absolute threshold, ATR multiplier, ATR length
- Momentum Confirmation: lookback bars, minimum spread growth %
- Visuals: background shading, markers, and all colors

This indicator is a filter for market conditions. It does not predict price and
is not financial advice. Always test settings on your own instruments and
timeframes before trading with them.

Thông báo miễn trừ trách nhiệm

Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.