這是搭配 "NK-Box" 使用的選項指標。可以在與 "NK-Box" 完全相同的範圍內繪製線條。
"SVWAP" 是 "Static VWAP" 的縮寫,為筆者自創的名稱。它是將前一個期間 VWAP 的最終確定值,在當前期間繪製成一條線。(靜態=Static)
可以使用短期與長期 2 條 SVWAP 來建立背景。
※僅適用於可取得成交量數據的交易標的。
⇩ 上方為 "Hist",下方為 "Current"
"SVWAP" 是 "Static VWAP" 的縮寫,為筆者自創的名稱。它是將前一個期間 VWAP 的最終確定值,在當前期間繪製成一條線。(靜態=Static)
可以使用短期與長期 2 條 SVWAP 來建立背景。
※僅適用於可取得成交量數據的交易標的。
來源: TradingView
⇩ 上方為 "Hist",下方為 "Current"
[NOTICE & LICENSE]
・僅供學習與提供資訊之用,不構成投資建議。對於因使用本工具而產生之任何經濟損失,概不負責。
・已於 Pine Script v6 測試運作。以「現狀 (As-is)」提供,不保證因未來 TradingView 系統規格變更而進行更新,亦不接受個別的安裝支援或修改請求。
・適用 MIT 授權條款。
// SPDX-FileCopyrightText: 2026 NK-report https://www.nk-report.com/
// SPDX-License-Identifier: MIT
//
// Disclaimer: This script is for educational purposes only and does not constitute investment advice.
//@version=6
// ※計算負荷がゼロに等しいため過去表示の制限を撤廃。TVのガベージコレクションに任せるためmax_lines_count=500を設定
indicator("NK-box_option2_SVWAP & Zone (History)", shorttitle="NK-opt2 SVWAP (Hist)", overlay=true, max_lines_count=500)
// ==============================================================================
// 【01】 免責・ライセンス
// ==============================================================================
//
// 本指標免費公開: https://www.nk-report.com/p/tw-tradingview.html
//
// 免責聲明:本腳本僅供學習與資訊參考之用,不構成任何投資建議。
//
// 1. 本程式碼截至 2026 年,已在 Pine Script v6 環境下完成運作測試。
// 日後若因規格變動而導致任何異常,恕不提供個別的技術支援或修改服務。
// 2. 本腳本係基於一般的計算邏輯,並透過 LLM 獨立編寫而成。
// 本程式碼採用 MIT 授權條款 (MIT License) 進行發布。
// ------------------------------------------------------------------------------
// MIT License
//
// Copyright 2026 NK-report https://www.nk-report.com/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ==============================================================================
// 【02】 UI設定と言語
// ==============================================================================
const string GRP_GENERAL = "▼ 週期設定 (雙時間級別)"
const string GRP_OFFSET = "▼ 繪圖位置微調 (偏移)"
const string GRP_LINES = "▼ SVWAP 線條與區域設定"
const string LBL_BASE_TF = "基準週期 1 (短期 SVWAP)"
const string TT_BASE_TF = "※請指定與 NK-box 本體相同的數值。\n區間寬度將與短期 SVWAP 自動同步。"
const string LBL_MACRO_TF = "基準週期 2 (長期 SVWAP)"
const string TT_MACRO_TF = "※請指定大於基準週期 1 的時間。\n※若設定較短,系統會自動將其修正為等於基準週期 1。"
const string OPT_TF_D = "1天"
const string OPT_TF_W = "1週"
const string OPT_TF_1M = "1個月"
const string OPT_TF_3M = "3個月"
const string OPT_TF_12M = "12個月"
const string LBL_OFS_L = "左端偏移調整 (K線數)"
const string LBL_OFS_R = "右端偏移調整 (K線數)"
const string TT_OFS = "負值向左移動,正值向右移動。\n※請指定與 NK-box 本體相同的數值。"
const string OPT_STY_S = "實線"
const string OPT_STY_D = "虛線"
const string OPT_STY_DT = "點線"
// 期間設定 (デフォルト: 短期3ヶ月 / 長期12ヶ月)
i_base_tf_str = input.string(OPT_TF_3M, title=LBL_BASE_TF, options=[OPT_TF_D, OPT_TF_W, OPT_TF_1M, OPT_TF_3M, OPT_TF_12M], group=GRP_GENERAL, tooltip=TT_BASE_TF)
i_macro_tf_str = input.string(OPT_TF_12M, title=LBL_MACRO_TF, options=[OPT_TF_D, OPT_TF_W, OPT_TF_1M, OPT_TF_3M, OPT_TF_12M], group=GRP_GENERAL, tooltip=TT_MACRO_TF)
// オフセット設定
i_offset_left = input.int(-1, title=LBL_OFS_L, group=GRP_OFFSET, tooltip=TT_OFS)
i_offset_right = input.int(0, title=LBL_OFS_R, group=GRP_OFFSET, tooltip=TT_OFS)
// 短期 SVWAP
i_show_b = input.bool(true, title="顯示短期 SVWAP", inline="b", group=GRP_LINES)
i_col_b = input.color(color.green, title="", inline="b", group=GRP_LINES)
i_wid_b = input.int(2, title="", minval=1, maxval=5, inline="b", group=GRP_LINES)
i_sty_b = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], inline="b", group=GRP_LINES)
// 長期 SVWAP
i_show_a = input.bool(true, title="顯示長期 SVWAP", inline="a", group=GRP_LINES)
i_col_a = input.color(color.blue, title="", inline="a", group=GRP_LINES)
i_wid_a = input.int(2, title="", minval=1, maxval=5, inline="a", group=GRP_LINES)
i_sty_a = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], inline="a", group=GRP_LINES)
// ゾーン設定 (紫の5%不透明度 = transp 95)
i_show_zone = input.bool(true, title="顯示區域 (短期與長期之間)", group=GRP_LINES, inline="zone")
i_col_zone = input.color(color.new(color.purple, 95), title="", group=GRP_LINES, inline="zone")
// ==============================================================================
// 【03】 全コード共通仕様(時間・安全装置処理)
// ==============================================================================
// TF文字列を内部インデックスに変換する関数 (安全装置の比較用)
f_tf_idx(tf_str) =>
tf_str == OPT_TF_D ? 1 : tf_str == OPT_TF_W ? 2 : tf_str == OPT_TF_1M ? 3 : tf_str == OPT_TF_3M ? 4 : tf_str == OPT_TF_12M ? 5 : 0
// ユーザーが長期を短期より短く設定した場合、強制的に短期と同じにする安全装置
int idx_base = f_tf_idx(i_base_tf_str)
int idx_macro = f_tf_idx(i_macro_tf_str)
string safe_macro_str = idx_macro < idx_base ? i_base_tf_str : i_macro_tf_str
// Pine内部識別子への変換
string req_base = switch i_base_tf_str
OPT_TF_D => "1D"
OPT_TF_W => "1W"
OPT_TF_1M => "1M"
OPT_TF_3M => "3M"
OPT_TF_12M => "12M"
string req_macro = switch safe_macro_str
OPT_TF_D => "1D"
OPT_TF_W => "1W"
OPT_TF_1M => "1M"
OPT_TF_3M => "3M"
OPT_TF_12M => "12M"
bool is_new_base = timeframe.change(req_base) or barstate.isfirst
bool is_new_macro = timeframe.change(req_macro) or barstate.isfirst
f_get_style(sty_str) =>
sty_str == OPT_STY_S ? line.style_solid : sty_str == OPT_STY_D ? line.style_dashed : line.style_dotted
// ==============================================================================
// 【05】 このコード固有の計算仕様 (Specific Indicator Logic)
// ==============================================================================
// 短期SVWAP [B] 系統
var float cum_pv_b = 0.0
var float cum_v_b = 0.0
var float prev_calc_vwap_b = na
// 長期SVWAP [A] 系統
var float cum_pv_a = 0.0
var float cum_v_a = 0.0
var float prev_calc_vwap_a = na
// 現在の投影期間(箱)に描画するための保持変数
var float proj_vwap_b = na
var float proj_vwap_a = na
var int start_bar = na
bool do_draw = false
int draw_start = na
int draw_end = na
float draw_b = na
float draw_a = na
// ① 裏側の計算:期間が切り替わったら、過去の累積からVWAPを確定させリセットする
if is_new_macro
prev_calc_vwap_a := cum_v_a > 0 ? cum_pv_a / cum_v_a : na
cum_pv_a := 0.0
cum_v_a := 0.0
if is_new_base
prev_calc_vwap_b := cum_v_b > 0 ? cum_pv_b / cum_v_b : na
cum_pv_b := 0.0
cum_v_b := 0.0
// ② 表側の投影:ベース期間(箱の幅)が切り替わるタイミングで、AとB両方の座標をセット
if not na(start_bar)
do_draw := true
draw_start := math.max(0, start_bar + i_offset_left)
draw_end := math.max(0, bar_index[1] + i_offset_right)
draw_b := proj_vwap_b
draw_a := proj_vwap_a
start_bar := bar_index
proj_vwap_b := prev_calc_vwap_b
proj_vwap_a := prev_calc_vwap_a
// ③ 毎ティックの出来高加重累積 (超軽量ストック)
float typ = hlc3
float vol = volume
if not na(typ) and not na(vol)
float pv = typ * vol
cum_pv_b += pv
cum_v_b += vol
cum_pv_a += pv
cum_v_a += vol
// ==============================================================================
// 【06】 描画と出力 (Rendering & Outputs)
// ==============================================================================
if do_draw
// 配列管理を完全撤廃し、TV標準のガベージコレクションに丸投げしてスクロールズレを防止
bool req_line_b = i_show_b or i_show_zone
bool req_line_a = i_show_a or i_show_zone
line l_b = req_line_b ? line.new(draw_start, draw_b, draw_end, draw_b, color=i_show_b ? i_col_b : na, width=i_wid_b, style=f_get_style(i_sty_b)) : na
line l_a = req_line_a ? line.new(draw_start, draw_a, draw_end, draw_a, color=i_show_a ? i_col_a : na, width=i_wid_a, style=f_get_style(i_sty_a)) : na
// ゾーン塗りつぶし
if i_show_zone and not na(l_b) and not na(l_a)
linefill.new(l_b, l_a, i_col_zone)
// データウィンドウ出力
plot(proj_vwap_b, title="短期 SVWAP", color=i_col_b, display=display.data_window, editable=false)
plot(proj_vwap_a, title="長期 SVWAP", color=i_col_a, display=display.data_window, editable=false)
// SPDX-FileCopyrightText: 2026 NK-report https://www.nk-report.com/
// SPDX-License-Identifier: MIT
//
// Disclaimer: This script is for educational purposes only and does not constitute investment advice.
//@version=6
indicator("NK-box_option2_SVWAP & Zone (Current)", shorttitle="NK-opt2 SVWAP (Curr)", overlay=true)
// ==============================================================================
// 【01】 免責・ライセンス
// ==============================================================================
//
// 本指標免費公開: https://www.nk-report.com/p/tw-tradingview.html
//
// 免責聲明:本腳本僅供學習與資訊參考之用,不構成任何投資建議。
//
// 1. 本程式碼截至 2026 年,已在 Pine Script v6 環境下完成運作測試。
// 日後若因規格變動而導致任何異常,恕不提供個別的技術支援或修改服務。
// 2. 本腳本係基於一般的計算邏輯,並透過 LLM 獨立編寫而成。
// 本程式碼採用 MIT 授權條款 (MIT License) 進行發布。
// ------------------------------------------------------------------------------
// MIT License
//
// Copyright 2026 NK-report https://www.nk-report.com/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ==============================================================================
// 【02】 UI設定と言語
// ==============================================================================
const string GRP_GENERAL = "▼ 週期設定 (雙時間級別)"
const string GRP_OFFSET = "▼ 繪圖位置微調 (偏移)"
const string GRP_LINES = "▼ SVWAP 線條與區域設定"
const string LBL_BASE_TF = "基準週期 1 (短期 SVWAP)"
const string TT_BASE_TF = "※請指定與 NK-box 本體相同的數值。\n區間寬度將與短期 SVWAP 自動同步。"
const string LBL_MACRO_TF = "基準週期 2 (長期 SVWAP)"
const string TT_MACRO_TF = "※請指定大於基準週期 1 的時間。\n※若設定較短,系統會自動將其修正為等於基準週期 1。"
const string OPT_TF_D = "1天"
const string OPT_TF_W = "1週"
const string OPT_TF_1M = "1個月"
const string OPT_TF_3M = "3個月"
const string OPT_TF_12M = "12個月"
const string LBL_OFS_L = "左端偏移調整 (K線數換算)"
const string LBL_OFS_R = "右端偏移調整 (K線數換算)"
const string TT_OFS = "負值向左移動,正值向右移動。\n※Current版為基於時間計算(毫秒)的概估位移。\n※請指定與 NK-box 本體相同的數值。"
const string OPT_STY_S = "實線"
const string OPT_STY_D = "虛線"
const string OPT_STY_DT = "點線"
// 期間設定 (デフォルト: 短期3ヶ月 / 長期12ヶ月)
i_base_tf_str = input.string(OPT_TF_3M, title=LBL_BASE_TF, options=[OPT_TF_D, OPT_TF_W, OPT_TF_1M, OPT_TF_3M, OPT_TF_12M], group=GRP_GENERAL, tooltip=TT_BASE_TF)
i_macro_tf_str = input.string(OPT_TF_12M, title=LBL_MACRO_TF, options=[OPT_TF_D, OPT_TF_W, OPT_TF_1M, OPT_TF_3M, OPT_TF_12M], group=GRP_GENERAL, tooltip=TT_MACRO_TF)
i_offset_left = input.int(-1, title=LBL_OFS_L, group=GRP_OFFSET, tooltip=TT_OFS)
i_offset_right = input.int(0, title=LBL_OFS_R, group=GRP_OFFSET, tooltip=TT_OFS)
// 短期 SVWAP
i_show_b = input.bool(true, title="顯示短期 SVWAP", inline="b", group=GRP_LINES)
i_col_b = input.color(color.green, title="", inline="b", group=GRP_LINES)
i_wid_b = input.int(2, title="", minval=1, maxval=5, inline="b", group=GRP_LINES)
i_sty_b = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], inline="b", group=GRP_LINES)
// 長期 SVWAP
i_show_a = input.bool(true, title="顯示長期 SVWAP", inline="a", group=GRP_LINES)
i_col_a = input.color(color.blue, title="", inline="a", group=GRP_LINES)
i_wid_a = input.int(2, title="", minval=1, maxval=5, inline="a", group=GRP_LINES)
i_sty_a = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], inline="a", group=GRP_LINES)
// ゾーン設定 (紫の5%不透明度 = transp 95)
i_show_zone = input.bool(true, title="顯示區域 (短期與長期之間)", group=GRP_LINES, inline="zone")
i_col_zone = input.color(color.new(color.purple, 95), title="", group=GRP_LINES, inline="zone")
// ==============================================================================
// 【03】 全コード共通仕様(時間・安全装置処理)
// ==============================================================================
const int MS_PER_SEC = 1000
// 強制ガードロジック: インデックス化して判定
f_tf_idx(tf_str) =>
tf_str == OPT_TF_D ? 1 : tf_str == OPT_TF_W ? 2 : tf_str == OPT_TF_1M ? 3 : tf_str == OPT_TF_3M ? 4 : tf_str == OPT_TF_12M ? 5 : 0
// ユーザーが長期を短期より短く設定した場合、強制的に短期と同じにする安全装置
int idx_base = f_tf_idx(i_base_tf_str)
int idx_macro = f_tf_idx(i_macro_tf_str)
string safe_macro_str = idx_macro < idx_base ? i_base_tf_str : i_macro_tf_str
// Pine内部識別子への変換
string req_base = switch i_base_tf_str
OPT_TF_D => "1D"
OPT_TF_W => "1W"
OPT_TF_1M => "1M"
OPT_TF_3M => "3M"
OPT_TF_12M => "12M"
string req_macro = switch safe_macro_str
OPT_TF_D => "1D"
OPT_TF_W => "1W"
OPT_TF_1M => "1M"
OPT_TF_3M => "3M"
OPT_TF_12M => "12M"
bool is_new_base = timeframe.change(req_base) or barstate.isfirst
bool is_new_macro = timeframe.change(req_macro) or barstate.isfirst
f_get_style(sty_str) =>
sty_str == OPT_STY_S ? line.style_solid : sty_str == OPT_STY_D ? line.style_dashed : line.style_dotted
// ------------------------------------------------------------------------------
// オブジェクトの事前生成 (Current版専用インフラ)
// ------------------------------------------------------------------------------
var line l_b = line.new(na, na, na, na, xloc=xloc.bar_time, width=i_wid_b, style=f_get_style(i_sty_b), color=i_show_b ? i_col_b : na)
var line l_a = line.new(na, na, na, na, xloc=xloc.bar_time, width=i_wid_a, style=f_get_style(i_sty_a), color=i_show_a ? i_col_a : na)
if barstate.isfirst
if i_show_zone
linefill.new(l_b, l_a, i_col_zone)
// ==============================================================================
// 【05】 このコード固有の計算仕様 (Specific Indicator Logic)
// ==============================================================================
// 短期 SVWAP [B] 系統
var float cum_pv_b = 0.0
var float cum_v_b = 0.0
var float prev_calc_vwap_b = na
// 長期 SVWAP [A] 系統
var float cum_pv_a = 0.0
var float cum_v_a = 0.0
var float prev_calc_vwap_a = na
// 投影空間のX軸変数とY軸値
var int start_t = na
var int end_t = na
var float proj_vwap_b = na
var float proj_vwap_a = na
bool do_draw = false
int draw_start = na
int draw_end = na
// ① 裏側の計算:期間が確定した瞬間にSVWAPをロック
if is_new_macro
prev_calc_vwap_a := cum_v_a > 0 ? cum_pv_a / cum_v_a : na
cum_pv_a := 0.0
cum_v_a := 0.0
if is_new_base
prev_calc_vwap_b := cum_v_b > 0 ? cum_pv_b / cum_v_b : na
cum_pv_b := 0.0
cum_v_b := 0.0
// ② 表側の投影:ベース期間(箱の幅)が切り替わるタイミングで未来時間をセット
proj_vwap_b := prev_calc_vwap_b
proj_vwap_a := prev_calc_vwap_a
start_t := time
int tc = time_close(req_base)
end_t := not na(tc) ? tc : time + timeframe.in_seconds(req_base) * MS_PER_SEC
// ③ 毎ティックの出来高加重累積
float typ = hlc3
float vol = volume
if not na(typ) and not na(vol)
float pv = typ * vol
cum_pv_b += pv
cum_v_b += vol
cum_pv_a += pv
cum_v_a += vol
// 描画フラグの生成 (現在の時間軸空間へ投影)
if (not na(proj_vwap_b) or not na(proj_vwap_a)) and not na(start_t) and not na(end_t)
do_draw := true
int bar_ms = timeframe.in_seconds(timeframe.period) * MS_PER_SEC
draw_start := start_t + (i_offset_left * bar_ms)
draw_end := end_t + (i_offset_right * bar_ms)
// ==============================================================================
// 【06】 描画と出力 (Rendering & Outputs)
// ==============================================================================
if do_draw
bool req_line_b = i_show_b or i_show_zone
bool req_line_a = i_show_a or i_show_zone
if req_line_b
line.set_xy1(l_b, draw_start, proj_vwap_b)
line.set_xy2(l_b, draw_end, proj_vwap_b)
else
line.set_xy1(l_b, na, na)
line.set_xy2(l_b, na, na)
if req_line_a
line.set_xy1(l_a, draw_start, proj_vwap_a)
line.set_xy2(l_a, draw_end, proj_vwap_a)
else
line.set_xy1(l_a, na, na)
line.set_xy2(l_a, na, na)
// データウィンドウ出力
plot(proj_vwap_b, title="短期 SVWAP", color=i_col_b, display=display.data_window, editable=false)
plot(proj_vwap_a, title="長期 SVWAP", color=i_col_a, display=display.data_window, editable=false)
* 設定好喜歡的顏色配置後,請在設定分頁中點擊「儲存為預設值」。
* 若未按照「新增 → 指標 → 貼上」的順序操作,可能無法正確顯示。
