OPEN-SOURCE SCRIPT
Telah dikemas kini Markov Chain Trend Probability

A Markov Chain is a mathematical model that predicts future states based on the current state, assuming that the future depends only on the present (not the past). Originally developed by Russian mathematician Andrey Markov, this concept is widely used in:
Here's an example of a Markov chain: If the weather is sunny, the probability that will be sunny 30 min later is say 90%. However, if the state changes, i.e. it starts raining, how the probability that will be raining 30 min later is say 70% and only 30% sunny.
Similar concept can be applied to markets price action and trends.
Mathematical Foundation
The core principle follows the Markov Property: P(X_{t+1}|X_t, X_{t-1}, ..., X_0) = P(X_{t+1}|X_t)
Transition Matrix :
-------------Next State
Current----[Up] [Down]
[Up]--------P11 P12
[Down]-----P21 P22
Probability Calculations:
P(Up→Up) = Count(Up→Up) / Count(Up states)
P(Down→Down) = Count(Down→Down) / Count(Down states)
Steady-state probability: π = πP (where π is the stationary distribution)
State Definition:
State = UPTREND if (Price_t - Price_{t-n})/ATR > threshold
State = DOWNTREND if (Price_t - Price_{t-n})/ATR < -threshold
How It Works in Trading
This indicator applies Markov Chain theory to market trends by:
How to Use
Parameters:
Trading Applications:
The indicator works on any timeframe and asset class. Enjoy!
- Finance: Risk modeling, portfolio optimization, credit scoring, algorithmic trading
- Weather Forecasting: Predicting sunny/rainy days, temperature patterns, storm tracking
Here's an example of a Markov chain: If the weather is sunny, the probability that will be sunny 30 min later is say 90%. However, if the state changes, i.e. it starts raining, how the probability that will be raining 30 min later is say 70% and only 30% sunny.
Similar concept can be applied to markets price action and trends.
Mathematical Foundation
The core principle follows the Markov Property: P(X_{t+1}|X_t, X_{t-1}, ..., X_0) = P(X_{t+1}|X_t)
Transition Matrix :
-------------Next State
Current----[Up] [Down]
[Up]--------P11 P12
[Down]-----P21 P22
Probability Calculations:
P(Up→Up) = Count(Up→Up) / Count(Up states)
P(Down→Down) = Count(Down→Down) / Count(Down states)
Steady-state probability: π = πP (where π is the stationary distribution)
State Definition:
State = UPTREND if (Price_t - Price_{t-n})/ATR > threshold
State = DOWNTREND if (Price_t - Price_{t-n})/ATR < -threshold
How It Works in Trading
This indicator applies Markov Chain theory to market trends by:
- Defining States: Classifies market conditions as UPTREND or DOWNTREND based on price movement relative to ATR (Average True Range)
- Learning Transitions: Analyzes historical data to calculate probabilities of moving from one state to another
- Predicting Probabilities: Estimates the likelihood of future trend continuation or reversal
How to Use
Parameters:
- Lookback Period: Number of bars to analyze for trend detection (default: 14)
- ATR Threshold: Sensitivity multiplier for state changes (default: 0.5)
- Historical Periods: Sample size for probability calculations (default: 33)
Trading Applications:
- Trend confirmation for entry/exit decisions
- Risk assessment through probability analysis
- Market regime identification
- Early warning system for potential trend reversals
The indicator works on any timeframe and asset class. Enjoy!
Nota Keluaran
Update Notes for Version 2.0New Feature: Brier Score Accuracy Measurement
I've added a rolling Brier Score to measure the real-time accuracy of the Markov Chain predictions. This provides traders with immediate feedback on model performance.
The Brier Score is a statistical measure that evaluates how well probabilistic predictions match actual outcomes. It calculates the mean squared difference between predicted probabilities and actual binary results (uptrend/downtrend). The score ranges from 0 (perfect predictions) to 1 (worst possible predictions), with 0.25 being the baseline for random guessing. Lower scores indicate better calibrated predictions. So the lower, the better.
Brier Score Line: Orange line showing rolling prediction accuracy (lower is better)
Accuracy Classification: Table now displays model performance categories:
- Exceptional (<10%)
- Excellent (10-15%)
- Very Good (15-20%)
- Good (20-25%)
- Fair (25-30%)
- Poor (>30%)
The Brier Score helps identify when market conditions are favorable for the Markov approach versus when they're too chaotic.
Scores below 25% indicate the model is beating random chance, with scores below 15% showing excellent predictive accuracy.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.