本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Average Daily Range Base Rates", "ADR Stats", overlay = true, max_labels_count = 500, max_lines_count = 50)
// Average Daily Range (ADR) with base rates: how often a day covered a given share of its ADR,
// and how often price moved a given share of the ADR away from the day's open.
// All statistics are computed on completed daily bars, so they are the same on every chart timeframe.
// By default the last 500 days are counted; the window can be changed or switched to the whole history.
// Statistics only, no trading advice.
lang = input.string("English", "Language / Sprache", options = ["English", "Deutsch"], display = display.none)
len = input.int(10, "ADR length in days", minval = 2, display = display.none)
lookback = input.int(500, "Statistics over the last N days (0 = whole history)", minval = 0, maxval = 5000, step = 50, display = display.none)
showLv = input.bool(true, "Show levels from the day's open (intraday charts)", display = display.none)
showHist = input.bool(false, "Show levels of past days", display = display.none)
lblMode = input.string("Latest day", "Level labels", options = ["Latest day", "All days", "Off"], display = display.none)
lw = input.int(2, "Line width", minval = 1, maxval = 4, display = display.none)
tblPos = input.string(position.top_right, "Table position", options = [position.top_right, position.bottom_right, position.top_left, position.bottom_left], display = display.none)
color cOpen = #d1d4dc
color cUp = #26a69a
color cDn = #ef5350
bool en = lang == "English"
L(string de, string eng) =>
en ? eng : de
if timeframe.in_seconds() > 86400
runtime.error("Please use a daily or intraday timeframe.")
// ---- Statistics on daily bars -------------------------------------------------------------
// adr = average range of the n days BEFORE the current day, so each day is compared
// with an ADR that was already known at its open. The counts are returned with [1],
// which means only completed days are counted.
// Count inside a rolling window of w daily bars; w = 0 means the whole history
win(float c, int w) =>
w > 0 ? c - nz(c[w]) : c
f_stats(int n, int w) =>
float r = high - low
float adr = ta.sma(r, n)[1]
bool ok = not na(adr) and adr > 0
float q = ok ? r / adr : na
float ex = ok ? math.max(high - open, open - low) / adr : na
var int t0 = na
if ok and na(t0)
t0 := time
float days = win(ta.cum(ok ? 1 : 0), w)
float r50 = win(ta.cum(ok and q >= 0.50 ? 1 : 0), w)
float r75 = win(ta.cum(ok and q >= 0.75 ? 1 : 0), w)
float r100 = win(ta.cum(ok and q >= 1.00 ? 1 : 0), w)
float r125 = win(ta.cum(ok and q >= 1.25 ? 1 : 0), w)
float r150 = win(ta.cum(ok and q >= 1.50 ? 1 : 0), w)
float o50 = win(ta.cum(ok and ex >= 0.50 ? 1 : 0), w)
float o100 = win(ta.cum(ok and ex >= 1.00 ? 1 : 0), w)
// first day inside the window (the window ends with the last completed day)
int tFirst = w > 0 and not na(time[w]) ? math.max(time[w], nz(t0)) : t0
[days[1], r50[1], r75[1], r100[1], r125[1], r150[1], o50[1], o100[1], tFirst, adr]
[nDays, n50, n75, n100, n125, n150, m50, m100, tStart, adrToday] = request.security(syminfo.tickerid, "D", f_stats(len, lookback), lookahead = barmerge.lookahead_on)
// ---- Current day on the chart ---------------------------------------------------------------
bool newDay = ta.change(time("D")) != 0
var float dOpen = na
var float dHi = na
var float dLo = na
if newDay
dOpen := open
dHi := high
dLo := low
else if not na(dOpen)
dHi := math.max(dHi, high)
dLo := math.min(dLo, low)
bool drawLv = showLv and timeframe.isintraday and not na(dOpen) and not na(adrToday)
color hide = color.new(color.black, 100)
bool histLv = drawLv and showHist
// Past days switched on: plots, so the whole history stays visible
plot(histLv ? dOpen : na, "Day open", newDay ? hide : color.new(cOpen, 30), 1)
plot(histLv ? dOpen + 0.5 * adrToday : na, "Open +0.5 x ADR", newDay ? hide : color.new(cUp, 20), lw, linestyle = plot.linestyle_dashed)
plot(histLv ? dOpen + adrToday : na, "Open +1 x ADR", newDay ? hide : cUp, lw)
plot(histLv ? dOpen - 0.5 * adrToday : na, "Open -0.5 x ADR", newDay ? hide : color.new(cDn, 20), lw, linestyle = plot.linestyle_dashed)
plot(histLv ? dOpen - adrToday : na, "Open -1 x ADR", newDay ? hide : cDn, lw)
// Past days switched off: line objects for the latest day only, older ones are removed
var line[] lns = array.new<line>()
ln(float y, color c, int w, string st) =>
line.new(bar_index, y, bar_index, y, color = c, width = w, style = st)
if drawLv and not showHist
if newDay
for l in lns
line.delete(l)
array.clear(lns)
array.push(lns, ln(dOpen, color.new(cOpen, 30), 1, line.style_solid))
array.push(lns, ln(dOpen + 0.5 * adrToday, color.new(cUp, 20), lw, line.style_dashed))
array.push(lns, ln(dOpen + adrToday, cUp, lw, line.style_solid))
array.push(lns, ln(dOpen - 0.5 * adrToday, color.new(cDn, 20), lw, line.style_dashed))
array.push(lns, ln(dOpen - adrToday, cDn, lw, line.style_solid))
else
for l in lns
line.set_x2(l, bar_index)
// Light labels at the right end of the levels; they move along during the day
var label[] cur = array.new<label>()
mk(float y, string txt, color c) =>
label.new(bar_index + 1, y, txt, style = label.style_label_left, color = color.new(color.black, 100), textcolor = color.new(c, 25), size = size.small)
if drawLv and newDay and lblMode != "Off"
if lblMode == "Latest day" or not showHist
for lb in cur
label.delete(lb)
array.clear(cur)
array.push(cur, mk(dOpen + adrToday, "+1 × ADR", cUp))
array.push(cur, mk(dOpen + 0.5 * adrToday, "+0.5 × ADR", cUp))
array.push(cur, mk(dOpen, L("Eröffnung", "Open"), cOpen))
array.push(cur, mk(dOpen - 0.5 * adrToday, "−0.5 × ADR", cDn))
array.push(cur, mk(dOpen - adrToday, "−1 × ADR", cDn))
else if drawLv
for lb in cur
label.set_x(lb, bar_index + 1)
// Raw counts in the data window (for checking)
plot(nDays, "Days", display = display.data_window)
plot(n50, "Days range >= 50% ADR", display = display.data_window)
plot(n75, "Days range >= 75% ADR", display = display.data_window)
plot(n100, "Days range >= 100% ADR", display = display.data_window)
plot(n125, "Days range >= 125% ADR", display = display.data_window)
plot(n150, "Days range >= 150% ADR", display = display.data_window)
plot(m50, "Days move from open >= 0.5 x ADR", display = display.data_window)
plot(m100, "Days move from open >= 1 x ADR", display = display.data_window)
plot(adrToday, "ADR", display = display.data_window)
pct(float a, float b) =>
b > 0 ? str.tostring(100.0 * a / b, "#.0") + " %" : "–"
row(table tb, int r, string label, string value) =>
table.cell(tb, 0, r, label, text_color = color.silver, text_halign = text.align_left)
table.cell(tb, 1, r, value, text_color = color.white, text_halign = text.align_right)
head(table tb, int r, string label) =>
table.cell(tb, 0, r, label, text_color = color.gray, text_halign = text.align_left, text_size = size.small)
table.cell(tb, 1, r, "")
var table t = table.new(tblPos, 2, 15, bgcolor = color.new(color.black, 20), border_width = 1, border_color = color.new(color.gray, 70))
if barstate.islast
table.cell(t, 0, 0, L("ADR-Basisraten", "ADR base rates"), text_color = color.white, text_halign = text.align_left)
table.cell(t, 1, 0, str.tostring(nDays, "#") + L(" Tage", " days"), text_color = color.white, text_halign = text.align_right)
row(t, 1, L("seit", "since"), na(tStart) ? "–" : str.format_time(tStart, "yyyy-MM-dd", syminfo.timezone))
row(t, 2, "ADR(" + str.tostring(len) + ")", na(adrToday) ? "–" : str.tostring(adrToday, format.mintick))
row(t, 3, L("ADR in % vom Kurs", "ADR in % of price"), na(adrToday) ? "–" : str.tostring(100.0 * adrToday / close, "#.00") + " %")
row(t, 4, L("Heute bisher", "Today so far"), na(dHi) or na(adrToday) ? "–" : str.tostring(100.0 * (dHi - dLo) / adrToday, "#") + L(" % des ADR", " % of ADR"))
head(t, 5, L("Tagesspanne erreichte", "Day range reached"))
row(t, 6, L("≥ 50 % des ADR", "≥ 50 % of ADR"), pct(n50, nDays))
row(t, 7, L("≥ 75 % des ADR", "≥ 75 % of ADR"), pct(n75, nDays))
row(t, 8, L("≥ 100 % des ADR", "≥ 100 % of ADR"), pct(n100, nDays))
row(t, 9, L("≥ 125 % des ADR", "≥ 125 % of ADR"), pct(n125, nDays))
row(t, 10, L("≥ 150 % des ADR", "≥ 150 % of ADR"), pct(n150, nDays))
head(t, 11, L("Größte Bewegung ab Eröffnung", "Largest move from the open"))
row(t, 12, L("≥ 0,5 × ADR", "≥ 0.5 × ADR"), pct(m50, nDays))
row(t, 13, L("≥ 1 × ADR", "≥ 1 × ADR"), pct(m100, nDays))
head(t, 14, L("Abgeschlossene Tageskerzen. Keine Empfehlung.", "Completed daily bars. Not advice."))