本页源码来自 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/
// © Indicatr
//
// ADR RANGE METER
// How much of the average daily range (ADR) has today already used, where the day would end
// if it reached a full ADR from here, whether today is ahead of or behind its usual pace for
// this time of day, and how often each weekday actually reached its ADR.
indicator("ADR Range Meter [Indicatr]", "ADR Meter", overlay = true, max_labels_count = 50)
// ════════════════════════════════════════════════════════════════════════════════════
// INPUTS
// ════════════════════════════════════════════════════════════════════════════════════
G_R = "1 · Range"
i_len = input.int(14, "ADR length (days)", minval = 2, maxval = 100, group = G_R, tooltip = "Average high-low range of the last completed days. Built from the chart's own bars, no request.security.")
i_day = input.string("Symbol session", "Day boundary", options = ["Symbol session", "Midnight exchange time", "Midnight UTC", "Midnight New York", "Midnight London", "Midnight Berlin"], group = G_R, tooltip = "Symbol session = the same days as the symbol's daily candles (forex and CFDs start at 17:00 New York). The midnight options cut the day at 00:00 in that time zone.")
i_warn = input.int(75, "Warning level (% of ADR)", minval = 10, maxval = 99, group = G_R)
G_C = "2 · Chart"
i_proj = input.bool(true, "ADR targets", group = G_C, tooltip = "Upper target = today's low + ADR. Lower target = today's high - ADR. They close in on price while the day develops. The day has used a full ADR when they meet today's range.")
i_zone = input.bool(true, "Shade room left to the targets", group = G_C)
i_range = input.bool(true, "Today's high and low", group = G_C)
i_mark = input.bool(true, "Mark the bar that completes the ADR", group = G_C)
i_ext = input.bool(false, "150% extension", group = G_C)
i_exh = input.bool(true, "Tint the rest of the day once the ADR is used up", group = G_C)
i_lbls = input.bool(true, "Price labels at the last bar", group = G_C)
i_txt = input.string("Small", "Label size", options = ["Tiny", "Small", "Normal"], group = G_C)
i_colUp = input.color(#089981, "Upper", inline = "c", group = G_C)
i_colDn = input.color(#f23645, "Lower", inline = "c", group = G_C)
i_colEx = input.color(#ff9800, "Used up", inline = "c", group = G_C)
G_D = "3 · Dashboard"
i_dbShow = input.bool(true, "Show dashboard", group = G_D)
i_theme = input.string("Auto", "Theme", options = ["Auto", "Light", "Dark"], group = G_D)
i_dbPos = input.string("Top right", "Position", options = ["Top right", "Top left", "Bottom right", "Bottom left", "Middle right", "Middle left"], group = G_D)
i_dbSize = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal", "Large"], group = G_D)
// ════════════════════════════════════════════════════════════════════════════════════
// THEME
// ════════════════════════════════════════════════════════════════════════════════════
float bgLum = (0.299 * color.r(chart.bg_color) + 0.587 * color.g(chart.bg_color) + 0.114 * color.b(chart.bg_color)) / 255.0
bool DARK = i_theme == "Dark" or (i_theme == "Auto" and bgLum < 0.5)
color C_BG = DARK ? #1e1e28 : #ffffff
color C_HEAD = DARK ? #2a2a3c : #f0f3fa
color C_SECT = DARK ? #24243a : #f8f9fd
color C_BORDER = DARK ? #34344a : #e6e8ef
color C_FRAME = DARK ? #4a4a60 : #d1d4dc
color C_TXT = DARK ? #e9e9f0 : #131722
color C_DIM = DARK ? #9a9cab : #787b86
color C_BULL = DARK ? #26a69a : #089981
color C_BEAR = DARK ? #ef5350 : #f23645
color C_WARN = #ff9800
color C_EMPTY = DARK ? #2e2e42 : #eceef4
color C_RNG = DARK ? #8a8ca0 : #9598a1
f_size(string s) =>
switch s
"Tiny" => size.tiny
"Normal" => size.normal
"Large" => size.large
=> size.small
f_pos(string s) =>
switch s
"Top left" => position.top_left
"Bottom right" => position.bottom_right
"Bottom left" => position.bottom_left
"Middle right" => position.middle_right
"Middle left" => position.middle_left
=> position.top_right
// Usage colour: calm below the warning level, orange up to 100 %, red above
f_useCol(float pct) => pct >= 100 ? C_BEAR : pct >= i_warn ? C_WARN : C_BULL
// ════════════════════════════════════════════════════════════════════════════════════
// DAILY RANGES FROM THE CHART'S OWN BARS
// ════════════════════════════════════════════════════════════════════════════════════
bool SESS = i_day == "Symbol session"
string TZ = switch i_day
"Midnight UTC" => "UTC"
"Midnight New York" => "America/New_York"
"Midnight London" => "Europe/London"
"Midnight Berlin" => "Europe/Berlin"
=> syminfo.timezone
bool intraday = timeframe.isintraday
int dayKey = SESS ? time_tradingday : year(time, TZ) * 10000 + month(time, TZ) * 100 + dayofmonth(time, TZ)
bool newDay = dayKey != dayKey[1]
var float dHi = na
var float dLo = na
var int dT0 = na
var int dDow = na
var float adr = na
var float[] ranges = array.new_float()
// weekday statistics (index 0 = Sunday ... 6 = Saturday): days and days that reached the ADR
var int[] wdDays = array.new_int(7, 0)
var int[] wdHits = array.new_int(7, 0)
var int allDays = 0
var int allHits = 0
// pace: for every hour since the day started, the average share of the final range done by then
var float[] hrSnap = array.new_float(24, na)
var float[] paceSum = array.new_float(24, 0.0)
var int[] paceCnt = array.new_int(24, 0)
if newDay and not na(dHi)
float rng = dHi - dLo
// statistics for the day that just ended, measured against the ADR known at its start
if not na(adr) and adr > 0
int w = dDow - 1
array.set(wdDays, w, array.get(wdDays, w) + 1)
allDays += 1
if rng >= adr
array.set(wdHits, w, array.get(wdHits, w) + 1)
allHits += 1
if rng > 0
float lastSnap = 0.0
for h = 0 to 23
float s = array.get(hrSnap, h)
lastSnap := na(s) ? lastSnap : s
array.set(paceSum, h, array.get(paceSum, h) + lastSnap / rng)
array.set(paceCnt, h, array.get(paceCnt, h) + 1)
array.push(ranges, rng)
if array.size(ranges) > i_len
array.shift(ranges)
adr := array.size(ranges) >= math.min(i_len, 3) ? array.avg(ranges) : na
if newDay
dHi := high
dLo := low
dT0 := time
dDow := SESS ? dayofweek(time_tradingday, "UTC") : dayofweek(time, TZ)
array.fill(hrSnap, na)
else
dHi := math.max(dHi, high)
dLo := math.min(dLo, low)
// hours since the day started (works for sessions that begin at any clock time)
int hIdx = math.max(0, math.min(23, int((time_close - 1 - dT0) / 3600000)))
array.set(hrSnap, hIdx, dHi - dLo)
int hourNow = hour(time_close - 1, TZ)
float dRange = dHi - dLo
float usedPct = adr > 0 ? dRange / adr * 100 : na
bool done = usedPct >= 100
float projHi = dLo + adr
float projLo = dHi - adr
float paceExp = array.get(paceCnt, hIdx) > 0 ? array.get(paceSum, hIdx) / array.get(paceCnt, hIdx) * 100 : na
// today's progress against a typical day: share of the ADR done vs. typical share done by this hour
float paceDiff = na(usedPct) or na(paceExp) ? na : usedPct - paceExp
// ════════════════════════════════════════════════════════════════════════════════════
// CHART
// ════════════════════════════════════════════════════════════════════════════════════
NOSCALE = display.all - display.price_scale
bool show = intraday and not na(adr) and not newDay // break every line at the new day
bool live = show and not done
float tHi = i_proj and live ? projHi : na
float tLo = i_proj and live ? projLo : na
color rngC = done ? color.new(i_colEx, 15) : color.new(C_RNG, 35)
// today's high / low (orange once the full ADR is used)
pHiR = plot(show ? dHi : na, "Day high", i_range ? rngC : na, 1, plot.style_linebr, display = NOSCALE)
pLoR = plot(show ? dLo : na, "Day low", i_range ? rngC : na, 1, plot.style_linebr, display = NOSCALE)
// ADR targets: soft glow under a crisp line
plot(tHi, "Upper target glow", color.new(i_colUp, 85), 7, plot.style_linebr, display = display.pane)
plot(tLo, "Lower target glow", color.new(i_colDn, 85), 7, plot.style_linebr, display = display.pane)
pHiT = plot(tHi, "Upper ADR target", i_colUp, 2, plot.style_linebr, display = NOSCALE)
pLoT = plot(tLo, "Lower ADR target", i_colDn, 2, plot.style_linebr, display = NOSCALE)
// room left: gradient that is strongest at the target and fades towards today's range
fill(pHiT, pHiR, i_zone ? tHi : na, i_zone ? dHi : na, color.new(i_colUp, 62), color.new(i_colUp, 96), "Room up")
fill(pLoR, pLoT, i_zone ? dLo : na, i_zone ? tLo : na, color.new(i_colDn, 96), color.new(i_colDn, 62), "Room down")
// 150 % extension
plot(i_ext and show ? dLo + 1.5 * adr : na, "Upper 150%", color.new(i_colUp, 50), 1, plot.style_linebr, display = NOSCALE)
plot(i_ext and show ? dHi - 1.5 * adr : na, "Lower 150%", color.new(i_colDn, 50), 1, plot.style_linebr, display = NOSCALE)
// the bar that completes a full ADR, marked on the side that did it
bool hitFull = intraday and not newDay and done and not done[1]
bool hitUp = hitFull and high >= dHi
plotshape(i_mark and hitUp, "ADR complete (high)", shape.labeldown, location.abovebar, i_colEx, text = "ADR ✓", textcolor = color.white, size = size.tiny)
plotshape(i_mark and hitFull and not hitUp, "ADR complete (low)", shape.labelup, location.belowbar, i_colEx, text = "ADR ✓", textcolor = color.white, size = size.tiny)
bgcolor(intraday and i_exh and done and not newDay ? color.new(i_colEx, 94) : na, title = "ADR used up")
// labels at the last bar
var label lbHi = na
var label lbLo = na
if barstate.islast
label.delete(lbHi)
label.delete(lbLo)
if intraday and i_lbls and not na(adr)
string LS = f_size(i_txt)
int xL = bar_index + 2
if not done
lbHi := label.new(xL, projHi, "ADR HIGH " + str.tostring(projHi, format.mintick) + " +" + str.tostring(projHi - close, format.mintick), color = i_colUp, textcolor = color.white, style = label.style_label_left, size = LS)
lbLo := label.new(xL, projLo, "ADR LOW " + str.tostring(projLo, format.mintick) + " −" + str.tostring(close - projLo, format.mintick), color = i_colDn, textcolor = color.white, style = label.style_label_left, size = LS)
else
bool upSide = close - dLo >= dHi - close
lbHi := label.new(xL, upSide ? dHi : dLo, "ADR DONE · " + str.tostring(math.round(usedPct)) + "%", color = i_colEx, textcolor = color.white, style = label.style_label_left, size = LS)
// ════════════════════════════════════════════════════════════════════════════════════
// DASHBOARD
// ════════════════════════════════════════════════════════════════════════════════════
var table db = table.new(f_pos(i_dbPos), 10, 16, bgcolor = C_BG, frame_color = C_FRAME, frame_width = 1, border_color = C_BORDER, border_width = 1)
float COLW = i_dbSize == "Large" ? 2.2 : i_dbSize == "Normal" ? 1.8 : i_dbSize == "Small" ? 1.5 : 1.25
f_cell(int c, int r, string t, color tc, color bg, string sz) =>
table.cell(db, c, r, t, width = COLW, text_color = tc, bgcolor = bg, text_size = sz)
f_row(int r, string k, string v, color vc, string sz) =>
table.cell(db, 0, r, k, text_color = C_DIM, bgcolor = C_BG, text_halign = text.align_left, text_size = sz)
table.merge_cells(db, 0, r, 4, r)
table.cell(db, 5, r, v, text_color = vc, bgcolor = C_BG, text_halign = text.align_right, text_size = sz)
table.merge_cells(db, 5, r, 9, r)
f_head(int r, string t, string sz) =>
table.cell(db, 0, r, t, text_color = C_DIM, bgcolor = C_HEAD, text_size = sz)
table.merge_cells(db, 0, r, 9, r)
f_pct(int a, int b) => b > 0 ? str.tostring(math.round(a / b * 100)) + "%" : "—"
if barstate.islast and i_dbShow
table.clear(db, 0, 0, 9, 15)
string sz = f_size(i_dbSize)
string szS = i_dbSize == "Large" ? size.normal : i_dbSize == "Normal" ? size.small : size.tiny
int r = 0
table.cell(db, 0, r, "ADR RANGE METER", text_color = C_TXT, bgcolor = C_HEAD, text_size = sz)
table.merge_cells(db, 0, r, 9, r)
r += 1
if not intraday
table.cell(db, 0, r, "Use an intraday chart (1 min to 4 h)", text_color = C_DIM, bgcolor = C_BG, text_size = sz)
table.merge_cells(db, 0, r, 9, r)
else if na(adr)
table.cell(db, 0, r, "Collecting daily ranges …", text_color = C_DIM, bgcolor = C_BG, text_size = sz)
table.merge_cells(db, 0, r, 9, r)
else
color uc = f_useCol(usedPct)
string state = usedPct >= 100 ? "ADR REACHED" : usedPct >= i_warn ? "LATE IN THE RANGE" : "ROOM TO RUN"
table.cell(db, 0, r, str.tostring(math.round(usedPct)) + "% OF ADR USED · " + state, text_color = color.white, bgcolor = uc, text_size = sz)
table.merge_cells(db, 0, r, 9, r)
r += 1
// gauge: ten segments, filled up to today's usage (more than 100 % stays full)
int filled = int(math.min(10, math.round(usedPct / 10)))
for k = 0 to 9
color seg = k < filled ? color.new(f_useCol((k + 1) * 10.0), 15 + (9 - k) * 3) : C_EMPTY
f_cell(k, r, " ", C_TXT, seg, szS)
r += 1
f_row(r, "ADR " + str.tostring(i_len) + " days", str.tostring(adr, format.mintick) + " (" + str.tostring(adr / close * 100, "0.00") + "%)", C_TXT, sz)
r += 1
f_row(r, "Today's range", str.tostring(dRange, format.mintick), C_TXT, sz)
r += 1
f_row(r, "To ADR high", usedPct >= 100 ? "reached" : str.tostring(projHi - close, format.mintick) + " ↑", usedPct >= 100 ? C_DIM : C_BULL, sz)
r += 1
f_row(r, "To ADR low", usedPct >= 100 ? "reached" : str.tostring(close - projLo, format.mintick) + " ↓", usedPct >= 100 ? C_DIM : C_BEAR, sz)
r += 1
string paceTxt = na(paceDiff) ? "—" : str.tostring(math.round(paceExp)) + "% typical · " + (paceDiff >= 0 ? "ahead +" : "behind ") + str.tostring(math.round(paceDiff)) + "%"
f_row(r, "Pace at " + str.format("{0,number,00}:00", hourNow), paceTxt, na(paceDiff) ? C_DIM : paceDiff >= 0 ? C_WARN : C_TXT, sz)
r += 1
f_head(r, "FULL ADR REACHED · " + str.tostring(allDays) + " DAYS", szS)
r += 1
f_row(r, "All days", f_pct(allHits, allDays), C_TXT, sz)
r += 1
// weekday row: Mo-Fr (Sa/Su only if the market trades on weekends)
bool wknd = array.get(wdDays, 0) + array.get(wdDays, 6) > 0
string[] wn = array.from("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa")
int[] order = wknd ? array.from(1, 2, 3, 4, 5, 6, 0) : array.from(1, 2, 3, 4, 5)
int n = array.size(order)
int span = 10 / n
for j = 0 to n - 1
int w = array.get(order, j)
int c0 = j * span
int c1 = j == n - 1 ? 9 : c0 + span - 1
bool today = w == dDow - 1
int dd = array.get(wdDays, w)
float hr = dd > 0 ? array.get(wdHits, w) / dd * 100 : na
color hc = na(hr) ? C_DIM : hr >= 60 ? C_BULL : hr >= 40 ? C_TXT : C_BEAR
table.cell(db, c0, r, array.get(wn, w), text_color = today ? C_TXT : C_DIM, bgcolor = today ? C_HEAD : C_SECT, text_size = szS)
table.cell(db, c0, r + 1, na(hr) ? "—" : str.tostring(math.round(hr)) + "%", text_color = hc, bgcolor = today ? C_HEAD : C_BG, text_size = sz)
if c1 > c0
table.merge_cells(db, c0, r, c1, r)
table.merge_cells(db, c0, r + 1, c1, r + 1)
r += 2
table.cell(db, 0, r, (SESS ? "Days = symbol session" : "Days from " + i_day) + " · stats from the chart's history", text_color = C_DIM, bgcolor = C_SECT, text_size = szS)
table.merge_cells(db, 0, r, 9, r)
// ════════════════════════════════════════════════════════════════════════════════════
// ALERTS
// ════════════════════════════════════════════════════════════════════════════════════
bool hitWarn = intraday and not newDay and usedPct >= i_warn and usedPct[1] < i_warn
alertcondition(hitWarn, "ADR warning level reached", "ADR Range Meter: {{ticker}} has used the warning share of its average daily range")
alertcondition(hitFull, "Full ADR reached", "ADR Range Meter: {{ticker}} has used 100% of its average daily range")