VWAP Multisession Ultimate

Roro_trades · study · 236 行 · 点赞 9 · TradingView 原页

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

Pine Script

//@version=6
indicator("Multi-Session-VWAP 1.5 – Extension-Bänder", overlay = true, max_polylines_count = 15)
 
// 1. Einstellungen: feste IANA-Zeitzonen, einstellbare lokale Uhrzeiten.
const string BERLIN = "Europe/Berlin"
const string NEW_YORK = "America/New_York"
const string A = "Asien · Europe/Berlin"
const string E = "London/Europa · Europe/Berlin"
const string N = "New York · America/New_York"
const string W = "Woche · Europe/Berlin"
aSH = input.int(2, "Start Stunde", 0, 23, group = A, inline = "start")
aSM = input.int(30, "Minute", 0, 59, group = A, inline = "start")
eSH = input.int(9, "Start Stunde", 0, 23, group = E, inline = "start")
eSM = input.int(0, "Minute", 0, 59, group = E, inline = "start")
nSH = input.int(9, "Start Stunde", 0, 23, group = N, inline = "start")
nSM = input.int(30, "Minute", 0, 59, group = N, inline = "start")
aShow = input.bool(true, "Aktiv anzeigen", group = A)
aColor = input.color(color.rgb(0, 188, 188), "Aktiv Farbe", group = A)
aWidth = input.int(2, "Aktiv Stärke", 1, 5, group = A)
aRef = input.bool(true, "Vortag anzeigen", group = A)
aRefColor = input.color(color.rgb(144, 238, 144), "Vortag Farbe", group = A)
aRefWidth = input.int(2, "Vortag Stärke", 1, 5, group = A)
eShow = input.bool(true, "Aktiv anzeigen", group = E)
eColor = input.color(color.yellow, "Aktiv Farbe", group = E)
eWidth = input.int(2, "Aktiv Stärke", 1, 5, group = E)
eRef = input.bool(true, "Vortag anzeigen", group = E)
eRefColor = input.color(color.gray, "Vortag Farbe", group = E)
eRefWidth = input.int(2, "Vortag Stärke", 1, 5, group = E)
nShow = input.bool(true, "Aktiv anzeigen", group = N)
nColor = input.color(color.rgb(156, 39, 176), "Aktiv Farbe", group = N)
nWidth = input.int(2, "Aktiv Stärke", 1, 5, group = N)
nRef = input.bool(true, "Vortag anzeigen", group = N)
nRefColor = input.color(color.rgb(139, 90, 43), "Vortag Farbe", group = N)
nRefWidth = input.int(2, "Vortag Stärke", 1, 5, group = N)
wShow = input.bool(true, "Anzeigen", group = W)
wColor = input.color(color.rgb(255, 64, 160), "Farbe", group = W)
wWidth = input.int(2, "Stärke", 1, 5, group = W)
 
// Tages-VWAP: die schwarze Vortageskurve ist standardmäßig sichtbar.
const string D = "Tages-VWAP ab 00:00 · Europe/Berlin"
dShow = input.bool(true, "Aktuellen Tages-VWAP anzeigen", group = D)
dColor = input.color(color.blue, "Aktuell Farbe", group = D)
dWidth = input.int(2, "Aktuell Stärke", 1, 5, group = D)
dRef = input.bool(true, "Vortag anzeigen", group = D)
dRefColor = input.color(color.black, "Vortag Farbe", group = D)
dRefWidth = input.int(2, "Vortag Stärke", 1, 5, group = D)
 
// 2. Nur Standard-Zeitcharts ab einer Minute: maximal 7200 Punkte/Woche.
if barstate.isfirst and (not chart.is_standard or not timeframe.isintraday or timeframe.in_seconds() < 60)
    runtime.error("Bitte Standard-Intraday-Kerzen ab 1 Minute verwenden.")
 
f_midnight(int t, int offset) =>
    timestamp(BERLIN, year(t, BERLIN), month(t, BERLIN), dayofmonth(t, BERLIN) + offset, 0, 0)
 
// Nur die aktuell vorhandenen Kurven werden gezeichnet. Alte Zeichnungen
// werden gelöscht, statt historische plot-Serien dauerhaft zurückzulassen.
f_draw(array<chart.point> points, bool enabled, color col, int width) =>
    var polyline drawing = na
    if barstate.islast
        if not na(drawing)
            polyline.delete(drawing)
            drawing := na
        if enabled and array.size(points) >= 2
            drawing := polyline.new(points, curved = false, closed = false, xloc = xloc.bar_time, line_color = col, line_style = line.style_solid, line_width = width)
    drawing
 
// 3. Pro Startzeit zwei unabhängige laufende Akkumulatoren.
// Um Mitternacht wird HEUTE zu VORTAG, samt bisherigem Kurvenverlauf.
// Der Vortag akkumuliert danach weiter, ohne Reset um Mitternacht.
// var + Rollback: pro Echtzeitupdate nur das aktuelle Balkenvolumen addieren.
f_pair(string tz, int sh, int sm, bool showToday, color todayColor, int todayWidth, bool showPrevious, color previousColor, int previousWidth) =>
    var int storedDay = na
    var float todayPV = 0.0
    var float todayV = 0.0
    var float previousPV = 0.0
    var float previousV = 0.0
    var bool previousExists = false
    var array<chart.point> todayPoints = array.new<chart.point>()
    var array<chart.point> previousPoints = array.new<chart.point>()
    int day = f_midnight(time, 0)
    if na(storedDay) or day != storedDay
        // Ausnahme: Freitag wird am Montag zum Vortag, wenn keine Wochenendbars vorliegen.
        bool adjacent = not na(storedDay) and day == f_midnight(storedDay, 1)
        bool fridayToMonday = not na(storedDay) and dayofweek(storedDay, BERLIN) == dayofweek.friday and dayofweek(day, BERLIN) == dayofweek.monday and day == f_midnight(storedDay, 3)
        previousExists := (adjacent or fridayToMonday) and array.size(todayPoints) > 0
        previousPV := previousExists ? todayPV : 0.0
        previousV := previousExists ? todayV : 0.0
        previousPoints := previousExists ? todayPoints : array.new<chart.point>()
        todayPoints := array.new<chart.point>()
        todayPV := 0.0
        todayV := 0.0
        storedDay := day
    // Berlin-Datum anhand Mittag in die Startzeitzone übersetzen.
    int noon = timestamp(BERLIN, year(time, BERLIN), month(time, BERLIN), dayofmonth(time, BERLIN), 12, 0)
    int startAt = timestamp(tz, year(noon, tz), month(noon, tz), dayofmonth(noon, tz), sh, sm)
    float pv = hlc3 * volume
    bool valid = not na(high) and not na(low) and not na(close) and not na(hlc3) and not na(volume) and volume > 0 and not na(pv)
    if valid
        if time >= startAt
            todayPV += pv
            todayV += volume
        if previousExists
            previousPV += pv
            previousV += volume
    float todayValue = time >= startAt and todayV > 0 and not na(todayPV) ? todayPV / todayV : na
    float previousValue = previousExists and previousV > 0 and not na(previousPV) ? previousPV / previousV : na
    if not na(todayValue)
        array.push(todayPoints, chart.point.from_time(time, todayValue))
    if not na(previousValue)
        array.push(previousPoints, chart.point.from_time(time, previousValue))
    f_draw(todayPoints, showToday, todayColor, todayWidth)
    f_draw(previousPoints, showPrevious, previousColor, previousWidth)
    [todayValue, previousValue]
 
[aToday, aPrevious] = f_pair(BERLIN, aSH, aSM, aShow, aColor, aWidth, aRef, aRefColor, aRefWidth)
[eToday, ePrevious] = f_pair(BERLIN, eSH, eSM, eShow, eColor, eWidth, eRef, eRefColor, eRefWidth)
[nToday, nPrevious] = f_pair(NEW_YORK, nSH, nSM, nShow, nColor, nWidth, nRef, nRefColor, nRefWidth)
[dToday, dPrevious] = f_pair(BERLIN, 0, 0, dShow, dColor, dWidth, dRef, dRefColor, dRefWidth)
 
// 4. Genau eine Wochenkurve. Montag 00:00 bis Samstag 00:00 Berlin.
// Am Wochenwechsel werden Punkte UND Summen der Vorwoche verworfen.
int daysSinceMonday = (dayofweek(time, BERLIN) + 5) % 7
int weekID = timestamp(BERLIN, year(time, BERLIN), month(time, BERLIN), dayofmonth(time, BERLIN) - daysSinceMonday, 0, 0)
var int currentWeek = na
var float weekPV = 0.0
var float weekV = 0.0
var array<chart.point> weekPoints = array.new<chart.point>()
if na(currentWeek) or currentWeek != weekID
    currentWeek := weekID
    weekPV := 0.0
    weekV := 0.0
    weekPoints := array.new<chart.point>()
int weekday = dayofweek(time, BERLIN)
bool workday = weekday >= dayofweek.monday and weekday <= dayofweek.friday
float barPV = hlc3 * volume
bool validBar = not na(high) and not na(low) and not na(close) and not na(hlc3) and not na(volume) and volume > 0 and not na(barPV)
if workday and validBar
    weekPV += barPV
    weekV += volume
float weekValue = workday and weekV > 0 and not na(weekPV) ? weekPV / weekV : na
if not na(weekValue)
    array.push(weekPoints, chart.point.from_time(time, weekValue))
f_draw(weekPoints, wShow, wColor, wWidth)
 
 
 
// Aktuelle Werte: Preisachse, Statuszeile und Datenfenster.
// show_last begrenzt auf letzten Balken; keine alten Chartkurven erzeugen.
plot(aShow ? aToday : na, "Asien heute", color = aColor, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)
plot(aRef ? aPrevious : na, "Asien Vortag", color = aRefColor, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)
plot(eShow ? eToday : na, "London heute", color = eColor, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)
plot(eRef ? ePrevious : na, "London Vortag", color = eRefColor, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)
plot(nShow ? nToday : na, "New York heute", color = nColor, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)
plot(nRef ? nPrevious : na, "New York Vortag", color = nRefColor, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)
plot(dShow ? dToday : na, "Tages-VWAP heute", color = dColor, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)
plot(dRef ? dPrevious : na, "Tages-VWAP Vortag", color = dRefColor, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)
plot(wShow and weekV > 0 ? weekPV / weekV : na, "Wochen-VWAP", color = wColor, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)
 
// 5. Extension-Bänder der HEUTIGEN VWAPs.
const string B = "Extension-Bänder heute"
b1 = input.float(1.0, "Faktor Band 1", minval = 0.0, group = B)
b2 = input.float(2.0, "Faktor Band 2", minval = 0.0, group = B)
b3 = input.float(3.0, "Faktor Band 3", minval = 0.0, group = B)
bShow1 = input.bool(true, "Band 1 anzeigen", group = B)
bShow2 = input.bool(true, "Band 2 anzeigen", group = B)
bShow3 = input.bool(true, "Band 3 anzeigen", group = B)
bColor1 = input.color(color.orange, "Band 1 Farbe", group = B)
bColor2 = input.color(color.orange, "Band 2 Farbe", group = B)
bColor3 = input.color(color.red, "Band 3 Farbe", group = B)
bWidth = input.int(1, "Band Stärke", minval = 1, maxval = 5, group = B)
 
// Gewichteter Welford-Algorithmus für stabile Populationsvarianz von HLC3.
// Weder Vortagesvolumen noch Vortagesvarianz werden übernommen.
f_bands(string tz, int sh, int sm, bool enabled) =>
    var int storedDay = na
    var float weight = 0.0
    var float mean = 0.0
    var float m2 = 0.0
    var array<chart.point> u1p = array.new<chart.point>()
    var array<chart.point> l1p = array.new<chart.point>()
    var array<chart.point> u2p = array.new<chart.point>()
    var array<chart.point> l2p = array.new<chart.point>()
    var array<chart.point> u3p = array.new<chart.point>()
    var array<chart.point> l3p = array.new<chart.point>()
    int day = f_midnight(time, 0)
    if na(storedDay) or storedDay != day
        storedDay := day
        weight := 0.0
        mean := 0.0
        m2 := 0.0
        u1p := array.new<chart.point>()
        l1p := array.new<chart.point>()
        u2p := array.new<chart.point>()
        l2p := array.new<chart.point>()
        u3p := array.new<chart.point>()
        l3p := array.new<chart.point>()
    int noon = timestamp(BERLIN, year(time, BERLIN), month(time, BERLIN), dayofmonth(time, BERLIN), 12, 0)
    int startAt = timestamp(tz, year(noon, tz), month(noon, tz), dayofmonth(noon, tz), sh, sm)
    bool valid = time >= startAt and not na(high) and not na(low) and not na(close) and not na(hlc3) and not na(volume) and volume > 0 and not na(hlc3 * volume)
    if enabled and valid
        float newWeight = weight + volume
        float delta = hlc3 - mean
        float newMean = mean + (volume / newWeight) * delta
        m2 += volume * delta * (hlc3 - newMean)
        mean := newMean
        weight := newWeight
    float distance = weight > 0 ? math.sqrt(math.max(m2 / weight, 0.0)) : na
    float u1 = enabled and weight > 0 ? mean + b1 * distance : na
    float l1 = enabled and weight > 0 ? mean - b1 * distance : na
    float u2 = enabled and weight > 0 ? mean + b2 * distance : na
    float l2 = enabled and weight > 0 ? mean - b2 * distance : na
    float u3 = enabled and weight > 0 ? mean + b3 * distance : na
    float l3 = enabled and weight > 0 ? mean - b3 * distance : na
    if not na(u1)
        array.push(u1p, chart.point.from_time(time, u1))
        array.push(l1p, chart.point.from_time(time, l1))
        array.push(u2p, chart.point.from_time(time, u2))
        array.push(l2p, chart.point.from_time(time, l2))
        array.push(u3p, chart.point.from_time(time, u3))
        array.push(l3p, chart.point.from_time(time, l3))
    f_draw(u1p, enabled and bShow1, bColor1, bWidth)
    f_draw(l1p, enabled and bShow1, bColor1, bWidth)
    f_draw(u2p, enabled and bShow2, bColor2, bWidth)
    f_draw(l2p, enabled and bShow2, bColor2, bWidth)
    f_draw(u3p, enabled and bShow3, bColor3, bWidth)
    f_draw(l3p, enabled and bShow3, bColor3, bWidth)
    [u1, l1, u2, l2, u3, l3]
 
[dU1, dL1, dU2, dL2, dU3, dL3] = f_bands(BERLIN, 0, 0, dShow)
 
plot(bShow1 ? dU1 : na, "Tag Band 1 oben", color = bColor1, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)
plot(bShow1 ? dL1 : na, "Tag Band 1 unten", color = bColor1, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)
plot(bShow2 ? dU2 : na, "Tag Band 2 oben", color = bColor2, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)
plot(bShow2 ? dL2 : na, "Tag Band 2 unten", color = bColor2, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)
plot(bShow3 ? dU3 : na, "Tag Band 3 oben", color = bColor3, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)
plot(bShow3 ? dL3 : na, "Tag Band 3 unten", color = bColor3, show_last = 1, display = display.price_scale + display.status_line + display.data_window, editable = false)

← 返回列表