OPEN-SOURCE SCRIPT

Market Regime Engine

262
Market Regime Engine

Market Regime Engine is a multi-layer market-state and historical research framework designed to identify what the market is doing, where it is in the broader market cycle, how mature the current regime is, and how similar historical environments have behaved afterward.

Rather than defining trend from a single indicator, the engine processes price, volume, volatility, momentum, and market structure through several independent layers and combines them into a standardized:

Regime Score: -100 → +100

The architecture is:

Price + Volume → Fast Engine → Structure Engine → Context Engine → Regime Score → Regime + Stage → Regime Age → Historical Cohort

The objective is to remain responsive to genuine changes in market behavior without allowing a single moving-average cross, high-volume candle, or isolated structural signal to completely change the market classification.

Fast Engine

The Fast Engine is the most responsive part of the model and receives substantial weight in the final score.

It analyzes:

20 SMA location — whether price is above or below its short-term trend mean.
20 SMA slope — whether the trend itself is rising, falling, or flattening.
Displacement — candle-body expansion normalized by ATR.
Relative Volume (RVOL) — determines whether directional movement is being accompanied by meaningful participation.

The combination of price relative to the 20 SMA, SMA slope, displacement, and volume provides the first indication that market behavior is changing.

ATR normalization allows these measurements to adapt across instruments and volatility regimes.

Structure Engine

The Structure Engine asks whether price structure confirms what the Fast Engine is detecting.

It tracks:

Swing highs
Swing lows
Higher highs
Higher lows
Lower highs
Lower lows
Break of Structure (BOS)
Change of Character (CHoCH)

A BOS identifies a meaningful break of established swing structure and receives one of the largest individual weights in the model.

A CHoCH identifies a potential change in the prevailing structural direction and is particularly useful when an established trend begins deteriorating.

This creates an important distinction between simply moving above or below the 20 SMA and actually changing market structure.

Context Engine

The Context Engine determines whether the surrounding environment supports the signals coming from price and structure.

It incorporates:

ATR — normalizes price movement and allows the engine to compare displacement and SMA distance across changing volatility environments.

ADX/DMI — measures trend strength and directional confirmation. ADX itself does not determine whether the market is bullish or bearish; it strengthens an already established directional condition.

Fair Value Gaps (FVG) — identify recent price imbalances that provide additional directional context.

Order Blocks — identify recent opposing candles preceding meaningful displacement.

FVG and Order Block information intentionally receive relatively small weights because they are treated as contextual evidence rather than primary directional signals.

Regime Score

All of these components feed into a single standardized score:

-100 ←──────── 0 ────────→ +100

Negative values represent increasing bearish alignment, while positive values represent increasing bullish alignment.

The full weighting framework is:

Component Maximum Weight
Price vs. 20 SMA ±15
20 SMA Slope ±15
Relative Volume ±10
Displacement ±10
Swing Structure ±10
Break of Structure ±20
CHoCH ±10
ADX/DMI ±5
FVG ±2.5
Order Block ±2.5
Maximum Score ±100

This hierarchy is intentional.

The engine places greater importance on price, the 20 SMA, volume, displacement and structural breaks, while FVGs and Order Blocks act as secondary confirmation.

Regime Classification

The Regime Score is translated into five market states:

Strong Bull — broad bullish alignment with strong directional confirmation.

Bull — bullish evidence dominates, but the environment is not strong enough to qualify as Strong Bull.

Range / Neutral — directional evidence is weak, balanced, or conflicting.

Bear — bearish evidence dominates.

Strong Bear — broad bearish alignment with strong downside confirmation.

A confirmation mechanism prevents every short-lived fluctuation from changing the official regime.

For example, price briefly crossing below a rising 20 SMA does not automatically terminate a Bull regime. Other components must deteriorate sufficiently for the aggregate score to confirm a meaningful transition.

This provides the responsiveness of a fast indicator without making the classification excessively sensitive to noise.

Regime vs. Market Stage

One of the most important features of the full engine is that Regime and Stage are separate calculations.

Regime = tactical market condition

Regime answers:

What is the market doing right now?

It is relatively fast and responsive.

Stage = structural market cycle

Stage answers:

Where is the market within the broader trend cycle?

The model uses four stages:

Stage 1 — Base / Accumulation

Typically characterized by flattening trend, weaker ADX, overlapping price structure, and stabilization following a bearish environment.

Stage 2 — Markup

Characterized by a rising 20 SMA, bullish structure, price above the trend mean, structural upside progression and strengthening trend conditions.

Stage 3 — Distribution

Represents deterioration following a bullish environment. The 20 SMA may flatten, bullish structure begins failing, lower highs may develop, and bearish CHoCH can signal that the previous advance is losing control.

Stage 4 — Markdown

Characterized by a falling 20 SMA, bearish structure, price below the trend mean and established downside progression.

Because Stage and Regime are independent, the model can recognize transitions such as:

Strong Bull / Stage 2 → Bull / Stage 2 → Range / Stage 2 → Range / Stage 3 → Bear / Stage 3 → Bear / Stage 4

This provides considerably more information than simply labeling every bar "uptrend" or "downtrend."

Regime Age

Once a confirmed regime begins, the engine counts how many bars that regime has survived.

This produces Regime Age.

For example:

Bull — Age 4
Bull — Age 8
Bull — Age 13
Bull — Age 21

The numbers 8, 13 and 21 do not determine the regime or Stage.

They are strictly research checkpoints.

A market does not become more bullish because it reaches Age 13, nor does it become bearish because it reaches Age 21.

Instead, regime age allows the model to investigate whether the statistical behavior of a market changes as a regime matures.

Historical Cohort Engine

The full Market Regime Engine extends beyond classification by maintaining a historical cohort research layer.

At the designated regime-age checkpoints:

8 bars
13 bars
21 bars

the engine studies subsequent market behavior over:

5 bars
10 bars
20 bars

The research layer can evaluate characteristics such as:

Continuation probability
Average forward return
Historical sample size
Direction-adjusted performance

The larger framework can also be extended to measure:

Median return
Maximum Favorable Excursion (MFE)
Maximum Adverse Excursion (MAE)
Regime survival rate
Regime failure rate
Probability of a new high or low
Probability of transitioning into another regime

This creates a distinction between classification and expectancy.

The Regime Engine tells you:

What environment are we in?

The Historical Cohort Engine asks:

What has historically happened after environments like this?

Importantly, historical cohort statistics do not feed back into the Regime Score. They remain an independent research layer.

Distance From the 20 SMA

The full engine also measures price's distance from its 20 SMA in ATR units:

(Price − 20 SMA) / ATR

This provides information that a simple Bull/Bear classification cannot.

For example, two markets might both have a +55 Bull Regime Score, but one could be:

0.30 ATR above its 20 SMA

while the other is:

2.20 ATR above its 20 SMA.

The directional environment may be similar, but the second market is substantially more extended.

SMA distance is therefore treated primarily as location information rather than additional directional points, helping avoid double-counting the same trend information.

Full Dashboard

The larger version exposes the internal workings of the engine rather than displaying only the final regime.

The dashboard reports:

Current Regime
Regime Score
Market Stage
Regime Age
Price vs. 20 SMA
SMA slope
RVOL
Displacement
BOS
CHoCH
ADX
FVG
Order Block context
ATR-normalized SMA distance
5-bar historical cohort results
10-bar historical cohort results
20-bar historical cohort results

This makes the indicator transparent: instead of simply being told that the market is Bullish, the user can see why the model reached that conclusion.

Example

Suppose the dashboard reports:

Regime: BULL
Score: +32.5
Stage: Stage 2 — Markup
Age: 9 bars

with:

Price above 20 SMA: +15
Rising SMA: +15
RVOL: 0
Displacement: 0
BOS: 0
CHoCH: 0
ADX: 0
Bullish FVG: +2.5

The result is:

+15 + 15 + 2.5 = +32.5

The correct interpretation is not simply "the market is going higher."

Instead, the engine is saying:

The market remains structurally bullish and in a Stage-2 environment, but immediate momentum, volume and structural-break confirmation are currently limited.

That distinction is the purpose of the model.

Philosophy of the Indicator

Market Regime Engine is built around the idea that:

Regime ≠ Trade Entry

A bullish regime does not mean every bar should be bought, just as a bearish regime does not mean every bar should be sold.

The engine is designed to establish environment and directional context.

Execution can then be handled separately using the trader's preferred methodology—price location, pullbacks, candlestick confirmation, support/resistance, volume profile, or other entry criteria.

The framework therefore separates three different questions:

Regime:
What is the market doing?

Stage:
Where are we in the broader cycle?

Historical Cohort:
What happened historically after comparable conditions?

Together, these create a market-state framework that attempts to remain fast enough to recognize meaningful change, structured enough to resist noise, and transparent enough to understand exactly why the market received its current classification.

For research and educational purposes only. Market Regime Engine does not predict future prices and is not financial advice.

คำจำกัดสิทธิ์ความรับผิดชอบ

ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน