MM Flow
FlowScripts

Perfected FlowScript

Vegas Channel

Vegas Channel Perfected is indexed as 328_vegas_channel.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

11.6 KB

Lines

298

Native Signal Candidates

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

6/6 guarded

Fast Cross Up

line 238:9

Trigger

(showShapes) && (crossUp)

Value

low

Deps

crossUp:var, showShapes:input

Fast Cross Down

line 248:9

Trigger

(showShapes) && (crossDn)

Value

high

Deps

crossDn:var, showShapes:input

Slow Cross Up

line 258:9

Trigger

(showShapes) && (crossSlowUp)

Value

low

Deps

crossSlowUp:var, showShapes:input

Slow Cross Down

line 268:9

Trigger

(showShapes) && (crossSlowDn)

Value

high

Deps

crossSlowDn:var, showShapes:input

Touch Slow Hi

line 278:9

Trigger

(showShapes) && (touchSlowHi && !crossSlowUp && !crossSlowDn)

Value

emaSlowHi

Deps

crossSlowDn:var, crossSlowUp:var, showShapes:input, touchSlowHi:var

Touch Slow Lo

line 288:9

Trigger

(showShapes) && (touchSlowLo && !crossSlowUp && !crossSlowDn)

Value

emaSlowLo

Deps

crossSlowDn:var, crossSlowUp:var, showShapes:input, touchSlowLo:var

328_vegas_channel.flowscript

source path: kscripts/perfected/328_vegas_channel.flowscript · sha256:dd6431969ba892abe2ffa012e98f1ca4c516244b57a03aa625a8ea7a61452990

hash verified
//@version=2
// ============================================================================
// Vegas Channel — Multi-EMA Trend-Following Channel [Perfected]
// Author: K (@kakaflex) — Perfected by kScript_Perfection_Engineer
// Tags: #vegas, #ema, #channel, #trend, #multitimeframe
//
// Production-quality Vegas Channel with 5 EMAs (12, 144, 169, 288, 576)
// forming concentric trend channels, regime detection, color-coded fills,
// and cross/touch shape markers for signal generation.
//
// RESEARCH FOUNDATION:
//  1. Manasa & Reddy (2025), "Trend Analysis by Weighted and Exponential
//     Moving Average": EMA offers stability vs. WMA; Vegas Channel leverages
//     multiple EMAs for multi-timeframe trend analysis.
//  2. Hurst, Ooi & Pedersen (2014), "A Century of Evidence on Trend-Following
//     Investing": time-series momentum profitable across asset classes since
//     1880; concentric EMA channels capture trend direction and strength.
//  3. Keltner (1960) / Raschke (1980s), "Keltner Channels": channel systems
//     using moving averages as central lines with bands for trend identification.
//  4. Murphy (1999), "Technical Analysis of the Financial Markets": multiple
//     EMA alignment (fast above slow) as a robust trend-confirmation framework.
// ============================================================================

define(
    title="Vegas Channel Perfected",
    position="onchart",
    axis=false
)

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

// --- EMA Configuration ---
var ema1Len = input(name="ema1Len", type="number", defaultValue=12, label="Fast EMA (Trend) Length", constraints={min: 5, max: 50, step: 1}, group="EMAs")
var ema2Len = input(name="ema2Len", type="number", defaultValue=144, label="Slow EMA 1 Length", constraints={min: 50, max: 200, step: 1}, group="EMAs")
var ema3Len = input(name="ema3Len", type="number", defaultValue=169, label="Slow EMA 2 Length", constraints={min: 50, max: 250, step: 1}, group="EMAs")
var ema4Len = input(name="ema4Len", type="number", defaultValue=288, label="Medium EMA Length", constraints={min: 100, max: 400, step: 1}, group="EMAs")
var ema5Len = input(name="ema5Len", type="number", defaultValue=576, label="Very Slow EMA Length", constraints={min: 200, max: 800, step: 1}, group="EMAs")

// --- Display ---
var showFast = input(name="showFast", type="boolean", defaultValue=true, label="Show Fast EMA (12)", group="Display")
var showMedium = input(name="showMedium", type="boolean", defaultValue=true, label="Show Medium EMA (288)", group="Display")
var showSlow = input(name="showSlow", type="boolean", defaultValue=true, label="Show Slow EMAs (144, 169)", group="Display")
var showVerySlow = input(name="showVerySlow", type="boolean", defaultValue=true, label="Show Very Slow EMA (576)", group="Display")
var showFill = input(name="showFill", type="boolean", defaultValue=true, label="Show Channel Fills", group="Display")
var showShapes = input(name="showShapes", type="boolean", defaultValue=true, label="Show Cross/Touch Shapes", 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 color1 = input(name="color1", type="color", defaultValue="#2962ff", label="Fast EMA Color", group="Colors")
var color2 = input(name="color2", type="color", defaultValue="#ff9800", label="Slow EMA 1 Color", group="Colors")
var color3 = input(name="color3", type="color", defaultValue="#9c27b0", label="Slow EMA 2 Color", group="Colors")
var color4 = input(name="color4", type="color", defaultValue="#26a69a", label="Medium EMA Color", group="Colors")
var color5 = input(name="color5", type="color", defaultValue="#ef5350", label="Very Slow EMA Color", group="Colors")
var bullFillColor = input(name="bullFillColor", type="color", defaultValue="#26a69a", label="Bullish Fill Color", group="Colors")
var bearFillColor = input(name="bearFillColor", type="color", defaultValue="#ef5350", label="Bearish Fill Color", group="Colors")

// ---------------------------------------------------------------------------
// Data Sources
// ---------------------------------------------------------------------------
timeseries bars = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries src = bars.close
timeseries high = bars.high
timeseries low = bars.low

// isnan guard: prevent computation on incomplete data
if (isnan(src[0]) || isnan(high[0]) || isnan(low[0])) {
    return
}

// ---------------------------------------------------------------------------
// EMA Calculations
// ---------------------------------------------------------------------------
timeseries ema1 = ema(src, ema1Len)
timeseries ema2 = ema(src, ema2Len)
timeseries ema3 = ema(src, ema3Len)
timeseries ema4 = ema(src, ema4Len)
timeseries ema5 = ema(src, ema5Len)

// Sort slow EMAs for channel ordering (lower to upper)
timeseries emaSlowLo = math.min(ema2, ema3)
timeseries emaSlowHi = math.max(ema2, ema3)

// ---------------------------------------------------------------------------
// Regime Detection (Murphy 1999 multi-EMA alignment framework)
// ---------------------------------------------------------------------------
// Bullish: price above all EMAs, fast EMA above slow EMAs
// Bearish: price below all EMAs, fast EMA below slow EMAs
// Neutral: price between EMAs or mixed alignment

var aboveAll = src > ema1 && src > ema2 && src > ema3 && src > ema4 && src > ema5
var belowAll = src < ema1 && src < ema2 && src < ema3 && src < ema4 && src < ema5
var fastAboveSlow = ema1 > emaSlowHi
var fastBelowSlow = ema1 < emaSlowLo

var regime = 0    // 0 = neutral, 1 = bull, -1 = bear
if (aboveAll && fastAboveSlow) {
    regime = 1
} else if (belowAll && fastBelowSlow) {
    regime = -1
}

// ---------------------------------------------------------------------------
// Static State Tracking (session-level persistence)
// ---------------------------------------------------------------------------
static lastRegime = 0
static regimeChangeCount = 0
static lastBarIndex = 0
static crossUpCount = 0
static crossDownCount = 0
static sessionHighPrice = 0.0
static sessionLowPrice = 999999999.0

var currentBarIndex = bar_index[0]

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

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

    if (src > sessionHighPrice) {
        sessionHighPrice = src
    }
    if (src < sessionLowPrice) {
        sessionLowPrice = src
    }
}

// ---------------------------------------------------------------------------
// Cross & Touch Detection (shape markers)
// ---------------------------------------------------------------------------
var crossUp = cross(src, ema1) && src > ema1
var crossDn = cross(src, ema1) && src < ema1
var crossSlowUp = cross(src, emaSlowHi) && src > emaSlowHi
var crossSlowDn = cross(src, emaSlowLo) && src < emaSlowLo
var crossMediumUp = cross(src, ema4) && src > ema4
var crossMediumDn = cross(src, ema4) && src < ema4
var touchSlowHi = high >= emaSlowHi && low <= emaSlowHi
var touchSlowLo = high >= emaSlowLo && low <= emaSlowLo

if (crossUp) {
    crossUpCount = crossUpCount + 1
}
if (crossDn) {
    crossDownCount = crossDownCount + 1
}

// ---------------------------------------------------------------------------
// Plots
// ---------------------------------------------------------------------------

// --- Fast EMA (regime-colored) ---
if (showFast) {
    plotLine(
        value=ema1,
        width=lineWidth,
        colors=[color1, color1],
        colorIndex=regime == 1 ? 0 : 1,
        label=["EMA " + str(ema1Len)],
        desc=["Fast EMA — short-term trend filter"]
    )
}

// --- Slow EMAs (144 & 169) with channel fill ---
if (showSlow) {
    if (showFill) {
        plotLine(
            value=[emaSlowLo, emaSlowHi],
            width=1,
            colors=[opacity(bullFillColor, 15), opacity(bearFillColor, 15)],
            colorIndex=regime == 1 ? 0 : 1,
            fill=true,
            label=["Vegas Inner Lo", "Vegas Inner Hi"],
            desc=["Vegas inner channel lower bound", "Vegas inner channel upper bound"]
        )
    }
    plotLine(
        value=ema2,
        width=lineWidth,
        colors=[color2],
        label=["EMA " + str(ema2Len)],
        desc=["Slow EMA 1 — intermediate trend support/resistance"]
    )
    plotLine(
        value=ema3,
        width=lineWidth,
        colors=[color3],
        label=["EMA " + str(ema3Len)],
        desc=["Slow EMA 2 — intermediate trend support/resistance"]
    )
}

// --- Medium EMA (288) ---
if (showMedium) {
    plotLine(
        value=ema4,
        width=lineWidth,
        colors=[color4],
        label=["EMA " + str(ema4Len)],
        desc=["Medium EMA — medium-term trend direction"]
    )
}

// --- Very Slow EMA (576) ---
if (showVerySlow) {
    plotLine(
        value=ema5,
        width=lineWidth,
        colors=[color5],
        label=["EMA " + str(ema5Len)],
        desc=["Very Slow EMA — long-term macro trend"]
    )
}

// --- Outer channel fill (medium vs very slow) ---
if (showFill && showMedium && showVerySlow) {
    var outerLo = math.min(ema4, ema5)
    var outerHi = math.max(ema4, ema5)
    plotLine(
        value=[outerLo, outerHi],
        width=1,
        colors=[opacity(bullFillColor, 10), opacity(bearFillColor, 10)],
        colorIndex=regime == 1 ? 0 : 1,
        fill=true,
        label=["Vegas Outer Lo", "Vegas Outer Hi"],
        desc=["Vegas outer channel lower bound", "Vegas outer channel upper bound"]
    )
}

// --- Shape Markers ---
if (showShapes) {
    if (crossUp) {
        plotShape(
            value=low,
            shape="triangleup",
            colorIndex=color1,
            width=shapeSize,
            label=["Fast Cross Up"],
            desc=["Price crossed above fast EMA — short-term bullish signal"]
        )
    }
    if (crossDn) {
        plotShape(
            value=high,
            shape="triangledown",
            colorIndex=color1,
            width=shapeSize,
            label=["Fast Cross Down"],
            desc=["Price crossed below fast EMA — short-term bearish signal"]
        )
    }
    if (crossSlowUp) {
        plotShape(
            value=low,
            shape="triangleup",
            colorIndex=color2,
            width=shapeSize,
            label=["Slow Cross Up"],
            desc=["Price crossed above slow EMA channel — bullish trend confirmation"]
        )
    }
    if (crossSlowDn) {
        plotShape(
            value=high,
            shape="triangledown",
            colorIndex=color3,
            width=shapeSize,
            label=["Slow Cross Down"],
            desc=["Price crossed below slow EMA channel — bearish trend confirmation"]
        )
    }
    if (touchSlowHi && !crossSlowUp && !crossSlowDn) {
        plotShape(
            value=emaSlowHi,
            shape="circle",
            colorIndex=opacity(color2, 50),
            width=shapeSize,
            label=["Touch Slow Hi"],
            desc=["Price touched slow EMA upper channel — resistance test"]
        )
    }
    if (touchSlowLo && !crossSlowUp && !crossSlowDn) {
        plotShape(
            value=emaSlowLo,
            shape="circle",
            colorIndex=opacity(color3, 50),
            width=shapeSize,
            label=["Touch Slow Lo"],
            desc=["Price touched slow EMA lower channel — support test"]
        )
    }
}