AVWAP Proxy v2

gonFlist · study · 216 行 · 点赞 11 · TradingView 原页

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

Pine Script

// © gonFlist
 
//@version=6
indicator("AVWAP Proxy v2", shorttitle="AVWAP v2", overlay=true)
 
// All calculations use the New York regular session.
const string NY_TZ = "America/New_York"
const string RTH_SESSION = "0930-1600:23456"
 
string anchor = input.string(
     "Session",
     "Anchor period",
     options=["Session", "Week", "Month", "Year"],
     group="Calculation")
 
bool showChartVwap = input.bool(true, "Show chart symbol VWAP", group="Chart symbol")
string chartResolutionInput = input.string(
     "1S",
     "Calculation resolution",
     options=["Chart", "1T", "1S", "1m"],
     group="Chart symbol")
 
bool showProxy1 = input.bool(true, "Show proxy 1", group="Proxy 1")
string proxy1Symbol = input.symbol("CME_MINI:ES1!", "Symbol", group="Proxy 1")
string proxy1Resolution = input.string(
     "1m",
     "Volume resolution",
     options=["1T", "1S", "1m"],
     group="Proxy 1")
 
bool showProxy2 = input.bool(true, "Show proxy 2", group="Proxy 2")
string proxy2Symbol = input.symbol("AMEX:SPY", "Symbol", group="Proxy 2")
string proxy2Resolution = input.string(
     "1m",
     "Volume resolution",
     options=["1T", "1S", "1m"],
     group="Proxy 2")
 
// Native chart-symbol VWAP: price * volume from the chart symbol's selected resolution.
f_chart_vwap(array<int> times, array<float> highs, array<float> lows, array<float> closes, array<float> volumes, string anchorType, string resolution) =>
    var float sumPriceVolume = 0.0
    var float sumVolume = 0.0
    var int previousAnchorKey = na
    var int activeSessionDate = na
    var int latestRthDate = na
    var bool sawRthOpen = false
 
    int intrabarCount = array.size(times)
    if intrabarCount > 0
        for i = 0 to intrabarCount - 1
            int barTime = array.get(times, i)
            int h = hour(barTime, NY_TZ)
            int m = minute(barTime, NY_TZ)
            int dow = dayofweek(barTime, NY_TZ)
            bool weekday = dow >= dayofweek.monday and dow <= dayofweek.friday
            bool inRth = weekday and ((h > 9) or (h == 9 and m >= 30)) and h < 16
 
            if inRth
                int y = year(barTime, NY_TZ)
                int mo = month(barTime, NY_TZ)
                int d = dayofmonth(barTime, NY_TZ)
                int dateKey = y * 10000 + mo * 100 + d
                int daysSinceMonday = (dow + 5) % 7
                int weekKey = timestamp(NY_TZ, y, mo, d - daysSinceMonday, 0, 0)
                int monthKey = y * 100 + mo
 
                int currentAnchorKey = switch anchorType
                    "Session" => dateKey
                    "Week" => weekKey
                    "Month" => monthKey
                    => y
 
                if na(previousAnchorKey) or currentAnchorKey != previousAnchorKey
                    sumPriceVolume := 0.0
                    sumVolume := 0.0
                    previousAnchorKey := currentAnchorKey
 
                if na(activeSessionDate) or dateKey != activeSessionDate
                    activeSessionDate := dateKey
                    latestRthDate := dateKey
                    sawRthOpen := false
 
                if h == 9 and m == 30
                    sawRthOpen := true
 
                float barHigh = array.get(highs, i)
                float barLow = array.get(lows, i)
                float barClose = array.get(closes, i)
                float barVolume = nz(array.get(volumes, i), 0.0)
                float priceForVwap = resolution == "1T" ? barClose : (barHigh + barLow + barClose) / 3.0
 
                if barVolume > 0
                    sumPriceVolume += priceForVwap * barVolume
                    sumVolume += barVolume
 
                latestRthDate := dateKey
 
    float result = sumVolume > 0 ? sumPriceVolume / sumVolume : na
    [result, latestRthDate, sawRthOpen]
 
// Proxy-volume VWAP: aggregate the proxy's volume within each chart bar,
// then weight that chart bar's HLC3 by the proxy volume.
f_proxy_weighted_vwap(array<int> times, array<float> volumes, string anchorType, float chartBarPrice) =>
    var float sumPriceVolume = 0.0
    var float sumVolume = 0.0
    var int previousAnchorKey = na
    var int activeSessionDate = na
    var int latestRthDate = na
    var bool sawRthOpen = false
 
    float proxyVolumeThisChartBar = 0.0
    int intrabarCount = array.size(times)
    if intrabarCount > 0
        for i = 0 to intrabarCount - 1
            int barTime = array.get(times, i)
            int h = hour(barTime, NY_TZ)
            int m = minute(barTime, NY_TZ)
            int dow = dayofweek(barTime, NY_TZ)
            bool weekday = dow >= dayofweek.monday and dow <= dayofweek.friday
            bool inRth = weekday and ((h > 9) or (h == 9 and m >= 30)) and h < 16
 
            if inRth
                int y = year(barTime, NY_TZ)
                int mo = month(barTime, NY_TZ)
                int d = dayofmonth(barTime, NY_TZ)
                int dateKey = y * 10000 + mo * 100 + d
                int daysSinceMonday = (dow + 5) % 7
                int weekKey = timestamp(NY_TZ, y, mo, d - daysSinceMonday, 0, 0)
                int monthKey = y * 100 + mo
 
                int currentAnchorKey = switch anchorType
                    "Session" => dateKey
                    "Week" => weekKey
                    "Month" => monthKey
                    => y
 
                if na(previousAnchorKey) or currentAnchorKey != previousAnchorKey
                    sumPriceVolume := 0.0
                    sumVolume := 0.0
                    previousAnchorKey := currentAnchorKey
 
                if na(activeSessionDate) or dateKey != activeSessionDate
                    activeSessionDate := dateKey
                    latestRthDate := dateKey
                    sawRthOpen := false
 
                if h == 9 and m == 30
                    sawRthOpen := true
 
                float proxyVolume = nz(array.get(volumes, i), 0.0)
                proxyVolumeThisChartBar += proxyVolume
                latestRthDate := dateKey
 
    if proxyVolumeThisChartBar > 0
        sumPriceVolume += chartBarPrice * proxyVolumeThisChartBar
        sumVolume += proxyVolumeThisChartBar
 
    float result = sumVolume > 0 ? sumPriceVolume / sumVolume : na
    [result, latestRthDate, sawRthOpen]
 
// The chart symbol can use its own chart timeframe, 1T, or 1S.
string chartRequestResolution = chartResolutionInput == "Chart" ? timeframe.period : chartResolutionInput == "1m" ? "1" : chartResolutionInput
string chartPriceResolution = chartResolutionInput == "Chart" and timeframe.isticks ? "1T" : chartResolutionInput
 
[chartTimes, chartHighs, chartLows, chartCloses, chartVolumes] = request.security_lower_tf(
     syminfo.tickerid,
     chartRequestResolution,
     [time, high, low, close, volume],
     ignore_invalid_symbol=true,
     ignore_invalid_timeframe=true)
[chartVwap, chartDataDate, chartSawOpen] = f_chart_vwap(
     chartTimes, chartHighs, chartLows, chartCloses, chartVolumes, anchor, chartPriceResolution)
 
// Proxy 1: ES by default. Its volume weights the SPX chart price.
[proxy1Times, proxy1Volumes] = request.security_lower_tf(
     proxy1Symbol,
     proxy1Resolution == "1m" ? "1" : proxy1Resolution,
     [time, volume],
     ignore_invalid_symbol=true,
     ignore_invalid_timeframe=true)
[proxy1Vwap, proxy1DataDate, proxy1SawOpen] = f_proxy_weighted_vwap(
     proxy1Times, proxy1Volumes, anchor, hlc3)
 
// Proxy 2: SPY by default. Its volume weights the SPX chart price.
[proxy2Times, proxy2Volumes] = request.security_lower_tf(
     proxy2Symbol,
     proxy2Resolution == "1m" ? "1" : proxy2Resolution,
     [time, volume],
     ignore_invalid_symbol=true,
     ignore_invalid_timeframe=true)
[proxy2Vwap, proxy2DataDate, proxy2SawOpen] = f_proxy_weighted_vwap(
     proxy2Times, proxy2Volumes, anchor, hlc3)
 
bool chartInRth = not na(time(timeframe.period, RTH_SESSION, NY_TZ))
int chartDate = year(time, NY_TZ) * 10000 + month(time, NY_TZ) * 100 + dayofmonth(time, NY_TZ)
 
plot(
     showChartVwap and chartInRth and chartDataDate == chartDate and chartSawOpen ? chartVwap : na,
     title="Chart symbol RTH VWAP",
     color=color.rgb(255, 232, 240),
     linewidth=1,
     style=plot.style_linebr)
 
plot(
     showProxy1 and chartInRth and proxy1DataDate == chartDate and proxy1SawOpen ? proxy1Vwap : na,
     title="Proxy 1 volume-weighted chart price",
     color=color.yellow,
     linewidth=1,
     style=plot.style_linebr)
 
plot(
     showProxy2 and chartInRth and proxy2DataDate == chartDate and proxy2SawOpen ? proxy2Vwap : na,
     title="Proxy 2 volume-weighted chart price",
     color=color.rgb(173, 216, 230),
     linewidth=1,
     style=plot.style_linebr)

← 返回列表