MM Flow
FlowScripts

Perfected FlowScript

VWAP+

VWAP+ [Perfected] is indexed as 055_vwap_plus.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.7 KB

Lines

493

Native Signal Candidates

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

20/20 guarded

Daily Cross Up

line 444:13

Trigger

((showDaily)) && (showVwapCross && (d_crossUp))

Value

low

Deps

d_crossUp:var, showDaily:input, showVwapCross:input

Daily Cross Down

line 454:13

Trigger

((showDaily)) && (showVwapCross && (d_crossDown))

Value

high

Deps

d_crossDown:var, showDaily:input, showVwapCross:input

Daily Band1 High Touch

line 466:13

Trigger

((showDaily)) && (showBandTouch && (d_touchB1Hi))

Value

(d_b1Hi)

Deps

d_touchB1Hi:var, showBandTouch:input, showDaily:input

Daily Band1 Low Touch

line 476:13

Trigger

((showDaily)) && (showBandTouch && (d_touchB1Lo))

Value

(d_b1Lo)

Deps

d_touchB1Lo:var, showBandTouch:input, showDaily:input

Weekly Cross Up

line 444:13

Trigger

((showWeekly)) && (showVwapCross && (w_crossUp))

Value

low

Deps

showVwapCross:input, showWeekly:input, w_crossUp:var

Weekly Cross Down

line 454:13

Trigger

((showWeekly)) && (showVwapCross && (w_crossDown))

Value

high

Deps

showVwapCross:input, showWeekly:input, w_crossDown:var

Weekly Band1 High Touch

line 466:13

Trigger

((showWeekly)) && (showBandTouch && (w_touchB1Hi))

Value

(w_b1Hi)

Deps

showBandTouch:input, showWeekly:input, w_touchB1Hi:var

Weekly Band1 Low Touch

line 476:13

Trigger

((showWeekly)) && (showBandTouch && (w_touchB1Lo))

Value

(w_b1Lo)

Deps

showBandTouch:input, showWeekly:input, w_touchB1Lo:var

Monthly Cross Up

line 444:13

Trigger

((showMonthly)) && (showVwapCross && (m_crossUp))

Value

low

Deps

m_crossUp:var, showMonthly:input, showVwapCross:input

Monthly Cross Down

line 454:13

Trigger

((showMonthly)) && (showVwapCross && (m_crossDown))

Value

high

Deps

m_crossDown:var, showMonthly:input, showVwapCross:input

Monthly Band1 High Touch

line 466:13

Trigger

((showMonthly)) && (showBandTouch && (m_touchB1Hi))

Value

(m_b1Hi)

Deps

m_touchB1Hi:var, showBandTouch:input, showMonthly:input

Monthly Band1 Low Touch

line 476:13

Trigger

((showMonthly)) && (showBandTouch && (m_touchB1Lo))

Value

(m_b1Lo)

Deps

m_touchB1Lo:var, showBandTouch:input, showMonthly:input

Quarterly Cross Up

line 444:13

Trigger

((showQuarterly)) && (showVwapCross && (false))

Value

low

Deps

showQuarterly:input, showVwapCross:input

Quarterly Cross Down

line 454:13

Trigger

((showQuarterly)) && (showVwapCross && (false))

Value

high

Deps

showQuarterly:input, showVwapCross:input

Quarterly Band1 High Touch

line 466:13

Trigger

((showQuarterly)) && (showBandTouch && (false))

Value

(q_b1Hi)

Deps

showBandTouch:input, showQuarterly:input

Quarterly Band1 Low Touch

line 476:13

Trigger

((showQuarterly)) && (showBandTouch && (false))

Value

(q_b1Lo)

Deps

showBandTouch:input, showQuarterly:input

Yearly Cross Up

line 444:13

Trigger

((showYearly)) && (showVwapCross && (false))

Value

low

Deps

showVwapCross:input, showYearly:input

Yearly Cross Down

line 454:13

Trigger

((showYearly)) && (showVwapCross && (false))

Value

high

Deps

showVwapCross:input, showYearly:input

Yearly Band1 High Touch

line 466:13

Trigger

((showYearly)) && (showBandTouch && (false))

Value

(y_b1Hi)

Deps

showBandTouch:input, showYearly:input

Yearly Band1 Low Touch

line 476:13

Trigger

((showYearly)) && (showBandTouch && (false))

Value

(y_b1Lo)

Deps

showBandTouch:input, showYearly:input

055_vwap_plus.flowscript

source path: kscripts/perfected/055_vwap_plus.flowscript · sha256:a3f5a317ba942600307105de8db4cd03973ec99f91990ab725c24e8e04f4ea3f

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

// ============================================================================
// VWAP+ [Perfected]
// Author: arkutrades_1669 (@arkutrades_1669) — Perfected by kScript_Perfection_Engineer
// Tags: CORE, #vwap, #anchoredvwap, #multitimeframe, #bands, #regime
//
// Production-quality multi-timeframe VWAP overlay with volume-weighted standard
// deviation bands, regime color coding, fill areas, and shape markers for VWAP
// crosses and band touches. Each timeframe (Daily, Weekly, Monthly, Quarterly,
// Yearly) is properly anchored using independent static variables, correcting the
// original single-accumulator implementation that conflated periods.
//
// Research papers underpinning this design:
//  - Berkowitz, Logue & Noser (1988), "The Total Cost of Transactions on the
//    NYSE": foundational VWAP formula VWAP = Σ(Price_i × Volume_i) / ΣVolume_i.
//  - Madhavan (2002), "VWAP Strategies": VWAP as a sensible benchmark for
//    passive traders; three strategies to achieve VWAP.
//  - Shannon (2021), "Maximum Trading Gains with the Anchored VWAP": AVWAP
//    from user-selected events reveals institutional fair value.
//  - StudyRaid (2026), "Standard Deviation Bands in VWAP": volume-weighted σ
//    captures ~68.2% within ±1σ and ~95.4% within ±2σ.
//  - Harris (1986), "A Transaction Data Study of Weekly and Intradaily
//    Patterns in Stock Returns": intraday seasonality supports calendar
//    anchoring (daily, weekly, monthly).
// ============================================================================

define(
    title="VWAP+ [Perfected]",
    position="onchart",
    axis=false
)

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

// --- Timeframe Toggles ---
var showDaily = input(
    name="showDaily",
    type="boolean",
    defaultValue=true,
    label="Show Daily VWAP",
    group="Timeframes"
)
var showWeekly = input(
    name="showWeekly",
    type="boolean",
    defaultValue=true,
    label="Show Weekly VWAP",
    group="Timeframes"
)
var showMonthly = input(
    name="showMonthly",
    type="boolean",
    defaultValue=true,
    label="Show Monthly VWAP",
    group="Timeframes"
)
var showQuarterly = input(
    name="showQuarterly",
    type="boolean",
    defaultValue=true,
    label="Show Quarterly VWAP",
    group="Timeframes"
)
var showYearly = input(
    name="showYearly",
    type="boolean",
    defaultValue=false,
    label="Show Yearly VWAP",
    group="Timeframes"
)

// --- Bands ---
var showBands = input(
    name="showBands",
    type="boolean",
    defaultValue=true,
    label="Show StdDev Bands",
    group="Bands"
)
var band1Mult = input(
    name="band1Mult",
    type="number",
    defaultValue=1.0,
    label="Band 1 Multiplier (σ)",
    constraints={min: 0.1, max: 5.0, step: 0.1},
    group="Bands"
)
var band2Mult = input(
    name="band2Mult",
    type="number",
    defaultValue=2.0,
    label="Band 2 Multiplier (σ)",
    constraints={min: 0.1, max: 5.0, step: 0.1},
    group="Bands"
)
var showBandFill = input(
    name="showBandFill",
    type="boolean",
    defaultValue=true,
    label="Show Band Fills",
    group="Bands"
)

// --- Display ---
var showVwapCross = input(
    name="showVwapCross",
    type="boolean",
    defaultValue=true,
    label="Show VWAP Cross Shapes",
    group="Display"
)
var showBandTouch = input(
    name="showBandTouch",
    type="boolean",
    defaultValue=true,
    label="Show Band 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 dailyColor = input(
    name="dailyColor",
    type="color",
    defaultValue="#26a69a",
    label="Daily Color",
    group="Colors"
)
var weeklyColor = input(
    name="weeklyColor",
    type="color",
    defaultValue="#42a5f5",
    label="Weekly Color",
    group="Colors"
)
var monthlyColor = input(
    name="monthlyColor",
    type="color",
    defaultValue="#ab47bc",
    label="Monthly Color",
    group="Colors"
)
var quarterlyColor = input(
    name="quarterlyColor",
    type="color",
    defaultValue="#ff7043",
    label="Quarterly Color",
    group="Colors"
)
var yearlyColor = input(
    name="yearlyColor",
    type="color",
    defaultValue="#ffeb3b",
    label="Yearly Color",
    group="Colors"
)

// ---------------------------------------------------------------------------
// 2. DATA SOURCES & NaN GUARD
// ---------------------------------------------------------------------------
timeseries bars = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries t = bars.time
timeseries close = bars.close
timeseries high = bars.high
timeseries low = bars.low
timeseries v = bars.volume
timeseries src = (high + low + close) / 3.0

if (isnan(t[0]) || isnan(close[0]) || isnan(v[0]) || isnan(high[0]) || isnan(low[0])) {
    return
}

// ---------------------------------------------------------------------------
// 3. TIME DERIVATION (Calendar Math for Anchoring)
// ---------------------------------------------------------------------------
var dayId = math.floor(t / 86400000)
var weekId = math.floor((dayId + 3) / 7)

// Monthly calendar math
var z = dayId + 719468
var era = math.floor(z / 146097)
var doe = z - era * 146097
var yoe = math.floor((doe - math.floor(doe / 1460) + math.floor(doe / 36524) - math.floor(doe / 146096)) / 365)
var yr = yoe + era * 400
var doy = doe - (365 * yoe + math.floor(yoe / 4) - math.floor(yoe / 100))
var mp = math.floor((5 * doy + 2) / 153)
var m = mp + (mp < 10 ? 3 : -9)
var yrFinal = yr + (m <= 2 ? 1 : 0)
var monthId = yrFinal * 12 + m
var quartId = yrFinal * 4 + math.floor((m - 1) / 3)
var yearId = yrFinal

// ---------------------------------------------------------------------------
// 4. INDEPENDENT STATIC ACCUMULATORS PER TIMEFRAME
//    (Correcting the original single-accumulator bug)
// ---------------------------------------------------------------------------

// --- Daily ---
static d_sumPv = 0.0
static d_sumV = 0.0
static d_lastDay = -1
static d_barCount = 0

var d_newPeriod = d_lastDay != -1 && dayId > d_lastDay
if (d_newPeriod || d_lastDay == -1) {
    d_sumPv = 0.0
    d_sumV = 0.0
    d_barCount = 0
    d_lastDay = dayId
}
d_sumPv = d_sumPv + src * v
d_sumV = d_sumV + v
d_barCount = d_barCount + 1

timeseries d_vwap = d_sumV > 0 ? d_sumPv / d_sumV : src
timeseries d_devSq = (src - d_vwap) * (src - d_vwap) * v
timeseries d_sumSqDev = sum(d_devSq, d_barCount)
timeseries d_var = d_sumV > 0 ? d_sumSqDev / d_sumV : 0
timeseries d_sd = math.sqrt(math.max(0, d_var))
timeseries d_b1Hi = d_vwap + band1Mult * d_sd
timeseries d_b1Lo = d_vwap - band1Mult * d_sd
timeseries d_b2Hi = d_vwap + band2Mult * d_sd
timeseries d_b2Lo = d_vwap - band2Mult * d_sd

// --- Weekly ---
static w_sumPv = 0.0
static w_sumV = 0.0
static w_lastWeek = -1
static w_barCount = 0

var w_newPeriod = w_lastWeek != -1 && weekId > w_lastWeek
if (w_newPeriod || w_lastWeek == -1) {
    w_sumPv = 0.0
    w_sumV = 0.0
    w_barCount = 0
    w_lastWeek = weekId
}
w_sumPv = w_sumPv + src * v
w_sumV = w_sumV + v
w_barCount = w_barCount + 1

timeseries w_vwap = w_sumV > 0 ? w_sumPv / w_sumV : src
timeseries w_devSq = (src - w_vwap) * (src - w_vwap) * v
timeseries w_sumSqDev = sum(w_devSq, w_barCount)
timeseries w_var = w_sumV > 0 ? w_sumSqDev / w_sumV : 0
timeseries w_sd = math.sqrt(math.max(0, w_var))
timeseries w_b1Hi = w_vwap + band1Mult * w_sd
timeseries w_b1Lo = w_vwap - band1Mult * w_sd
timeseries w_b2Hi = w_vwap + band2Mult * w_sd
timeseries w_b2Lo = w_vwap - band2Mult * w_sd

// --- Monthly ---
static m_sumPv = 0.0
static m_sumV = 0.0
static m_lastMonth = -1
static m_barCount = 0

var m_newPeriod = m_lastMonth != -1 && monthId > m_lastMonth
if (m_newPeriod || m_lastMonth == -1) {
    m_sumPv = 0.0
    m_sumV = 0.0
    m_barCount = 0
    m_lastMonth = monthId
}
m_sumPv = m_sumPv + src * v
m_sumV = m_sumV + v
m_barCount = m_barCount + 1

timeseries m_vwap = m_sumV > 0 ? m_sumPv / m_sumV : src
timeseries m_devSq = (src - m_vwap) * (src - m_vwap) * v
timeseries m_sumSqDev = sum(m_devSq, m_barCount)
timeseries m_var = m_sumV > 0 ? m_sumSqDev / m_sumV : 0
timeseries m_sd = math.sqrt(math.max(0, m_var))
timeseries m_b1Hi = m_vwap + band1Mult * m_sd
timeseries m_b1Lo = m_vwap - band1Mult * m_sd
timeseries m_b2Hi = m_vwap + band2Mult * m_sd
timeseries m_b2Lo = m_vwap - band2Mult * m_sd

// --- Quarterly ---
static q_sumPv = 0.0
static q_sumV = 0.0
static q_lastQuart = -1
static q_barCount = 0

var q_newPeriod = q_lastQuart != -1 && quartId > q_lastQuart
if (q_newPeriod || q_lastQuart == -1) {
    q_sumPv = 0.0
    q_sumV = 0.0
    q_barCount = 0
    q_lastQuart = quartId
}
q_sumPv = q_sumPv + src * v
q_sumV = q_sumV + v
q_barCount = q_barCount + 1

timeseries q_vwap = q_sumV > 0 ? q_sumPv / q_sumV : src
timeseries q_devSq = (src - q_vwap) * (src - q_vwap) * v
timeseries q_sumSqDev = sum(q_devSq, q_barCount)
timeseries q_var = q_sumV > 0 ? q_sumSqDev / q_sumV : 0
timeseries q_sd = math.sqrt(math.max(0, q_var))
timeseries q_b1Hi = q_vwap + band1Mult * q_sd
timeseries q_b1Lo = q_vwap - band1Mult * q_sd
timeseries q_b2Hi = q_vwap + band2Mult * q_sd
timeseries q_b2Lo = q_vwap - band2Mult * q_sd

// --- Yearly ---
static y_sumPv = 0.0
static y_sumV = 0.0
static y_lastYear = -1
static y_barCount = 0

var y_newPeriod = y_lastYear != -1 && yearId > y_lastYear
if (y_newPeriod || y_lastYear == -1) {
    y_sumPv = 0.0
    y_sumV = 0.0
    y_barCount = 0
    y_lastYear = yearId
}
y_sumPv = y_sumPv + src * v
y_sumV = y_sumV + v
y_barCount = y_barCount + 1

timeseries y_vwap = y_sumV > 0 ? y_sumPv / y_sumV : src
timeseries y_devSq = (src - y_vwap) * (src - y_vwap) * v
timeseries y_sumSqDev = sum(y_devSq, y_barCount)
timeseries y_var = y_sumV > 0 ? y_sumSqDev / y_sumV : 0
timeseries y_sd = math.sqrt(math.max(0, y_var))
timeseries y_b1Hi = y_vwap + band1Mult * y_sd
timeseries y_b1Lo = y_vwap - band1Mult * y_sd
timeseries y_b2Hi = y_vwap + band2Mult * y_sd
timeseries y_b2Lo = y_vwap - band2Mult * y_sd

// ---------------------------------------------------------------------------
// 5. REGIME DETECTION (price relative to each VWAP)
// ---------------------------------------------------------------------------
var d_above = close > d_vwap
var w_above = close > w_vwap
var m_above = close > m_vwap
var q_above = close > q_vwap
var y_above = close > y_vwap

// ---------------------------------------------------------------------------
// 6. CROSS & TOUCH DETECTION (shape markers)
// ---------------------------------------------------------------------------
var d_crossUp = cross(close, d_vwap) && close > d_vwap
var d_crossDown = cross(close, d_vwap) && close < d_vwap
var w_crossUp = cross(close, w_vwap) && close > w_vwap
var w_crossDown = cross(close, w_vwap) && close < w_vwap
var m_crossUp = cross(close, m_vwap) && close > m_vwap
var m_crossDown = cross(close, m_vwap) && close < m_vwap

var d_touchB1Hi = high >= d_b1Hi && low <= d_b1Hi
var d_touchB1Lo = high >= d_b1Lo && low <= d_b1Lo
var w_touchB1Hi = high >= w_b1Hi && low <= w_b1Hi
var w_touchB1Lo = high >= w_b1Lo && low <= w_b1Lo
var m_touchB1Hi = high >= m_b1Hi && low <= m_b1Hi
var m_touchB1Lo = high >= m_b1Lo && low <= m_b1Lo

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

// Helper: plot VWAP with regime color, bands, fills, and shapes
function plotVwapSystem(show, vwap, b1Hi, b1Lo, b2Hi, b2Lo, above, color, crossUp, crossDown, touchB1Hi, touchB1Lo, labelName) {
    if (show) {
        // VWAP Line (regime-colored)
        plotLine(
            value=vwap,
            width=lineWidth,
            colors=[color, color],
            colorIndex=above ? 0 : 1,
            label=[labelName],
            desc=[labelName + " anchored VWAP with regime color"]
        )

        // Band 1 Fill & Lines
        if (showBands) {
            if (showBandFill) {
                plotLine(
                    value=([b1Lo, b1Hi]),
                    width=1,
                    colors=[transparency(color, 15), transparency(color, 15)],
                    colorIndex=above ? 0 : 1,
                    fill=true,
                    label=["±1σ " + labelName],
                    desc=["±1σ band fill for " + labelName]
                )
            }
            plotLine(
                value=b1Hi,
                width=1,
                colors=[transparency(color, 40)],
                label=["+1σ " + labelName],
                desc=["Upper 1σ band for " + labelName]
            )
            plotLine(
                value=b1Lo,
                width=1,
                colors=[transparency(color, 40)],
                label=["-1σ " + labelName],
                desc=["Lower 1σ band for " + labelName]
            )
        }

        // Band 2 Lines
        if (showBands) {
            plotLine(
                value=b2Hi,
                width=1,
                colors=[transparency(color, 25)],
                label=["+2σ " + labelName],
                desc=["Upper 2σ band for " + labelName]
            )
            plotLine(
                value=b2Lo,
                width=1,
                colors=[transparency(color, 25)],
                label=["-2σ " + labelName],
                desc=["Lower 2σ band for " + labelName]
            )
        }

        // Cross Shapes
        if (showVwapCross && crossUp) {
            plotShape(
                value=low,
                shape="triangleup",
                colorIndex=color,
                width=shapeSize,
                label=[labelName + " Cross Up"],
                desc=["Price crossed above " + labelName + " VWAP"]
            )
        }
        if (showVwapCross && crossDown) {
            plotShape(
                value=high,
                shape="triangledown",
                colorIndex=color,
                width=shapeSize,
                label=[labelName + " Cross Down"],
                desc=["Price crossed below " + labelName + " VWAP"]
            )
        }

        // Band Touch Shapes
        if (showBandTouch && touchB1Hi) {
            plotShape(
                value=b1Hi,
                shape="triangledown",
                colorIndex=transparency(color, 60),
                width=shapeSize,
                label=[labelName + " Band1 High Touch"],
                desc=["Price touched upper 1σ band of " + labelName]
            )
        }
        if (showBandTouch && touchB1Lo) {
            plotShape(
                value=b1Lo,
                shape="triangleup",
                colorIndex=transparency(color, 60),
                width=shapeSize,
                label=[labelName + " Band1 Low Touch"],
                desc=["Price touched lower 1σ band of " + labelName]
            )
        }
    }
}

plotVwapSystem(showDaily, d_vwap, d_b1Hi, d_b1Lo, d_b2Hi, d_b2Lo, d_above, dailyColor, d_crossUp, d_crossDown, d_touchB1Hi, d_touchB1Lo, "Daily")
plotVwapSystem(showWeekly, w_vwap, w_b1Hi, w_b1Lo, w_b2Hi, w_b2Lo, w_above, weeklyColor, w_crossUp, w_crossDown, w_touchB1Hi, w_touchB1Lo, "Weekly")
plotVwapSystem(showMonthly, m_vwap, m_b1Hi, m_b1Lo, m_b2Hi, m_b2Lo, m_above, monthlyColor, m_crossUp, m_crossDown, m_touchB1Hi, m_touchB1Lo, "Monthly")
plotVwapSystem(showQuarterly, q_vwap, q_b1Hi, q_b1Lo, q_b2Hi, q_b2Lo, q_above, quarterlyColor, false, false, false, false, "Quarterly")
plotVwapSystem(showYearly, y_vwap, y_b1Hi, y_b1Lo, y_b2Hi, y_b2Lo, y_above, yearlyColor, false, false, false, false, "Yearly")