2SD Bands Tracker with Multi-Anchor VWAP & Cross-Market Mapping

wyndmillwillie · study · 111 行 · 点赞 6 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © wyndmillwillie
 
//@version=6
indicator("2SD bands and VWAP tracking v3 WW", overlay=true, max_labels_count=100, max_lines_count=500)
 
// ─── Inputs ────────────────────────────────────────────────────────────────
stdevLength        = input.int(50,  "Std Dev Lookback", minval=2)
mult               = input.float(2.0, "Std Dev Multiplier", step=0.1)
showBandLabels     = input.bool(true, "Show Floating Band Labels")
labelMode          = input.string("ES + SPX + CFD", "Label Shows", options=["ES Only","SPX Only","CFD Only","ES + SPX","ES + CFD","ES + SPX + CFD"])
spxCashSymbol      = input.symbol("SPX", "SPX Cash Symbol")
spxCfdSymbol       = input.symbol("FOREXCOM:SPXUSD", "SPX CFD Symbol")
labelBgTransp      = input.int(60, "Label Background Transparency (0–100)", minval=0, maxval=100)
upperLabelBaseCol  = input.color(color.new(color.blue, 0),   "Upper Label Base Color")
lowerLabelBaseCol  = input.color(color.new(color.purple, 0), "Lower Label Base Color")
vwapLineColor      = input.color(color.new(color.orange, 0),  "VWAP Line Color")
upperBandLineColor = input.color(color.new(color.gray, 0),   "+2σ Band Line Color")
lowerBandLineColor = input.color(color.new(color.gray, 0),   "−2σ Band Line Color")
vwapAnchorMode     = input.string("Full Session (Globex)", "VWAP Anchor Mode", options=["Full Session (Globex)","RTH Only (Cash)","Continuous (No Reset)"])
rthSessionStr      = input.session("0930-1600", "Cash Session (Exchange Time)")
 
// ─── Basic series / volatility ─────────────────────────────────────────────
isIntra   = timeframe.isintraday
src       = hlc3
dev       = ta.stdev(src, stdevLength)
 
// ─── VWAP: Full Session (Globex) ───────────────────────────────────────────
vwapFull = ta.vwap(src)
 
// ─── VWAP: Continuous (No Reset) ───────────────────────────────────────────
var float contCumPV  = 0.0
var float contCumVol = 0.0
contCumPV  += src * volume
contCumVol += volume
vwapCont   = contCumVol > 0 ? contCumPV / contCumVol : na
 
// ─── VWAP: RTH Only (Cash Session) ─────────────────────────────────────────
inRTH   = not na(time(timeframe.period, rthSessionStr))
var float rthCumPV  = 0.0
var float rthCumVol = 0.0
if inRTH and not inRTH[1]
    rthCumPV  := 0.0
    rthCumVol := 0.0
if inRTH
    rthCumPV  += src * volume
    rthCumVol += volume
vwapRTH = (inRTH and rthCumVol > 0) ? rthCumPV / rthCumVol : na
 
// ─── Select active VWAP based on anchor mode ───────────────────────────────
vwapSelected = vwapAnchorMode=="Full Session (Globex)" ? vwapFull : vwapAnchorMode=="RTH Only (Cash)" ? vwapRTH : vwapCont
vwap        = vwapSelected
 
// ─── Bands around selected VWAP ────────────────────────────────────────────
upperBand = isIntra and not na(vwap) ? vwap + mult * dev : na
lowerBand = isIntra and not na(vwap) ? vwap - mult * dev : na
 
// ─── Reference Data (SPX cash + SPX CFD) ──────────────────────────────────
spxCashClose = request.security(spxCashSymbol, timeframe.period, close, gaps=barmerge.gaps_off)
spxCfdClose  = request.security(spxCfdSymbol,  timeframe.period, close, gaps=barmerge.gaps_off)
 
// Conversion used ONLY for label text
f_convertToRef(priceES, refClose) => (not na(priceES) and not na(refClose)) ? priceES - (close - refClose) : na
 
upperSPX = f_convertToRef(upperBand, spxCashClose)
lowerSPX = f_convertToRef(lowerBand, spxCashClose)
upperCFD = f_convertToRef(upperBand, spxCfdClose)
lowerCFD = f_convertToRef(lowerBand, spxCfdClose)
 
// ─── Plot ES-only series (uses symbol price scale automatically) ──────────
plot(vwap,      title="VWAP",     linewidth=2, color=vwapLineColor)
plot(upperBand, title="+2σ Band",               color=upperBandLineColor, style=plot.style_line)
plot(lowerBand, title="−2σ Band",               color=lowerBandLineColor, style=plot.style_line)
 
// ─── Label mode flags ─────────────────────────────────────────────────────
showES  = labelMode=="ES Only" or labelMode=="ES + SPX" or labelMode=="ES + CFD" or labelMode=="ES + SPX + CFD"
showSPX = labelMode=="SPX Only" or labelMode=="ES + SPX" or labelMode=="ES + SPX + CFD"
showCFD = labelMode=="CFD Only" or labelMode=="ES + CFD" or labelMode=="ES + SPX + CFD"
 
// ─── Background colors with user transparency ─────────────────────────────
bgUpper = color.new(upperLabelBaseCol, labelBgTransp)
bgLower = color.new(lowerLabelBaseCol, labelBgTransp)
 
// ─── Floating labels anchored ONLY to ES band values ──────────────────────
var label lblUpper = na
var label lblLower = na
 
if barstate.islast and showBandLabels and isIntra and not na(upperBand) and not na(lowerBand)
    if not na(lblUpper)
        label.delete(lblUpper)
    if not na(lblLower)
        label.delete(lblLower)
 
    upperText = ""
    if showES and not na(upperBand)
        upperText := "ES U:  " + str.tostring(upperBand, format.mintick)
    if showSPX and not na(upperSPX)
        upperText := (upperText!="" ? upperText + "\n" : "") + "SPX U: " + str.tostring(upperSPX, format.mintick)
    if showCFD and not na(upperCFD)
        upperText := (upperText!="" ? upperText + "\n" : "") + "CFD U: " + str.tostring(upperCFD, format.mintick)
 
    lowerText = ""
    if showES and not na(lowerBand)
        lowerText := "ES L:  " + str.tostring(lowerBand, format.mintick)
    if showSPX and not na(lowerSPX)
        lowerText := (lowerText!="" ? lowerText + "\n" : "") + "SPX L: " + str.tostring(lowerSPX, format.mintick)
    if showCFD and not na(lowerCFD)
        lowerText := (lowerText!="" ? lowerText + "\n" : "") + "CFD L: " + str.tostring(lowerCFD, format.mintick)
 
    lblUpper := label.new(bar_index, upperBand, upperText, xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_left, textalign=text.align_left, color=bgUpper, textcolor=color.white)
    lblLower := label.new(bar_index, lowerBand, lowerText, xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_left, textalign=text.align_left, color=bgLower, textcolor=color.white)

← 返回列表