MM Flow
FlowScripts

Perfected FlowScript

Hull Suite by InSilico • [Leviathan]

Hull Suite by InSilico — Perfected [Leviathan] is indexed as 149_hull_suite_by_insilico_leviathan.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

onchart

Axis

false

Size

14.2 KB

Lines

488

Native Signal Candidates

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

4/4 guarded

Hull Cross Up

line 416:9

Trigger

(showCrossShapes) && (crossUp)

Value

hullValue

Deps

crossUp:var, showCrossShapes:input

Hull Cross Down

line 426:9

Trigger

(showCrossShapes) && (crossDown)

Value

hullValue

Deps

crossDown:var, showCrossShapes:input

Bull Regime

line 436:9

Trigger

(showCrossShapes) && (regimeCrossBull)

Value

hullValue

Deps

regimeCrossBull:var, showCrossShapes:input

Bear Regime

line 446:9

Trigger

(showCrossShapes) && (regimeCrossBear)

Value

hullValue

Deps

regimeCrossBear:var, showCrossShapes:input

149_hull_suite_by_insilico_leviathan.flowscript

source path: kscripts/perfected/149_hull_suite_by_insilico_leviathan.flowscript · sha256:93152b91eeb7457a31f6f6aa264e664564140cbe2453520d6c5da7c1109c38d1

hash verified
//@version=2
// ============================================================================
// Hull Suite by InSilico — Perfected Edition [Leviathan]
// Author: Leviathan (@Leviathan) | Enhanced by kScript Perfection Engineer
// Tags: CORE, #hull, #hullsuite, #insilico, #hma, #ehma, #thma, #ribbon
//
// Production-quality Hull Moving Average suite. Implements Alan Hull's original
// HMA plus EMA-based (EHMA) and Triple-weighted (THMA) variants with multiple
// length configurations, full ribbon visualization, regime color coding,
// crossover shape markers, and static state tracking for trend persistence.
//
// Research papers underpinning this design:
//  1. Hull (2005), "The Hull Moving Average": HMA = WMA(√n, 2×WMA(n/2) − WMA(n))
//     eliminates lag while preserving smoothness.
//  2. Manasa & Reddy (2025), "Trend Analysis by Weighted and Exponential Moving
//     Average": WMA responds faster than SMA/EMA of same length; validates WMA
//     as the foundational building block.
//  3. Wilder (1978), "New Concepts in Technical Trading Systems": moving average
//     as trend-line; price above rising MA favors longs, below falling MA
//     favors shorts.
//  4. Shannon (2021), "Maximum Trading Gains with the Anchored VWAP": trend
//     confirmation via multiple-period overlays reduces false signals.
// ============================================================================

define(
    title="Hull Suite by InSilico — Perfected [Leviathan]",
    position="onchart",
    axis=false
)

// -----------------------------------------------------------------------------
// 1. USER INPUTS — grouped with constraints
// -----------------------------------------------------------------------------

// --- Hull Variants ---
var src = input(
    name="src",
    type="select",
    defaultValue="close",
    label="Source",
    options=["close", "open", "high", "low", "hl2", "hlc3", "ohlc4"],
    group="Hull Settings"
)
var variant = input(
    name="variant",
    type="select",
    defaultValue="HMA",
    label="Primary Hull Variant",
    options=["HMA", "EHMA", "THMA"],
    group="Hull Settings"
)
var hmaLen = input(
    name="hmaLen",
    type="number",
    defaultValue=20,
    label="Primary HMA Length",
    constraints={min: 2, max: 200, step: 1},
    group="Hull Settings"
)
var lenMult = input(
    name="lenMult",
    type="number",
    defaultValue=1.0,
    label="Length Multiplier",
    constraints={min: 0.1, max: 5.0, step: 0.1},
    group="Hull Settings"
)

// --- Ribbon ---
var showRibbon = input(
    name="showRibbon",
    type="boolean",
    defaultValue=true,
    label="Show Hull Ribbon",
    group="Ribbon"
)
var ribbonLen1 = input(
    name="ribbonLen1",
    type="number",
    defaultValue=9,
    label="Ribbon Fast Length",
    constraints={min: 2, max: 100, step: 1},
    group="Ribbon"
)
var ribbonLen2 = input(
    name="ribbonLen2",
    type="number",
    defaultValue=16,
    label="Ribbon Mid Length",
    constraints={min: 2, max: 100, step: 1},
    group="Ribbon"
)
var ribbonLen3 = input(
    name="ribbonLen3",
    type="number",
    defaultValue=25,
    label="Ribbon Slow Length",
    constraints={min: 2, max: 100, step: 1},
    group="Ribbon"
)
var ribbonLen4 = input(
    name="ribbonLen4",
    type="number",
    defaultValue=36,
    label="Ribbon Very Slow Length",
    constraints={min: 2, max: 100, step: 1},
    group="Ribbon"
)

// --- Display ---
var showPrimary = input(
    name="showPrimary",
    type="boolean",
    defaultValue=true,
    label="Show Primary Hull",
    group="Display"
)
var showLagBand = input(
    name="showLagBand",
    type="boolean",
    defaultValue=true,
    label="Show Lag Band Fill",
    group="Display"
)
var showCrossShapes = input(
    name="showCrossShapes",
    type="boolean",
    defaultValue=true,
    label="Show Crossover Shapes",
    group="Display"
)
var showRegimeFill = input(
    name="showRegimeFill",
    type="boolean",
    defaultValue=true,
    label="Show Regime Fill",
    group="Display"
)
var bandFill = input(
    name="bandFill",
    type="number",
    defaultValue=30,
    label="Lag Band Fill Opacity %",
    constraints={min: 0, max: 100, step: 5},
    group="Display"
)
var lineWidth = input(
    name="lineWidth",
    type="number",
    defaultValue=2,
    label="Line Width",
    constraints={min: 1, max: 4, step: 1},
    group="Display"
)
var shapeSize = input(
    name="shapeSize",
    type="number",
    defaultValue=2,
    label="Shape Size",
    constraints={min: 1, max: 4, step: 1},
    group="Display"
)

// --- Colors ---
var upColor = input(
    name="upColor",
    type="color",
    defaultValue="#00e676",
    label="Rising Color",
    group="Colors"
)
var dnColor = input(
    name="dnColor",
    type="color",
    defaultValue="#ff1744",
    label="Falling Color",
    group="Colors"
)
var lineColor = input(
    name="lineColor",
    type="color",
    defaultValue="#ffffff",
    label="Primary Line Color",
    group="Colors"
)
var ribbonColor1 = input(
    name="ribbonColor1",
    type="color",
    defaultValue="#00e676",
    label="Ribbon Fast Color",
    group="Colors"
)
var ribbonColor2 = input(
    name="ribbonColor2",
    type="color",
    defaultValue="#69f0ae",
    label="Ribbon Mid Color",
    group="Colors"
)
var ribbonColor3 = input(
    name="ribbonColor3",
    type="color",
    defaultValue="#ff8a80",
    label="Ribbon Slow Color",
    group="Colors"
)
var ribbonColor4 = input(
    name="ribbonColor4",
    type="color",
    defaultValue="#ff1744",
    label="Ribbon Very Slow Color",
    group="Colors"
)

// -----------------------------------------------------------------------------
// 2. DATA SOURCES & NaN GUARD
// -----------------------------------------------------------------------------
timeseries price = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries source = (src == "close") ? price.close
    : (src == "open") ? price.open
    : (src == "high") ? price.high
    : (src == "low") ? price.low
    : (src == "hl2") ? (price.high + price.low) / 2.0
    : (src == "hlc3") ? (price.high + price.low + price.close) / 3.0
    : price.ohlc4

if (isnan(source[0])) {
    return
}

// -----------------------------------------------------------------------------
// 3. PRIMARY HULL COMPUTATION
// -----------------------------------------------------------------------------
var L = math.max(2, math.round(hmaLen * lenMult))
var halfL = math.max(1, math.round(L / 2))
var sqrtL = math.max(1, math.round(math.sqrt(L)))

// HMA = WMA(sqrtL, 2*WMA(halfL, src) - WMA(L, src))
timeseries wmaHalf = wma(source, halfL)
timeseries wmaFull = wma(source, L)
timeseries rawDiff = 2.0 * wmaHalf - wmaFull
timeseries hma = wma(rawDiff, sqrtL)

// EHMA = EMA-based Hull (smoother)
timeseries emaHalf = ema(source, halfL)
timeseries emaFull = ema(source, L)
timeseries eDiff = 2.0 * emaHalf - emaFull
timeseries ehma = ema(eDiff, sqrtL)

// THMA = triple-weighted Hull (most smoothing)
timeseries wmaHalfThma = wma(wmaHalf, halfL)
timeseries wmaFullThma = wma(wmaFull, L)
timeseries tDiff = 3.0 * wmaHalfThma - 2.0 * wmaFullThma
timeseries thma = wma(tDiff, sqrtL)

// Select variant
timeseries hullValue = (variant == "HMA") ? hma
    : (variant == "EHMA") ? ehma : thma

// 2-bar lag for trend direction
timeseries hullLag = hullValue[2]

// -----------------------------------------------------------------------------
// 4. RIBBON HULL COMPUTATION (HMA only, 4 lengths)
// -----------------------------------------------------------------------------

// Ribbon 1
var L1 = math.max(2, math.round(ribbonLen1 * lenMult))
var halfL1 = math.max(1, math.round(L1 / 2))
var sqrtL1 = math.max(1, math.round(math.sqrt(L1)))
timeseries wmaHalf1 = wma(source, halfL1)
timeseries wmaFull1 = wma(source, L1)
timeseries rawDiff1 = 2.0 * wmaHalf1 - wmaFull1
timeseries hull1 = wma(rawDiff1, sqrtL1)

// Ribbon 2
var L2 = math.max(2, math.round(ribbonLen2 * lenMult))
var halfL2 = math.max(1, math.round(L2 / 2))
var sqrtL2 = math.max(1, math.round(math.sqrt(L2)))
timeseries wmaHalf2 = wma(source, halfL2)
timeseries wmaFull2 = wma(source, L2)
timeseries rawDiff2 = 2.0 * wmaHalf2 - wmaFull2
timeseries hull2 = wma(rawDiff2, sqrtL2)

// Ribbon 3
var L3 = math.max(2, math.round(ribbonLen3 * lenMult))
var halfL3 = math.max(1, math.round(L3 / 2))
var sqrtL3 = math.max(1, math.round(math.sqrt(L3)))
timeseries wmaHalf3 = wma(source, halfL3)
timeseries wmaFull3 = wma(source, L3)
timeseries rawDiff3 = 2.0 * wmaHalf3 - wmaFull3
timeseries hull3 = wma(rawDiff3, sqrtL3)

// Ribbon 4
var L4 = math.max(2, math.round(ribbonLen4 * lenMult))
var halfL4 = math.max(1, math.round(L4 / 2))
var sqrtL4 = math.max(1, math.round(math.sqrt(L4)))
timeseries wmaHalf4 = wma(source, halfL4)
timeseries wmaFull4 = wma(source, L4)
timeseries rawDiff4 = 2.0 * wmaHalf4 - wmaFull4
timeseries hull4 = wma(rawDiff4, sqrtL4)

// -----------------------------------------------------------------------------
// 5. TREND & REGIME DETECTION
// -----------------------------------------------------------------------------
var rising = hullValue > hullLag
var falling = hullValue < hullLag

var regimeBull = rising && hullValue > hull1
var regimeBear = falling && hullValue < hull1
var regimeNeutral = !regimeBull && !regimeBear

var regimeColor = regimeBull ? upColor : (regimeBear ? dnColor : "#9e9e9e")

// Ribbon alignment
var ribbonAlignedUp = hull1 > hull2 && hull2 > hull3 && hull3 > hull4
var ribbonAlignedDown = hull1 < hull2 && hull2 < hull3 && hull3 < hull4
var ribbonSpread = hull1 - hull4

// -----------------------------------------------------------------------------
// 6. CROSSOVER DETECTION (static state)
// -----------------------------------------------------------------------------
static prevRising = false
static prevRegimeBull = false
static prevRegimeBear = false

var crossUp = rising && !prevRising
var crossDown = falling && prevRising
var regimeCrossBull = regimeBull && !prevRegimeBull
var regimeCrossBear = regimeBear && !prevRegimeBear

if (!isLiveUpdate) {
    prevRising = rising
    prevRegimeBull = regimeBull
    prevRegimeBear = regimeBear
}

// -----------------------------------------------------------------------------
// 7. PLOTTING
// -----------------------------------------------------------------------------

// --- Ribbon Fill ---
if (showRibbon && showRegimeFill) {
    var ribbonBull = hull1 > hull4
    plotLine(
        value=[hull4, hull1],
        width=1,
        colors=[transparency(upColor, 10), transparency(dnColor, 10)],
        colorIndex=ribbonBull ? 0 : 1,
        fill=true,
        label=["Ribbon Slow", "Ribbon Fast"],
        desc=["Hull ribbon fill between fast and slow lengths"]
    )
}

// --- Ribbon Lines ---
if (showRibbon) {
    plotLine(
        value=hull1,
        width=1,
        colors=[ribbonColor1],
        label=["Hull " + ribbonLen1],
        desc=["Fast Hull MA (" + ribbonLen1 + ")"]
    )
    plotLine(
        value=hull2,
        width=1,
        colors=[ribbonColor2],
        label=["Hull " + ribbonLen2],
        desc=["Mid Hull MA (" + ribbonLen2 + ")"]
    )
    plotLine(
        value=hull3,
        width=1,
        colors=[ribbonColor3],
        label=["Hull " + ribbonLen3],
        desc=["Slow Hull MA (" + ribbonLen3 + ")"]
    )
    plotLine(
        value=hull4,
        width=1,
        colors=[ribbonColor4],
        label=["Hull " + ribbonLen4],
        desc=["Very slow Hull MA (" + ribbonLen4 + ")"]
    )
}

// --- Primary Hull (regime-colored) ---
if (showPrimary) {
    plotLine(
        value=hullValue,
        width=lineWidth,
        colors=[regimeColor],
        label=["Hull " + variant],
        desc=["Primary Hull MA (" + variant + " " + L + ")"]
    )
}

// --- Lag Band Fill ---
if (showLagBand && showPrimary) {
    var lagBull = rising
    plotLine(
        value=[hullLag, hullValue],
        width=1,
        colors=[transparency(upColor, bandFill), transparency(dnColor, bandFill)],
        colorIndex=lagBull ? 0 : 1,
        fill=true,
        label=["Hull", "Lag"],
        desc=["Lag band fill between Hull and 2-bar lag"]
    )
}

// --- Crossover Shapes ---
if (showCrossShapes) {
    if (crossUp) {
        plotShape(
            value=hullValue,
            shape="triangleup",
            colorIndex=upColor,
            width=shapeSize,
            label=["Hull Cross Up"],
            desc=["Hull crossed above lag — rising trend detected"]
        )
    }
    if (crossDown) {
        plotShape(
            value=hullValue,
            shape="triangledown",
            colorIndex=dnColor,
            width=shapeSize,
            label=["Hull Cross Down"],
            desc=["Hull crossed below lag — falling trend detected"]
        )
    }
    if (regimeCrossBull) {
        plotShape(
            value=hullValue,
            shape="circle",
            colorIndex=upColor,
            width=shapeSize,
            label=["Bull Regime"],
            desc=["Hull entered bullish regime (above fast ribbon)"]
        )
    }
    if (regimeCrossBear) {
        plotShape(
            value=hullValue,
            shape="circle",
            colorIndex=dnColor,
            width=shapeSize,
            label=["Bear Regime"],
            desc=["Hull entered bearish regime (below fast ribbon)"]
        )
    }
}

// -----------------------------------------------------------------------------
// 8. REGIME TABLE (last bar only)
// -----------------------------------------------------------------------------
if (isLastBar) {
    var regimeText = regimeBull ? "BULLISH" : (regimeBear ? "BEARISH" : "NEUTRAL")
    var regimeTextColor = regimeBull ? upColor : (regimeBear ? dnColor : "#9e9e9e")
    var alignText = ribbonAlignedUp ? "ALIGNED UP" : (ribbonAlignedDown ? "ALIGNED DOWN" : "MIXED")

    plotTable(
        data=[
            ["Hull Suite — Perfected", regimeText],
            ["Variant", variant],
            ["Length", L],
            ["Hull Value", hullValue[0]],
            ["Hull Lag", hullLag[0]],
            ["Ribbon Spread", ribbonSpread[0]],
            ["Alignment", alignText]
        ],
        position="top_right",
        headerRow=true,
        headerColumn=true,
        textColor="#e2e8f0",
        headerTextColor="#ffffff",
        backgroundColor=transparency("#0f172a", 40),
        headerBackgroundColor=transparency("#1e293b", 40),
        borderColor=transparency("#334155", 40),
        borderWidth=1,
        fontSize=10,
        cellPadding=4
    )
}