This indicator draws a box on the current period based on the high, low, and midline of the previous period.
You can plot various lines that align with the box's range.
To prevent display errors, I have created separate versions for historical data and the current period.
Hist: Displays data up to the previous period.
Current: Displays only the latest data.
You can adjust the horizontal offset of the box. Use this if the display is misaligned depending on the ticker.
The calculated values of the lines remain the same regardless of the offset adjustment.
⇩ "Hist" (Top), "Current" (Bottom)
You can plot various lines that align with the box's range.
To prevent display errors, I have created separate versions for historical data and the current period.
Hist: Displays data up to the previous period.
Current: Displays only the latest data.
You can adjust the horizontal offset of the box. Use this if the display is misaligned depending on the ticker.
The calculated values of the lines remain the same regardless of the offset adjustment.
Source: TradingView
⇩ "Hist" (Top), "Current" (Bottom)
[NOTICE & LICENSE]
・For educational and informational purposes only; does not constitute investment advice. We assume no liability for any economic loss arising from the use of this tool.
・Tested on Pine Script v6. Provided "As-Is" with no guarantee of updates in response to future TradingView specification changes. We do not provide individual installation support or accept modification requests.
・Governed by the MIT License.
// 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 (History)", shorttitle="NK-box (Hist)", overlay=true, max_boxes_count=500, max_lines_count=500)
// ==============================================================================
// 【01】 免責・ライセンス
// ==============================================================================
//
// Indicator is published for free: https://www.nk-report.com/p/tradingview-code.html
//
// Disclaimer: This script is for educational and informational purposes only and does not constitute investment advice.
//
// 1. This code has been tested with Pine Script v6 as of 2026.
// We do not provide individual support or modifications for any issues caused by future specification changes.
// 2. This script was independently created using an LLM, based on general calculation logic.
// It is distributed under the 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設定と言語
// ==============================================================================
// 1. 言語辞書の定義(テキストの定数化)
// ※外国語版を作成する際は、このブロックの「右辺のテキスト」のみを翻訳してください
const string GRP_GENERAL = "▼ General Settings"
const string GRP_OFFSET = "▼ Position Adjustment (Offset)"
const string GRP_BOX_FRAME = "▼ Box Frame Settings (High/Low)"
const string GRP_BOX_BG = "▼ Box Background Settings"
const string GRP_MID = "▼ Midline Settings"
const string GRP_ANL = "▼ Analysis Line Settings (Calculated from previous period)"
const string GRP_ZONE = "▼ Background against Midline"
const string LBL_TF = "Base Timeframe"
const string TT_TF = "* Please specify a timeframe equal to or higher than the chart's timeframe.\n(e.g., Select '1 Week' or '1 Month' on a Daily chart)"
const string OPT_TF_D = "1 Day"
const string OPT_TF_W = "1 Week"
const string OPT_TF_1M = "1 Month"
const string OPT_TF_3M = "3 Months"
const string OPT_TF_12M = "12 Months"
const string LBL_OFS_L = "Left Offset (Bars)"
const string LBL_OFS_R = "Right Offset (Bars)"
const string TT_OFS = "Negative values move left, positive values move right."
const string LBL_FRAME = "High/Low Frame"
const string LBL_BG = "Box Background"
const string LBL_MID = "Midline"
const string LBL_OPEN = "Open"
const string LBL_CLOSE = "Close"
const string LBL_WCL = "Weighted Close (WCL)"
const string LBL_PIVOT = "Pivot"
const string LBL_MEDIAN = "Median"
const string LBL_TWAP = "TWAP"
const string OPT_STY_S = "Solid"
const string OPT_STY_D = "Dashed"
const string OPT_STY_DT = "Dotted"
const string LBL_DATA_H = "High"
const string LBL_DATA_L = "Low"
// 2. UI(ユーザー入力画面)の構築
// ※言語辞書で定義した定数を呼び出してUIを構築します
i_tf_str = input.string(OPT_TF_1M, title=LBL_TF, options=[OPT_TF_D, OPT_TF_W, OPT_TF_1M, OPT_TF_3M, OPT_TF_12M], group=GRP_GENERAL, tooltip=TT_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)
// ボックス枠
i_show_frame = input.bool(true, title=LBL_FRAME, group=GRP_BOX_FRAME, inline="frame")
i_col_frame = input.color(color.black, title="", group=GRP_BOX_FRAME, inline="frame")
i_wid_frame = input.int(2, title="", minval=1, maxval=5, group=GRP_BOX_FRAME, inline="frame")
i_sty_frame = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_BOX_FRAME, inline="frame")
// ボックス背景
i_show_bg = input.bool(false, title=LBL_BG, group=GRP_BOX_BG, inline="bg")
i_col_bg = input.color(color.new(color.teal, 90), title="", group=GRP_BOX_BG, inline="bg")
// ミッドライン
i_show_mid = input.bool(true, title=LBL_MID, group=GRP_MID, inline="mid")
i_col_mid = input.color(color.black, title="", group=GRP_MID, inline="mid")
i_wid_mid = input.int(2, title="", minval=1, maxval=5, group=GRP_MID, inline="mid")
i_sty_mid = input.string(OPT_STY_DT, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_MID, inline="mid")
// 分析ライン
i_show_open = input.bool(false, title=LBL_OPEN, group=GRP_ANL, inline="l1")
i_col_open = input.color(color.yellow, title="", group=GRP_ANL, inline="l1")
i_wid_open = input.int(2, title="", minval=1, maxval=5, group=GRP_ANL, inline="l1")
i_sty_open = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_ANL, inline="l1")
i_show_close = input.bool(false, title=LBL_CLOSE, group=GRP_ANL, inline="l2")
i_col_close = input.color(color.purple, title="", group=GRP_ANL, inline="l2")
i_wid_close = input.int(2, title="", minval=1, maxval=5, group=GRP_ANL, inline="l2")
i_sty_close = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_ANL, inline="l2")
i_show_wcl = input.bool(false, title=LBL_WCL, group=GRP_ANL, inline="l3")
i_col_wcl = input.color(color.red, title="", group=GRP_ANL, inline="l3")
i_wid_wcl = input.int(2, title="", minval=1, maxval=5, group=GRP_ANL, inline="l3")
i_sty_wcl = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_ANL, inline="l3")
i_show_piv = input.bool(false, title=LBL_PIVOT, group=GRP_ANL, inline="l4")
i_col_piv = input.color(color.blue, title="", group=GRP_ANL, inline="l4")
i_wid_piv = input.int(2, title="", minval=1, maxval=5, group=GRP_ANL, inline="l4")
i_sty_piv = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_ANL, inline="l4")
i_show_med = input.bool(false, title=LBL_MEDIAN, group=GRP_ANL, inline="l5")
i_col_med = input.color(color.green, title="", group=GRP_ANL, inline="l5")
i_wid_med = input.int(2, title="", minval=1, maxval=5, group=GRP_ANL, inline="l5")
i_sty_med = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_ANL, inline="l5")
i_show_twap = input.bool(false, title=LBL_TWAP, group=GRP_ANL, inline="l6")
i_col_twap = input.color(color.orange, title="", group=GRP_ANL, inline="l6")
i_wid_twap = input.int(2, title="", minval=1, maxval=5, group=GRP_ANL, inline="l6")
i_sty_twap = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_ANL, inline="l6")
// 背景塗りつぶし
i_fill_open = input.bool(false, title=LBL_OPEN, group=GRP_ZONE, inline="z1")
c_fill_open = input.color(color.new(color.yellow, 90), title="", group=GRP_ZONE, inline="z1")
i_fill_close = input.bool(false, title=LBL_CLOSE, group=GRP_ZONE, inline="z1")
c_fill_close = input.color(color.new(color.purple, 90), title="", group=GRP_ZONE, inline="z1")
i_fill_wcl = input.bool(false, title=LBL_WCL, group=GRP_ZONE, inline="z2")
c_fill_wcl = input.color(color.new(color.red, 90), title="", group=GRP_ZONE, inline="z2")
i_fill_piv = input.bool(false, title=LBL_PIVOT, group=GRP_ZONE, inline="z2")
c_fill_piv = input.color(color.new(color.blue, 90), title="", group=GRP_ZONE, inline="z2")
i_fill_med = input.bool(false, title=LBL_MEDIAN, group=GRP_ZONE, inline="z3")
c_fill_med = input.color(color.new(color.green, 90), title="", group=GRP_ZONE, inline="z3")
i_fill_twap = input.bool(false, title=LBL_TWAP, group=GRP_ZONE, inline="z4")
c_fill_twap = input.color(color.new(color.orange, 90), title="", group=GRP_ZONE, inline="z4")
// ==============================================================================
// 【03】 全コード共通仕様(時間・インフラ処理)
// ==============================================================================
// UIで選択された定数をPine内部のシステム識別子に変換
string req_tf = switch i_tf_str
OPT_TF_D => "1D"
OPT_TF_W => "1W"
OPT_TF_1M => "1M"
OPT_TF_3M => "3M"
OPT_TF_12M => "12M"
// ==============================================================================
// 【04】 各カテゴリ共通仕様 (描画インフラと判定ロジック)
// ==============================================================================
bool is_new_period = timeframe.change(req_tf) 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)
// ==============================================================================
var float curr_o = na
var float curr_h = na
var float curr_l = na
var float prev_o = na
var float prev_h = na
var float prev_l = na
var float prev_c = na
var int start_bar = na
// --- 描画エンジンへ引き渡すためのスナップショット変数(MVC分離用) ---
bool do_draw = false
int draw_start = na
int draw_end = na
float draw_o = na
float draw_h = na
float draw_l = na
float draw_c = na
if is_new_period
// ① 描画キューの生成:計算用の変数が新しい期間のもので上書きされる前に、描画用の確定値を退避させます。
if not na(prev_o) and 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_o := prev_o
draw_h := prev_h
draw_l := prev_l
draw_c := prev_c
// ② 次の期間へのバケツリレー
prev_o := curr_o
prev_h := curr_h
prev_l := curr_l
prev_c := close[1]
curr_o := open
curr_h := high
curr_l := low
start_bar := bar_index
else
curr_h := math.max(curr_h, high)
curr_l := math.min(curr_l, low)
// ==============================================================================
// 【06】 描画と出力 (Rendering & Outputs)
// ==============================================================================
// 1. チャートへの視覚的出力(スタンプ描画)
if do_draw
// 【05】で退避したスナップショットを用いて純粋な計算を行います
float val_mid = (draw_h + draw_l) / 2.0
float val_wcl = (draw_h + draw_l + draw_c * 2.0) / 4.0
float val_piv = (draw_h + draw_l + draw_c) / 3.0
float val_med = (draw_o + draw_c) / 2.0
float val_twap = (draw_o + draw_h + draw_l + draw_c) / 4.0
if i_show_frame or i_show_bg
box.new(draw_start, draw_h, draw_end, draw_l, border_color=i_show_frame ? i_col_frame : na, border_width=i_wid_frame, border_style=f_get_style(i_sty_frame), bgcolor=i_show_bg ? i_col_bg : na)
bool need_mid = i_show_mid or i_fill_open or i_fill_close or i_fill_wcl or i_fill_piv or i_fill_med or i_fill_twap
line l_mid = need_mid ? line.new(draw_start, val_mid, draw_end, val_mid, color=i_show_mid ? i_col_mid : na, width=i_wid_mid, style=f_get_style(i_sty_mid)) : na
if i_show_open or i_fill_open
line l_open = line.new(draw_start, draw_o, draw_end, draw_o, color=i_show_open ? i_col_open : na, width=i_wid_open, style=f_get_style(i_sty_open))
if i_fill_open and not na(l_mid)
linefill.new(l_mid, l_open, c_fill_open)
if i_show_close or i_fill_close
line l_close = line.new(draw_start, draw_c, draw_end, draw_c, color=i_show_close ? i_col_close : na, width=i_wid_close, style=f_get_style(i_sty_close))
if i_fill_close and not na(l_mid)
linefill.new(l_mid, l_close, c_fill_close)
if i_show_wcl or i_fill_wcl
line l_wcl = line.new(draw_start, val_wcl, draw_end, val_wcl, color=i_show_wcl ? i_col_wcl : na, width=i_wid_wcl, style=f_get_style(i_sty_wcl))
if i_fill_wcl and not na(l_mid)
linefill.new(l_mid, l_wcl, c_fill_wcl)
if i_show_piv or i_fill_piv
line l_piv = line.new(draw_start, val_piv, draw_end, val_piv, color=i_show_piv ? i_col_piv : na, width=i_wid_piv, style=f_get_style(i_sty_piv))
if i_fill_piv and not na(l_mid)
linefill.new(l_mid, l_piv, c_fill_piv)
if i_show_med or i_fill_med
line l_med = line.new(draw_start, val_med, draw_end, val_med, color=i_show_med ? i_col_med : na, width=i_wid_med, style=f_get_style(i_sty_med))
if i_fill_med and not na(l_mid)
linefill.new(l_mid, l_med, c_fill_med)
if i_show_twap or i_fill_twap
line l_twap = line.new(draw_start, val_twap, draw_end, val_twap, color=i_show_twap ? i_col_twap : na, width=i_wid_twap, style=f_get_style(i_sty_twap))
if i_fill_twap and not na(l_mid)
linefill.new(l_mid, l_twap, c_fill_twap)
// 2. データウィンドウへの出力 (辞書定数を参照)
float out_mid = (prev_h + prev_l) / 2.0
float out_wcl = (prev_h + prev_l + prev_c * 2.0) / 4.0
float out_piv = (prev_h + prev_l + prev_c) / 3.0
float out_med = (prev_o + prev_c) / 2.0
float out_twap = (prev_o + prev_h + prev_l + prev_c) / 4.0
plot(prev_h, title=LBL_DATA_H, color=i_col_frame, display=display.data_window, editable=false)
plot(prev_l, title=LBL_DATA_L, color=i_col_frame, display=display.data_window, editable=false)
plot(out_mid, title=LBL_MID, color=i_col_mid, display=display.data_window, editable=false)
plot(prev_o, title=LBL_OPEN, color=i_col_open, display=display.data_window, editable=false)
plot(prev_c, title=LBL_CLOSE, color=i_col_close, display=display.data_window, editable=false)
plot(out_wcl, title=LBL_WCL, color=i_col_wcl, display=display.data_window, editable=false)
plot(out_piv, title=LBL_PIVOT, color=i_col_piv, display=display.data_window, editable=false)
plot(out_med, title=LBL_MEDIAN, color=i_col_med, display=display.data_window, editable=false)
plot(out_twap, title=LBL_TWAP, color=i_col_twap, 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 (Current)", shorttitle="NK-box (Curr)", overlay=true)
// ==============================================================================
// 【01】 免責・ライセンス
// ==============================================================================
//
// Indicator is published for free: https://www.nk-report.com/p/tradingview-code.html
//
// Disclaimer: This script is for educational and informational purposes only and does not constitute investment advice.
//
// 1. This code has been tested with Pine Script v6 as of 2026.
// We do not provide individual support or modifications for any issues caused by future specification changes.
// 2. This script was independently created using an LLM, based on general calculation logic.
// It is distributed under the 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設定と言語
// ==============================================================================
// 1. 言語辞書の定義(テキストの定数化)
// ※外国語版を作成する際は、このブロックの「右辺のテキスト」のみを翻訳してください
const string GRP_GENERAL = "▼ General Settings"
const string GRP_OFFSET = "▼ Position Adjustment (Offset)"
const string GRP_BOX_FRAME = "▼ Box Frame Settings (High/Low)"
const string GRP_BOX_BG = "▼ Box Background Settings"
const string GRP_MID = "▼ Midline Settings"
const string GRP_ANL = "▼ Analysis Line Settings (Calculated from previous period)"
const string GRP_ZONE = "▼ Background against Midline"
const string LBL_TF = "Base Timeframe"
const string TT_TF = "* Please specify a timeframe equal to or higher than the chart's timeframe.\n(e.g., Select '1 Week' or '1 Month' on a Daily chart)"
const string OPT_TF_D = "1 Day"
const string OPT_TF_W = "1 Week"
const string OPT_TF_1M = "1 Month"
const string OPT_TF_3M = "3 Months"
const string OPT_TF_12M = "12 Months"
const string LBL_OFS_L = "Left Offset (in Bars)"
const string LBL_OFS_R = "Right Offset (in Bars)"
const string TT_OFS = "Negative values move left, positive values move right.\n* In the Current version, this is an approximate movement calculated by time (milliseconds)."
const string LBL_FRAME = "High/Low Frame"
const string LBL_BG = "Box Background"
const string LBL_MID = "Midline"
const string LBL_OPEN = "Open"
const string LBL_CLOSE = "Close"
const string LBL_WCL = "Weighted Close (WCL)"
const string LBL_PIVOT = "Pivot"
const string LBL_MEDIAN = "Median"
const string LBL_TWAP = "TWAP"
const string OPT_STY_S = "Solid"
const string OPT_STY_D = "Dashed"
const string OPT_STY_DT = "Dotted"
const string LBL_DATA_H = "High"
const string LBL_DATA_L = "Low"
// 2. UI(ユーザー入力画面)の構築
// ※言語辞書で定義した定数を呼び出してUIを構築します
i_tf_str = input.string(OPT_TF_1M, title=LBL_TF, options=[OPT_TF_D, OPT_TF_W, OPT_TF_1M, OPT_TF_3M, OPT_TF_12M], group=GRP_GENERAL, tooltip=TT_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)
// ボックス枠
i_show_frame = input.bool(true, title=LBL_FRAME, group=GRP_BOX_FRAME, inline="frame")
i_col_frame = input.color(color.black, title="", group=GRP_BOX_FRAME, inline="frame")
i_wid_frame = input.int(2, title="", minval=1, maxval=5, group=GRP_BOX_FRAME, inline="frame")
i_sty_frame = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_BOX_FRAME, inline="frame")
// ボックス背景
i_show_bg = input.bool(false, title=LBL_BG, group=GRP_BOX_BG, inline="bg")
i_col_bg = input.color(color.new(color.teal, 90), title="", group=GRP_BOX_BG, inline="bg")
// ミッドライン
i_show_mid = input.bool(true, title=LBL_MID, group=GRP_MID, inline="mid")
i_col_mid = input.color(color.black, title="", group=GRP_MID, inline="mid")
i_wid_mid = input.int(2, title="", minval=1, maxval=5, group=GRP_MID, inline="mid")
i_sty_mid = input.string(OPT_STY_DT, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_MID, inline="mid")
// 分析ライン
i_show_open = input.bool(false, title=LBL_OPEN, group=GRP_ANL, inline="l1")
i_col_open = input.color(color.yellow, title="", group=GRP_ANL, inline="l1")
i_wid_open = input.int(2, title="", minval=1, maxval=5, group=GRP_ANL, inline="l1")
i_sty_open = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_ANL, inline="l1")
i_show_close = input.bool(false, title=LBL_CLOSE, group=GRP_ANL, inline="l2")
i_col_close = input.color(color.purple, title="", group=GRP_ANL, inline="l2")
i_wid_close = input.int(2, title="", minval=1, maxval=5, group=GRP_ANL, inline="l2")
i_sty_close = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_ANL, inline="l2")
i_show_wcl = input.bool(false, title=LBL_WCL, group=GRP_ANL, inline="l3")
i_col_wcl = input.color(color.red, title="", group=GRP_ANL, inline="l3")
i_wid_wcl = input.int(2, title="", minval=1, maxval=5, group=GRP_ANL, inline="l3")
i_sty_wcl = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_ANL, inline="l3")
i_show_piv = input.bool(false, title=LBL_PIVOT, group=GRP_ANL, inline="l4")
i_col_piv = input.color(color.blue, title="", group=GRP_ANL, inline="l4")
i_wid_piv = input.int(2, title="", minval=1, maxval=5, group=GRP_ANL, inline="l4")
i_sty_piv = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_ANL, inline="l4")
i_show_med = input.bool(false, title=LBL_MEDIAN, group=GRP_ANL, inline="l5")
i_col_med = input.color(color.green, title="", group=GRP_ANL, inline="l5")
i_wid_med = input.int(2, title="", minval=1, maxval=5, group=GRP_ANL, inline="l5")
i_sty_med = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_ANL, inline="l5")
i_show_twap = input.bool(false, title=LBL_TWAP, group=GRP_ANL, inline="l6")
i_col_twap = input.color(color.orange, title="", group=GRP_ANL, inline="l6")
i_wid_twap = input.int(2, title="", minval=1, maxval=5, group=GRP_ANL, inline="l6")
i_sty_twap = input.string(OPT_STY_S, title="", options=[OPT_STY_S, OPT_STY_D, OPT_STY_DT], group=GRP_ANL, inline="l6")
// 背景塗りつぶし
i_fill_open = input.bool(false, title=LBL_OPEN, group=GRP_ZONE, inline="z1")
c_fill_open = input.color(color.new(color.yellow, 90), title="", group=GRP_ZONE, inline="z1")
i_fill_close = input.bool(false, title=LBL_CLOSE, group=GRP_ZONE, inline="z1")
c_fill_close = input.color(color.new(color.purple, 90), title="", group=GRP_ZONE, inline="z1")
i_fill_wcl = input.bool(false, title=LBL_WCL, group=GRP_ZONE, inline="z2")
c_fill_wcl = input.color(color.new(color.red, 90), title="", group=GRP_ZONE, inline="z2")
i_fill_piv = input.bool(false, title=LBL_PIVOT, group=GRP_ZONE, inline="z2")
c_fill_piv = input.color(color.new(color.blue, 90), title="", group=GRP_ZONE, inline="z2")
i_fill_med = input.bool(false, title=LBL_MEDIAN, group=GRP_ZONE, inline="z3")
c_fill_med = input.color(color.new(color.green, 90), title="", group=GRP_ZONE, inline="z3")
i_fill_twap = input.bool(false, title=LBL_TWAP, group=GRP_ZONE, inline="z4")
c_fill_twap = input.color(color.new(color.orange, 90), title="", group=GRP_ZONE, inline="z4")
// ==============================================================================
// 【03】 全コード共通仕様(時間・インフラ処理)
// ==============================================================================
// UIで選択された定数をPine内部のシステム識別子に変換
string req_tf = switch i_tf_str
OPT_TF_D => "1D"
OPT_TF_W => "1W"
OPT_TF_1M => "1M"
OPT_TF_3M => "3M"
OPT_TF_12M => "12M"
// 時間計算用の定数 (Current版のオフセット概算に使用)
const int MS_PER_SEC = 1000
// ==============================================================================
// 【04】 各カテゴリ共通仕様 (描画インフラと判定ロジック)
// ==============================================================================
bool is_new_period = timeframe.change(req_tf) 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版専用インフラ)
// チャート上に1セットだけ図形を作り、毎ティック座標を更新するための準備
// ------------------------------------------------------------------------------
var box b_main = box.new(na, na, na, na, xloc=xloc.bar_time, border_width=i_wid_frame, border_style=f_get_style(i_sty_frame), border_color=i_show_frame ? i_col_frame : na, bgcolor=i_show_bg ? i_col_bg : na)
var line l_mid = line.new(na, na, na, na, xloc=xloc.bar_time, width=i_wid_mid, style=f_get_style(i_sty_mid), color=i_show_mid ? i_col_mid : na)
var line l_open = line.new(na, na, na, na, xloc=xloc.bar_time, width=i_wid_open, style=f_get_style(i_sty_open), color=i_show_open ? i_col_open : na)
var line l_close = line.new(na, na, na, na, xloc=xloc.bar_time, width=i_wid_close, style=f_get_style(i_sty_close), color=i_show_close ? i_col_close : na)
var line l_wcl = line.new(na, na, na, na, xloc=xloc.bar_time, width=i_wid_wcl, style=f_get_style(i_sty_wcl), color=i_show_wcl ? i_col_wcl : na)
var line l_piv = line.new(na, na, na, na, xloc=xloc.bar_time, width=i_wid_piv, style=f_get_style(i_sty_piv), color=i_show_piv ? i_col_piv : na)
var line l_med = line.new(na, na, na, na, xloc=xloc.bar_time, width=i_wid_med, style=f_get_style(i_sty_med), color=i_show_med ? i_col_med : na)
var line l_twap = line.new(na, na, na, na, xloc=xloc.bar_time, width=i_wid_twap, style=f_get_style(i_sty_twap), color=i_show_twap ? i_col_twap : na)
if barstate.isfirst
if i_fill_open
linefill.new(l_mid, l_open, c_fill_open)
if i_fill_close
linefill.new(l_mid, l_close, c_fill_close)
if i_fill_wcl
linefill.new(l_mid, l_wcl, c_fill_wcl)
if i_fill_piv
linefill.new(l_mid, l_piv, c_fill_piv)
if i_fill_med
linefill.new(l_mid, l_med, c_fill_med)
if i_fill_twap
linefill.new(l_mid, l_twap, c_fill_twap)
// ==============================================================================
// 【05】 このコード固有の計算仕様 (Specific Indicator Logic)
// ==============================================================================
var float temp_o = na
var float temp_h = na
var float temp_l = na
var float prev_o = na
var float prev_h = na
var float prev_l = na
var float prev_c = na
var int start_t = na
var int end_t = na
// 描画エンジンへ引き渡すためのフラグと変数
bool do_draw = false
int draw_start = na
int draw_end = na
float val_mid = na
float val_wcl = na
float val_piv = na
float val_med = na
float val_twap = na
if is_new_period
// ① 期間が切り替わった瞬間、今まで蓄積していたデータを「前期間の確定値」として昇格させる
prev_o := temp_o
prev_h := temp_h
prev_l := temp_l
prev_c := close[1]
// ② 新しい期間の高値・安値記録をスタート
temp_o := open
temp_h := high
temp_l := low
// ③ X軸(横幅)を今期間の開始時刻から未来の終了時刻に設定
start_t := time
int tc = time_close(req_tf)
end_t := not na(tc) ? tc : time + timeframe.in_seconds(req_tf) * MS_PER_SEC
else
// 期間中は裏で次の期間のための高値・安値を蓄積 (Y座標の高さには一切影響させません)
temp_h := math.max(temp_h, high)
temp_l := math.min(temp_l, low)
if not na(prev_o) 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)
val_mid := (prev_h + prev_l) / 2.0
val_wcl := (prev_h + prev_l + prev_c * 2.0) / 4.0
val_piv := (prev_h + prev_l + prev_c) / 3.0
val_med := (prev_o + prev_c) / 2.0
val_twap := (prev_o + prev_h + prev_l + prev_c) / 4.0
// ==============================================================================
// 【06】 描画と出力 (Rendering & Outputs)
// ==============================================================================
// 1. チャートへの視覚的出力(オブジェクト座標の更新)
if do_draw
if i_show_frame or i_show_bg
box.set_lefttop(b_main, draw_start, prev_h)
box.set_rightbottom(b_main, draw_end, prev_l)
bool need_mid = i_show_mid or i_fill_open or i_fill_close or i_fill_wcl or i_fill_piv or i_fill_med or i_fill_twap
if need_mid
line.set_xy1(l_mid, draw_start, val_mid)
line.set_xy2(l_mid, draw_end, val_mid)
if i_show_open or i_fill_open
line.set_xy1(l_open, draw_start, prev_o)
line.set_xy2(l_open, draw_end, prev_o)
if i_show_close or i_fill_close
line.set_xy1(l_close, draw_start, prev_c)
line.set_xy2(l_close, draw_end, prev_c)
if i_show_wcl or i_fill_wcl
line.set_xy1(l_wcl, draw_start, val_wcl)
line.set_xy2(l_wcl, draw_end, val_wcl)
if i_show_piv or i_fill_piv
line.set_xy1(l_piv, draw_start, val_piv)
line.set_xy2(l_piv, draw_end, val_piv)
if i_show_med or i_fill_med
line.set_xy1(l_med, draw_start, val_med)
line.set_xy2(l_med, draw_end, val_med)
if i_show_twap or i_fill_twap
line.set_xy1(l_twap, draw_start, val_twap)
line.set_xy2(l_twap, draw_end, val_twap)
// 2. データウィンドウへの出力 (辞書定数を参照)
// ※【05】で純粋な計算として算出済みの数値をそのまま出力します
plot(prev_h, title=LBL_DATA_H, color=i_col_frame, display=display.data_window, editable=false)
plot(prev_l, title=LBL_DATA_L, color=i_col_frame, display=display.data_window, editable=false)
plot(val_mid, title=LBL_MID, color=i_col_mid, display=display.data_window, editable=false)
plot(prev_o, title=LBL_OPEN, color=i_col_open, display=display.data_window, editable=false)
plot(prev_c, title=LBL_CLOSE, color=i_col_close, display=display.data_window, editable=false)
plot(val_wcl, title=LBL_WCL, color=i_col_wcl, display=display.data_window, editable=false)
plot(val_piv, title=LBL_PIVOT, color=i_col_piv, display=display.data_window, editable=false)
plot(val_med, title=LBL_MEDIAN, color=i_col_med, display=display.data_window, editable=false)
plot(val_twap, title=LBL_TWAP, color=i_col_twap, display=display.data_window, editable=false)
* After setting your preferred colors, please click "Save as default" in the settings tab.
* The indicator may not display correctly unless you follow the steps: Create New -> Indicator -> Paste.
