本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ProofCharts
//@version=6
indicator("Which Levels Matter Today? Key Levels Explained", shorttitle = "Key Levels Explained", overlay = true, max_lines_count = 500, max_labels_count = 6)
// Draws yesterday's high, low and close, today's open and last week's high and low, each with a short plain
// explanation (hover the labels), marks the ones price touched today, and says where price is between them.
// Built only from this chart's own intraday candles. The first day and week on the chart may be cut off, so they
// are skipped. A level never changes during its day, and a closed candle's values never change afterwards.
G_LOOK = "Display"
iDays = input.int(10, "Days of lines kept", minval = 1, maxval = 80, group = G_LOOK, tooltip = "Each day gets its own set of lines, drawn from the day's first candle. Older days are removed.")
iWeek = input.bool(true, "Show last week's high and low", group = G_LOOK, tooltip = "Lines for the high and low of the previous week.")
iPanel = input.bool(true, "Panel", group = G_LOOK, tooltip = "Box with one sentence about where price is and a list of the levels.")
iPos = input.string("Top right", "Panel position", options = ["Top right", "Bottom right", "Bottom left", "Top left"], group = G_LOOK, tooltip = "Where the panel sits on the chart.", active = iPanel)
// ---------- Calculation ----------
ATR_LEN = 14
REL = 1e-9 // price equality: |a - b| <= 1e-9 * |b| counts as equal (removes float noise)
float atr = ta.atr(ATR_LEN)
bool intraday = not timeframe.isdwm
bool newDay = bar_index == 0 or timeframe.change("1D")
bool newWk = bar_index == 0 or timeframe.change("1W")
var int dayNo = 0
var int wkNo = 0
var float dH = na
var float dL = na
var float dC = na
var float wH = na
var float wL = na
var float PDH = na
var float PDL = na
var float PDC = na
var float TO = na
var float PWH = na
var float PWL = na
var int dayStart = 0
if newWk
if bar_index > 0
if wkNo >= 1
PWH := wH
PWL := wL
wkNo += 1
wH := high
wL := low
else
wH := math.max(wH, high)
wL := math.min(wL, low)
if newDay
if bar_index > 0
if dayNo >= 1
PDH := dH
PDL := dL
PDC := dC
dayNo += 1
TO := open
dH := high
dL := low
dayStart := bar_index
else
dH := math.max(dH, high)
dL := math.min(dL, low)
dC := close
// levels in a fixed order: 0 PDH, 1 PDL, 2 PDC, 3 TO, 4 PWH, 5 PWL (bit = 2^index)
float[] lv = array.from(PDH, PDL, PDC, TO, PWH, PWL)
int touched = 0
int atBits = 0
float above = na
float below = na
if intraday
for i = 0 to 5
float v = array.get(lv, i)
if not na(v)
int bit = int(math.pow(2, i))
float tol = REL * math.abs(v)
if dayNo >= 1 and dL - tol <= v and v <= dH + tol
touched += bit
if math.abs(close - v) <= tol
atBits += bit
else if v > close and (na(above) or v < above)
above := v
else if v < close and (na(below) or v > below)
below := v
int aboveBits = 0
int belowBits = 0
if intraday
for i = 0 to 5
float v = array.get(lv, i)
if not na(v) and not na(above) and v == above
aboveBits += int(math.pow(2, i))
if not na(v) and not na(below) and v == below
belowBits += int(math.pow(2, i))
float distUp = not na(above) and not na(atr) and atr > 0 ? (above - close) / atr : na
float distDn = not na(below) and not na(atr) and atr > 0 ? (close - below) / atr : na
// ---------- Look ----------
color C_PANEL = #0f172a
color C_HEAD = #1e293b
color C_TEXT = #e2e8f0
color C_MUTED = #94a3b8
color C_GREEN = #22c55e
color C_RED = #ef4444
color C_AMBER = #f59e0b
color C_PURPLE = #a855f7
color C_SKY = #38bdf8
color[] COLS = array.from(C_GREEN, C_RED, C_MUTED, C_AMBER, C_PURPLE, C_SKY)
string[] NAMES = array.from("yesterday's high", "yesterday's low", "yesterday's close", "today's open", "last week's high", "last week's low")
string[] SHORT = array.from("Yesterday's high", "Yesterday's low", "Yesterday's close", "Today's open", "Last week's high", "Last week's low")
string[] TIPS = array.from("Yesterday's high: the top of yesterday's range. Many traders mark it as a reference.", "Yesterday's low: the bottom of yesterday's range. Many traders mark it as a reference.", "Yesterday's close: the last price of yesterday's last candle.", "Today's open: the first price of today's first candle.", "Last week's high: the top of last week's range.", "Last week's low: the bottom of last week's range.")
// ---------- Values in the Data Window (also used by the tests) ----------
plot(intraday ? PDH : na, "Yesterday's high", display = display.data_window)
plot(intraday ? PDL : na, "Yesterday's low", display = display.data_window)
plot(intraday ? PDC : na, "Yesterday's close", display = display.data_window)
plot(intraday ? TO : na, "Today's open", display = display.data_window)
plot(intraday ? PWH : na, "Last week's high", display = display.data_window)
plot(intraday ? PWL : na, "Last week's low", display = display.data_window)
plot(touched, "Touched today (bits: 1 PDH 2 PDL 4 PDC 8 TO 16 PWH 32 PWL)", display = display.data_window)
plot(atBits, "Price at level (bits)", display = display.data_window)
plot(aboveBits, "Nearest level above (bits)", display = display.data_window)
plot(belowBits, "Nearest level below (bits)", display = display.data_window)
plot(distUp, "Distance to the level above (average candles)", display = display.data_window)
plot(distDn, "Distance to the level below (average candles)", display = display.data_window)
// ---------- Lines: one set per day, from the day's first candle ----------
var line[] lns = array.new<line>()
var int[] lnDay = array.new_int()
var line[] today = array.new<line>()
if intraday and newDay
array.clear(today)
for i = 0 to 5
float v = array.get(lv, i)
if not na(v) and (iWeek or i < 4)
line ln = line.new(bar_index, v, bar_index, v, color = color.new(array.get(COLS, i), i == 2 ? 30 : 10), width = i >= 4 ? 2 : 1, style = i == 2 ? line.style_dotted : i == 3 ? line.style_dashed : line.style_solid)
array.push(lns, ln)
array.push(lnDay, dayNo)
array.push(today, ln)
while array.size(lnDay) > 0 and array.get(lnDay, 0) <= dayNo - iDays
line.delete(array.shift(lns))
array.shift(lnDay)
if intraday and array.size(today) > 0
for ln in today
line.set_x2(ln, bar_index)
// ---------- Labels and panel (last candle) ----------
bitOn(int bits, int i) => int(bits / int(math.pow(2, i))) % 2 == 1
namesOf(int bits) =>
string s = ""
for i = 0 to 5
if bitOn(bits, i)
s += (s == "" ? "" : " = ") + array.get(NAMES, i)
s
px(float v) => str.tostring(v, format.mintick)
whereSentence() =>
string s = ""
bool any = false
for i = 0 to 5
if not na(array.get(lv, i))
any := true
if not any
s := "Waiting for a full day on the chart: levels start on the second new day."
else
string lead = "Price is "
if atBits > 0
s := "Price is at " + namesOf(atBits) + "."
lead := " It is "
if aboveBits > 0 and belowBits > 0
s += lead + "between " + namesOf(belowBits) + " and " + namesOf(aboveBits) + (na(distUp) ? "" : ", " + str.tostring(distUp, "0.0") + " average candles below " + namesOf(aboveBits)) + "."
else if aboveBits > 0
s += lead + "below all levels shown" + (na(distUp) ? "" : ", " + str.tostring(distUp, "0.0") + " average candles below " + namesOf(aboveBits)) + "."
else if belowBits > 0
s += lead + "above all levels shown" + (na(distDn) ? "" : ", " + str.tostring(distDn, "0.0") + " average candles above " + namesOf(belowBits)) + "."
s
var label[] labs = array.new<label>()
pos = iPos == "Bottom right" ? position.bottom_right : iPos == "Bottom left" ? position.bottom_left : iPos == "Top left" ? position.top_left : position.top_right
// Breaks a text into lines of at most n characters, at spaces only (no word is changed), so the panel fits the chart
// of a phone held upright (compliance/phone_fit.py; owner's Android screenshot 2026-11-03: wide panels were cut off).
wrapText(string s, int n) =>
string out = ""
int len = -1
for w in str.split(s, " ")
int wl = str.length(w)
if len < 0
out := w
len := wl
else if len + 1 + wl > n
out := out + "\n" + w
len := wl
else
out := out + " " + w
len := len + 1 + wl
out
var table tb = table.new(pos, 3, 8, bgcolor = color.new(C_PANEL, 5), frame_color = color.new(C_MUTED, 70), frame_width = 1)
var bool merged = false
if barstate.islast
while array.size(labs) > 0
label.delete(array.pop(labs))
if intraday
// one label per price: levels at the same price share a label ("Yesterday's close = Today's open")
for i = 0 to 5
float v = array.get(lv, i)
bool dup = false
if i > 0
for j = 0 to i - 1
if not na(array.get(lv, j)) and (iWeek or j < 4) and array.get(lv, j) == v
dup := true
if not na(v) and (iWeek or i < 4) and not dup
string txt = array.get(SHORT, i)
string tip = array.get(TIPS, i)
if i < 5
for j = i + 1 to 5
if (iWeek or j < 4) and array.get(lv, j) == v
txt += " = " + array.get(SHORT, j)
tip += " " + array.get(TIPS, j)
bool tch = bitOn(touched, i)
array.push(labs, label.new(bar_index + 3, v, txt + (tch ? " ✓" : ""), style = label.style_label_left, color = color.new(array.get(COLS, i), 25), textcolor = i == 2 ? C_PANEL : color.white, size = size.small, tooltip = tip + (tch ? " Touched today." : " Not touched today.")))
if iPanel
if not merged
table.merge_cells(tb, 0, 0, 2, 0)
table.merge_cells(tb, 0, 1, 2, 1)
merged := true
table.cell(tb, 0, 0, wrapText("KEY LEVELS EXPLAINED" + (barstate.isconfirmed ? "" : " · live"), 30), text_color = color.white, bgcolor = C_HEAD, text_size = size.small, text_halign = text.align_left)
table.cell(tb, 0, 1, wrapText(intraday ? whereSentence() : "Use an intraday chart (for example 5, 15 or 60 minutes).", 30), text_color = C_TEXT, text_size = size.small, text_halign = text.align_left, tooltip = "Average candle = ATR 14. A level equal to the close counts as 'at' the level.")
for i = 0 to 5
float v = array.get(lv, i)
bool show = intraday and not na(v) and (iWeek or i < 4)
bool tch = bitOn(touched, i)
table.cell(tb, 0, i + 2, show ? array.get(SHORT, i) : "", text_color = array.get(COLS, i), text_size = size.small, text_halign = text.align_left, tooltip = array.get(TIPS, i))
table.cell(tb, 1, i + 2, show ? px(v) : "", text_color = C_TEXT, text_size = size.small)
table.cell(tb, 2, i + 2, show ? (tch ? "✓ touched today" : "not yet today") : "", text_color = tch ? C_TEXT : C_MUTED, text_size = size.tiny)