本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// © cryptalent
// Anchored TWAP Bands | Version 1.5.1 | 2026-11-06 (Asia/Taipei)
// Eight manually selected anchors. All anchors are disabled by default.
// Each enabled anchor uses a shared High/Low sample count:
// Upper = sum(High) / N; Lower = sum(Low) / N.
// Width = mean(High - Low). Every included chart bar has equal weight.
// Pivot bar includes the entire candle containing the selected date.
// Next bar open starts at the first opening time >= the anchor timestamp.
// Current-bar High/Low values develop; timeframe or input changes recalculate.
// Missing/invalid price data suppresses both bounds for that anchor.
// Native price fields show both bounds. No endpoint price tags are drawn.
// Each band uses two plots and one fill, with fillgaps disabled.
indicator("Anchored TWAP Bands [cryptalent]", shorttitle = "ATWAP Bands", overlay = true, format = format.price, max_labels_count = 1)
const string GROUP_CALC = "Calculation"
const string GROUP_BANDS = "Band appearance"
const string GROUP_A = "TWAP A"
const string GROUP_B = "TWAP B"
const string GROUP_C = "TWAP C"
const string GROUP_D = "TWAP D"
const string GROUP_E = "TWAP E"
const string GROUP_F = "TWAP F"
const string GROUP_G = "TWAP G"
const string GROUP_H = "TWAP H"
const string ANCHOR_TIP = "設定錨定日期與時間,或選取指標並拖曳對應的時間標記。Pivot bar:包含所選日期的整根 K 棒;週中日期會包含該週 K。Next bar open:從開盤時間大於或等於所選時間的第一根起算。每個錨點固定產生 High TWAP 上緣與 Low TWAP 下緣,使用相同起點與根數;第一根即取該 K 棒的 High/Low,之後持續累計平均。各組預設時間皆為台北 08:00,設定欄位依圖表時區顯示。"
string anchorAlignmentInput = input.string("Pivot bar", "Anchor alignment", options = ["Pivot bar", "Next bar open"], group = GROUP_CALC, display = display.none, tooltip = "Pivot bar 適合標記顯著高低點所在的 K 棒:輸入週中日期也會包含該根週 K,整根來源數值納入一次。Next bar open 保留舊版起算方式,時間落在 K 棒中途時從下一根起算。當日期落在休市空檔,兩種模式均從之後第一根可用 K 棒起算。")
bool fillBandsInput = input.bool(true, "Fill bands", group = GROUP_BANDS, display = display.none, tooltip = "填滿每組 High TWAP 與 Low TWAP 之間的區域。兩條邊界使用相同樣本:上緣為錨點以來 High 的平均,下緣為 Low 的平均;帶寬等於 High−Low 的平均值。採目前圖表時框,切換時框會重新計算;未收盤時兩條邊界持續更新。")
int fillTransparencyInput = input.int(88, "Fill transparency", minval = 0, maxval = 100, group = GROUP_BANDS, display = display.none, active = fillBandsInput, tooltip = "0 為完全不透明,100 為完全透明。預設 88,方便多組帶狀重疊時保留 K 棒可讀性;只調整填色,不改變上下緣的計算或線寬。")
bool enableAInput = input.bool(false, "Enable A", group = GROUP_A, display = display.none, tooltip = "預設關閉。勾選後設定 Anchor A 的日期與時間,該錨點需位於目前商品已載入的歷史資料內。")
int anchorAInput = input.time(timestamp("14 Jan 2015 08:00 +0800"), "Anchor A", group = GROUP_A, display = display.none, active = enableAInput, tooltip = ANCHOR_TIP)
color colorAInput = input.color(#9ECFE0, "Line", group = GROUP_A, inline = "A_style", display = display.none, active = enableAInput)
int widthAInput = input.int(2, "", minval = 1, maxval = 5, group = GROUP_A, inline = "A_style", display = display.none, active = enableAInput, tooltip = "A 帶上下邊界的顏色與粗細。填色沿用同一顏色,透明度在 Band appearance 設定。")
bool enableBInput = input.bool(false, "Enable B", group = GROUP_B, display = display.none, tooltip = "預設關閉。勾選後設定 Anchor B 的日期與時間,該錨點需位於目前商品已載入的歷史資料內。")
int anchorBInput = input.time(timestamp("16 Jan 2016 08:00 +0800"), "Anchor B", group = GROUP_B, display = display.none, active = enableBInput, tooltip = ANCHOR_TIP)
color colorBInput = input.color(#D8BE8C, "Line", group = GROUP_B, inline = "B_style", display = display.none, active = enableBInput)
int widthBInput = input.int(2, "", minval = 1, maxval = 5, group = GROUP_B, inline = "B_style", display = display.none, active = enableBInput, tooltip = "B 帶上下邊界的顏色與粗細。填色沿用同一顏色,透明度在 Band appearance 設定。")
bool enableCInput = input.bool(false, "Enable C", group = GROUP_C, display = display.none, tooltip = "預設關閉。勾選後設定 Anchor C 的日期與時間,該錨點需位於目前商品已載入的歷史資料內。")
int anchorCInput = input.time(timestamp("17 Dec 2017 08:00 +0800"), "Anchor C", group = GROUP_C, display = display.none, active = enableCInput, tooltip = ANCHOR_TIP)
color colorCInput = input.color(#B6A3D8, "Line", group = GROUP_C, inline = "C_style", display = display.none, active = enableCInput)
int widthCInput = input.int(2, "", minval = 1, maxval = 5, group = GROUP_C, inline = "C_style", display = display.none, active = enableCInput, tooltip = "C 帶上下邊界的顏色與粗細。填色沿用同一顏色,透明度在 Band appearance 設定。")
bool enableDInput = input.bool(false, "Enable D", group = GROUP_D, display = display.none, tooltip = "預設關閉。勾選後設定 Anchor D 的日期與時間,該錨點需位於目前商品已載入的歷史資料內。")
int anchorDInput = input.time(timestamp("15 Dec 2018 08:00 +0800"), "Anchor D", group = GROUP_D, display = display.none, active = enableDInput, tooltip = ANCHOR_TIP)
color colorDInput = input.color(#A6C7A8, "Line", group = GROUP_D, inline = "D_style", display = display.none, active = enableDInput)
int widthDInput = input.int(2, "", minval = 1, maxval = 5, group = GROUP_D, inline = "D_style", display = display.none, active = enableDInput, tooltip = "D 帶上下邊界的顏色與粗細。填色沿用同一顏色,透明度在 Band appearance 設定。")
bool enableEInput = input.bool(false, "Enable E", group = GROUP_E, display = display.none, tooltip = "預設關閉。勾選後設定 Anchor E 的日期與時間,該錨點需位於目前商品已載入的歷史資料內。")
int anchorEInput = input.time(timestamp("13 Mar 2020 08:00 +0800"), "Anchor E", group = GROUP_E, display = display.none, active = enableEInput, tooltip = ANCHOR_TIP)
color colorEInput = input.color(#D39CA6, "Line", group = GROUP_E, inline = "E_style", display = display.none, active = enableEInput)
int widthEInput = input.int(2, "", minval = 1, maxval = 5, group = GROUP_E, inline = "E_style", display = display.none, active = enableEInput, tooltip = "E 帶上下邊界的顏色與粗細。填色沿用同一顏色,透明度在 Band appearance 設定。")
bool enableFInput = input.bool(false, "Enable F", group = GROUP_F, display = display.none, tooltip = "預設關閉。勾選後設定 Anchor F 的日期與時間,該錨點需位於目前商品已載入的歷史資料內。")
int anchorFInput = input.time(timestamp("21 Nov 2022 08:00 +0800"), "Anchor F", group = GROUP_F, display = display.none, active = enableFInput, tooltip = ANCHOR_TIP)
color colorFInput = input.color(#78B4AD, "Line", group = GROUP_F, inline = "F_style", display = display.none, active = enableFInput)
int widthFInput = input.int(2, "", minval = 1, maxval = 5, group = GROUP_F, inline = "F_style", display = display.none, active = enableFInput, tooltip = "F 帶上下邊界的顏色與粗細。填色沿用同一顏色,透明度在 Band appearance 設定。")
bool enableGInput = input.bool(false, "Enable G", group = GROUP_G, display = display.none, tooltip = "預設關閉。勾選後設定 Anchor G 的日期與時間,該錨點需位於目前商品已載入的歷史資料內。")
int anchorGInput = input.time(timestamp("06 Oct 2025 08:00 +0800"), "Anchor G", group = GROUP_G, display = display.none, active = enableGInput, tooltip = ANCHOR_TIP)
color colorGInput = input.color(#849DC7, "Line", group = GROUP_G, inline = "G_style", display = display.none, active = enableGInput)
int widthGInput = input.int(2, "", minval = 1, maxval = 5, group = GROUP_G, inline = "G_style", display = display.none, active = enableGInput, tooltip = "G 帶上下邊界的顏色與粗細。填色沿用同一顏色,透明度在 Band appearance 設定。")
bool enableHInput = input.bool(false, "Enable H", group = GROUP_H, display = display.none, tooltip = "預設關閉。勾選後設定 Anchor H 的日期與時間,該錨點需位於目前商品已載入的歷史資料內。")
int anchorHInput = input.time(timestamp("10 Nov 2021 08:00 +0800"), "Anchor H", group = GROUP_H, display = display.none, active = enableHInput, tooltip = ANCHOR_TIP)
color colorHInput = input.color(#C4B5A4, "Line", group = GROUP_H, inline = "H_style", display = display.none, active = enableHInput)
int widthHInput = input.int(2, "", minval = 1, maxval = 5, group = GROUP_H, inline = "H_style", display = display.none, active = enableHInput, tooltip = "H 帶上下邊界的顏色與粗細。填色沿用同一顏色,透明度在 Band appearance 設定。")
if barstate.isfirst
if not chart.is_standard or timeframe.isticks
runtime.error("請使用一般時間型 K 線或線形圖。此版本不支援 Tick、Heikin Ashi、Renko 等非標準圖表。")
var int firstLoadedTime = time
// One call per anchor, with synchronized High/Low sampling.
// State: 0 = valid/disabled, 1 = missing history, 2 = pending, 3 = invalid prices.
f_anchoredTwapBand(float highValue, float lowValue, int anchorTime, bool enabled, int firstTime, bool includePivotBar) =>
var float highTotal = 0.0
var float lowTotal = 0.0
var int sampleCount = 0
var bool rangeComplete = true
bool historyComplete = anchorTime >= firstTime
bool containsAnchor = includePivotBar and time <= anchorTime and not na(time_close) and anchorTime < time_close
bool isActive = enabled and historyComplete and (time >= anchorTime or containsAnchor)
if isActive
if na(highValue) or na(lowValue) or highValue < lowValue
rangeComplete := false
else
highTotal += highValue
lowTotal += lowValue
sampleCount += 1
bool isComplete = isActive and rangeComplete and sampleCount > 0
float upperValue = isComplete ? highTotal / sampleCount : na
float lowerValue = isComplete ? lowTotal / sampleCount : na
int stateCode = 0
if enabled
if not historyComplete
stateCode := 1
else if not isActive
stateCode := 2
else if not rangeComplete
stateCode := 3
[upperValue, lowerValue, stateCode]
f_statusText(string anchorName, int stateCode) =>
string message = ""
if stateCode == 1
message := anchorName + "|錨點早於已載入資料,請載入歷史或重選錨點\n"
else if stateCode == 2
message := anchorName + "|等待錨定 K 棒\n"
else if stateCode == 3
message := anchorName + "|High/Low 資料缺漏或異常,請檢查資料或重選錨點\n"
message
bool alignPivotBar = anchorAlignmentInput == "Pivot bar"
[upperA, lowerA, stateA] = f_anchoredTwapBand(high, low, anchorAInput, enableAInput, firstLoadedTime, alignPivotBar)
[upperB, lowerB, stateB] = f_anchoredTwapBand(high, low, anchorBInput, enableBInput, firstLoadedTime, alignPivotBar)
[upperC, lowerC, stateC] = f_anchoredTwapBand(high, low, anchorCInput, enableCInput, firstLoadedTime, alignPivotBar)
[upperD, lowerD, stateD] = f_anchoredTwapBand(high, low, anchorDInput, enableDInput, firstLoadedTime, alignPivotBar)
[upperE, lowerE, stateE] = f_anchoredTwapBand(high, low, anchorEInput, enableEInput, firstLoadedTime, alignPivotBar)
[upperF, lowerF, stateF] = f_anchoredTwapBand(high, low, anchorFInput, enableFInput, firstLoadedTime, alignPivotBar)
[upperG, lowerG, stateG] = f_anchoredTwapBand(high, low, anchorGInput, enableGInput, firstLoadedTime, alignPivotBar)
[upperH, lowerH, stateH] = f_anchoredTwapBand(high, low, anchorHInput, enableHInput, firstLoadedTime, alignPivotBar)
// Both bounds retain native price-scale, status-line and Data Window values.
upperPlotA = plot(upperA, title = "TWAP A High", color = colorAInput, linewidth = widthAInput, style = plot.style_linebr, trackprice = false, display = display.all)
lowerPlotA = plot(lowerA, title = "TWAP A Low", color = colorAInput, linewidth = widthAInput, style = plot.style_linebr, trackprice = false, display = display.all)
fill(upperPlotA, lowerPlotA, color = fillBandsInput ? color.new(colorAInput, fillTransparencyInput) : na, title = "TWAP A Band", fillgaps = false)
upperPlotB = plot(upperB, title = "TWAP B High", color = colorBInput, linewidth = widthBInput, style = plot.style_linebr, trackprice = false, display = display.all)
lowerPlotB = plot(lowerB, title = "TWAP B Low", color = colorBInput, linewidth = widthBInput, style = plot.style_linebr, trackprice = false, display = display.all)
fill(upperPlotB, lowerPlotB, color = fillBandsInput ? color.new(colorBInput, fillTransparencyInput) : na, title = "TWAP B Band", fillgaps = false)
upperPlotC = plot(upperC, title = "TWAP C High", color = colorCInput, linewidth = widthCInput, style = plot.style_linebr, trackprice = false, display = display.all)
lowerPlotC = plot(lowerC, title = "TWAP C Low", color = colorCInput, linewidth = widthCInput, style = plot.style_linebr, trackprice = false, display = display.all)
fill(upperPlotC, lowerPlotC, color = fillBandsInput ? color.new(colorCInput, fillTransparencyInput) : na, title = "TWAP C Band", fillgaps = false)
upperPlotD = plot(upperD, title = "TWAP D High", color = colorDInput, linewidth = widthDInput, style = plot.style_linebr, trackprice = false, display = display.all)
lowerPlotD = plot(lowerD, title = "TWAP D Low", color = colorDInput, linewidth = widthDInput, style = plot.style_linebr, trackprice = false, display = display.all)
fill(upperPlotD, lowerPlotD, color = fillBandsInput ? color.new(colorDInput, fillTransparencyInput) : na, title = "TWAP D Band", fillgaps = false)
upperPlotE = plot(upperE, title = "TWAP E High", color = colorEInput, linewidth = widthEInput, style = plot.style_linebr, trackprice = false, display = display.all)
lowerPlotE = plot(lowerE, title = "TWAP E Low", color = colorEInput, linewidth = widthEInput, style = plot.style_linebr, trackprice = false, display = display.all)
fill(upperPlotE, lowerPlotE, color = fillBandsInput ? color.new(colorEInput, fillTransparencyInput) : na, title = "TWAP E Band", fillgaps = false)
upperPlotF = plot(upperF, title = "TWAP F High", color = colorFInput, linewidth = widthFInput, style = plot.style_linebr, trackprice = false, display = display.all)
lowerPlotF = plot(lowerF, title = "TWAP F Low", color = colorFInput, linewidth = widthFInput, style = plot.style_linebr, trackprice = false, display = display.all)
fill(upperPlotF, lowerPlotF, color = fillBandsInput ? color.new(colorFInput, fillTransparencyInput) : na, title = "TWAP F Band", fillgaps = false)
upperPlotG = plot(upperG, title = "TWAP G High", color = colorGInput, linewidth = widthGInput, style = plot.style_linebr, trackprice = false, display = display.all)
lowerPlotG = plot(lowerG, title = "TWAP G Low", color = colorGInput, linewidth = widthGInput, style = plot.style_linebr, trackprice = false, display = display.all)
fill(upperPlotG, lowerPlotG, color = fillBandsInput ? color.new(colorGInput, fillTransparencyInput) : na, title = "TWAP G Band", fillgaps = false)
upperPlotH = plot(upperH, title = "TWAP H High", color = colorHInput, linewidth = widthHInput, style = plot.style_linebr, trackprice = false, display = display.all)
lowerPlotH = plot(lowerH, title = "TWAP H Low", color = colorHInput, linewidth = widthHInput, style = plot.style_linebr, trackprice = false, display = display.all)
fill(upperPlotH, lowerPlotH, color = fillBandsInput ? color.new(colorHInput, fillTransparencyInput) : na, title = "TWAP H Band", fillgaps = false)
// A combined diagnostic appears only when an enabled anchor cannot produce TWAP.
var label statusLabel = na
if barstate.islast
string statusText = f_statusText("A", stateA) + f_statusText("B", stateB) + f_statusText("C", stateC) + f_statusText("D", stateD) + f_statusText("E", stateE) + f_statusText("F", stateF) + f_statusText("G", stateG) + f_statusText("H", stateH)
if str.length(statusText) > 0
if na(statusLabel)
statusLabel := label.new(x = bar_index + 3, y = high, text = statusText, xloc = xloc.bar_index, yloc = yloc.price, style = label.style_none, textcolor = #D8BE8C, textalign = text.align_left, size = size.normal, text_font_family = font.family_monospace)
else
label.set_xy(statusLabel, bar_index + 3, high)
label.set_text(statusLabel, statusText)
else if not na(statusLabel)
label.delete(statusLabel)
statusLabel := na