本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Multi VWAP (D/W/M/Y) - v2
// Four anchored VWAPs in one indicator: the day, the week, the month and the year. Each one starts again
// at the first bar of its period and is the volume-weighted average of hlc3 since then. Each can be
// switched on or off. Four distinct palettes adapt to the chart background; line widths are 2/2/3/4.
// A period that is not longer than one chart bar is not drawn (a daily VWAP on a daily chart would be
// the bar's own hlc3). A period that began before the chart's first bar is not drawn either: its average
// would start at that bar, not at the period start (a chart holds a limited number of bars, so the year is
// often cut). A one-line note names such a period. Without volume on the symbol no line is drawn and
// the note says so.
indicator("Multi VWAP (D/W/M/Y) [Quanty]", shorttitle="MVWAP", overlay=true, max_labels_count=10)
bool showD = input.bool(true, "Daily")
bool showW = input.bool(true, "Weekly")
bool showM = input.bool(true, "Monthly")
bool showY = input.bool(true, "Yearly")
// v2: Four opaque palettes, Aurora (cool blues to violet) is the default; automatic light/dark sets; labels match their lines.
string palette = input.string("Aurora", "Palette", options=["Aurora", "Studio", "Slate", "Dusk"])
linearChannel(float channel) =>
float s = channel / 255.0
s <= 0.04045 ? s / 12.92 : math.pow((s + 0.055) / 1.055, 2.4)
float bgLight = 0.2126 * linearChannel(color.r(chart.bg_color)) + 0.7152 * linearChannel(color.g(chart.bg_color)) + 0.0722 * linearChannel(color.b(chart.bg_color))
bool darkTheme = bgLight < 0.179
color lightD = switch palette
"Aurora" => #078BB7
"Studio" => #B18A18
"Slate" => #7E8490
=> #A078B5
color darkD = switch palette
"Aurora" => #3FCAF8
"Studio" => #C5A752
"Slate" => #B8BCC2
=> #BCA0CB
color colD = darkTheme ? darkD : lightD
color lightW = switch palette
"Aurora" => #2455B8
"Studio" => #2678B5
"Slate" => #2467AE
=> #B46A16
color darkW = switch palette
"Aurora" => #638DE0
"Studio" => #5394C4
"Slate" => #3B87D6
=> #BF8139
color colW = darkTheme ? darkW : lightW
color lightM = switch palette
"Aurora" => #8B298F
"Studio" => #8046B8
"Slate" => #28252E
=> #245E9E
color darkM = switch palette
"Aurora" => #CC5CD0
"Studio" => #915FC1
"Slate" => #66646B
=> #4879AE
color colM = darkTheme ? darkM : lightM
color lightY = switch palette
"Aurora" => #211B43
"Studio" => #28252E
"Slate" => #945900
=> #47234D
color darkY = switch palette
"Aurora" => #6556BB
"Studio" => #66646B
"Slate" => #AA6600
=> #775C7B
color colY = darkTheme ? darkY : lightY
// a period is drawn only on charts with bars shorter than the period
int chartSec = timeframe.in_seconds()
bool fitD = chartSec < timeframe.in_seconds("D")
bool fitW = chartSec < timeframe.in_seconds("W")
bool fitM = chartSec < timeframe.in_seconds("M")
bool fitY = chartSec < timeframe.in_seconds("12M")
// volume: none on this symbol, or none so far
bool noVol = ta.cum(nz(volume)) == 0
bool newD = timeframe.change("D")
bool newW = timeframe.change("W")
bool newM = timeframe.change("M")
bool newY = timeframe.change("12M")
float vD = ta.vwap(hlc3, newD)
float vW = ta.vwap(hlc3, newW)
float vM = ta.vwap(hlc3, newM)
float vY = ta.vwap(hlc3, newY)
// a period counts once its start is on the chart (the first, cut period of each line is skipped)
var bool fullD = false
var bool fullW = false
var bool fullM = false
var bool fullY = false
if bar_index > 0
fullD := fullD or newD
fullW := fullW or newW
fullM := fullM or newM
fullY := fullY or newY
bool onD = showD and fitD and fullD and not noVol
bool onW = showW and fitW and fullW and not noVol
bool onM = showM and fitM and fullM and not noVol
bool onY = showY and fitY and fullY and not noVol
// the line is hidden on the first bar of a new period, so the jump to the new start is not drawn
plot(onD ? vD : na, "Daily", newD ? na : colD, 2)
plot(onW ? vW : na, "Weekly", newW ? na : colW, 2)
plot(onM ? vM : na, "Monthly", newM ? na : colM, 3)
plot(onY ? vY : na, "Yearly", newY ? na : colY, 4)
// short names right of the last bar
var label lD = na
var label lW = na
var label lM = na
var label lY = na
var label lN = na
put(label lb, bool on, float y, string txt, color lineCol) =>
label.delete(lb)
on and not na(y) ? label.new(bar_index + 2, y, txt, style=label.style_label_left, color=color.new(color.black, 100),
textcolor=lineCol, size=size.small) : na
if barstate.islast
lD := put(lD, onD, vD, "D", colD)
lW := put(lW, onW, vW, "W", colW)
lM := put(lM, onM, vM, "M", colM)
lY := put(lY, onY, vY, "Y", colY)
string cut = (showD and fitD and not fullD ? " D" : "") + (showW and fitW and not fullW ? " W" : "") +
(showM and fitM and not fullM ? " M" : "") + (showY and fitY and not fullY ? " Y" : "")
string note = noVol ? "Multi VWAP: no volume data on this symbol, no line is drawn" :
cut != "" ? "Multi VWAP:" + cut + " not drawn, the chart history does not reach the start; use a higher timeframe" : ""
label.delete(lN)
lN := note != "" ? label.new(bar_index, high, note, style=label.style_label_lower_right, color=color.new(color.black, 100),
textcolor=color.new(chart.fg_color, 30), size=size.small) : na