MM Flow
FlowScripts

Perfected FlowScript

Angel 1H EMA/SMA + VWAP + Supertrend Confluence

Angel 1H Confluence [Perfected] is indexed as 113_angel_1h_ema_sma_vwap_supertrend_confluence.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

15.1 KB

Lines

384

Native Signal Candidates

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

4/4 guarded

Bull Confluence

line 344:9

Trigger

(showConfluenceShapes) && (confluenceUp)

Value

l

Deps

confluenceUp:var, showConfluenceShapes:input

Bear Confluence

line 354:9

Trigger

(showConfluenceShapes) && (confluenceDown)

Value

h

Deps

confluenceDown:var, showConfluenceShapes:input

Perfect Bull

line 364:9

Trigger

(showConfluenceShapes) && (perfectBull)

Value

l

Deps

perfectBull:var, showConfluenceShapes:input

Perfect Bear

line 374:9

Trigger

(showConfluenceShapes) && (perfectBear)

Value

h

Deps

perfectBear:var, showConfluenceShapes:input

113_angel_1h_ema_sma_vwap_supertrend_confluence.flowscript

source path: kscripts/perfected/113_angel_1h_ema_sma_vwap_supertrend_confluence.flowscript · sha256:6a2e8bfc6257840b8fc1f7b860ef8b53a07d0037729f6258da3fd8019fc77ae1

hash verified
//@version=2
// Source: kiyotaka.ai/hub/kscript (original) — Perfected by kScript_Perfection_Engineer

// ============================================================================
// Angel 1H EMA/SMA + VWAP + Supertrend Confluence [Perfected Edition]
// Author: Archangel Michael (@Archangel Michael) — Perfected by kScript_Perfection_Engineer
// Tags: CORE, #confluence, #ema, #sma, #vwap, #supertrend, #trend, #regime
//
// Multi-timeframe trend regime overlay combining 200/50/20 SMA/EMA hierarchy,
// anchored session VWAP (SVWAP), weekly VWAP (WVWAP), and ATR-based Supertrend
// for trend permission. Adds alignment scoring, color coding by confluence
// strength, and shape markers for high-confluence signals.
//
// Research papers underpinning this design:
//  - Wilder (1978), "New Concepts in Technical Trading Systems": foundational
//    EMA/ATR formulas; EMA_t = α·P_t + (1−α)·EMA_{t−1} where α = 2/(N+1).
//  - Berkowitz, Logue & Noser (1988), "The Total Cost of Transactions on the
//    NYSE": VWAP = Σ(P_i · V_i) / ΣV_i as institutional fair-value benchmark.
//  - Zakamulin (2023), "Optimal Trend-Following with Transaction Costs":
//    optimal trend-following strategy resembles SMA crossover under plausible
//    trend dynamics; hierarchical confirmation framework (regime → trend →
//    entry) is mathematically justified.
//  - Rahman (2024), "Optimising Supertrend Parameters using Bayesian
//    Optimisation": Supertrend ATR-based dynamic trailing stop logic
//    validated across diverse datasets.
//  - Schmidt (2011), "Evaluation of Super-Trend Indicator's Parameters":
//    systematic parameter robustness analysis across 12 major FOREX pairs.
//  - Hurst, Ooi & Pedersen (2014), "A Century of Evidence on Trend-Following
//    Investing": time-series momentum profitable since 1880 across asset
//    classes; multi-indicator confirmation reduces false signals.
//  - Jegadeesh & Titman (1993), "Returns to Buying Winners and Selling Losers":
//    momentum anomaly — past winners tend to keep winning; confluence
//    indicators exploit this persistence.
// ============================================================================

define(
    title="Angel 1H Confluence [Perfected]",
    position="onchart",
    axis=false
)

// ---------------------------------------------------------------------------
// User Inputs
// ---------------------------------------------------------------------------

// --- Moving Averages Group ---
var lenRegime = input(name="lenRegime", type="number", defaultValue=200, label="Regime MA Length (SMA)", constraints={min: 50, max: 500, step: 10}, group="Moving Averages")
var lenTrend = input(name="lenTrend", type="number", defaultValue=50, label="Trend MA Length (SMA)", constraints={min: 10, max: 200, step: 5}, group="Moving Averages")
var lenPull = input(name="lenPull", type="number", defaultValue=20, label="Pullback MA Length (EMA)", constraints={min: 5, max: 100, step: 5}, group="Moving Averages")
var showRegime = input(name="showRegime", type="boolean", defaultValue=true, label="Show Regime MA", group="Moving Averages")
var showTrend = input(name="showTrend", type="boolean", defaultValue=true, label="Show Trend MA", group="Moving Averages")
var showPull = input(name="showPull", type="boolean", defaultValue=true, label="Show Pullback MA", group="Moving Averages")

// --- VWAP Group ---
var showSvwap = input(name="showSvwap", type="boolean", defaultValue=true, label="Show Session VWAP", group="VWAP")
var showWvwap = input(name="showWvwap", type="boolean", defaultValue=true, label="Show Weekly VWAP", group="VWAP")
var showVwapDev = input(name="showVwapDev", type="boolean", defaultValue=true, label="Show VWAP Deviation Bands", group="VWAP")
var vwapDevMult = input(name="vwapDevMult", type="number", defaultValue=1.0, label="VWAP Deviation Multiplier", constraints={min: 0.5, max: 3.0, step: 0.1}, group="VWAP")

// --- Supertrend Group ---
var showSuper = input(name="showSuper", type="boolean", defaultValue=true, label="Show Supertrend", group="Supertrend")
var stPeriod = input(name="stPeriod", type="number", defaultValue=10, label="Supertrend ATR Period", constraints={min: 5, max: 50, step: 1}, group="Supertrend")
var stMult = input(name="stMult", type="number", defaultValue=3.0, label="Supertrend Multiplier", constraints={min: 0.5, max: 10.0, step: 0.1}, group="Supertrend")

// --- Confluence Group ---
var showConfluence = input(name="showConfluence", type="boolean", defaultValue=true, label="Show Confluence Score", group="Confluence")
var showConfluenceShapes = input(name="showConfluenceShapes", type="boolean", defaultValue=true, label="Show High-Confluence Shapes", group="Confluence")
var minConfluence = input(name="minConfluence", type="number", defaultValue=3, label="Min Confluence for Shape Alert", constraints={min: 2, max: 5, step: 1}, group="Confluence")
var shapeSize = input(name="shapeSize", type="number", defaultValue=2, label="Shape Size", constraints={min: 1, max: 4, step: 1}, group="Confluence")

// --- Colors Group ---
var cRegime = input(name="cRegime", type="color", defaultValue="#9e9e9e", label="Regime MA Color", group="Colors")
var cTrend = input(name="cTrend", type="color", defaultValue="#2962ff", label="Trend MA Color", group="Colors")
var cPull = input(name="cPull", type="color", defaultValue="#ff6d00", label="Pullback MA Color", group="Colors")
var cSvwap = input(name="cSvwap", type="color", defaultValue="#26a69a", label="Session VWAP Color", group="Colors")
var cWvwap = input(name="cWvwap", type="color", defaultValue="#ab47bc", label="Weekly VWAP Color", group="Colors")
var cSuperUp = input(name="cSuperUp", type="color", defaultValue="#00e676", label="Supertrend Bull Color", group="Colors")
var cSuperDn = input(name="cSuperDn", type="color", defaultValue="#ff5252", label="Supertrend Bear Color", group="Colors")
var cConfluenceStrong = input(name="cConfluenceStrong", type="color", defaultValue="#26a69a", label="Strong Confluence Color", group="Colors")
var cConfluenceWeak = input(name="cConfluenceWeak", type="color", defaultValue="#ffca28", label="Weak Confluence Color", group="Colors")
var cConfluenceNone = input(name="cConfluenceNone", type="color", defaultValue="#9e9e9e", label="No Confluence Color", group="Colors")

// ---------------------------------------------------------------------------
// Data Sources
// ---------------------------------------------------------------------------
timeseries bars = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries c = bars.close
timeseries h = bars.high
timeseries l = bars.low
timeseries v = bars.volume
timeseries t = bars.time

// isnan guard on first use of external data
if (isnan(c[0]) || isnan(h[0]) || isnan(l[0]) || isnan(v[0]) || isnan(t[0])) {
    return
}

// ---------------------------------------------------------------------------
// Moving Averages
// ---------------------------------------------------------------------------
timeseries ma200 = sma(c, lenRegime)
timeseries ma50 = sma(c, lenTrend)
timeseries ema20 = ema(c, lenPull)

// ---------------------------------------------------------------------------
// Session VWAP (anchored daily)
// ---------------------------------------------------------------------------
static pv = 0.0
static vv = 0.0
var dayId = math.floor(t / 86400000)
static lastDay = -1

if (dayId != lastDay) {
    lastDay = dayId
    pv = ((h + l + c) / 3.0) * v
    vv = v
} else {
    pv = pv + ((h + l + c) / 3.0) * v
    vv = vv + v
}

timeseries svwap = (vv > 0) ? pv / vv : c

// Volume-weighted standard deviation for session VWAP
timeseries typ = (h + l + c) / 3.0
timeseries devSq = (typ - svwap) * (typ - svwap) * v
static sumDevSq = 0.0
if (dayId != lastDay) {
    sumDevSq = devSq
} else {
    sumDevSq = sumDevSq + devSq
}
timeseries svwapStd = (vv > 0 && sumDevSq > 0) ? math.sqrt(sumDevSq / vv) : 0.0

// ---------------------------------------------------------------------------
// Weekly VWAP (anchored weekly)
// ---------------------------------------------------------------------------
var dayIdW = math.floor(t / 86400000)
var weekId = math.floor((dayIdW + 3) / 7)
static lastWk = -1
static wPv = 0.0
static wV = 0.0

if (weekId != lastWk) {
    lastWk = weekId
    wPv = ((h + l + c) / 3.0) * v
    wV = v
} else {
    wPv = wPv + ((h + l + c) / 3.0) * v
    wV = wV + v
}

timeseries wvwap = (wV > 0) ? wPv / wV : c

// ---------------------------------------------------------------------------
// Supertrend (ATR-based dynamic trailing stop)
// ---------------------------------------------------------------------------
timeseries prevClose = isnan(c[1]) ? c : c[1]
timeseries tr = math.max(math.max(h - l, math.abs(h - prevClose)), math.abs(l - prevClose))
timeseries atr = rma(tr, stPeriod)
timeseries midP = (h + l) / 2.0
timeseries upBnd = midP + stMult * atr
timeseries dnBnd = midP - stMult * atr

static stDir = 1
static stUp = 0.0
static stDn = 0.0

if (stDir == 1) {
    stUp = math.min(upBnd, isnan(stUp) ? upBnd : stUp)
} else {
    stUp = upBnd
}
if (stDir == -1) {
    stDn = math.max(dnBnd, isnan(stDn) ? dnBnd : stDn)
} else {
    stDn = dnBnd
}

if (stDir == 1 && c < stDn) { stDir = -1 }
else if (stDir == -1 && c > stUp) { stDir = 1 }

timeseries stFinal = (stDir == 1) ? stDn : stUp
timeseries stColor = (stDir == 1) ? cSuperUp : cSuperDn

// ---------------------------------------------------------------------------
// Confluence Scoring (0–5 scale)
// ---------------------------------------------------------------------------
// Each indicator contributes +1 if bullish, -1 if bearish, 0 if neutral/edge

// Regime: price above/below 200 SMA
var regimeBull = c > ma200
var regimeBear = c < ma200
var regimeScore = regimeBull ? 1 : (regimeBear ? -1 : 0)

// Trend: price above/below 50 SMA
var trendBull = c > ma50
var trendBear = c < ma50
var trendScore = trendBull ? 1 : (trendBear ? -1 : 0)

// Pullback: price above/below 20 EMA
var pullBull = c > ema20
var pullBear = c < ema20
var pullScore = pullBull ? 1 : (pullBear ? -1 : 0)

// Supertrend: bullish/bearish direction
var stScore = (stDir == 1) ? 1 : -1

// Session VWAP: price above/below SVWAP
var svwapBull = c > svwap
var svwapBear = c < svwap
var svwapScore = svwapBull ? 1 : (svwapBear ? -1 : 0)

// Weekly VWAP: price above/below WVWAP
var wvwapBull = c > wvwap
var wvwapBear = c < wvwap
var wvwapScore = wvwapBull ? 1 : (wvwapBear ? -1 : 0)

// Total confluence score: sum of all aligned scores (absolute value = strength)
var confluenceTotal = regimeScore + trendScore + pullScore + stScore + svwapScore + wvwapScore

// Positive = bullish confluence, Negative = bearish confluence
var confluenceBull = confluenceTotal > 0
var confluenceStrength = math.abs(confluenceTotal)  // 0 to 6

// Confluence color: strong (≥4), medium (2-3), weak (0-1)
timeseries confluenceColor = (confluenceStrength >= 4) ? cConfluenceStrong : ((confluenceStrength >= 2) ? cConfluenceWeak : cConfluenceNone)

// ---------------------------------------------------------------------------
// High-Confluence Signal Detection (shape markers)
// ---------------------------------------------------------------------------
static prevConfluenceTotal = 0
var confluenceUp = prevConfluenceTotal < minConfluence && confluenceTotal >= minConfluence
var confluenceDown = prevConfluenceTotal > -minConfluence && confluenceTotal <= -minConfluence
var perfectBull = confluenceTotal == 6
var perfectBear = confluenceTotal == -6

prevConfluenceTotal = confluenceTotal

// ---------------------------------------------------------------------------
// Plotting
// ---------------------------------------------------------------------------

// --- Regime MA (200 SMA) ---
if (showRegime) {
    plotLine(
        value=ma200,
        width=2,
        colors=[cRegime],
        label=["Regime MA"],
        desc=["Regime filter: price above = bullish regime, below = bearish regime"]
    )
}

// --- Trend MA (50 SMA) ---
if (showTrend) {
    plotLine(
        value=ma50,
        width=2,
        colors=[cTrend],
        label=["Trend MA"],
        desc=["Trend filter: intermediate trend direction"]
    )
}

// --- Pullback MA (20 EMA) ---
if (showPull) {
    plotLine(
        value=ema20,
        width=1,
        colors=[cPull],
        label=["Pullback MA"],
        desc=["Pullback entry timing: pullbacks to 20 EMA in trend direction"]
    )
}

// --- Session VWAP ---
if (showSvwap) {
    plotLine(
        value=svwap,
        width=2,
        colors=[cSvwap],
        label=["SVWAP"],
        desc=["Session VWAP — daily institutional fair value benchmark"]
    )
}

// --- Weekly VWAP ---
if (showWvwap) {
    plotLine(
        value=wvwap,
        width=2,
        colors=[cWvwap],
        label=["WVWAP"],
        desc=["Weekly VWAP — weekly institutional fair value benchmark"]
    )
}

// --- VWAP Deviation Bands ---
if (showSvwap && showVwapDev) {
    timeseries svwapHi = svwap + vwapDevMult * svwapStd
    timeseries svwapLo = svwap - vwapDevMult * svwapStd
    plotLine(
        value=svwapHi,
        width=1,
        colors=[opacity(cSvwap, 40)],
        label=["SVWAP +σ"],
        desc=["Session VWAP upper deviation band"]
    )
    plotLine(
        value=svwapLo,
        width=1,
        colors=[opacity(cSvwap, 40)],
        label=["SVWAP -σ"],
        desc=["Session VWAP lower deviation band"]
    )
}

// --- Supertrend ---
if (showSuper) {
    plotLine(
        value=stFinal,
        width=2,
        colors=[cSuperUp, cSuperDn],
        colorIndex=(stDir == 1) ? 0 : 1,
        label=["Supertrend"],
        desc=["ATR-based dynamic trailing stop. Bullish = below price; Bearish = above price."]
    )
}

// --- Confluence Score (off-chart) ---
if (showConfluence) {
    plotBar(
        value=confluenceTotal,
        colors=[confluenceColor],
        label=["Confluence"],
        desc=["Confluence score: +6 = perfect bull, -6 = perfect bear. 6 indicators: regime(200), trend(50), pull(20), supertrend, sVWAP, wVWAP."]
    )
}

// --- High-Confluence Shape Markers ---
if (showConfluenceShapes) {
    if (confluenceUp) {
        plotShape(
            value=l,
            shape="triangleup",
            colorIndex=cConfluenceStrong,
            width=shapeSize,
            label=["Bull Confluence"],
            desc=["Bullish confluence >= " + minConfluence + " indicators aligned. Entry permission on pullbacks."]
        )
    }
    if (confluenceDown) {
        plotShape(
            value=h,
            shape="triangledown",
            colorIndex=cConfluenceStrong,
            width=shapeSize,
            label=["Bear Confluence"],
            desc=["Bearish confluence >= " + minConfluence + " indicators aligned. Entry permission on rallies."]
        )
    }
    if (perfectBull) {
        plotShape(
            value=l,
            shape="diamond",
            colorIndex=cConfluenceStrong,
            width=shapeSize + 1,
            label=["Perfect Bull"],
            desc=["ALL 6 indicators bullish — highest confluence signal. Trend-following entry optimal."]
        )
    }
    if (perfectBear) {
        plotShape(
            value=h,
            shape="diamond",
            colorIndex=cConfluenceStrong,
            width=shapeSize + 1,
            label=["Perfect Bear"],
            desc=["ALL 6 indicators bearish — highest confluence signal. Trend-following entry optimal."]
        )
    }
}