MM Flow
FlowScripts

Perfected FlowScript

Shannon Entropy

Shannon Entropy Pro is indexed as 308_shannon_entropy.flowscript. The exact FlowScript is viewable here; chart observe and trading automation use the current mmflow chart/recipe signal proxy until FlowScript v2 transpilation is available.

This exact FlowScript uses the v2 source dialect. It is viewable and connected to the chart/automation proxy, but direct editor execution needs the FlowScript v2 transpiler.

Dialect

flowscript-v2

Compatibility

flowscript-v2-needs-transpile

Position

offchart

Axis

true

Size

13.9 KB

Lines

335

Native Signal Candidates

4 source-level chart marker triggers extracted from guarded FlowScript v2 blocks.

4/4 guarded

Efficient Regime

line 265:9

Trigger

(showShapes) && (crossEfficient)

Value

smoothed

Deps

crossEfficient:var, showShapes:input

Predictable Regime

line 275:9

Trigger

(showShapes) && (crossPredictable)

Value

smoothed

Deps

crossPredictable:var, showShapes:input

Extreme Efficiency

line 285:9

Trigger

(showShapes) && (extremeEfficient)

Value

smoothed

Deps

extremeEfficient:var, showShapes:input

Extreme Predictability

line 295:9

Trigger

(showShapes) && (extremePredictable)

Value

smoothed

Deps

extremePredictable:var, showShapes:input

308_shannon_entropy.flowscript

source path: kscripts/perfected/308_shannon_entropy.flowscript · sha256:d18a8a0152caa82908988ed3dfa29dda9d46cfea93fe130fb86fecaa55d3f333

hash verified
//@version=2
// ============================================================================
// Shannon Entropy Pro — Production-Quality Market Regime Detector
// Author: L (@Lucak47cash) | Perfected by: kScript Perfection Engineer
// Tags: CORE, #entropy, #regime, #efficiency, #random_walk, #predictability
//
// Market regime detection via Shannon entropy applied to discretized return
// distributions. High entropy → market behaving close to random walk
// (efficient / unpredictable); low entropy → concentrated, predictable
// directional state.
//
// FORMULA FOUNDATION:
// 1. Returns: r_t = (P_t − P_{t−1}) / P_{t−1}
// 2. Discretization: Bin returns into 4 buckets using rolling mean ± stddev
//    boundaries for stability (streaming histogram approximation).
// 3. Shannon Entropy: H = −Σ p_i × log₂(p_i)  where p_i = proportion in bin i
// 4. Normalization: H_norm = H / log₂(N_bins)  ∈ [0, 1]
// 5. Z-Score: z = (H_norm − μ) / σ  over rolling window
// 6. Regime: High H_norm → efficient / random; Low H_norm → predictable / trending
//
// RESEARCH BASIS:
//  • Mehl et al. (2019), "Fast Trading and the Virtue of Entropy: Evidence
//    from the Foreign Exchange Market". High-frequency trading is positively
//    correlated with entropy; higher entropy brings prices closer to the
//    random walk hypothesis.
//  • Shternshis et al. (2022), "Measuring Market Efficiency: The Shannon
//    Entropy of High-Frequency Financial Time Series". Proposes computational
//    methodology for genuine entropy estimation by filtering out micro-
//    structure noise and volatility clustering.
//  • Kizilersu, Kreer, Thomas & Feindt (2016), "Universal Behaviour in the
//    Stock Market: Time Dynamics of the Electronic Orderbook". Orderbook
//    changes follow a left-truncated Weibull distribution with a universal
//    shape parameter corresponding to maximum entropy.
//  • Shannon (1948), "A Mathematical Theory of Communication". Foundational
//    definition of information entropy H = −Σ p(x) log p(x).
// ============================================================================

define(
    title="Shannon Entropy Pro",
    position="offchart",
    axis=true
)

// ------------------------------------------------------------------------------
// User Inputs — grouped and constrained
// ------------------------------------------------------------------------------

// --- Entropy Settings ---
var lookback = input(name="lookback", type="number", defaultValue=64, label="Lookback Window", constraints={min: 10, max: 200, step: 1}, group="Entropy")
var smooth = input(name="smooth", type="number", defaultValue=5, label="Smoothing Length", constraints={min: 1, max: 50, step: 1}, group="Entropy")

// --- Z-Score ---
var zLookback = input(name="zLookback", type="number", defaultValue=50, label="Z-Score Lookback", constraints={min: 10, max: 200, step: 1}, group="Z-Score")

// --- Thresholds & Regime ---
var hiThr = input(name="hiThr", type="number", defaultValue=0.75, label="High Entropy Threshold (Efficient)", constraints={min: 0.5, max: 0.95, step: 0.05}, group="Thresholds")
var loThr = input(name="loThr", type="number", defaultValue=0.45, label="Low Entropy Threshold (Predictable)", constraints={min: 0.05, max: 0.5, step: 0.05}, group="Thresholds")

// --- Display ---
var showGuides = input(name="showGuides", type="boolean", defaultValue=true, label="Show Guide Lines", group="Display")
var showRawBars = input(name="showRawBars", type="boolean", defaultValue=true, label="Show Entropy Histogram", group="Display")
var showShapes = input(name="showShapes", type="boolean", defaultValue=true, label="Show Regime Shapes", group="Display")
var showZScore = input(name="showZScore", type="boolean", defaultValue=true, label="Show Z-Score Line", group="Display")

// --- Colors ---
var hiColor = input(name="hiColor", type="color", defaultValue="#ef5350", label="High Entropy (Random) Color", group="Colors")
var loColor = input(name="loColor", type="color", defaultValue="#26a69a", label="Low Entropy (Predictable) Color", group="Colors")
var neutralColor = input(name="neutralColor", type="color", defaultValue="#9e9e9e", label="Neutral Color", group="Colors")
var lineColor = input(name="lineColor", type="color", defaultValue="#2962ff", label="Base Line Color", group="Colors")

// ------------------------------------------------------------------------------
// Data Sources
// ------------------------------------------------------------------------------
timeseries priceBars = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries px = priceBars.close

// NaN guard — prevent execution before data is loaded
if (isnan(px[0])) {
    return
}

// ------------------------------------------------------------------------------
// Return Computation
// ------------------------------------------------------------------------------
// Log-like returns normalized by price level for stationarity.
timeseries ret = px[1] > 0 ? (px[0] - px[1]) / px[1] : 0.0

// ------------------------------------------------------------------------------
// Rolling Distribution Statistics
// ------------------------------------------------------------------------------
// Rolling mean and stddev of returns over the lookback window.
// These define the bin boundaries for discretization.

timeseries retMean = sma(ret, lookback)
timeseries retStd = stddev(ret, lookback)

// Guard against zero stddev (perfectly flat price)
var effStd = retStd > 0 ? retStd : 1e-9

// ------------------------------------------------------------------------------
// Discretization into 4 Bins (rolling mean ± stddev boundaries)
// ------------------------------------------------------------------------------
// CRITICAL FIX: The original script computed entropy of a uniform distribution
// (approxP = 1/bins always), which yielded maximum entropy at all times —
// a fundamental conceptual error. We now compute a genuine streaming histogram
// using rolling bin proportions via SMA of binary indicators.
//
// Bin 1: ret < mean − stddev       (extreme negative)
// Bin 2: mean − stddev ≤ ret < mean  (moderate negative)
// Bin 3: mean ≤ ret < mean + stddev  (moderate positive)
// Bin 4: ret ≥ mean + stddev       (extreme positive)

var inBin1 = ret < retMean - effStd ? 1.0 : 0.0
var inBin2 = ret >= retMean - effStd && ret < retMean ? 1.0 : 0.0
var inBin3 = ret >= retMean && ret < retMean + effStd ? 1.0 : 0.0
var inBin4 = ret >= retMean + effStd ? 1.0 : 0.0

// Rolling proportions via SMA — streaming histogram approximation.
// Each sma(inBin_i, lookback) gives the proportion of returns that fell
// into bin i over the last lookback bars, evaluated with the boundaries
// active at each bar. This is the best practical approximation in a
// streaming DSL without array support.

var p1 = sma(inBin1, lookback)
var p2 = sma(inBin2, lookback)
var p3 = sma(inBin3, lookback)
var p4 = sma(inBin4, lookback)

// ------------------------------------------------------------------------------
// Shannon Entropy Computation
// ------------------------------------------------------------------------------
// H = −Σ p_i × log₂(p_i)  for i = 1..4
// We guard against p_i = 0 to avoid log(0) undefined behavior.

var h1 = p1 > 0.001 ? -p1 * math.log(p1 + 1e-9) / math.log(2.0) : 0.0
var h2 = p2 > 0.001 ? -p2 * math.log(p2 + 1e-9) / math.log(2.0) : 0.0
var h3 = p3 > 0.001 ? -p3 * math.log(p3 + 1e-9) / math.log(2.0) : 0.0
var h4 = p4 > 0.001 ? -p4 * math.log(p4 + 1e-9) / math.log(2.0) : 0.0

var entropy = h1 + h2 + h3 + h4
var maxEntropy = math.log(4.0) / math.log(2.0)  // log₂(4) = 2.0
var entropyNorm = entropy / maxEntropy

// Smooth the normalized entropy for cleaner signal
timeseries smoothed = ema(entropyNorm, smooth)

// ------------------------------------------------------------------------------
// Z-Score Normalization
// ------------------------------------------------------------------------------
timeseries entMean = sma(smoothed, zLookback)
timeseries entStd = stddev(smoothed, zLookback)
timeseries entZ = entStd > 0 ? (smoothed - entMean) / entStd : 0.0

// ------------------------------------------------------------------------------
// Regime Detection
// ------------------------------------------------------------------------------
// High entropy = efficient / random walk (hard to predict, distribution is uniform)
// Low entropy = concentrated / predictable (trending or structural, distribution is peaked)
var regime = 0      // 0 = neutral, 1 = efficient, -1 = predictable
var extremeRegime = 0

if (smoothed >= hiThr) {
    regime = 1      // Efficient / random
} else if (smoothed <= loThr) {
    regime = -1     // Predictable / trending
}

if (smoothed >= 0.9) {
    extremeRegime = 1   // Extreme efficiency
} else if (smoothed <= 0.2) {
    extremeRegime = -1  // Extreme predictability
}

// ------------------------------------------------------------------------------
// Static State Tracking (Session-level persistence)
// ------------------------------------------------------------------------------
static lastRegime = 0
static regimeChangeCount = 0
static sessionHighEnt = -999999.0
static sessionLowEnt = 999999.0
static extremeEfficientCount = 0
static extremePredictableCount = 0
static lastBarIndex = 0

var currentBarIndex = bar_index[0]

if (!isnan(currentBarIndex) && currentBarIndex != lastBarIndex) {
    lastBarIndex = currentBarIndex

    if (smoothed > sessionHighEnt) {
        sessionHighEnt = smoothed
    }
    if (smoothed < sessionLowEnt) {
        sessionLowEnt = smoothed
    }

    if (lastRegime != regime) {
        lastRegime = regime
        regimeChangeCount = regimeChangeCount + 1
    }

    if (extremeRegime == 1) {
        extremeEfficientCount = extremeEfficientCount + 1
    } else if (extremeRegime == -1) {
        extremePredictableCount = extremePredictableCount + 1
    }
}

// ------------------------------------------------------------------------------
// Plots — Multiple Visualization Types (Line, Bar, Shape, Fill, HLine)
// ------------------------------------------------------------------------------

hline(value=0.5, color="#ffffff20", width=1)

if (showGuides) {
    hline(value=hiThr, color=opacity(hiColor, 30), width=1)
    hline(value=loThr, color=opacity(loColor, 30), width=1)
    hline(value=1.0, color="#ffffff15", width=1)
    hline(value=0.0, color="#ffffff15", width=1)
}

// Entropy histogram (raw normalized entropy)
if (showRawBars) {
    plotBar(
        value=smoothed,
        colors=[hiColor, loColor],
        width=1,
        label=["Entropy Norm"],
        desc=["Normalized Shannon entropy (1.0 = uniform / random, 0.0 = concentrated / predictable)"]
    )
}

// Z-score line with regime-coded fill
if (showZScore) {
    plotLine(
        value=entZ,
        width=2,
        colors=[hiColor, loColor, neutralColor],
        colorIndex=regime == 1 ? 0 : (regime == -1 ? 1 : 2),
        label=["Entropy Z-Score"],
        desc=["Z-score of normalized entropy. Regime: efficient / predictable / neutral."],
        fill=true
    )
}

// Main entropy line with regime-coded fill
plotLine(
    value=smoothed,
    width=2,
    colors=[hiColor, loColor, neutralColor],
    colorIndex=regime == 1 ? 0 : (regime == -1 ? 1 : 2),
    label=["Shannon Entropy"],
    desc=["Normalized Shannon entropy of return distribution. High = efficient / random, Low = predictable / trending."],
    fill=true
)

// Regime transition shapes
if (showShapes) {
    var crossEfficient = smoothed >= hiThr && !isnan(smoothed[1]) && smoothed[1] < hiThr
    var crossPredictable = smoothed <= loThr && !isnan(smoothed[1]) && smoothed[1] > loThr
    var extremeEfficient = smoothed >= 0.9 && !isnan(smoothed[1]) && smoothed[1] < 0.9
    var extremePredictable = smoothed <= 0.2 && !isnan(smoothed[1]) && smoothed[1] > 0.2

    if (crossEfficient) {
        plotShape(
            value=smoothed,
            shape="triangleup",
            color=hiColor,
            width=3,
            label=["Efficient Regime"],
            desc=["Market entered efficient / random-walk regime (high entropy)"]
        )
    }
    if (crossPredictable) {
        plotShape(
            value=smoothed,
            shape="triangledown",
            color=loColor,
            width=3,
            label=["Predictable Regime"],
            desc=["Market entered predictable / trending regime (low entropy)"]
        )
    }
    if (extremeEfficient) {
        plotShape(
            value=smoothed,
            shape="diamond",
            color=hiColor,
            width=4,
            label=["Extreme Efficiency"],
            desc=["Extreme market efficiency detected — near-random walk conditions"]
        )
    }
    if (extremePredictable) {
        plotShape(
            value=smoothed,
            shape="diamond",
            color=loColor,
            width=4,
            label=["Extreme Predictability"],
            desc=["Extreme predictability detected — concentrated directional state"]
        )
    }
}

// ------------------------------------------------------------------------------
// Regime Table (last bar only)
// ------------------------------------------------------------------------------
if (isLastBar) {
    var regimeText = regime == 1 ? "EFFICIENT" : (regime == -1 ? "PREDICTABLE" : "NEUTRAL")
    var regimeTextColor = regime == 1 ? hiColor : (regime == -1 ? loColor : neutralColor)

    plotTable(
        data=[
            ["Entropy Pro", regimeText],
            ["Entropy", smoothed[0]],
            ["Z-Score", entZ[0]],
            ["Regime Changes", regimeChangeCount],
            ["Extreme Efficient", extremeEfficientCount],
            ["Extreme Predictable", extremePredictableCount]
        ],
        position="top_right",
        headerRow=true,
        headerColumn=true,
        textColor="#e2e8f0",
        headerTextColor="#ffffff",
        backgroundColor=opacity("#0f172a", 40),
        headerBackgroundColor=opacity("#1e293b", 40),
        borderColor=opacity("#334155", 40),
        borderWidth=1,
        fontSize=10,
        cellPadding=4
    )
}