本页源码来自 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/
// © veet97
//@version=6
indicator("ATR & Daily Range", shorttitle="Range Dashboard", overlay=true)
// ============================================================
// DISPLAY CONTROLS
// ============================================================
showATR = input.bool(true, "ATR", group="Display")
showADR = input.bool(true, "ADR %", group="Display")
showEMA = input.bool(true, "21D EMA distance (ATRs)", group="Display")
showLODPercent = input.bool(true, "Low of day distance (%)", group="Display")
showLODATR = input.bool(true, "Low of day distance (ATR %)", group="Display")
// ============================================================
// PERIOD SETTINGS
// ============================================================
atrPeriod = input.int(14, "ATR period", minval=1, group="Periods")
adrPeriod = input.int(14, "ADR period", minval=1, group="Periods")
// ============================================================
// ATR — CHART TIMEFRAME
// ============================================================
// Standard Wilder ATR calculated on each chart bar.
atrValue = ta.atr(atrPeriod)
// ============================================================
// DAILY METRICS
// ============================================================
// Daily ATR is used for the LOD ATR percentage.
// ADR and 21D EMA preserve their original calculations.
// lookahead_off prevents future data leakage.
[adrPercent, dailyEMA21, requestedDailyLow, dailyATR] = request.security(
syminfo.tickerid, "D",
[ta.sma(close != 0.0 ? ((high - low) / close) * 100.0 : na, adrPeriod),
ta.ema(close, 21),
low,
ta.atr(atrPeriod)],
gaps=barmerge.gaps_off,
lookahead=barmerge.lookahead_off)
// ============================================================
// LOW OF DAY TRACKING
// ============================================================
// Accumulate the current day's low on intraday charts.
bool newDay = timeframe.change("D")
var float intradayLow = na
if newDay or na(intradayLow)
intradayLow := low
else
intradayLow := math.min(intradayLow, low)
// Intraday charts use accumulated LOD.
// Daily and higher charts use the requested daily low.
dailyLow = timeframe.isintraday ? intradayLow : requestedDailyLow
// ============================================================
// CALCULATIONS
// ============================================================
// Validate denominators.
validATR = not na(atrValue) and atrValue > 0.0
validDailyATR = not na(dailyATR) and dailyATR > 0.0
validDailyLow = not na(dailyLow) and dailyLow > 0.0
// 21D EMA distance in chart-timeframe ATR multiples.
// Original calculation preserved.
emaDistance = validATR and not na(dailyEMA21) ? (close - dailyEMA21) / atrValue : na
// LOD distance as percentage of the day's low.
//
// Example:
// Price = 152
// LOD = 148
// Result = 2.70%
lodDistancePercent = validDailyLow ? ((close - dailyLow) / dailyLow) * 100.0 : na
// LOD distance as percentage of Daily ATR.
//
// Example:
// Price = 152
// LOD = 148
// Daily ATR = 5
// Result = 80.00%
//
// 100% means price is one full Daily ATR above LOD.
lodDistanceATR = validDailyATR and not na(dailyLow) ? ((close - dailyLow) / dailyATR) * 100.0 : na
// ============================================================
// FORMATTING
// ============================================================
formatATRMultiple(float value) =>
na(value) ? "N/A" : str.tostring(value, "0.00") + "x ATR"
formatPercent(float value) =>
na(value) ? "N/A" : str.tostring(value, "0.00") + "%"
// ============================================================
// DASHBOARD DIMENSIONS
// ============================================================
// Each metric has an independent display toggle.
int enabledCount = (showATR ? 1 : 0) + (showADR ? 1 : 0) + (showEMA ? 1 : 0) + (showLODPercent ? 1 : 0) + (showLODATR ? 1 : 0)
int rowCount = enabledCount > 0 ? enabledCount + 1 : 1
// ============================================================
// DASHBOARD COLORS
// ============================================================
color headerBG = color.rgb(30, 41, 59)
color rowBG = color.new(color.rgb(15, 23, 42), 10)
color labelColor = color.rgb(203, 213, 225)
color valueColor = color.rgb(125, 211, 252)
// ============================================================
// DASHBOARD INITIALIZATION
// ============================================================
var table dashboard = table.new(
position.top_right, 2, rowCount,
frame_color=color.rgb(71, 85, 105),
frame_width=enabledCount > 0 ? 1 : 0,
border_color=color.rgb(51, 65, 85),
border_width=enabledCount > 0 ? 1 : 0)
// ============================================================
// DRAW DASHBOARD ROW
// ============================================================
drawRow(table panel, int row, string label, string value) =>
table.cell(
panel, 0, row, label,
text_color=labelColor,
bgcolor=rowBG,
text_size=size.small,
text_halign=text.align_left)
table.cell(
panel, 1, row, value,
text_color=valueColor,
bgcolor=rowBG,
text_size=size.small,
text_halign=text.align_right)
// ============================================================
// DASHBOARD RENDERING
// ============================================================
// Render dashboard only on the latest chart bar.
// Calculations continue executing on every bar.
if barstate.islast
table.clear(dashboard, 0, 0, 1, rowCount - 1)
if enabledCount > 0
// Header
table.cell(
dashboard, 0, 0, "METRIC",
text_color=color.white,
bgcolor=headerBG,
text_size=size.small,
text_halign=text.align_left)
table.cell(
dashboard, 1, 0, "VALUE",
text_color=color.white,
bgcolor=headerBG,
text_size=size.small,
text_halign=text.align_right)
int row = 1
// ATR — chart timeframe
if showATR
drawRow(
dashboard, row,
"ATR (" + str.tostring(atrPeriod) + ") · Chart",
na(atrValue) ? "N/A" : str.tostring(atrValue, format.mintick))
row += 1
// ADR — daily percentage
if showADR
drawRow(
dashboard, row,
"ADR % (" + str.tostring(adrPeriod) + ") · Daily",
formatPercent(adrPercent))
row += 1
// 21D EMA distance
if showEMA
drawRow(
dashboard, row,
"21D EMA distance",
formatATRMultiple(emaDistance))
row += 1
// LOD distance — price percentage
if showLODPercent
drawRow(
dashboard, row,
"LOD distance (%)",
formatPercent(lodDistancePercent))
row += 1
// LOD distance — percentage of Daily ATR
if showLODATR
drawRow(
dashboard, row,
"LOD distance (ATR %)",
formatPercent(lodDistanceATR))