Perfected FlowScript
VWAP MTF Pro [KYO]
VWAP MTF Pro [KYO] is indexed as 215_vwap_mtf_pro_kyo.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
412
Native Signal Candidates
6 source-level chart marker triggers extracted from guarded FlowScript v2 blocks.
VWAP Cross Up
line 353:5
Trigger
(showVwapCross && vwapCrossUp)
Value
low
Deps
showVwapCross:input, vwapCrossUp:var
VWAP Cross Down
line 363:5
Trigger
(showVwapCross && vwapCrossDown)
Value
high
Deps
showVwapCross:input, vwapCrossDown:var
Band 1 High Touch
line 373:5
Trigger
(showBandTouch && touchBand1Hi)
Value
band1Hi_1
Deps
showBandTouch:input, touchBand1Hi:var
Band 1 Low Touch
line 383:5
Trigger
(showBandTouch && touchBand1Lo)
Value
band1Lo_1
Deps
showBandTouch:input, touchBand1Lo:var
Band 2 High Touch
line 393:5
Trigger
(showBandTouch && touchBand2Hi)
Value
band2Hi_1
Deps
showBandTouch:input, touchBand2Hi:var
Band 2 Low Touch
line 403:5
Trigger
(showBandTouch && touchBand2Lo)
Value
band2Lo_1
Deps
showBandTouch:input, touchBand2Lo:var
215_vwap_mtf_pro_kyo.flowscript
source path: kscripts/perfected/215_vwap_mtf_pro_kyo.flowscript · sha256:32a3af147caead740b2bdd38f8d486260a7ccc7036ab53a8fc7d4358ee61c15a
//@version=2
// Source: kiyotaka.ai/hub/kscript (original) — Perfected by kScript_Perfection_Engineer
// ============================================================================
// VWAP MTF Pro [KYO] — Perfected
// Author: K — Perfected by kScript_Perfection_Engineer
// Tags: CORE, #vwap, #mtf, #anchoredvwap, #deviationbands, #kyo
//
// Multi-Timeframe VWAP with volume-weighted standard deviation bands,
// anchored computation using proper calendar math, regime detection,
// colour-coded fills, and shape markers for cross and band-touch events.
// Supports three simultaneous timeframes (Session, Daily, Weekly, Monthly)
// with independent anchor resets and deviation overlays.
//
// Research papers underpinning this design:
// 1. Berkowitz, Logue & Noser (1988), "The Total Cost of Transactions on the
// NYSE": foundational VWAP formula VWAP = Σ(Price_i × Volume_i) / ΣVolume_i
// as institutional benchmark.
// 2. Madhavan (2002), "VWAP Strategies": when VWAP is a sensible benchmark
// and how traders adjust strategies to match it.
// 3. Shannon (2021), "Maximum Trading Gains with the Anchored VWAP":
// AVWAP from a user-selected event reveals institutional fair value;
// anchor quality decays over 60–90 trading days.
// 4. StudyRaid (2026), "Standard Deviation Bands in VWAP": σ = √[Σ(Vᵢ·(Pᵢ−VWAP)²)/ΣVᵢ];
// ~68.2% of price activity within ±1σ, ~95.4% within ±2σ.
// 5. Harris (1986), "A Transaction Data Study of Weekly and Intradaily
// Patterns in Stock Returns": intraday seasonality supports session
// anchoring (Tokyo, London, New York, Sydney).
// ============================================================================
define(
title="VWAP MTF Pro [KYO]",
position="onchart",
axis=false
)
// ---------------------------------------------------------------------------
// 1. USER INPUTS — All grouped, all numeric inputs carry constraints
// ---------------------------------------------------------------------------
// -- Timeframes --
var anchor1 = input(name="anchor1", type="select", defaultValue="Daily", label="MTF 1", options=["Session", "Daily", "Weekly", "Monthly"], group="Timeframes")
var anchor2 = input(name="anchor2", type="select", defaultValue="Weekly", label="MTF 2", options=["Session", "Daily", "Weekly", "Monthly"], group="Timeframes")
var anchor3 = input(name="anchor3", type="select", defaultValue="Monthly", label="MTF 3", options=["Session", "Daily", "Weekly", "Monthly"], group="Timeframes")
var showMTF2 = input(name="showMTF2", type="boolean", defaultValue=true, label="Show MTF 2", group="Timeframes")
var showMTF3 = input(name="showMTF3", type="boolean", defaultValue=true, label="Show MTF 3", group="Timeframes")
// -- 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 band3Mult = input(name="band3Mult", type="number", defaultValue=3.0, label="Band 3 Multiplier (σ)", constraints={min: 0.1, max: 5.0, step: 0.1}, group="Bands")
var showBand1 = input(name="showBand1", type="boolean", defaultValue=true, label="Show Band 1", group="Bands")
var showBand2 = input(name="showBand2", type="boolean", defaultValue=true, label="Show Band 2", group="Bands")
var showBand3 = input(name="showBand3", type="boolean", defaultValue=false, label="Show Band 3", group="Bands")
var showFill = input(name="showFill", 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 vwap1Color = input(name="vwap1Color", type="color", defaultValue="#26a69a", label="MTF 1 Color", group="Colors")
var vwap2Color = input(name="vwap2Color", type="color", defaultValue="#ff9800", label="MTF 2 Color", group="Colors")
var vwap3Color = input(name="vwap3Color", type="color", defaultValue="#9c27b0", label="MTF 3 Color", group="Colors")
var band1Color = input(name="band1Color", type="color", defaultValue="#9e9e9e", label="Band 1 Line Color", group="Colors")
var band2Color = input(name="band2Color", type="color", defaultValue="#9e9e9e", label="Band 2 Line Color", group="Colors")
var band3Color = input(name="band3Color", type="color", defaultValue="#9e9e9e", label="Band 3 Line Color", group="Colors")
var aboveColor = input(name="aboveColor", type="color", defaultValue="#26a69a", label="Above VWAP Color (Bull)", group="Colors")
var belowColor = input(name="belowColor", type="color", defaultValue="#ef5350", label="Below VWAP Color (Bear)", group="Colors")
// ---------------------------------------------------------------------------
// 2. DATA SOURCES — with isnan() 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 typ = (high + low + close) / 3
timeseries pv = typ * bars.volume
// Guard: prevent computation on incomplete data
if (isnan(t[0]) || isnan(close[0]) || isnan(bars.volume[0]) || isnan(high[0]) || isnan(low[0])) {
return
}
// ---------------------------------------------------------------------------
// 3. TIME DERIVATION (Calendar Math)
// ---------------------------------------------------------------------------
timeseries rawH = math.floor((t / 3600000) % 24)
var day_id = math.floor(t / 86400000)
var week_id = math.floor((day_id + 3) / 7)
// Monthly calendar math
var z = day_id + 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 y = 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 year_final = y + (m <= 2 ? 1 : 0)
var month_id = year_final * 12 + m
// ---------------------------------------------------------------------------
// 4. ANCHOR DETECTION HELPER (static variables per VWAP instance)
// ---------------------------------------------------------------------------
// -- MTF 1 static state --
static last_day_id_1 = -1
static last_week_id_1 = -1
static last_month_id_1 = -1
static barCount_1 = 0
var newPeriod_1 = false
if (anchor1 == "Session") {
newPeriod_1 = rawH == 0 && barCount_1 > 0
} else if (anchor1 == "Daily") {
newPeriod_1 = last_day_id_1 != -1 && day_id > last_day_id_1
last_day_id_1 = day_id
} else if (anchor1 == "Weekly") {
newPeriod_1 = last_week_id_1 != -1 && week_id > last_week_id_1
last_week_id_1 = week_id
} else if (anchor1 == "Monthly") {
newPeriod_1 = last_month_id_1 != -1 && month_id > last_month_id_1
last_month_id_1 = month_id
}
if (newPeriod_1 || barCount_1 == 0) {
barCount_1 = 1
} else {
barCount_1 = barCount_1 + 1
}
// -- MTF 2 static state --
static last_day_id_2 = -1
static last_week_id_2 = -1
static last_month_id_2 = -1
static barCount_2 = 0
var newPeriod_2 = false
if (anchor2 == "Session") {
newPeriod_2 = rawH == 0 && barCount_2 > 0
} else if (anchor2 == "Daily") {
newPeriod_2 = last_day_id_2 != -1 && day_id > last_day_id_2
last_day_id_2 = day_id
} else if (anchor2 == "Weekly") {
newPeriod_2 = last_week_id_2 != -1 && week_id > last_week_id_2
last_week_id_2 = week_id
} else if (anchor2 == "Monthly") {
newPeriod_2 = last_month_id_2 != -1 && month_id > last_month_id_2
last_month_id_2 = month_id
}
if (newPeriod_2 || barCount_2 == 0) {
barCount_2 = 1
} else {
barCount_2 = barCount_2 + 1
}
// -- MTF 3 static state --
static last_day_id_3 = -1
static last_week_id_3 = -1
static last_month_id_3 = -1
static barCount_3 = 0
var newPeriod_3 = false
if (anchor3 == "Session") {
newPeriod_3 = rawH == 0 && barCount_3 > 0
} else if (anchor3 == "Daily") {
newPeriod_3 = last_day_id_3 != -1 && day_id > last_day_id_3
last_day_id_3 = day_id
} else if (anchor3 == "Weekly") {
newPeriod_3 = last_week_id_3 != -1 && week_id > last_week_id_3
last_week_id_3 = week_id
} else if (anchor3 == "Monthly") {
newPeriod_3 = last_month_id_3 != -1 && month_id > last_month_id_3
last_month_id_3 = month_id
}
if (newPeriod_3 || barCount_3 == 0) {
barCount_3 = 1
} else {
barCount_3 = barCount_3 + 1
}
// ---------------------------------------------------------------------------
// 5. VWAP COMPUTATION (anchored, not rolling)
// ---------------------------------------------------------------------------
// MTF 1
timeseries sumPV_1 = sum(pv, barCount_1)
timeseries sumV_1 = sum(bars.volume, barCount_1)
timeseries vwap1 = sumV_1 > 0 ? sumPV_1 / sumV_1 : typ
// Volume-weighted standard deviation for MTF 1
timeseries devSq_1 = (typ - vwap1) * (typ - vwap1) * bars.volume
timeseries sumSqDev_1 = sum(devSq_1, barCount_1)
timeseries variance_1 = sumV_1 > 0 ? sumSqDev_1 / sumV_1 : 0
timeseries sd_1 = math.sqrt(math.max(0, variance_1))
timeseries band1Hi_1 = vwap1 + band1Mult * sd_1
timeseries band1Lo_1 = vwap1 - band1Mult * sd_1
timeseries band2Hi_1 = vwap1 + band2Mult * sd_1
timeseries band2Lo_1 = vwap1 - band2Mult * sd_1
timeseries band3Hi_1 = vwap1 + band3Mult * sd_1
timeseries band3Lo_1 = vwap1 - band3Mult * sd_1
// MTF 2
timeseries sumPV_2 = sum(pv, barCount_2)
timeseries sumV_2 = sum(bars.volume, barCount_2)
timeseries vwap2 = sumV_2 > 0 ? sumPV_2 / sumV_2 : typ
// MTF 3
timeseries sumPV_3 = sum(pv, barCount_3)
timeseries sumV_3 = sum(bars.volume, barCount_3)
timeseries vwap3 = sumV_3 > 0 ? sumPV_3 / sumV_3 : typ
// ---------------------------------------------------------------------------
// 6. REGIME DETECTION (price relative to primary VWAP)
// ---------------------------------------------------------------------------
var aboveVwap1 = close > vwap1
// ---------------------------------------------------------------------------
// 7. CROSS & TOUCH DETECTION (shape markers)
// ---------------------------------------------------------------------------
var vwapCrossUp = cross(close, vwap1) && close > vwap1
var vwapCrossDown = cross(close, vwap1) && close < vwap1
var touchBand1Hi = high >= band1Hi_1 && low <= band1Hi_1
var touchBand1Lo = high >= band1Lo_1 && low <= band1Lo_1
var touchBand2Hi = high >= band2Hi_1 && low <= band2Hi_1
var touchBand2Lo = high >= band2Lo_1 && low <= band2Lo_1
// ---------------------------------------------------------------------------
// 8. VISUALIZATION
// ---------------------------------------------------------------------------
// 8.1 Primary VWAP (regime-coloured)
plotLine(
value=vwap1,
width=lineWidth,
colors=[aboveColor, belowColor],
colorIndex=aboveVwap1 ? 0 : 1,
label=["VWAP " + anchor1],
desc=["Primary anchored VWAP (MTF 1)"]
)
// 8.2 Secondary VWAPs
if (showMTF2) {
plotLine(
value=vwap2,
width=lineWidth,
colors=[vwap2Color],
label=["VWAP " + anchor2],
desc=["Secondary anchored VWAP (MTF 2)"]
)
}
if (showMTF3) {
plotLine(
value=vwap3,
width=lineWidth,
colors=[vwap3Color],
label=["VWAP " + anchor3],
desc=["Tertiary anchored VWAP (MTF 3)"]
)
}
// 8.3 Band 1 Fill & Lines
if (showBand1) {
if (showFill) {
plotLine(
value=([band1Lo_1, band1Hi_1]),
width=1,
colors=[opacity(aboveColor, 15), opacity(belowColor, 15)],
colorIndex=aboveVwap1 ? 0 : 1,
fill=true,
label=["±1σ Fill"],
desc=["±1σ band fill (regime-coloured)"]
)
}
plotLine(
value=band1Hi_1,
width=1,
colors=[band1Color],
label=["+1σ " + anchor1],
desc=["Upper band at 1σ multiplier for MTF 1"]
)
plotLine(
value=band1Lo_1,
width=1,
colors=[band1Color],
label=["-1σ " + anchor1],
desc=["Lower band at 1σ multiplier for MTF 1"]
)
}
// 8.4 Band 2 Fill & Lines
if (showBand2) {
if (showFill) {
plotLine(
value=([band2Lo_1, band2Hi_1]),
width=1,
colors=[opacity(aboveColor, 10), opacity(belowColor, 10)],
colorIndex=aboveVwap1 ? 0 : 1,
fill=true,
label=["±2σ Fill"],
desc=["±2σ band fill (regime-coloured)"]
)
}
plotLine(
value=band2Hi_1,
width=1,
colors=[band2Color],
label=["+2σ " + anchor1],
desc=["Upper band at 2σ multiplier for MTF 1"]
)
plotLine(
value=band2Lo_1,
width=1,
colors=[band2Color],
label=["-2σ " + anchor1],
desc=["Lower band at 2σ multiplier for MTF 1"]
)
}
// 8.5 Band 3 Lines
if (showBand3) {
plotLine(
value=band3Hi_1,
width=1,
colors=[band3Color],
label=["+3σ " + anchor1],
desc=["Upper band at 3σ multiplier for MTF 1"]
)
plotLine(
value=band3Lo_1,
width=1,
colors=[band3Color],
label=["-3σ " + anchor1],
desc=["Lower band at 3σ multiplier for MTF 1"]
)
}
// 8.6 Shape Markers
if (showVwapCross && vwapCrossUp) {
plotShape(
value=low,
shape="triangleup",
colorIndex=aboveColor,
width=shapeSize,
label=["VWAP Cross Up"],
desc=["Price crossed above VWAP " + anchor1]
)
}
if (showVwapCross && vwapCrossDown) {
plotShape(
value=high,
shape="triangledown",
colorIndex=belowColor,
width=shapeSize,
label=["VWAP Cross Down"],
desc=["Price crossed below VWAP " + anchor1]
)
}
if (showBandTouch && touchBand1Hi) {
plotShape(
value=band1Hi_1,
shape="triangledown",
colorIndex=band1Color,
width=shapeSize,
label=["Band 1 High Touch"],
desc=["Price touched upper 1σ band"]
)
}
if (showBandTouch && touchBand1Lo) {
plotShape(
value=band1Lo_1,
shape="triangleup",
colorIndex=band1Color,
width=shapeSize,
label=["Band 1 Low Touch"],
desc=["Price touched lower 1σ band"]
)
}
if (showBandTouch && touchBand2Hi) {
plotShape(
value=band2Hi_1,
shape="triangledown",
colorIndex=band2Color,
width=shapeSize,
label=["Band 2 High Touch"],
desc=["Price touched upper 2σ band"]
)
}
if (showBandTouch && touchBand2Lo) {
plotShape(
value=band2Lo_1,
shape="triangleup",
colorIndex=band2Color,
width=shapeSize,
label=["Band 2 Low Touch"],
desc=["Price touched lower 2σ band"]
)
}