本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0
// © Dots3Red
// TradingView: https://www.tradingview.com/u/Dots3Red/
//@version=6
indicator("Key Levels - PDH PDL PWH PWL & ADR Statistics [Dots3Red]",
shorttitle = "Key Levels [D3R]",
overlay = true,
max_labels_count = 500)
// COLORS (house palette)
C_TXT = color.new(#f8fafc, 0)
C_DIM = color.new(#94a3b8, 0)
C_BG = color.new(#131722, 5)
C_BORD = color.new(#334155, 0)
C_GOOD = color.new(#00c896, 0)
C_BAD = color.new(#ff4466, 0)
C_AMBER = color.new(#ffb700, 0)
// INPUTS
GRP_LV = "📐 Levels"
GRP_ENG = "🎯 Hold / Break Grading"
GRP_VIS = "🎨 Visuals"
GRP_HUD = "🖥️ Dashboard"
show_day = input.bool(true, "Previous Day High / Low", group=GRP_LV)
show_week = input.bool(true, "Previous Week High / Low", group=GRP_LV)
adr_len = input.int(14, "ADR Length (days)", minval=2, maxval=100, group=GRP_LV)
show_adr = input.bool(true, "Show ADR Projection Lines", group=GRP_LV,
tooltip="Today's low + ADR and today's high − ADR: where price would be if today ends up an average-range day.")
atr_len = input.int(14, "ATR Length", minval=2, group=GRP_ENG)
hold_atr = input.float(1.0, "Hold = Rejects By (× ATR)", minval=0.1, step=0.1, group=GRP_ENG,
tooltip="A test counts as HELD if price moves this far back away from the level before breaking it.")
brk_atr = input.float(0.25, "Break = Closes Beyond By (× ATR)", minval=0.0, step=0.05, group=GRP_ENG,
tooltip="A test counts as BROKEN if a bar closes this far beyond the level. If both happen on the same bar, it counts as broken.")
horizon = input.int(30, "Max Bars to Resolve a Test", minval=3, maxval=300, group=GRP_ENG,
tooltip="A test that neither holds nor breaks within this many bars is counted as undecided, so Held + Broke can add up to less than 100%.")
min_n = input.int(10, "Min Tests to Show Rates", minval=3, group=GRP_ENG)
col_dh = input.color(#ff00aa, "Prev Day High", group=GRP_VIS)
col_dl = input.color(#00f0ff, "Prev Day Low", group=GRP_VIS)
col_wh = input.color(#ff4466, "Prev Week High", group=GRP_VIS)
col_wl = input.color(#00c896, "Prev Week Low", group=GRP_VIS)
col_adr = input.color(#ffb700, "ADR Projections", group=GRP_VIS)
w_day = input.int(1, "Daily Line Thickness", minval=1, maxval=6, group=GRP_VIS)
w_week = input.int(2, "Weekly Line Thickness", minval=1, maxval=6, group=GRP_VIS)
w_adr = input.int(1, "ADR Line Thickness", minval=1, maxval=6, group=GRP_VIS)
show_tag = input.bool(true, "Price Tags at Right Edge", group=GRP_VIS)
show_lbl = input.bool(false, "Label Each Test", group=GRP_VIS)
show_hud = input.bool(true, "Show Dashboard", group=GRP_HUD)
hud_pos = input.string("Top Right", "Position",
options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=GRP_HUD)
// LEVELS (built from this chart's own bars)
bool nd = timeframe.change("D")
bool nw = timeframe.change("W")
var float day_hi = na
var float day_lo = na
var float pdh = na
var float pdl = na
var float wk_hi = na
var float wk_lo = na
var float pwh = na
var float pwl = na
if nd
pdh := day_hi
pdl := day_lo
day_hi := high
day_lo := low
else
day_hi := math.max(nz(day_hi, high), high)
day_lo := math.min(nz(day_lo, low), low)
if nw
pwh := wk_hi
pwl := wk_lo
wk_hi := high
wk_lo := low
else
wk_hi := math.max(nz(wk_hi, high), high)
wk_lo := math.min(nz(wk_lo, low), low)
float adr = request.security(syminfo.tickerid, "D", ta.sma(high - low, adr_len)[1], lookahead = barmerge.lookahead_on)
float today_rg = day_hi - day_lo
float adr_used = adr > 0 ? today_rg / adr * 100.0 : na
float adr_up = day_lo + adr
float adr_dn = day_hi - adr
float atr = ta.atr(atr_len)
var bool[] touched = array.new<bool>(4, false)
var int[] p_bar = array.new<int>(4, -1)
var float[] p_lvl = array.new<float>(4, na)
var float[] p_atr = array.new<float>(4, na)
var float[] st_n = array.new<float>(4, 0.0)
var float[] st_hold = array.new<float>(4, 0.0)
var float[] st_brk = array.new<float>(4, 0.0)
string[] names = array.from("PDH", "PDL", "PWH", "PWL")
color[] cols = array.from(col_dh, col_dl, col_wh, col_wl)
float[] lv_now = array.from(pdh, pdl, pwh, pwl)
if nd
array.set(touched, 0, false)
array.set(touched, 1, false)
if nw
array.set(touched, 2, false)
array.set(touched, 3, false)
f_enabled(int i) => i < 2 ? show_day : show_week
f_pct(int i, float[] arr) =>
float n = array.get(st_n, i)
n >= min_n ? array.get(arr, i) / n * 100.0 : na
if barstate.isconfirmed
for i = 0 to 3
int pb = array.get(p_bar, i)
if pb >= 0 and bar_index > pb
bool upper = i % 2 == 0
float lv = array.get(p_lvl, i)
float a = array.get(p_atr, i)
bool brk = upper ? close >= lv + brk_atr * a : close <= lv - brk_atr * a
bool hold = upper ? low <= lv - hold_atr * a : high >= lv + hold_atr * a
bool timed = bar_index - pb >= horizon
if brk or hold or timed
array.set(st_n, i, array.get(st_n, i) + 1.0)
if brk
array.set(st_brk, i, array.get(st_brk, i) + 1.0)
else if hold
array.set(st_hold, i, array.get(st_hold, i) + 1.0)
array.set(p_bar, i, -1)
for i = 0 to 3
float lv = array.get(lv_now, i)
if f_enabled(i) and not na(lv) and not na(atr) and not array.get(touched, i)
bool upper = i % 2 == 0
bool reached = upper ? high >= lv : low <= lv
if reached
array.set(touched, i, true)
bool gapped = upper ? open > lv : open < lv
// a test bar that already closes through the level is an immediate break
bool brk_now = upper ? close >= lv + brk_atr * atr : close <= lv - brk_atr * atr
if not gapped and array.get(p_bar, i) < 0 and brk_now
array.set(st_n, i, array.get(st_n, i) + 1.0)
array.set(st_brk, i, array.get(st_brk, i) + 1.0)
else if not gapped and array.get(p_bar, i) < 0
array.set(p_bar, i, bar_index)
array.set(p_lvl, i, lv)
array.set(p_atr, i, atr)
if show_lbl
float h = f_pct(i, st_hold)
label.new(bar_index, upper ? high : low,
array.get(names, i) + (na(h) ? "" : " hold " + str.tostring(math.round(h)) + "%"),
style = upper ? label.style_label_down : label.style_label_up,
color = color.new(#000000, 100),
textcolor = array.get(cols, i),
size = size.small)
plot(pdh, "Prev Day High", color = nd ? na : col_dh, linewidth=w_day, style=plot.style_linebr, display = show_day ? display.all : display.none)
plot(pdl, "Prev Day Low", color = nd ? na : col_dl, linewidth=w_day, style=plot.style_linebr, display = show_day ? display.all : display.none)
plot(pwh, "Prev Week High", color = nw ? na : col_wh, linewidth=w_week, style=plot.style_linebr, display = show_week ? display.all : display.none)
plot(pwl, "Prev Week Low", color = nw ? na : col_wl, linewidth=w_week, style=plot.style_linebr, display = show_week ? display.all : display.none)
plot(adr_up, "ADR High Projection", color = nd ? na : color.new(col_adr, 30), linewidth=w_adr, style=plot.style_circles, display = show_adr ? display.all : display.none)
plot(adr_dn, "ADR Low Projection", color = nd ? na : color.new(col_adr, 30), linewidth=w_adr, style=plot.style_circles, display = show_adr ? display.all : display.none)
var label[] tags = array.new<label>()
f_tag(string txt, float y, color c) =>
label.new(bar_index + 3, y, txt + " " + str.tostring(y, format.mintick),
style = label.style_label_left,
color = color.new(#000000, 100),
textcolor = c,
size = size.small)
if barstate.islast
while array.size(tags) > 0
label.delete(array.pop(tags))
if show_tag
if show_day and not na(pdh)
array.push(tags, f_tag("PDH", pdh, col_dh))
array.push(tags, f_tag("PDL", pdl, col_dl))
if show_week and not na(pwh)
array.push(tags, f_tag("PWH", pwh, col_wh))
array.push(tags, f_tag("PWL", pwl, col_wl))
if show_adr and not na(adr)
array.push(tags, f_tag("ADR ↑", adr_up, col_adr))
array.push(tags, f_tag("ADR ↓", adr_dn, col_adr))
// DASHBOARD
f_hud_pos(string s) =>
switch s
"Top Right" => position.top_right
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
=> position.bottom_left
var table hud = table.new(f_hud_pos(hud_pos), 5, 8,
bgcolor=C_BG, border_color=C_BORD,
border_width=1, frame_color=C_BORD, frame_width=2)
f_lvl_row(int row, int i, color bg) =>
bool en = f_enabled(i)
float lv = array.get(lv_now, i)
float n = array.get(st_n, i)
float h = f_pct(i, st_hold)
float b = f_pct(i, st_brk)
table.cell(hud, 0, row, array.get(names, i), text_color = en ? array.get(cols, i) : C_DIM, bgcolor=bg, text_size=size.small)
table.cell(hud, 1, row, not en or na(lv) ? "—" : str.tostring(lv, format.mintick), text_color=C_TXT, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 2, row, en ? str.tostring(int(n)) : "—", text_color=C_TXT, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 3, row, not en ? "off" : na(h) ? "…" : str.tostring(math.round(h)) + "%",
text_color = not en or na(h) ? C_DIM : C_GOOD, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 4, row, not en ? "off" : na(b) ? "…" : str.tostring(math.round(b)) + "%",
text_color = not en or na(b) ? C_DIM : C_BAD, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
if show_hud and barstate.islast
color bgA = color.new(#1e293b, 40)
color bgB = color.new(#0f172a, 40)
table.cell(hud, 0, 0, "KEY LEVELS [D3R]", text_color=C_TXT,
bgcolor=color.new(#1e293b, 0), text_size=size.small, text_halign=text.align_center)
table.merge_cells(hud, 0, 0, 4, 0)
table.cell(hud, 0, 1, "Level", text_color=C_DIM, bgcolor=bgB, text_size=size.small)
table.cell(hud, 1, 1, "Price", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 2, 1, "Tests", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 3, 1, "Held", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
tooltip="Share of first tests where price moved " + str.tostring(hold_atr) + "× ATR back away from the level before closing through it.")
table.cell(hud, 4, 1, "Broke", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
tooltip="Share of first tests where a bar closed " + str.tostring(brk_atr) + "× ATR beyond the level. The remainder are undecided (timed out).")
f_lvl_row(2, 0, bgA)
f_lvl_row(3, 1, bgB)
f_lvl_row(4, 2, bgA)
f_lvl_row(5, 3, bgB)
table.cell(hud, 0, 6, "ADR (" + str.tostring(adr_len) + "d)", text_color=C_DIM, bgcolor=bgA, text_size=size.small)
table.cell(hud, 1, 6, na(adr) ? "—" : str.tostring(adr, format.mintick), text_color=C_TXT, bgcolor=bgA, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 2, 6, "Today used", text_color=C_DIM, bgcolor=bgA, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 3, 6, na(adr_used) ? "—" : str.tostring(math.round(adr_used)) + "%",
text_color = na(adr_used) ? C_DIM : adr_used >= 100 ? C_BAD : adr_used >= 75 ? C_AMBER : C_GOOD,
bgcolor=bgA, text_size=size.small, text_halign=text.align_center)
table.merge_cells(hud, 3, 6, 4, 6)
string tf_note = timeframe.in_seconds() >= timeframe.in_seconds("D") ? "Use an intraday chart for daily levels" : "Resolve window " + str.tostring(horizon) + " bars"
table.cell(hud, 0, 7, tf_note, text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
table.merge_cells(hud, 0, 7, 4, 7)
// ALERTS
alertcondition(ta.crossover(high, pdh) and not nd, "Price Reached PDH", "D3R Key Levels: price reached the previous day high")
alertcondition(ta.crossunder(low, pdl) and not nd, "Price Reached PDL", "D3R Key Levels: price reached the previous day low")
alertcondition(ta.crossover(high, pwh) and not nw, "Price Reached PWH", "D3R Key Levels: price reached the previous week high")
alertcondition(ta.crossunder(low, pwl) and not nw, "Price Reached PWL", "D3R Key Levels: price reached the previous week low")
alertcondition(adr_used >= 100 and adr_used[1] < 100, "ADR Fully Used", "D3R Key Levels: today's range has reached the average daily range")