本页源码来自 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/
// © Holtermorter99
//
// Average Range & Expected Move — the average daily and weekly range of the symbol (completed periods only),
// how much of it today and this week have already used, the levels where the day would reach a full average
// range, and statistics of the chart itself: how often a day's range exceeds its average and how often the
// day closes inside the expected-move band around its open. Intraday charts only.
//@version=6
indicator("Average Range & Expected Move [Holtermorter99]", "AREM", overlay = true)
// ─────────────────────────────── Inputs ───────────────────────────────
const string G_RNG = "Ranges"
const string G_DISP = "Display"
const string G_COL = "Colors"
int adrLength = input.int(14, "Daily Average Length (days)", minval = 2, maxval = 100, group = G_RNG, tooltip = "Average of the high-low range of the last N completed trading days (TradingView's daily data).")
int awrLength = input.int(8, "Weekly Average Length (weeks)", minval = 2, maxval = 52, group = G_RNG, tooltip = "Average of the high-low range of the last N completed weeks.")
float bandMult = input.float(0.5, "Expected Move Band (× ADR)", minval = 0.1, maxval = 2.0, step = 0.05, group = G_RNG, tooltip = "Band drawn around the day's open: open ± this many ADR. The dashboard counts how often the day closes inside it.")
bool showTargets = input.bool(true, "Show Full-Range Levels", group = G_DISP, tooltip = "Where the day would reach one full ADR: the day's low + ADR and the day's high − ADR, updated as the day's range grows.")
bool showBand = input.bool(true, "Show Expected Move Band", group = G_DISP)
bool showDashboard = input.bool(true, "Show Dashboard", group = G_DISP)
string dashPosInput = input.string("Top Right", "Dashboard Position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], inline = "dash", group = G_DISP)
string dashSizeInput = input.string("Small", "", options = ["Tiny", "Small", "Normal"], inline = "dash", group = G_DISP)
color upColor = input.color(#26A69A, "Upper Level", group = G_COL)
color downColor = input.color(#EF5350, "Lower Level", group = G_COL)
color neutralColor = input.color(#787B86, "Band", group = G_COL)
// ─────────────────────────────── Data ───────────────────────────────
bool intraday = timeframe.isintraday
// Averages of COMPLETED periods: the previous higher-timeframe bar with lookahead on (no repainting).
float adr = request.security(syminfo.tickerid, "D", ta.sma(high - low, adrLength)[1], lookahead = barmerge.lookahead_on)
float awr = request.security(syminfo.tickerid, "W", ta.sma(high - low, awrLength)[1], lookahead = barmerge.lookahead_on)
bool newDay = timeframe.change("D")
bool newWeek = timeframe.change("W")
// Running high/low of the current day and week, from the chart's own bars (known bar by bar, no future data).
var float dHi = na
var float dLo = na
var float dOpen = na
var float dAdr = na // ADR frozen at the start of the day (used by the band and the statistics)
var float wHi = na
var float wLo = na
var bool dayFull = false // the day started inside the loaded history (not a partial first day)
// Statistics over completed days: count, range >= ADR, close inside the band.
var int nDays = 0
var int nOver = 0
var int nInside = 0
var float lastClose = na
if intraday
if newDay
// Finalize the previous day before starting the new one.
if dayFull and not na(dAdr) and dAdr > 0
nDays += 1
nOver += (dHi - dLo) >= dAdr ? 1 : 0
nInside += math.abs(lastClose - dOpen) <= bandMult * dAdr ? 1 : 0
dHi := high
dLo := low
dOpen := open
dAdr := adr
dayFull := bar_index > 0
else
dHi := math.max(nz(dHi, high), high)
dLo := math.min(nz(dLo, low), low)
if newWeek
wHi := high
wLo := low
else
wHi := math.max(nz(wHi, high), high)
wLo := math.min(nz(wLo, low), low)
lastClose := close
float dRange = dHi - dLo
float wRange = wHi - wLo
// Lines break at each new day (na on its first bar) so days are not joined by vertical segments.
float upLevel = intraday and showTargets and not newDay and not na(dAdr) ? dLo + dAdr : na
float dnLevel = intraday and showTargets and not newDay and not na(dAdr) ? dHi - dAdr : na
float bandUp = intraday and showBand and not newDay and not na(dAdr) ? dOpen + bandMult * dAdr : na
float bandDn = intraday and showBand and not newDay and not na(dAdr) ? dOpen - bandMult * dAdr : na
plot(upLevel, "Day Low + ADR", color = color.new(upColor, 20), style = plot.style_linebr, linewidth = 1)
plot(dnLevel, "Day High − ADR", color = color.new(downColor, 20), style = plot.style_linebr, linewidth = 1)
pBu = plot(bandUp, "Band Upper", color = color.new(neutralColor, 50), style = plot.style_linebr)
pBd = plot(bandDn, "Band Lower", color = color.new(neutralColor, 50), style = plot.style_linebr)
fill(pBu, pBd, color = color.new(neutralColor, 92), title = "Expected Move Band", fillgaps = false)
plot(adr, "ADR", display = display.data_window)
plot(dAdr, "Day ADR", display = display.data_window)
plot(dRange, "Day Range", display = display.data_window)
plot(not na(dAdr) and dAdr > 0 ? 100 * dRange / dAdr : na, "Day Range % of ADR", display = display.data_window)
plot(awr, "AWR", display = display.data_window)
plot(wRange, "Week Range", display = display.data_window)
// Alerts on bar close: the day's range reaches one full ADR.
bool reachedFull = barstate.isconfirmed and intraday and not na(dAdr) and dRange >= dAdr and (dRange[1] < dAdr[1] or newDay)
alertcondition(reachedFull, "Day range reached ADR", "AREM: today's range reached one full average daily range on {{ticker}} (range {{plot(\"Day Range\")}} vs ADR {{plot(\"Day ADR\")}})")
if reachedFull
alert("AREM: today's range reached one full ADR on " + syminfo.ticker + " (" + str.tostring(dRange, format.mintick) + " vs ADR " + str.tostring(dAdr, format.mintick) + ")", alert.freq_once_per_bar_close)
// ─────────────────────────────── Dashboard ───────────────────────────────
dashPosition(string p) =>
switch p
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
=> position.top_right
pct(float a, float b) =>
not na(a) and not na(b) and b > 0 ? str.tostring(100.0 * a / b, "#") + "%" : "—"
var table dash = na
string dashSize = dashSizeInput == "Tiny" ? size.tiny : dashSizeInput == "Normal" ? size.normal : size.small
if barstate.islast
table.delete(dash)
dash := na
if showDashboard
dash := table.new(dashPosition(dashPosInput), 4, 4, bgcolor = color.new(chart.bg_color, 10), frame_color = color.new(chart.fg_color, 80), frame_width = 1, border_color = color.new(chart.fg_color, 90), border_width = 1)
color dim = color.new(chart.fg_color, 30)
if not intraday
table.cell(dash, 0, 0, "AREM: use an intraday chart", text_color = downColor, text_size = dashSize)
else
array<string> heads = array.from("AREM", "Average", "So far", "Used")
for c = 0 to 3
table.cell(dash, c, 0, heads.get(c), text_color = c == 0 ? chart.fg_color : dim, text_size = dashSize)
table.cell(dash, 0, 1, "Day (ADR " + str.tostring(adrLength) + ")", text_color = dim, text_size = dashSize, text_halign = text.align_left)
table.cell(dash, 1, 1, str.tostring(dAdr, format.mintick), text_color = chart.fg_color, text_size = dashSize)
table.cell(dash, 2, 1, str.tostring(dRange, format.mintick), text_color = chart.fg_color, text_size = dashSize)
table.cell(dash, 3, 1, pct(dRange, dAdr), text_color = not na(dAdr) and dRange >= dAdr ? downColor : chart.fg_color, text_size = dashSize)
table.cell(dash, 0, 2, "Week (AWR " + str.tostring(awrLength) + ")", text_color = dim, text_size = dashSize, text_halign = text.align_left)
table.cell(dash, 1, 2, str.tostring(awr, format.mintick), text_color = chart.fg_color, text_size = dashSize)
table.cell(dash, 2, 2, str.tostring(wRange, format.mintick), text_color = chart.fg_color, text_size = dashSize)
table.cell(dash, 3, 2, pct(wRange, awr), text_color = chart.fg_color, text_size = dashSize)
string stats = nDays > 0 ? "Range ≥ ADR " + pct(nOver, nDays) + " · close in band " + pct(nInside, nDays) + " (n=" + str.tostring(nDays) + " days)" : "no completed days yet"
table.cell(dash, 0, 3, stats, text_color = dim, text_size = dashSize, text_halign = text.align_left, tooltip = "Completed days of the loaded history: share whose high-low range reached the ADR known at their open, and share that closed within open ± " + str.tostring(bandMult) + " × ADR.")
table.merge_cells(dash, 0, 3, 3, 3)