STRYK: SESSION VWAP

Es-Sniper · study · 375 行 · 点赞 11 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// =====================================================================================
// STRYK: SESSION VWAP v1.2 — Asia, London and New York each get their own VWAP.
//
// While a session is open its VWAP runs (curves with price). When the session closes the
// VWAP locks at its final value and keeps going as a flat line through the rest of the day.
// When the SAME session opens again the next day, its line snaps to the new open and starts
// over. Result: three stepping lines with curves in them, one per session.
// Each session: its own colour / width / style, ±1 / ±2 / ±3 standard-deviation bands
// (each band on/off, multipliers editable), and a gradient fill between the VWAP and its
// bands. Locked (closed-session) stretches can be faded so live sessions stand out.
// Sessions are set in Chicago time by default (change the time zone in Display).
// v1.1: naked prior-session VWAPs, session table, locked-VWAP cross / reject alerts, explicit
//       session states, widest-band fill, consistent volume weights, clean status line.
// v1.1.1: full plain-English tooltip on every setting.
// v1.1.2: fixes RE10140 'too many plots (75)' — constant line style, sorted band slots,
//         no hidden helper plots, two-step fixed-colour fill. Worst-case count is now 60 of 64.
// v1.2: shading only on the live session (auto-shade switch); locked sessions keep lines and bands, no shade.
// Context tool only — not financial advice.
// =====================================================================================
//@version=6
indicator("STRYK: SESSION VWAP", "STRYK:SV", overlay = true, max_labels_count = 30)
 
// --- 0 · Inputs -------------------------------------------------------------------------
string G0 = "0 · Display"
svOn   = input.bool(true, "INDICATOR ON", group = G0,
     tooltip = "Master switch. OFF hides every line, band, fill, tag, naked level and the table, and stops every alert.", display = display.none)
tzIn   = input.string("America/Chicago", "Time zone", group = G0,
     tooltip = "Time zone that every session time below is written in (IANA name such as America/Chicago, America/New_York, Europe/London, Asia/Tokyo). Using a named zone follows daylight-saving changes automatically.", display = display.none)
srcIn  = input.source(hlc3, "VWAP source", group = G0,
     tooltip = "Price fed into every VWAP and its bands. hlc3 (typical price: (high + low + close) ÷ 3) is the standard VWAP source; close or ohlc4 are alternatives.", display = display.none)
lockT  = input.int(40, "Locked line fade %", minval = 0, maxval = 90, group = G0, inline = "d1",
     tooltip = "LOCKED LINE FADE %: after a session closes, its VWAP keeps going as a flat line — drawn this much more transparent so the live session stands out (0 = same as live, 90 = barely visible). GAP AT SNAP: when a session re-opens, its line jumps (snaps) to the new open; ON leaves a one-candle gap there instead of a vertical step.", display = display.none)
snapGap = input.bool(false, "gap at snap", group = G0, inline = "d1",
     tooltip = "LOCKED LINE FADE %: after a session closes, its VWAP keeps going as a flat line — drawn this much more transparent so the live session stands out (0 = same as live, 90 = barely visible). GAP AT SNAP: when a session re-opens, its line jumps (snaps) to the new open; ON leaves a one-candle gap there instead of a vertical step.", display = display.none)
fillOn = input.bool(true, "Gradient fill", group = G0, inline = "d2",
     tooltip = "GRADIENT FILL: two-step shade between each VWAP and its widest switched-on band — light from the VWAP out, stronger between the next band in and the outer band (with one band on, a single light layer). Each session also has its own fill switch. STRENGTH: 0 = faint, 100 = strong (keep it low so candles stay readable).", display = display.none)
fillS  = input.int(20, "strength", minval = 0, maxval = 100, group = G0, inline = "d2",
     tooltip = "GRADIENT FILL: two-step shade between each VWAP and its widest switched-on band — light from the VWAP out, stronger between the next band in and the outer band (with one band on, a single light layer). Each session also has its own fill switch. STRENGTH: 0 = faint, 100 = strong (keep it low so candles stay readable).", display = display.none)
tagOn  = input.bool(true, "Right-edge tags", group = G0, inline = "d3",
     tooltip = "RIGHT-EDGE TAGS: a small tag beside the last candle with each session's name and VWAP value; 🔒 = the session is closed and its VWAP is locked. SIZE: tag text size.", display = display.none)
tagSz  = input.string("Small", "size", options = ["Tiny", "Small", "Normal", "Large"], group = G0, inline = "d3",
     tooltip = "RIGHT-EDGE TAGS: a small tag beside the last candle with each session's name and VWAP value; 🔒 = the session is closed and its VWAP is locked. SIZE: tag text size.", display = display.none)
 
// one block of inputs per session (same layout so every session is customised the same way)
string GA = "1 · ASIA"
aOn    = input.bool(true, "Show", group = GA, inline = "a0",
     tooltip = "ASIA ON / OFF and the name used in the tags, table and alerts. Turning a session off hides its VWAP, bands, fill, naked levels, table row and alerts.", display = display.none)
aNm    = input.string("ASIA", "name", group = GA, inline = "a0",
     tooltip = "ASIA ON / OFF and the name used in the tags, table and alerts. Turning a session off hides its VWAP, bands, fill, naked levels, table row and alerts.", display = display.none)
aSs    = input.session("1700-0200", "Session", group = GA,
     tooltip = "ASIA session hours in the time zone set in Display (default 17:00–02:00 Chicago, the CME open through Tokyo). The VWAP starts on the first candle inside these hours, runs until the last one, then locks. Hours can cross midnight (e.g. 1700-0200). Pick times that line up with your chart timeframe — a candle that straddles a custom boundary can't be split. A session already open on the first loaded candle is skipped until its next open (no partial VWAP).", display = display.none)
aCl    = input.color(#FFB347, "VWAP", group = GA, inline = "a1",
     tooltip = "ASIA VWAP line: colour, width (1–6) and style (solid / dashed / dotted). While the session is open the line is drawn in this colour; once it closes and locks, the same colour is faded by 'Locked line fade %'.", display = display.none)
aW     = input.int(2, "width", minval = 1, maxval = 6, group = GA, inline = "a1",
     tooltip = "ASIA VWAP line: colour, width (1–6) and style (solid / dashed / dotted). While the session is open the line is drawn in this colour; once it closes and locks, the same colour is faded by 'Locked line fade %'.", display = display.none)
aSt    = input.string("Solid", "style", options = ["Solid", "Dashed", "Dotted"], group = GA, inline = "a1",
     tooltip = "ASIA VWAP line: colour, width (1–6) and style (solid / dashed / dotted). While the session is open the line is drawn in this colour; once it closes and locks, the same colour is faded by 'Locked line fade %'.", display = display.none)
aB1    = input.bool(false, "±1", group = GA, inline = "a2",
     tooltip = "ASIA standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
aM1    = input.float(1.0, "", minval = 0.1, step = 0.1, group = GA, inline = "a2",
     tooltip = "ASIA standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
aB2    = input.bool(false, "±2", group = GA, inline = "a2",
     tooltip = "ASIA standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
aM2    = input.float(2.0, "", minval = 0.1, step = 0.1, group = GA, inline = "a2",
     tooltip = "ASIA standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
aB3    = input.bool(false, "±3", group = GA, inline = "a2",
     tooltip = "ASIA standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
aM3    = input.float(3.0, "", minval = 0.1, step = 0.1, group = GA, inline = "a2",
     tooltip = "ASIA standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
aBc    = input.color(#FFB347, "Bands", group = GA, inline = "a3",
     tooltip = "ASIA band lines: colour, width (1–4) and style. FILL = the gradient between the ASIA VWAP and its widest switched-on band (two-step shade: light from the VWAP out, stronger between the next band in and the outer band; strength set in Display). This switch is used only when 'Auto-shade live session only' (Display) is OFF. Shading shows only while the session is live; once it locks, the lines and bands stay but the shade goes.", display = display.none)
aBw    = input.int(1, "width", minval = 1, maxval = 4, group = GA, inline = "a3",
     tooltip = "ASIA band lines: colour, width (1–4) and style. FILL = the gradient between the ASIA VWAP and its widest switched-on band (two-step shade: light from the VWAP out, stronger between the next band in and the outer band; strength set in Display). This switch is used only when 'Auto-shade live session only' (Display) is OFF. Shading shows only while the session is live; once it locks, the lines and bands stay but the shade goes.", display = display.none)
aBs    = input.string("Dotted", "style", options = ["Solid", "Dashed", "Dotted"], group = GA, inline = "a3",
     tooltip = "ASIA band lines: colour, width (1–4) and style. FILL = the gradient between the ASIA VWAP and its widest switched-on band (two-step shade: light from the VWAP out, stronger between the next band in and the outer band; strength set in Display). This switch is used only when 'Auto-shade live session only' (Display) is OFF. Shading shows only while the session is live; once it locks, the lines and bands stay but the shade goes.", display = display.none)
aFl    = input.bool(false, "fill", group = GA, inline = "a3",
     tooltip = "ASIA band lines: colour, width (1–4) and style. FILL = the gradient between the ASIA VWAP and its widest switched-on band (two-step shade: light from the VWAP out, stronger between the next band in and the outer band; strength set in Display). This switch is used only when 'Auto-shade live session only' (Display) is OFF. Shading shows only while the session is live; once it locks, the lines and bands stay but the shade goes.", display = display.none)
 
string GL = "2 · LONDON"
lOn    = input.bool(true, "Show", group = GL, inline = "l0",
     tooltip = "LONDON ON / OFF and the name used in the tags, table and alerts. Turning a session off hides its VWAP, bands, fill, naked levels, table row and alerts.", display = display.none)
lNm    = input.string("LONDON", "name", group = GL, inline = "l0",
     tooltip = "LONDON ON / OFF and the name used in the tags, table and alerts. Turning a session off hides its VWAP, bands, fill, naked levels, table row and alerts.", display = display.none)
lSs    = input.session("0200-1030", "Session", group = GL,
     tooltip = "LONDON session hours in the time zone set in Display (default 02:00–10:30 Chicago = 08:00–16:30 UK). The VWAP starts on the first candle inside these hours, runs until the last one, then locks. Hours can cross midnight (e.g. 1700-0200). Pick times that line up with your chart timeframe — a candle that straddles a custom boundary can't be split. A session already open on the first loaded candle is skipped until its next open (no partial VWAP).", display = display.none)
lCl    = input.color(#4FC3F7, "VWAP", group = GL, inline = "l1",
     tooltip = "LONDON VWAP line: colour, width (1–6) and style (solid / dashed / dotted). While the session is open the line is drawn in this colour; once it closes and locks, the same colour is faded by 'Locked line fade %'.", display = display.none)
lW     = input.int(2, "width", minval = 1, maxval = 6, group = GL, inline = "l1",
     tooltip = "LONDON VWAP line: colour, width (1–6) and style (solid / dashed / dotted). While the session is open the line is drawn in this colour; once it closes and locks, the same colour is faded by 'Locked line fade %'.", display = display.none)
lSt    = input.string("Solid", "style", options = ["Solid", "Dashed", "Dotted"], group = GL, inline = "l1",
     tooltip = "LONDON VWAP line: colour, width (1–6) and style (solid / dashed / dotted). While the session is open the line is drawn in this colour; once it closes and locks, the same colour is faded by 'Locked line fade %'.", display = display.none)
lB1    = input.bool(true, "±1", group = GL, inline = "l2",
     tooltip = "LONDON standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
lM1    = input.float(1.0, "", minval = 0.1, step = 0.1, group = GL, inline = "l2",
     tooltip = "LONDON standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
lB2    = input.bool(false, "±2", group = GL, inline = "l2",
     tooltip = "LONDON standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
lM2    = input.float(2.0, "", minval = 0.1, step = 0.1, group = GL, inline = "l2",
     tooltip = "LONDON standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
lB3    = input.bool(false, "±3", group = GL, inline = "l2",
     tooltip = "LONDON standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
lM3    = input.float(3.0, "", minval = 0.1, step = 0.1, group = GL, inline = "l2",
     tooltip = "LONDON standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
lBc    = input.color(#4FC3F7, "Bands", group = GL, inline = "l3",
     tooltip = "LONDON band lines: colour, width (1–4) and style. FILL = the gradient between the LONDON VWAP and its widest switched-on band (two-step shade: light from the VWAP out, stronger between the next band in and the outer band; strength set in Display). This switch is used only when 'Auto-shade live session only' (Display) is OFF. Shading shows only while the session is live; once it locks, the lines and bands stay but the shade goes.", display = display.none)
lBw    = input.int(1, "width", minval = 1, maxval = 4, group = GL, inline = "l3",
     tooltip = "LONDON band lines: colour, width (1–4) and style. FILL = the gradient between the LONDON VWAP and its widest switched-on band (two-step shade: light from the VWAP out, stronger between the next band in and the outer band; strength set in Display). This switch is used only when 'Auto-shade live session only' (Display) is OFF. Shading shows only while the session is live; once it locks, the lines and bands stay but the shade goes.", display = display.none)
lBs    = input.string("Dotted", "style", options = ["Solid", "Dashed", "Dotted"], group = GL, inline = "l3",
     tooltip = "LONDON band lines: colour, width (1–4) and style. FILL = the gradient between the LONDON VWAP and its widest switched-on band (two-step shade: light from the VWAP out, stronger between the next band in and the outer band; strength set in Display). This switch is used only when 'Auto-shade live session only' (Display) is OFF. Shading shows only while the session is live; once it locks, the lines and bands stay but the shade goes.", display = display.none)
lFl    = input.bool(false, "fill", group = GL, inline = "l3",
     tooltip = "LONDON band lines: colour, width (1–4) and style. FILL = the gradient between the LONDON VWAP and its widest switched-on band (two-step shade: light from the VWAP out, stronger between the next band in and the outer band; strength set in Display). This switch is used only when 'Auto-shade live session only' (Display) is OFF. Shading shows only while the session is live; once it locks, the lines and bands stay but the shade goes.", display = display.none)
 
string GN = "3 · NEW YORK"
nOn    = input.bool(true, "Show", group = GN, inline = "n0",
     tooltip = "NEW YORK ON / OFF and the name used in the tags, table and alerts. Turning a session off hides its VWAP, bands, fill, naked levels, table row and alerts.", display = display.none)
nNm    = input.string("NEW YORK", "name", group = GN, inline = "n0",
     tooltip = "NEW YORK ON / OFF and the name used in the tags, table and alerts. Turning a session off hides its VWAP, bands, fill, naked levels, table row and alerts.", display = display.none)
nSs    = input.session("0830-1500", "Session", group = GN,
     tooltip = "NEW YORK session hours in the time zone set in Display (default 08:30–15:00 Chicago, the cash session). The VWAP starts on the first candle inside these hours, runs until the last one, then locks. Hours can cross midnight (e.g. 1700-0200). Pick times that line up with your chart timeframe — a candle that straddles a custom boundary can't be split. A session already open on the first loaded candle is skipped until its next open (no partial VWAP).", display = display.none)
nCl    = input.color(#B388FF, "VWAP", group = GN, inline = "n1",
     tooltip = "NEW YORK VWAP line: colour, width (1–6) and style (solid / dashed / dotted). While the session is open the line is drawn in this colour; once it closes and locks, the same colour is faded by 'Locked line fade %'.", display = display.none)
nW     = input.int(2, "width", minval = 1, maxval = 6, group = GN, inline = "n1",
     tooltip = "NEW YORK VWAP line: colour, width (1–6) and style (solid / dashed / dotted). While the session is open the line is drawn in this colour; once it closes and locks, the same colour is faded by 'Locked line fade %'.", display = display.none)
nSt    = input.string("Solid", "style", options = ["Solid", "Dashed", "Dotted"], group = GN, inline = "n1",
     tooltip = "NEW YORK VWAP line: colour, width (1–6) and style (solid / dashed / dotted). While the session is open the line is drawn in this colour; once it closes and locks, the same colour is faded by 'Locked line fade %'.", display = display.none)
nB1    = input.bool(true, "±1", group = GN, inline = "n2",
     tooltip = "NEW YORK standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
nM1    = input.float(1.0, "", minval = 0.1, step = 0.1, group = GN, inline = "n2",
     tooltip = "NEW YORK standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
nB2    = input.bool(false, "±2", group = GN, inline = "n2",
     tooltip = "NEW YORK standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
nM2    = input.float(2.0, "", minval = 0.1, step = 0.1, group = GN, inline = "n2",
     tooltip = "NEW YORK standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
nB3    = input.bool(false, "±3", group = GN, inline = "n2",
     tooltip = "NEW YORK standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
nM3    = input.float(3.0, "", minval = 0.1, step = 0.1, group = GN, inline = "n2",
     tooltip = "NEW YORK standard-deviation bands: tick ±1, ±2, ±3 to show each band, and type its multiplier (how many standard deviations from the VWAP). Bands are volume-weighted (the same weights as the VWAP), widen as the session develops, and lock with the VWAP when the session closes. Typical use: ±1 = normal rotation, ±2 = stretched, ±3 = extreme.", display = display.none)
nBc    = input.color(#B388FF, "Bands", group = GN, inline = "n3",
     tooltip = "NEW YORK band lines: colour, width (1–4) and style. FILL = the gradient between the NEW YORK VWAP and its widest switched-on band (two-step shade: light from the VWAP out, stronger between the next band in and the outer band; strength set in Display). This switch is used only when 'Auto-shade live session only' (Display) is OFF. Shading shows only while the session is live; once it locks, the lines and bands stay but the shade goes.", display = display.none)
nBw    = input.int(1, "width", minval = 1, maxval = 4, group = GN, inline = "n3",
     tooltip = "NEW YORK band lines: colour, width (1–4) and style. FILL = the gradient between the NEW YORK VWAP and its widest switched-on band (two-step shade: light from the VWAP out, stronger between the next band in and the outer band; strength set in Display). This switch is used only when 'Auto-shade live session only' (Display) is OFF. Shading shows only while the session is live; once it locks, the lines and bands stay but the shade goes.", display = display.none)
nBs    = input.string("Dotted", "style", options = ["Solid", "Dashed", "Dotted"], group = GN, inline = "n3",
     tooltip = "NEW YORK band lines: colour, width (1–4) and style. FILL = the gradient between the NEW YORK VWAP and its widest switched-on band (two-step shade: light from the VWAP out, stronger between the next band in and the outer band; strength set in Display). This switch is used only when 'Auto-shade live session only' (Display) is OFF. Shading shows only while the session is live; once it locks, the lines and bands stay but the shade goes.", display = display.none)
nFl    = input.bool(true, "fill", group = GN, inline = "n3",
     tooltip = "NEW YORK band lines: colour, width (1–4) and style. FILL = the gradient between the NEW YORK VWAP and its widest switched-on band (two-step shade: light from the VWAP out, stronger between the next band in and the outer band; strength set in Display). This switch is used only when 'Auto-shade live session only' (Display) is OFF. Shading shows only while the session is live; once it locks, the lines and bands stay but the shade goes.", display = display.none)
 
string GX = "4 · Naked VWAPs, table & alerts"
nkOn   = input.bool(true, "Naked prior-session VWAPs", group = GX, inline = "x1",
     tooltip = "NAKED PRIOR-SESSION VWAPs: when a session snaps to a new open, its previous final VWAP is kept as a thin dotted line — only if price never traded back to it while it was locked — until a later candle trades through it (wick touch) or it expires. Naked levels often act as magnets or reaction points. KEEP = how many per session (newest kept first). EXPIRE AFTER OPENS = drop a level after this many new opens of that session.", display = display.none)
nkMax  = input.int(2, "keep", minval = 1, maxval = 5, group = GX, inline = "x1",
     tooltip = "NAKED PRIOR-SESSION VWAPs: when a session snaps to a new open, its previous final VWAP is kept as a thin dotted line — only if price never traded back to it while it was locked — until a later candle trades through it (wick touch) or it expires. Naked levels often act as magnets or reaction points. KEEP = how many per session (newest kept first). EXPIRE AFTER OPENS = drop a level after this many new opens of that session.", display = display.none)
nkExp  = input.int(5, "expire after opens", minval = 1, maxval = 20, group = GX, inline = "x1",
     tooltip = "NAKED PRIOR-SESSION VWAPs: when a session snaps to a new open, its previous final VWAP is kept as a thin dotted line — only if price never traded back to it while it was locked — until a later candle trades through it (wick touch) or it expires. Naked levels often act as magnets or reaction points. KEEP = how many per session (newest kept first). EXPIRE AFTER OPENS = drop a level after this many new opens of that session.", display = display.none)
tbOn   = input.bool(true, "Session table", group = GX, inline = "x2",
     tooltip = "SESSION TABLE: one row per session — name with LIVE or 🔒 (locked), the VWAP, and the distance from the last price in points and ticks — the symbol's minimum price step (▲ price above the VWAP, ▼ below). POSITION: corner of the chart.", display = display.none)
tbPos  = input.string("Top right", "position", options = ["Top left", "Top right", "Bottom left", "Bottom right", "Middle right"], group = GX, inline = "x2",
     tooltip = "SESSION TABLE: one row per session — name with LIVE or 🔒 (locked), the VWAP, and the distance from the last price in points and ticks — the symbol's minimum price step (▲ price above the VWAP, ▼ below). POSITION: corner of the chart.", display = display.none)
tbSz   = input.string("Small", "text", options = ["Tiny", "Small", "Normal", "Large"], group = GX, inline = "x3",
     tooltip = "Table text size, background colour and text colour. Session names always use each session's own VWAP colour.", display = display.none)
tbBg   = input.color(#0F1626, "background", group = GX, inline = "x3",
     tooltip = "Table text size, background colour and text colour. Session names always use each session's own VWAP colour.", display = display.none)
tbTx   = input.color(#C9D1E0, "text", group = GX, inline = "x3",
     tooltip = "Table text size, background colour and text colour. Session names always use each session's own VWAP colour.", display = display.none)
alOn   = input.bool(true, "alert() messages", group = GX, inline = "x4",
     tooltip = "ALERTS on a LOCKED session VWAP (a closed session's flat line), closed candles only — live, still-moving VWAPs never alert. CROSS = the close crosses the level. REJECT = a candle touches the level and closes back on the side it came from (held as support / resistance). Each session has its own alert condition ('Asia VWAP', 'London VWAP', 'New York VWAP'); 'alert() messages' adds one combined message with the session, event and price for 'Any alert() function call'. INCLUDE REJECTIONS: off = crosses only.", display = display.none)
alRj   = input.bool(true, "include rejections", group = GX, inline = "x4",
     tooltip = "ALERTS on a LOCKED session VWAP (a closed session's flat line), closed candles only — live, still-moving VWAPs never alert. CROSS = the close crosses the level. REJECT = a candle touches the level and closes back on the side it came from (held as support / resistance). Each session has its own alert condition ('Asia VWAP', 'London VWAP', 'New York VWAP'); 'alert() messages' adds one combined message with the session, event and price for 'Any alert() function call'. INCLUDE REJECTIONS: off = crosses only.", display = display.none)
fAuto  = input.bool(true, "Auto-shade live session only", group = G0,
     tooltip = "ON: the shading follows the clock — whichever session is open right now is shaded automatically (when two sessions overlap, both are shaded), and the per-session FILL switches are ignored. OFF: only sessions with their FILL switch on are shaded. Either way, shading stops the moment a session closes and locks — older locked sessions keep their flat VWAP line and bands with no shading behind them. Needs 'Gradient fill' on.", display = display.none)
 
// --- 1 · Engine ---------------------------------------------------------------------------
bool  vis    = svOn and timeframe.isintraday                 // session VWAPs only make sense intraday
float wNow   = na(volume) ? 1.0 : volume                      // real volume; a zero-volume candle weighs 0 · no volume data at all → equal weights
// one session. States: waiting (no start seen yet) → LIVE (accumulating) → LOCKED (closed, holding its final value)
// → snaps to the next open of the same session. At each snap the finished session's final VWAP becomes a naked level.
// returns [vwap, standard deviation, live now, first candle of a new session, naked levels, their start times]
f_sv(string ss) =>
    bool  inS = not na(time(timeframe.period, ss, tzIn))
    bool  st  = inS and not inS[1] and bar_index > 0      // a session already open on the first bar is skipped (no partial VWAP)
    var float sPV  = na
    var float sV   = na
    var float sP2V = na
    var float sP   = 0.0                                   // equal-weight fallback (symbols that report zero volume)
    var float sP2  = 0.0
    var int   sN   = 0
    var float vw   = na
    var float sd   = na
    var array<float> nk  = array.new_float()               // naked prior-session VWAPs (newest last)
    var array<int>   nkT = array.new_int()                 // when each became naked
    var array<int>   nkN = array.new_int()                 // how many opens it has survived
    var bool tch = false                                   // locked VWAP already traded back to before the next open
    if not inS and not na(vw) and high >= vw and low <= vw
        tch := true
    bool pushed = false
    if st
        if not na(vw) and not tch                          // the finished session's final VWAP → naked level (only if never revisited)
            nk.push(vw)
            pushed := true
            nkT.push(time)
            nkN.push(0)
        if nkN.size() > 0
            for i = 0 to nkN.size() - 1
                nkN.set(i, nkN.get(i) + 1)
        while nk.size() > nkMax or (nkN.size() > 0 and nkN.get(0) > nkExp)
            nk.shift()
            nkT.shift()
            nkN.shift()
        tch  := false
        sP   := 0.0
        sP2  := 0.0
        sN   := 0
        sPV  := 0.0
        sV   := 0.0
        sP2V := 0.0
    if inS and not na(sV) and not na(srcIn)                // LIVE: accumulate
        sPV  += srcIn * wNow
        sV   += wNow
        sP2V += srcIn * srcIn * wNow
        sP   += srcIn
        sP2  += srcIn * srcIn
        sN   += 1
        vw := sV > 0 ? sPV / sV : sP / sN                  // no volume yet this session → plain average until it arrives
        sd := sV > 0 ? math.sqrt(math.max(0.0, sP2V / sV - vw * vw)) : math.sqrt(math.max(0.0, sP2 / sN - vw * vw))
    // a naked level is gone once a later candle trades through it (not on the candle that created it)
    int top = nk.size() - 1 - (pushed ? 1 : 0)
    if top >= 0
        for i = top to 0
            if high >= nk.get(i) and low <= nk.get(i)
                nk.remove(i)
                nkT.remove(i)
                nkN.remove(i)
    [vw, sd, inS, st, nk, nkT]
[aV, aD, aIn, aSt0, aNk, aNkT] = f_sv(aSs)
[lV, lD, lIn, lSt0, lNk, lNkT] = f_sv(lSs)
[nV, nD, nIn, nSt0, nNk, nNkT] = f_sv(nSs)
 
// --- 2 · Drawing ---------------------------------------------------------------------------
// Plot budget, TradingView limit 64: every non-constant plot argument costs a slot. So the line style is the
// constant linebr, where a na value at the snap gives the optional gap. Bands sit in sorted slots and the
// outer slot is the widest switched-on band, so fills need no hidden helper plots. Fills use fixed colours.
f_val(bool on, float v, bool st) => vis and on and not (snapGap and st) ? v : na
f_col(color c, bool inS) => inS ? c : color.new(c, math.min(100, color.t(c) + lockT))
f_bd(bool on, float v, float d, float m, bool st) => vis and on and not na(m) and not na(d) and not (snapGap and st) ? v + m * d : na
// switched-on multipliers sorted inner → outer; empty slots are na and sit on the inside
f_srt(bool b1, float m1, bool b2, float m2, bool b3, float m3) =>
    float x1 = b1 ? m1 : -1.0
    float x2 = b2 ? m2 : -1.0
    float x3 = b3 ? m3 : -1.0
    float hi = math.max(x1, x2, x3)
    float lo = math.min(x1, x2, x3)
    float md = x1 + x2 + x3 - hi - lo
    [lo < 0 ? na : lo, md < 0 ? na : md, hi < 0 ? na : hi]
// two-layer stepped gradient: a faint layer VWAP → outer band, a second layer from the next band out → outer band
int   fK  = 95 - math.round(fillS * 0.55)                     // strongest look: 95 = faint → 40 = strong
int   fT1 = math.round(100 - (100 - fK) * 0.45)
int   fT2 = math.round(100 - (100 - fK) * 0.65)
// shade only while the session is live — never the snap candle, so no slanted shade from the old locked level
f_fl(color c, bool on, int t, bool inS, bool st) => fillOn and (fAuto or on) and inS and not st ? color.new(c, t) : na
// ASIA
[aS1, aS2, aS3] = f_srt(aB1, aM1, aB2, aM2, aB3, aM3)
pA = plot(f_val(aOn, aV, aSt0), "Asia VWAP", f_col(aCl, aIn), aW, display = display.all - display.status_line, style = plot.style_linebr, linestyle = aSt == "Dashed" ? plot.linestyle_dashed : aSt == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid)
pAu1 = plot(f_bd(aOn, aV, aD, aS1, aSt0), "Asia +band inner", aBc, aBw, style = plot.style_linebr, linestyle = aBs == "Dashed" ? plot.linestyle_dashed : aBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pAd1 = plot(f_bd(aOn, aV, aD, -aS1, aSt0), "Asia −band inner", aBc, aBw, style = plot.style_linebr, linestyle = aBs == "Dashed" ? plot.linestyle_dashed : aBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pAu2 = plot(f_bd(aOn, aV, aD, aS2, aSt0), "Asia +band middle", aBc, aBw, style = plot.style_linebr, linestyle = aBs == "Dashed" ? plot.linestyle_dashed : aBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pAd2 = plot(f_bd(aOn, aV, aD, -aS2, aSt0), "Asia −band middle", aBc, aBw, style = plot.style_linebr, linestyle = aBs == "Dashed" ? plot.linestyle_dashed : aBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pAu3 = plot(f_bd(aOn, aV, aD, aS3, aSt0), "Asia +band outer", aBc, aBw, style = plot.style_linebr, linestyle = aBs == "Dashed" ? plot.linestyle_dashed : aBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pAd3 = plot(f_bd(aOn, aV, aD, -aS3, aSt0), "Asia −band outer", aBc, aBw, style = plot.style_linebr, linestyle = aBs == "Dashed" ? plot.linestyle_dashed : aBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
fill(pA, pAu3, f_fl(aBc, aFl, fT1, aIn, aSt0), "Asia fill +")
fill(pA, pAd3, f_fl(aBc, aFl, fT1, aIn, aSt0), "Asia fill −")
fill(pAu2, pAu3, f_fl(aBc, aFl, fT2, aIn, aSt0), "Asia fill + outer")
fill(pAd2, pAd3, f_fl(aBc, aFl, fT2, aIn, aSt0), "Asia fill − outer")
// LONDON
[lS1, lS2, lS3] = f_srt(lB1, lM1, lB2, lM2, lB3, lM3)
pL = plot(f_val(lOn, lV, lSt0), "London VWAP", f_col(lCl, lIn), lW, display = display.all - display.status_line, style = plot.style_linebr, linestyle = lSt == "Dashed" ? plot.linestyle_dashed : lSt == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid)
pLu1 = plot(f_bd(lOn, lV, lD, lS1, lSt0), "London +band inner", lBc, lBw, style = plot.style_linebr, linestyle = lBs == "Dashed" ? plot.linestyle_dashed : lBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pLd1 = plot(f_bd(lOn, lV, lD, -lS1, lSt0), "London −band inner", lBc, lBw, style = plot.style_linebr, linestyle = lBs == "Dashed" ? plot.linestyle_dashed : lBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pLu2 = plot(f_bd(lOn, lV, lD, lS2, lSt0), "London +band middle", lBc, lBw, style = plot.style_linebr, linestyle = lBs == "Dashed" ? plot.linestyle_dashed : lBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pLd2 = plot(f_bd(lOn, lV, lD, -lS2, lSt0), "London −band middle", lBc, lBw, style = plot.style_linebr, linestyle = lBs == "Dashed" ? plot.linestyle_dashed : lBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pLu3 = plot(f_bd(lOn, lV, lD, lS3, lSt0), "London +band outer", lBc, lBw, style = plot.style_linebr, linestyle = lBs == "Dashed" ? plot.linestyle_dashed : lBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pLd3 = plot(f_bd(lOn, lV, lD, -lS3, lSt0), "London −band outer", lBc, lBw, style = plot.style_linebr, linestyle = lBs == "Dashed" ? plot.linestyle_dashed : lBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
fill(pL, pLu3, f_fl(lBc, lFl, fT1, lIn, lSt0), "London fill +")
fill(pL, pLd3, f_fl(lBc, lFl, fT1, lIn, lSt0), "London fill −")
fill(pLu2, pLu3, f_fl(lBc, lFl, fT2, lIn, lSt0), "London fill + outer")
fill(pLd2, pLd3, f_fl(lBc, lFl, fT2, lIn, lSt0), "London fill − outer")
// NEW YORK
[nS1, nS2, nS3] = f_srt(nB1, nM1, nB2, nM2, nB3, nM3)
pN = plot(f_val(nOn, nV, nSt0), "New York VWAP", f_col(nCl, nIn), nW, display = display.all - display.status_line, style = plot.style_linebr, linestyle = nSt == "Dashed" ? plot.linestyle_dashed : nSt == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid)
pNu1 = plot(f_bd(nOn, nV, nD, nS1, nSt0), "New York +band inner", nBc, nBw, style = plot.style_linebr, linestyle = nBs == "Dashed" ? plot.linestyle_dashed : nBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pNd1 = plot(f_bd(nOn, nV, nD, -nS1, nSt0), "New York −band inner", nBc, nBw, style = plot.style_linebr, linestyle = nBs == "Dashed" ? plot.linestyle_dashed : nBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pNu2 = plot(f_bd(nOn, nV, nD, nS2, nSt0), "New York +band middle", nBc, nBw, style = plot.style_linebr, linestyle = nBs == "Dashed" ? plot.linestyle_dashed : nBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pNd2 = plot(f_bd(nOn, nV, nD, -nS2, nSt0), "New York −band middle", nBc, nBw, style = plot.style_linebr, linestyle = nBs == "Dashed" ? plot.linestyle_dashed : nBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pNu3 = plot(f_bd(nOn, nV, nD, nS3, nSt0), "New York +band outer", nBc, nBw, style = plot.style_linebr, linestyle = nBs == "Dashed" ? plot.linestyle_dashed : nBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
pNd3 = plot(f_bd(nOn, nV, nD, -nS3, nSt0), "New York −band outer", nBc, nBw, style = plot.style_linebr, linestyle = nBs == "Dashed" ? plot.linestyle_dashed : nBs == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid, display = display.pane)
fill(pN, pNu3, f_fl(nBc, nFl, fT1, nIn, nSt0), "New York fill +")
fill(pN, pNd3, f_fl(nBc, nFl, fT1, nIn, nSt0), "New York fill −")
fill(pNu2, pNu3, f_fl(nBc, nFl, fT2, nIn, nSt0), "New York fill + outer")
fill(pNd2, pNd3, f_fl(nBc, nFl, fT2, nIn, nSt0), "New York fill − outer")
 
// --- 3 · Right-edge tags (last bar only) ----------------------------------------------------
var array<label> tg = array.new<label>()
f_tag(bool on, float v, string nm, color c, bool inS) =>
    if vis and tagOn and on and not na(v)
        tg.push(label.new(bar_index + 2, v, nm + (inS ? " " : " 🔒 ") + str.tostring(v, format.mintick), style = label.style_label_left, color = color.new(#000000, 100), textcolor = c,
             size = tagSz == "Tiny" ? size.tiny : tagSz == "Normal" ? size.normal : tagSz == "Large" ? size.large : size.small,
             tooltip = nm + " VWAP " + (inS ? "— session open, still moving" : "— session closed, locked until the next " + nm + " open")))
if barstate.islast
    while tg.size() > 0
        label.delete(tg.pop())
    f_tag(aOn, aV, aNm, aCl, aIn)
    f_tag(lOn, lV, lNm, lCl, lIn)
    f_tag(nOn, nV, nNm, nCl, nIn)
 
// --- 4 · Naked prior-session VWAPs (last bar only) ------------------------------------------
var array<line> nkLs = array.new<line>()
f_nkDraw(array<float> p, array<int> t, color c, string nm) =>
    if p.size() > 0
        for i = 0 to p.size() - 1
            nkLs.push(line.new(t.get(i), p.get(i), time + 2 * timeframe.in_seconds() * 1000, p.get(i), xloc = xloc.bar_time, color = color.new(c, 30), style = line.style_dotted, width = 1))
            tg.push(label.new(bar_index + 2, p.get(i), "p" + nm + " " + str.tostring(p.get(i), format.mintick), style = label.style_label_left, color = color.new(#000000, 100), textcolor = color.new(c, 30), size = size.tiny,
                 tooltip = "Naked prior " + nm + " VWAP — the final VWAP of an earlier " + nm + " session that price has not traded back to yet. Often acts as a magnet / reaction level."))
if barstate.islast
    while nkLs.size() > 0
        line.delete(nkLs.pop())
    if vis and nkOn
        if aOn
            f_nkDraw(aNk, aNkT, aCl, aNm)
        if lOn
            f_nkDraw(lNk, lNkT, lCl, lNm)
        if nOn
            f_nkDraw(nNk, nNkT, nCl, nNm)
 
// --- 5 · Session table (last bar only) -------------------------------------------------------
var table sT = table.new(position.top_right, 4, 4)
f_tsz() => tbSz == "Tiny" ? size.tiny : tbSz == "Normal" ? size.normal : tbSz == "Large" ? size.large : size.small
f_trow(int r, bool on, string nm, float v, bool inS, color c) =>
    if on
        float d = na(v) ? na : close - v
        table.cell(sT, 0, r, nm + (inS ? " LIVE" : " 🔒"), text_color = c, bgcolor = tbBg, text_size = f_tsz(), text_halign = text.align_left,
             tooltip = nm + (inS ? ": session open — VWAP still moving" : ": session closed — VWAP locked until the next " + nm + " open"))
        table.cell(sT, 1, r, na(v) ? "–" : str.tostring(v, format.mintick), text_color = tbTx, bgcolor = tbBg, text_size = f_tsz())
        table.cell(sT, 2, r, na(d) ? "–" : (d >= 0 ? "▲ +" : "▼ −") + str.tostring(math.abs(d), format.mintick) + " (" + str.tostring(math.round(math.abs(d) / syminfo.mintick)) + "t)", text_color = na(d) ? tbTx : d >= 0 ? #00C27A : #FF3B5C, bgcolor = tbBg, text_size = f_tsz(),
             tooltip = "Last price minus the VWAP, in points and ticks. ▲ = price above the VWAP, ▼ = below.")
if barstate.islast
    table.clear(sT, 0, 0, 3, 3)
    if vis and tbOn
        table.set_position(sT, tbPos == "Top left" ? position.top_left : tbPos == "Bottom left" ? position.bottom_left : tbPos == "Bottom right" ? position.bottom_right : tbPos == "Middle right" ? position.middle_right : position.top_right)
        f_trow(0, aOn, aNm, aV, aIn, aCl)
        f_trow(1, lOn, lNm, lV, lIn, lCl)
        f_trow(2, nOn, nNm, nV, nIn, nCl)
 
// --- 6 · Alerts — a LOCKED session VWAP crossed or rejected (closed candles only) ------------
// 1 cross up · −1 cross down · 2 rejected from above (held as support) · −2 rejected from below (held as resistance)
f_ev(bool on, float v, bool inS) =>
    int e = 0
    if vis and on and barstate.isconfirmed and not inS and not na(v) and not na(v[1]) and v == v[1]
        if close > v and close[1] <= v
            e := 1
        else if close < v and close[1] >= v
            e := -1
        else if alRj and low <= v and high >= v
            e := close[1] > v and close > v ? 2 : close[1] < v and close < v ? -2 : 0
    e
int aE = f_ev(aOn, aV, aIn)
int lE = f_ev(lOn, lV, lIn)
int nE = f_ev(nOn, nV, nIn)
alertcondition(aE != 0, "Asia VWAP", "STRYK:SV Asia VWAP")
alertcondition(lE != 0, "London VWAP", "STRYK:SV London VWAP")
alertcondition(nE != 0, "New York VWAP", "STRYK:SV New York VWAP")
f_evTx(int e) => e == 1 ? "CROSS UP" : e == -1 ? "CROSS DOWN" : e == 2 ? "REJECTED (held as support)" : "REJECTED (held as resistance)"
if alOn and (aE != 0 or lE != 0 or nE != 0)
    string m = "STRYK:SV " + syminfo.ticker
    m += aE != 0 ? " · " + aNm + " VWAP " + f_evTx(aE) + " " + str.tostring(aV, format.mintick) : ""
    m += lE != 0 ? " · " + lNm + " VWAP " + f_evTx(lE) + " " + str.tostring(lV, format.mintick) : ""
    m += nE != 0 ? " · " + nNm + " VWAP " + f_evTx(nE) + " " + str.tostring(nV, format.mintick) : ""
    alert(m + " @ " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)

← 返回列表