Markov Regime Transition Matrix [Jayadev Rana]

441
WHAT IT IS

Markov Regime Transition Matrix turns price action into a sequence of four market states and estimates how the market moves between them. Instead of asking "what is the trend", it asks "given the state we are in, where does this market usually go next, how long does it usually stay, and how predictable is that". It is an information tool: there are no entry or exit signals.

THE FOUR STATES

Every bar is assigned one state from two features:
- Kaufman efficiency ratio over N bars (net move divided by the sum of bar-to-bar moves). Above the trend threshold the bar is trending, with direction taken from the sign of the net move: Trend up or Trend down.
- When the efficiency ratio is below the threshold the bar is ranging. The ATR percentile against a longer window splits ranging bars into Quiet range (low percentile) and Volatile range (high percentile).

THE MARKOV ESTIMATE

The script keeps the state of the last W bars (input, default 500) and counts every transition from one state to the next inside that window. Older transitions roll out as new ones arrive, so the matrix adapts to the recent character of the market. From the counts it derives:
- Transition matrix: the probability of moving from each state to each other state, shown as a colour-coded 4x4 table with the number of observations per row.
- Stationary distribution: the long-run share of time the chain spends in each state, found by power iteration.
- Expected dwell time per state: 1 / (1 - probability of staying), in bars.
- Entropy rate of the chain in bits per transition and a predictability percentage (100% means the next state is certain, 0% means all four are equally likely).
- For the current state: bars already spent in it, the stay probability, the most likely next state and its probability.
- Forecast hit rate: on each bar the script records which state it expected next and checks it against what actually happened; the rolling hit rate over the hit-rate window is shown next to the 25% random baseline. This is a live out-of-sample check of the matrix.

PANE AND TABLE

The pane plots the stay probability, the chain predictability, the forecast hit rate and the next-state probability, with 25% and 50% reference lines, and a colour lane showing the current state. The pane background can be shaded by state. The table shows the full matrix, stationary shares, dwell times, and the current-state summary. Data-window plots expose the state code, efficiency ratio, ATR percentile, bars in state and the entropy rate.

INPUTS

State features: efficiency ratio length and trend threshold, ATR length, ATR percentile window and volatile threshold.
Markov estimation: transition window, forecast hit-rate window, stationary-distribution iterations.
Display: table on/off and position, background shading, state colours.

ALERTS

State changed, and one alert per state entered. Informational only.

INTENDED USE

Any symbol and timeframe. Use it to judge whether the current regime is likely to persist (high stay probability, long dwell) or to break (low stay probability), and to see how predictable this market has been lately. It works as a context layer for other tools rather than as a signal source.

LIMITATIONS

The states are a deliberately simple discretisation; different thresholds give different chains. The matrix is estimated from a finite window, so rows with few observations are noisy (the n column shows how many transitions back each row). A first-order Markov assumption ignores longer memory. The hit rate measures the forecast of the state label only, not price direction or profitability. Values are computed on confirmed bars; the current bar can change state until it closes.

This is an educational and analytical tool, not financial advice. Past regime behaviour does not guarantee future regime behaviour.

Penafian

Maklumat dan penerbitan adalah tidak bertujuan, dan tidak membentuk, nasihat atau cadangan kewangan, pelaburan, dagangan atau jenis lain yang diberikan atau disahkan oleh TradingView. Baca lebih dalam Terma Penggunaan.