本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (c) Zac-Trades
// ZTC Liquidity Levels v1.3 - open source, comments in English
indicator("ZTC Liquidity Levels", overlay=true, max_lines_count=500, max_labels_count=500, max_boxes_count=500)
// ==========================================
// 1. GENERAL SETTINGS
// ==========================================
grp_general = "1. GENERAL SETTINGS"
draw_limit = input.int(150, title="Past Sessions to Show", minval=1, maxval=500, tooltip="How many past sessions stay on the chart", group=grp_general)
tf_limit = input.timeframe("30", title="Hide on Timeframes Above", tooltip="The indicator is hidden on timeframes bigger than this one", group=grp_general)
tz_input = input.string("America/New_York", title="Time Zone", options=["America/New_York", "Europe/London", "Europe/Paris", "Asia/Tokyo", "UTC"], group=grp_general)
show_labels = input.bool(true, title="Show Line Names", tooltip="Show text like 'Asia High' on the lines", group=grp_general)
label_size = input.string("Normal", title="Line Name Size", options=["Tiny", "Small", "Normal", "Large"], group=grp_general)
show_ribbon = input.bool(true, title="Show Session Strip", tooltip="A colored bar at the bottom of the chart while a session is running", group=grp_general)
strip_size = input.string("Medium", title="Strip Thickness", options=["Thin", "Medium", "Thick"], group=grp_general)
ribbon_transp = input.int(60, title="Strip Transparency", minval=0, maxval=100, tooltip="0 = solid color, 100 = invisible", group=grp_general)
show_bg = input.bool(true, title="Show Session Box", tooltip="A light box behind each session, from its High to its Low", group=grp_general)
bg_transp = input.int(88, title="Box Transparency", minval=0, maxval=100, tooltip="0 = solid color, 100 = invisible", group=grp_general)
get_label_size(size_str) =>
size_str == "Tiny" ? size.tiny : size_str == "Small" ? size.small : size_str == "Large" ? size.large : size.normal
get_line_style(style_str) =>
style_str == "Dashed" ? line.style_dashed : style_str == "Dotted" ? line.style_dotted : line.style_solid
// ==========================================
// 3. SESSION SETUP
// ==========================================
grp_sessions = "3. SESSION SETUP"
s1_on = input.bool(true, title="Session Name :", inline="S1n", group=grp_sessions)
s1_name = input.string("Asia", title="", inline="S1n", group=grp_sessions)
s1_time = input.session("2000-0000", title="Time :", inline="S1t", group=grp_sessions)
s1_h = input.bool(true, title="High", inline="S1t", group=grp_sessions)
s1_l = input.bool(true, title="Low", inline="S1t", group=grp_sessions)
s1_color = input.color(#e91e63, title="", inline="S1t", group=grp_sessions)
s1_w = input.int(1, title="Thickness", options=[1, 2, 3, 4], inline="S1b", group=grp_sessions)
s1_style = input.string("Solid", title="Style", options=["Solid", "Dashed", "Dotted"], inline="S1b", group=grp_sessions)
s2_on = input.bool(true, title="Session Name :", inline="S2n", group=grp_sessions)
s2_name = input.string("London", title="", inline="S2n", group=grp_sessions)
s2_time = input.session("0200-0500", title="Time :", inline="S2t", group=grp_sessions)
s2_h = input.bool(true, title="High", inline="S2t", group=grp_sessions)
s2_l = input.bool(true, title="Low", inline="S2t", group=grp_sessions)
s2_color = input.color(#2962ff, title="", inline="S2t", group=grp_sessions)
s2_w = input.int(1, title="Thickness", options=[1, 2, 3, 4], inline="S2b", group=grp_sessions)
s2_style = input.string("Solid", title="Style", options=["Solid", "Dashed", "Dotted"], inline="S2b", group=grp_sessions)
s3_on = input.bool(true, title="Session Name :", inline="S3n", group=grp_sessions)
s3_name = input.string("NY AM", title="", inline="S3n", group=grp_sessions)
s3_time = input.session("0700-1110", title="Time :", inline="S3t", group=grp_sessions)
s3_h = input.bool(true, title="High", inline="S3t", group=grp_sessions)
s3_l = input.bool(true, title="Low", inline="S3t", group=grp_sessions)
s3_color = input.color(#00897b, title="", inline="S3t", group=grp_sessions)
s3_w = input.int(1, title="Thickness", options=[1, 2, 3, 4], inline="S3b", group=grp_sessions)
s3_style = input.string("Solid", title="Style", options=["Solid", "Dashed", "Dotted"], inline="S3b", group=grp_sessions)
s4_on = input.bool(false, title="Session Name :", inline="S4n", group=grp_sessions)
s4_name = input.string("NY Lunch", title="", inline="S4n", group=grp_sessions)
s4_time = input.session("1200-1300", title="Time :", inline="S4t", group=grp_sessions)
s4_h = input.bool(false, title="High", inline="S4t", group=grp_sessions)
s4_l = input.bool(false, title="Low", inline="S4t", group=grp_sessions)
s4_color = input.color(#fdd835, title="", inline="S4t", group=grp_sessions)
s4_w = input.int(1, title="Thickness", options=[1, 2, 3, 4], inline="S4b", group=grp_sessions)
s4_style = input.string("Solid", title="Style", options=["Solid", "Dashed", "Dotted"], inline="S4b", group=grp_sessions)
s5_on = input.bool(false, title="Session Name :", inline="S5n", group=grp_sessions)
s5_name = input.string("NY PM", title="", inline="S5n", group=grp_sessions)
s5_time = input.session("1300-1600", title="Time :", inline="S5t", group=grp_sessions)
s5_h = input.bool(false, title="High", inline="S5t", group=grp_sessions)
s5_l = input.bool(false, title="Low", inline="S5t", group=grp_sessions)
s5_color = input.color(#8e24aa, title="", inline="S5t", group=grp_sessions)
s5_w = input.int(1, title="Thickness", options=[1, 2, 3, 4], inline="S5b", group=grp_sessions)
s5_style = input.string("Solid", title="Style", options=["Solid", "Dashed", "Dotted"], inline="S5b", group=grp_sessions)
// ==========================================
// 4. PREVIOUS DAY & WEEK
// ==========================================
grp_htf = "4. PREVIOUS DAY & WEEK"
col_pd = input.color(color.gray, title="Previous Day", group=grp_htf)
show_pdh = input.bool(true, title="High", inline="pd", group=grp_htf)
show_pdl = input.bool(true, title="Low", inline="pd", group=grp_htf)
w_pd = input.int(1, title="Thickness", options=[1, 2, 3, 4], inline="pd2", group=grp_htf)
st_pd = input.string("Solid", title="Style", options=["Solid", "Dashed", "Dotted"], inline="pd2", group=grp_htf)
col_pw = input.color(color.orange, title="Previous Week", group=grp_htf)
show_pwh = input.bool(true, title="High", inline="pw", group=grp_htf)
show_pwl = input.bool(true, title="Low", inline="pw", group=grp_htf)
w_pw = input.int(1, title="Thickness", options=[1, 2, 3, 4], inline="pw2", group=grp_htf)
st_pw = input.string("Solid", title="Style", options=["Solid", "Dashed", "Dotted"], inline="pw2", group=grp_htf)
// ==========================================
// 5. SESSION LIQUIDITY LEVELS LOGIC
// ==========================================
show_on_tf = timeframe.in_seconds(timeframe.period) <= timeframe.in_seconds(tf_limit)
draw_session_hl(show_h, show_l, sess_name, sess_time, line_color, line_width, line_style) =>
if (show_h or show_l) and show_on_tf
in_sess = not na(time(timeframe.period, sess_time, tz_input))
new_sess = in_sess and (bar_index == 0 or not in_sess[1])
var line sess_line_high = na
var line sess_line_low = na
var label sess_label_high = na
var label sess_label_low = na
var float max_high = na
var float min_low = na
var int high_bar = na
var int low_bar = na
var bool high_active = false
var bool low_active = false
var line[] lines_h_history = array.new_line()
var line[] lines_l_history = array.new_line()
var label[] labels_h_history = array.new_label()
var label[] labels_l_history = array.new_label()
if new_sess
if show_h
max_high := high
high_bar := bar_index
high_active := true
sess_line_high := line.new(high_bar, max_high, bar_index, max_high, color=line_color, width=line_width, style=get_line_style(line_style))
array.push(lines_h_history, sess_line_high)
if array.size(lines_h_history) > draw_limit
line.delete(array.shift(lines_h_history))
if show_labels
sess_label_high := label.new(bar_index, max_high, text=sess_name + " High\n ", color=color.new(color.white, 100), textcolor=line_color, style=label.style_label_right, textalign=text.align_right, size=get_label_size(label_size))
array.push(labels_h_history, sess_label_high)
if array.size(labels_h_history) > draw_limit
label.delete(array.shift(labels_h_history))
if show_l
min_low := low
low_bar := bar_index
low_active := true
sess_line_low := line.new(low_bar, min_low, bar_index, min_low, color=line_color, width=line_width, style=get_line_style(line_style))
array.push(lines_l_history, sess_line_low)
if array.size(lines_l_history) > draw_limit
line.delete(array.shift(lines_l_history))
if show_labels
sess_label_low := label.new(bar_index, min_low, text=sess_name + " Low\n ", color=color.new(color.white, 100), textcolor=line_color, style=label.style_label_right, textalign=text.align_right, size=get_label_size(label_size))
array.push(labels_l_history, sess_label_low)
if array.size(labels_l_history) > draw_limit
label.delete(array.shift(labels_l_history))
if in_sess and not new_sess
if show_h
if high > max_high
max_high := high
high_bar := bar_index
line.set_x1(sess_line_high, high_bar)
line.set_y1(sess_line_high, max_high)
line.set_y2(sess_line_high, max_high)
line.set_x2(sess_line_high, bar_index)
if show_labels
label.set_x(sess_label_high, bar_index)
label.set_y(sess_label_high, max_high)
if show_l
if low < min_low
min_low := low
low_bar := bar_index
line.set_x1(sess_line_low, low_bar)
line.set_y1(sess_line_low, min_low)
line.set_y2(sess_line_low, min_low)
line.set_x2(sess_line_low, bar_index)
if show_labels
label.set_x(sess_label_low, bar_index)
label.set_y(sess_label_low, min_low)
if not in_sess
if show_h and high_active
if high >= max_high
high_active := false
line.set_x2(sess_line_high, bar_index)
if show_labels
label.set_x(sess_label_high, bar_index)
if show_l and low_active
if low <= min_low
low_active := false
line.set_x2(sess_line_low, bar_index)
if show_labels
label.set_x(sess_label_low, bar_index)
// ==========================================
// 6. DAILY & WEEKLY LEVELS LOGIC (PDH/PDL/PWH/PWL)
// ==========================================
// New day / new week detected directly from the chart's data feed
bool is_new_day = ta.change(time("D")) != 0
bool is_new_week = ta.change(time("W")) != 0
// --- VARIABLES DAILY (PDH/PDL) ---
var float cur_day_h = na
var int cur_day_h_bar = na
var float cur_day_l = na
var int cur_day_l_bar = na
var line pdh_line = na
var label pdh_lbl = na
var bool pdh_active = false
var float pdh_val = na
var int pdh_start = na
var line pdl_line = na
var label pdl_lbl = na
var bool pdl_active = false
var float pdl_val = na
var int pdl_start = na
var line[] pdh_hist = array.new_line()
var label[] pdh_l_hist = array.new_label()
var line[] pdl_hist = array.new_line()
var label[] pdl_l_hist = array.new_label()
if is_new_day
if not na(cur_day_h) and show_on_tf
if show_pdh
pdh_val := cur_day_h
pdh_start := cur_day_h_bar
pdh_active := true
pdh_line := line.new(math.max(pdh_start, bar_index - 4000), pdh_val, bar_index, pdh_val, color=col_pd, width=w_pd, style=get_line_style(st_pd))
array.push(pdh_hist, pdh_line)
if array.size(pdh_hist) > draw_limit
line.delete(array.shift(pdh_hist))
if show_labels
pdh_lbl := label.new(bar_index, pdh_val, text="PDH\n ", color=color.new(color.white, 100), textcolor=col_pd, style=label.style_label_right, textalign=text.align_right, size=get_label_size(label_size))
array.push(pdh_l_hist, pdh_lbl)
if array.size(pdh_l_hist) > draw_limit
label.delete(array.shift(pdh_l_hist))
if show_pdl
pdl_val := cur_day_l
pdl_start := cur_day_l_bar
pdl_active := true
pdl_line := line.new(math.max(pdl_start, bar_index - 4000), pdl_val, bar_index, pdl_val, color=col_pd, width=w_pd, style=get_line_style(st_pd))
array.push(pdl_hist, pdl_line)
if array.size(pdl_hist) > draw_limit
line.delete(array.shift(pdl_hist))
if show_labels
pdl_lbl := label.new(bar_index, pdl_val, text="PDL\n ", color=color.new(color.white, 100), textcolor=col_pd, style=label.style_label_right, textalign=text.align_right, size=get_label_size(label_size))
array.push(pdl_l_hist, pdl_lbl)
if array.size(pdl_l_hist) > draw_limit
label.delete(array.shift(pdl_l_hist))
cur_day_h := high
cur_day_h_bar := bar_index
cur_day_l := low
cur_day_l_bar := bar_index
else
if high > cur_day_h or na(cur_day_h)
cur_day_h := high
cur_day_h_bar := bar_index
if low < cur_day_l or na(cur_day_l)
cur_day_l := low
cur_day_l_bar := bar_index
if pdh_active and show_on_tf and show_pdh
if high >= pdh_val
pdh_active := false
line.set_x2(pdh_line, bar_index)
if show_labels
label.set_x(pdh_lbl, bar_index)
if pdl_active and show_on_tf and show_pdl
if low <= pdl_val
pdl_active := false
line.set_x2(pdl_line, bar_index)
if show_labels
label.set_x(pdl_lbl, bar_index)
// --- VARIABLES WEEKLY (PWH/PWL) ---
var float cur_wk_h = na
var int cur_wk_h_bar = na
var float cur_wk_l = na
var int cur_wk_l_bar = na
var line pwh_line = na
var label pwh_lbl = na
var bool pwh_active = false
var float pwh_val = na
var int pwh_start = na
var line pwl_line = na
var label pwl_lbl = na
var bool pwl_active = false
var float pwl_val = na
var int pwl_start = na
var line[] pwh_hist = array.new_line()
var label[] pwh_l_hist = array.new_label()
var line[] pwl_hist = array.new_line()
var label[] pwl_l_hist = array.new_label()
if is_new_week
if not na(cur_wk_h) and show_on_tf
if show_pwh
pwh_val := cur_wk_h
pwh_start := cur_wk_h_bar
pwh_active := true
pwh_line := line.new(math.max(pwh_start, bar_index - 4000), pwh_val, bar_index, pwh_val, color=col_pw, width=w_pw, style=get_line_style(st_pw))
array.push(pwh_hist, pwh_line)
if array.size(pwh_hist) > draw_limit
line.delete(array.shift(pwh_hist))
if show_labels
pwh_lbl := label.new(bar_index, pwh_val, text="PWH\n ", color=color.new(color.white, 100), textcolor=col_pw, style=label.style_label_right, textalign=text.align_right, size=get_label_size(label_size))
array.push(pwh_l_hist, pwh_lbl)
if array.size(pwh_l_hist) > draw_limit
label.delete(array.shift(pwh_l_hist))
if show_pwl
pwl_val := cur_wk_l
pwl_start := cur_wk_l_bar
pwl_active := true
pwl_line := line.new(math.max(pwl_start, bar_index - 4000), pwl_val, bar_index, pwl_val, color=col_pw, width=w_pw, style=get_line_style(st_pw))
array.push(pwl_hist, pwl_line)
if array.size(pwl_hist) > draw_limit
line.delete(array.shift(pwl_hist))
if show_labels
pwl_lbl := label.new(bar_index, pwl_val, text="PWL\n ", color=color.new(color.white, 100), textcolor=col_pw, style=label.style_label_right, textalign=text.align_right, size=get_label_size(label_size))
array.push(pwl_l_hist, pwl_lbl)
if array.size(pwl_l_hist) > draw_limit
label.delete(array.shift(pwl_l_hist))
cur_wk_h := high
cur_wk_h_bar := bar_index
cur_wk_l := low
cur_wk_l_bar := bar_index
else
if high > cur_wk_h or na(cur_wk_h)
cur_wk_h := high
cur_wk_h_bar := bar_index
if low < cur_wk_l or na(cur_wk_l)
cur_wk_l := low
cur_wk_l_bar := bar_index
if pwh_active and show_on_tf and show_pwh
if high >= pwh_val
pwh_active := false
line.set_x2(pwh_line, bar_index)
if show_labels
label.set_x(pwh_lbl, bar_index)
if pwl_active and show_on_tf and show_pwl
if low <= pwl_val
pwl_active := false
line.set_x2(pwl_line, bar_index)
if show_labels
label.set_x(pwl_lbl, bar_index)
// ==========================================
// 7. RUN THE SESSION LEVELS
// ==========================================
draw_session_hl(s1_on and s1_h, s1_on and s1_l, s1_name, s1_time, s1_color, s1_w, s1_style)
draw_session_hl(s2_on and s2_h, s2_on and s2_l, s2_name, s2_time, s2_color, s2_w, s2_style)
draw_session_hl(s3_on and s3_h, s3_on and s3_l, s3_name, s3_time, s3_color, s3_w, s3_style)
draw_session_hl(s4_on and s4_h, s4_on and s4_l, s4_name, s4_time, s4_color, s4_w, s4_style)
draw_session_hl(s5_on and s5_h, s5_on and s5_l, s5_name, s5_time, s5_color, s5_w, s5_style)
// ==========================================
// 8. SESSION VISUALS (STRIP AND BOX)
// ==========================================
is_s1 = s1_on and not na(time(timeframe.period, s1_time, tz_input)) and show_on_tf
is_s2 = s2_on and not na(time(timeframe.period, s2_time, tz_input)) and show_on_tf
is_s3 = s3_on and not na(time(timeframe.period, s3_time, tz_input)) and show_on_tf
is_s4 = s4_on and not na(time(timeframe.period, s4_time, tz_input)) and show_on_tf
is_s5 = s5_on and not na(time(timeframe.period, s5_time, tz_input)) and show_on_tf
strip_thin = strip_size == "Thin"
strip_medium = strip_size == "Medium"
strip_thick = strip_size == "Thick"
// Number of trading days between each bar and the last bar (same count the session limit uses)
days_back = request.security(syminfo.tickerid, "D", (last_bar_index - bar_index)[1], lookahead=barmerge.lookahead_on) - 1
strip_in_limit = not na(days_back) and days_back < draw_limit
strip_any = strip_in_limit and (is_s1 or is_s2 or is_s3 or is_s4 or is_s5)
strip_col = is_s1 ? s1_color : is_s2 ? s2_color : is_s3 ? s3_color : is_s4 ? s4_color : is_s5 ? s5_color : na
strip_clr = strip_any ? color.new(strip_col, ribbon_transp) : na
plotshape(strip_any and show_ribbon and strip_thin ? 1 : na, title="Session Strip (thin)", style=shape.square, location=location.bottom, color=strip_clr, size=size.tiny, editable=false)
plotshape(strip_any and show_ribbon and strip_medium ? 1 : na, title="Session Strip (medium)", style=shape.square, location=location.bottom, color=strip_clr, size=size.small, editable=false)
plotshape(strip_any and show_ribbon and strip_thick ? 1 : na, title="Session Strip (thick)", style=shape.square, location=location.bottom, color=strip_clr, size=size.normal, editable=false)
draw_session_box(sess_on, sess_time, box_color) =>
if sess_on and show_bg and show_on_tf
in_sess = not na(time(timeframe.period, sess_time, tz_input))
new_sess = in_sess and (bar_index == 0 or not in_sess[1])
var box sess_box = na
var box[] box_history = array.new_box()
if new_sess
sess_box := box.new(bar_index, high, bar_index, low, border_color=na, bgcolor=color.new(box_color, bg_transp))
array.push(box_history, sess_box)
if array.size(box_history) > draw_limit
box.delete(array.shift(box_history))
if in_sess and not new_sess and not na(sess_box)
box.set_top(sess_box, math.max(box.get_top(sess_box), high))
box.set_bottom(sess_box, math.min(box.get_bottom(sess_box), low))
box.set_right(sess_box, bar_index)
draw_session_box(s1_on, s1_time, s1_color)
draw_session_box(s2_on, s2_time, s2_color)
draw_session_box(s3_on, s3_time, s3_color)
draw_session_box(s4_on, s4_time, s4_color)
draw_session_box(s5_on, s5_time, s5_color)