OPEN-SOURCE SCRIPT
Atualizado Markov Transition Matrix

Markov Transition Matrix
This indicator classifies higher-timeframe returns into three states and then tracks how often price transitions from one state to the next.
The three states are:
The result is a 3x3 transition matrix that answers questions such as:
How It Works
What The Table Shows
Inputs
How To Read It
Usage Notes
Practical Uses
This indicator classifies higher-timeframe returns into three states and then tracks how often price transitions from one state to the next.
The three states are:
- Bullish when the N-bar return is above the selected positive threshold
- Bearish when the N-bar return is below the selected negative threshold
- Neutral when the return stays between those thresholds
The result is a 3x3 transition matrix that answers questions such as:
- After a bullish state, how often does the next state stay bullish?
- After a neutral state, how often does the next state turn bearish?
- Which current state has the strongest tendency to persist or reverse?
How It Works
- Choose an analysis timeframe.
- Choose a lookback window in candles.
- For each completed bar in that timeframe, the script computes the return from close[lookback] to the current close.
- That return is classified as Bullish, Neutral, or Bearish using the threshold input.
- Each one-step transition from the previous state to the new state is added to the cumulative matrix.
What The Table Shows
- The main value in each cell is the one-step transition probability for that row and column.
- The detail row under each cell shows P^2, P^3, and the raw transition count.
- The summary row shows the latest confirmed state plus the current row probabilities for Bullish, Neutral, and Bearish.
- The bottom bar shows a simple directional bias: P(Bull) - P(Bear).
P^2 and P^3 in this script are powers of the one-step probability. They are not full 2-step or 3-step Markov-chain forecasts produced by matrix multiplication.
Inputs
- Threshold (%): Minimum absolute return needed to classify a bar as Bullish or Bearish.
- Timeframe: Higher timeframe used for the transition analysis.
- Lookback Window: Number of candles used to measure each return.
- Table Position: Screen location of the matrix.
How To Read It
- Diagonal cells show persistence. High values there mean a state often repeats.
- Off-diagonal cells show transitions. High values there mean a state often rotates into another one.
- A positive bottom-bar reading means Bullish transitions currently outweigh Bearish transitions from the latest confirmed state.
- A negative bottom-bar reading means Bearish transitions currently outweigh Bullish transitions from the latest confirmed state.
Usage Notes
- The chart timeframe must be less than or equal to the selected analysis timeframe.
- The matrix is cumulative across all available history in the selected timeframe context.
- Changing timeframe, threshold, or lookback recompiles the script and rebuilds the tally from scratch.
- This is a state-transition study. It does not place trades or generate broker orders.
Practical Uses
- Compare persistence vs reversal behavior across different symbols.
- Test whether a market spends more time trending or mean-reverting at a chosen timeframe.
- Check whether a recent state has historically led to bullish continuation, neutral drift, or bearish follow-through.
Notas de Lançamento
Markov Transition Matrix - Session UpdateThis update adds a more flexible transition model, a continuation-move panel, and live candle-state coloring. This information should be more helpful that simply knowing transitional information.
Changes In This Update
- Candle Diff was added so transitions can be measured across more than one analysis candle. A value of 1 keeps the original next-state behavior, while larger values measure state changes across a wider gap.
- The table title now reflects the selected analysis timeframe and candle difference, for example Daily 1 Candle Diff or 4h 2 Candle Diff.
- Average Move was added as a new panel beneath the matrix. It reports Bull -> Bull, Neutral -> Neutral, and Bear -> Bear continuation behavior.
- For each same-state continuation, the study now measures:
- average ending close from the origin close
- average high excursion
- average low excursion
- how often the ending close finishes up or down
- Those continuation values are shown in both percentage terms and absolute price terms.
- Display was added so users can choose to show the Transition Matrix, the Average Move panel, or Both.
- Live candle coloring was added for lower timeframes. Candles can now be colored Bullish, Neutral, or Bearish based on where the chart candle closes relative to the active higher-timeframe threshold lines.
- Color Candles was added as an on/off option.
- Custom Bull, Neutral, and Bear candle colors were added in the appearance settings.
- The input order was adjusted so Lookback Window now appears above Timeframe.
Behavior Notes
- The live candle coloring does not wait for the higher-timeframe candle to close. It uses the current threshold boundaries derived from the selected higher-timeframe reference close.
- A lower-timeframe candle only turns Bullish if it closes above the bullish threshold line.
- A lower-timeframe candle only turns Bearish if it closes below the bearish threshold line.
- If price closes between those lines, the candle remains Neutral.
Script de código aberto
Em verdadeiro espírito do TradingView, o criador deste script o tornou de código aberto, para que os traders possam revisar e verificar sua funcionalidade. Parabéns ao autor! Embora você possa usá-lo gratuitamente, lembre-se de que a republicação do código está sujeita às nossas Regras da Casa.
Aviso legal
As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.
Script de código aberto
Em verdadeiro espírito do TradingView, o criador deste script o tornou de código aberto, para que os traders possam revisar e verificar sua funcionalidade. Parabéns ao autor! Embora você possa usá-lo gratuitamente, lembre-se de que a republicação do código está sujeita às nossas Regras da Casa.
Aviso legal
As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.