OPEN-SOURCE SCRIPT
SMC N-Gram Probability Matrix [PhenLabs]

📊SMC N-Gram Probability Matrix [PhenLabs]
Version: PineScript™ v6
📌Description
The SMC N-Gram Probability Matrix applies computational linguistics methodology to Smart Money Concepts trading. By treating SMC patterns as a discrete “alphabet” and analyzing their sequential relationships through N-gram modeling, this indicator calculates the statistical probability of which pattern will appear next based on historical transitions.
Traditional SMC analysis is reactive—traders identify patterns after they form and then anticipate the next move. This indicator inverts that approach by building a transition probability matrix from up to 5,000 bars of pattern history, enabling traders to see which SMC formations most frequently follow their current market sequence.
The indicator detects and classifies 11 distinct SMC patterns including Fair Value Gaps, Order Blocks, Liquidity Sweeps, Break of Structure, and Change of Character in both bullish and bearish variants, then tracks how these patterns transition from one to another over time.
🚀Points of Innovation
🔧Core Components
🔥Key Features
🎨Visualization

📖Usage Guidelines
N-Gram Configuration
SMC Detection Settings
Display Settings
✅Best Use Cases
⚠️Limitations
💡What Makes This Unique
🔬How It Works
1. Pattern Detection Phase
2. Sequence Encoding Phase
3. Matrix Construction Phase
4. Probability Calculation Phase
5. Visualization Phase
Version: PineScript™ v6
📌Description
The SMC N-Gram Probability Matrix applies computational linguistics methodology to Smart Money Concepts trading. By treating SMC patterns as a discrete “alphabet” and analyzing their sequential relationships through N-gram modeling, this indicator calculates the statistical probability of which pattern will appear next based on historical transitions.
Traditional SMC analysis is reactive—traders identify patterns after they form and then anticipate the next move. This indicator inverts that approach by building a transition probability matrix from up to 5,000 bars of pattern history, enabling traders to see which SMC formations most frequently follow their current market sequence.
The indicator detects and classifies 11 distinct SMC patterns including Fair Value Gaps, Order Blocks, Liquidity Sweeps, Break of Structure, and Change of Character in both bullish and bearish variants, then tracks how these patterns transition from one to another over time.
🚀Points of Innovation
- First indicator to apply N-gram sequence modeling from computational linguistics to SMC pattern analysis
- Dynamic transition matrix rebuilds every 50 bars for adaptive probability calculations
- Supports bigram (2), trigram (3), and quadgram (4) sequence lengths for varying analysis depth
- Priority-based pattern classification ensures higher-significance patterns (CHoCH, BOS) take precedence
- Configurable minimum occurrence threshold filters out statistically insignificant predictions
- Real-time probability visualization with graphical confidence bars
🔧Core Components
- Pattern Alphabet System: 11 discrete SMC patterns encoded as integers for efficient matrix indexing and transition tracking
- Swing Point Detection: Uses ta.pivothigh/pivotlow with configurable sensitivity for non-repainting structure identification
- Transition Count Matrix: Flattened array storing occurrence counts for all possible pattern sequence transitions
- Context Encoder: Converts N-gram pattern sequences into unique integer IDs for matrix lookup
- Probability Calculator: Transforms raw transition counts into percentage probabilities for each possible next pattern
🔥Key Features
- Multi-Pattern SMC Detection: Simultaneously identifies FVGs, Order Blocks, Liquidity Sweeps, BOS, and CHoCH formations
- Adjustable N-Gram Length: Choose between 2-4 pattern sequences to balance specificity against sample size
- Flexible Lookback Range: Analyze anywhere from 100 to 5,000 historical bars for matrix construction
- Pattern Toggle Controls: Enable or disable individual SMC pattern types to customize analysis focus
- Probability Threshold Filtering: Set minimum occurrence requirements to ensure prediction reliability
- Alert Integration: Built-in alert conditions trigger when high-probability predictions emerge
🎨Visualization
- Probability Table: Displays current pattern, recent sequence, sample count, and top N predicted patterns with percentage probabilities
- Graphical Probability Bars: Visual bar representation (█░) showing relative probability strength at a glance
- Chart Pattern Markers: Color-coded labels placed directly on price bars identifying detected SMC formations
- Pattern Short Codes: Compact notation (F+, F-, O+, O-, L↑, L↓, B+, B-, C+, C-) for quick pattern identification
- Customizable Table Position: Place probability display in any corner of your chart
📖Usage Guidelines
N-Gram Configuration
- N-Gram Length: Default 2, Range 2-4. Lower values provide more samples but less specificity. Higher values capture complex sequences but require more historical data.
- Matrix Lookback Bars: Default 500, Range 100-5000. More bars increase statistical significance but may include outdated market behavior.
- Min Occurrences for Prediction: Default 2, Range 1-10. Higher values filter noise but may reduce prediction availability.
SMC Detection Settings
- Swing Detection Length: Default 5, Range 2-20. Controls pivot sensitivity for structure analysis.
- FVG Minimum Size: Default 0.1%, Range 0.01-2.0%. Filters insignificant gaps.
- Order Block Lookback: Default 10, Range 3-30. Bars to search for OB formations.
- Liquidity Sweep Threshold: Default 0.3%, Range 0.05-1.0%. Minimum wick extension beyond swing points.
Display Settings
- Show Probability Table: Toggle the probability matrix display on/off.
- Show Top N Probabilities: Default 5, Range 3-10. Number of predicted patterns to display.
- Show SMC Markers: Toggle on-chart pattern labels.
✅Best Use Cases
- Anticipating continuation or reversal patterns after liquidity sweeps
- Identifying high-probability BOS/CHoCH sequences for trend trading
- Filtering FVG and Order Block signals based on historical follow-through rates
- Building confluence by comparing predicted patterns with other technical analysis
- Studying how SMC patterns typically sequence on specific instruments or timeframes
⚠️Limitations
- Predictions are based solely on historical pattern frequency and do not account for fundamental factors
- Low sample counts produce unreliable probabilities—always check the Samples display
- Market regime changes can invalidate historical transition patterns
- The indicator requires sufficient historical data to build meaningful probability matrices
- Pattern detection uses standardized parameters that may not capture all institutional activity
💡What Makes This Unique
- Linguistic Modeling Applied to Markets: Treats SMC patterns like words in a language, analyzing how they “flow” together
- Quantified Pattern Relationships: Transforms subjective SMC analysis into objective probability percentages
- Adaptive Learning: Matrix rebuilds periodically to incorporate recent pattern behavior
- Comprehensive SMC Coverage: Tracks all major Smart Money Concepts in a unified probability framework
🔬How It Works
1. Pattern Detection Phase
- Each bar is analyzed for SMC formations using configurable detection parameters
- A priority hierarchy assigns the most significant pattern when multiple detections occur
2. Sequence Encoding Phase
- Detected patterns are stored in a rolling history buffer of recent classifications
- The current N-gram context is encoded into a unique integer identifier
3. Matrix Construction Phase
- Historical pattern sequences are iterated to count transition occurrences
- Each context-to-next-pattern transition increments the appropriate matrix cell
4. Probability Calculation Phase
- Current context ID retrieves corresponding transition counts from the matrix
- Raw counts are converted to percentages based on total context occurrences
5. Visualization Phase
- Probabilities are sorted and the top N predictions are displayed in the table
- Chart markers identify the current detected pattern for visual reference
💡Note:
This indicator performs best when used as a confluence tool alongside traditional SMC analysis. The probability predictions highlight statistically common pattern sequences but should not be used as standalone trading signals. Always verify predictions against price action context, higher timeframe structure, and your overall trading plan. Monitor the sample count to ensure predictions are based on adequate historical data.
開源腳本
秉持TradingView一貫精神,這個腳本的創作者將其設為開源,以便交易者檢視並驗證其功能。向作者致敬!您可以免費使用此腳本,但請注意,重新發佈代碼需遵守我們的社群規範。
TradingView Charting w/ Crypto Systems: phenlabs.com
Join our growing community: discord.gg/phen
All content provided by PhenLabs is for informational & educational purposes only. Past performance does not guarantee future results.
Join our growing community: discord.gg/phen
All content provided by PhenLabs is for informational & educational purposes only. Past performance does not guarantee future results.
免責聲明
這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。
開源腳本
秉持TradingView一貫精神,這個腳本的創作者將其設為開源,以便交易者檢視並驗證其功能。向作者致敬!您可以免費使用此腳本,但請注意,重新發佈代碼需遵守我們的社群規範。
TradingView Charting w/ Crypto Systems: phenlabs.com
Join our growing community: discord.gg/phen
All content provided by PhenLabs is for informational & educational purposes only. Past performance does not guarantee future results.
Join our growing community: discord.gg/phen
All content provided by PhenLabs is for informational & educational purposes only. Past performance does not guarantee future results.
免責聲明
這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。