Multi-Period VWAP - Commodity & Crypto

silvypeter · study · 159 行 · 点赞 4 · TradingView 原页

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

Pine Script

//@version=6
indicator("Multi-Period VWAP v4 - Commodity & Crypto", overlay=true)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 1. VWAP VISIBILITY
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
showSession = input.bool(true, "Session VWAP", group="VWAP Visibility")
showWeekly  = input.bool(true, "Weekly VWAP", group="VWAP Visibility")
showMonthly = input.bool(true, "Monthly VWAP", group="VWAP Visibility")
showYearly  = input.bool(true, "Yearly VWAP", group="VWAP Visibility")
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 2. CALCULATION SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
src = input.source(hlc3, "Price Source", group="Calculation")
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 3. SESSION SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
sessionInput = input.session("0000-2359:1234567", "Trading Session", group="Session Settings")
 
timezoneChoice = input.string(
     "Exchange",
     "Timezone",
     options=["Exchange", "Asia/Kolkata", "Etc/UTC", "America/New_York", "Europe/London"],
     group="Session Settings")
 
sessionTimezone = timezoneChoice == "Exchange" ? syminfo.timezone : timezoneChoice
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 4. COLORS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
sessionColor = input.color(color.orange, "Session Color", group="VWAP Colors")
weeklyColor  = input.color(color.blue, "Weekly Color", group="VWAP Colors")
monthlyColor = input.color(color.fuchsia, "Monthly Color", group="VWAP Colors")
yearlyColor  = input.color(color.lime, "Yearly Color", group="VWAP Colors")
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 5. LINE WIDTHS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
sessionWidth = input.int(2, "Session Width", minval=1, maxval=5, group="Line Width")
weeklyWidth  = input.int(2, "Weekly Width", minval=1, maxval=5, group="Line Width")
monthlyWidth = input.int(2, "Monthly Width", minval=1, maxval=5, group="Line Width")
yearlyWidth  = input.int(2, "Yearly Width", minval=1, maxval=5, group="Line Width")
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 6. COMMON PRICE AND VOLUME
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
vol = nz(volume, 0.0)
pv = src * vol
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 7. SESSION VWAP
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
inSession = not na(time(timeframe.period, sessionInput, sessionTimezone))
 
sessionStart = inSession and (
     barstate.isfirst or
     not inSession[1] or
     (sessionInput == "0000-2359:1234567" and timeframe.change("D")))
 
var float sessionPV = 0.0
var float sessionVol = 0.0
 
if timeframe.isintraday
    if sessionStart
        sessionPV := pv
        sessionVol := vol
    else if inSession
        sessionPV += pv
        sessionVol += vol
else
    if barstate.isfirst or timeframe.change("D")
        sessionPV := pv
        sessionVol := vol
    else
        sessionPV += pv
        sessionVol += vol
 
sessionVWAP = sessionVol > 0 and (not timeframe.isintraday or inSession) ? sessionPV / sessionVol : na
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 8. WEEKLY VWAP
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var float weeklyPV = 0.0
var float weeklyVol = 0.0
 
if barstate.isfirst or timeframe.change("W")
    weeklyPV := pv
    weeklyVol := vol
else
    weeklyPV += pv
    weeklyVol += vol
 
weeklyVWAP = weeklyVol > 0 ? weeklyPV / weeklyVol : na
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 9. MONTHLY VWAP
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var float monthlyPV = 0.0
var float monthlyVol = 0.0
 
if barstate.isfirst or timeframe.change("M")
    monthlyPV := pv
    monthlyVol := vol
else
    monthlyPV += pv
    monthlyVol += vol
 
monthlyVWAP = monthlyVol > 0 ? monthlyPV / monthlyVol : na
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 10. YEARLY VWAP
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var float yearlyPV = 0.0
var float yearlyVol = 0.0
 
if barstate.isfirst or timeframe.change("12M")
    yearlyPV := pv
    yearlyVol := vol
else
    yearlyPV += pv
    yearlyVol += vol
 
yearlyVWAP = yearlyVol > 0 ? yearlyPV / yearlyVol : na
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 11. PLOT VWAPS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
plot(showSession ? sessionVWAP : na,
     title="Session VWAP",
     color=sessionColor,
     linewidth=sessionWidth)
 
plot(showWeekly ? weeklyVWAP : na,
     title="Weekly VWAP",
     color=weeklyColor,
     linewidth=weeklyWidth)
 
plot(showMonthly ? monthlyVWAP : na,
     title="Monthly VWAP",
     color=monthlyColor,
     linewidth=monthlyWidth)
 
plot(showYearly ? yearlyVWAP : na,
     title="Yearly VWAP",
     color=yearlyColor,
     linewidth=yearlyWidth)

← 返回列表