Naive Bayes DNA Heatmap | GainzAlgoThe Naive Bayes Volume Heatmap is a predictive analytical suite that moves beyond traditional lagging indicators. While a standard RSI or MACD simply tells you where price has been, this system uses Gaussian Machine Learning to determine the statistical probability of where price is going.
By analyzing the Volume of a candle, the internal distribution of volume, delta, and price force, the indicator visualizes market sentiment as a multi-layered heatmap. It allows traders to see whether the current price action is backed by institutional flow or is simply noise.
Core Logic: The Naive Bayes Engine
The brain of the system is a Gaussian Naive Bayes (GNB) classifier. This is a machine learning algorithm that calculates the probability of an event based on prior conditions.
How it Learns
The model continuously "trains" itself on a lookback window (default 500 bars). It analyzes two primary features:
Intensity (Feature 1): Relative Volume (1m mode) or Net Delta (Footprint mode).
Directional Force (Feature 2): The relationship between price spread and volume (1m mode) or POC Distance (Footprint mode).
Here is the self contained function that does the heavy lifting of the probability analysis:
f_naive_bayes(float feat1, float feat2, float target, int len) =>
m1_f1 = ta.sma(target > 0 ? feat1 : na, len), m1_f2 = ta.sma(target > 0 ? feat2 : na, len)
m0_f1 = ta.sma(target <= 0 ? feat1 : na, len), m0_f2 = ta.sma(target <= 0 ? feat2 : na, len)
v1_f1 = math.pow(ta.stdev(target > 0 ? feat1 : na, len), 2), v1_f2 = math.pow(ta.stdev(target > 0 ? feat2 : na, len), 2)
v0_f1 = math.pow(ta.stdev(target <= 0 ? feat1 : na, len), 2), v0_f2 = math.pow(ta.stdev(target <= 0 ? feat2 : na, len), 2)
p1 = nz(ta.sma(target > 0 ? 1.0 : 0.0, len), 0.5)
l1 = f_pdf(feat1, nz(m1_f1), nz(v1_f1)) * f_pdf(feat2, nz(m1_f2), nz(v1_f2)) * p1
l0 = f_pdf(feat1, nz(m0_f1), nz(v0_f1)) * f_pdf(feat2, nz(m0_f2), nz(v0_f2)) * (1.0 - p1)
prob = nz(l1 / (l1 + l0 + 0.000001), 0.5)
This function is the engine of the indicator. It implements a Gaussian Naive Bayes Classifier directly in Pine Script to calculate the real-time probability of a bullish move.
Here is a breakdown of how this code processes market data:
Class Separation (The "M" and "V" Variables)
The function splits historical data into two buckets based on the target (Price Action):
Bucket 1 (Bullish): Data from bars that closed green.
Bucket 0 (Bearish): Data from bars that closed red.
It then calculates the Mean (m) and Variance (v) for each feature within those buckets. This creates two distinct "profiles"—essentially a mathematical fingerprint of what a Bullish bar looks like versus a Bearish one.
Bayesian Inference (The Result)
Finally, it applies Bayes' Theorem to combine these likelihoods with the Prior Probability (p1)—which is simply the historical win rate of green bars over the lookback period.
The final prob is a normalized value between 0 and 1. If the result is 0.85, the model is signaling an 85% statistical probability that the current market conditions align with historical bullish reversals.
The Math
As discussed above, the engine uses the Probability Density Function (PDF) to map these features onto a bell curve. It asks: "In the past, when we saw this specific volume intensity and this specific price force, how often did the next bar close green versus red?"
The result is a Win Probability %. If the probability is >50%, the bias is Bullish; <50% is Bearish.
The Heatmap
The Heatmap is a vertical stack of 20 independent probability layers.
Multi-Horizon Smoothing: Each layer represents a different generation of the Naive Bayes calculation, ranging from ultra-fast (5-bar smoothing) to long-term (100-bar smoothing).
Specialized Features
The Power Index (The White Line)
The Power Index is your Confluence Meter . It scans all 20 layers of the data and counts how many are currently signaling a trend above a 60% threshold.
A spiking Power Index indicates that the trend is synchronizing across all time horizons, a high-probability entry signal.
Footprint Mode vs. 1-Minute Mode
1-Minute Precision: When active, the script uses request.security_lower_tf to deconstruct the current chart bar into 1-minute slices. It finds the "hidden" intent inside the candle that standard indicators miss.
Footprint Analysis: This mode hooks into raw Exchange Order Flow. It calculates Aggressive Buying vs. Aggressive Selling to feed the Naive Bayes engine the most "raw" data possible.
The sidebars: Unique to Footprint mode, these wide neon bars appear to the right of the heatmap.
Real-Time Volume Scaling: The bars grow and shrink based on the current bar's Buy/Sell volume ratio.
Divergence Spotting: If the Heatmap is bright Aqua (Bullish) but the Pink Sell Box is 80% full, you are witnessing Absorption, big players are absorbing the selling, often leading to a massive squeeze.
How to Use the Suite
The Elite Entry
Identify the Bias: Check the NB Probability in the table. You want to see >65% for a high-probability trade.
Confirm the Match: Ensure the heatmap layers are expanding (moving from the dark center toward the bright edges).
Check the Power Index: Wait for the white line to curve upward, confirming momentum is stacking.
The Signal: When the "NB SIGNAL" cell in the table flips to ELITE LONG or ELITE SHORT, the statistical edge is at its peak.
The Elite Exit
Exit when the inner layers of the heatmap turn back to Midnight Charcoal or the opposite color. This indicates that the immediate heartbeat of the trend has faded, even if the longer-term layers are still colored.
Chỉ báo Pine Script®






















