Average Daily Range (Week / Month / Quarter / Year)

jabez4jc · study · 186 行 · 点赞 1 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Average Daily Range (Week / Month / Quarter / Year)", shorttitle="ADR WMQY", overlay=true)
 
// ─────────────────────────────────────────────
// Inputs
// ─────────────────────────────────────────────
const string G_LB  = "Lookback (trading days)"
const string G_DSP = "Display"
const string G_LVL = "Projected Range Levels"
 
daysWeek    = input.int(5,   "Week",    minval=1, group=G_LB, tooltip="Trading days in a week")
daysMonth   = input.int(21,  "Month",   minval=1, group=G_LB, tooltip="Trading days in a month")
daysQuarter = input.int(63,  "Quarter", minval=1, group=G_LB, tooltip="Trading days in a quarter")
daysYear    = input.int(252, "Year",    minval=1, group=G_LB, tooltip="Trading days in a year")
 
unitMode  = input.string("Points", "Show range as", options=["Points", "% of Price"], group=G_DSP,
     tooltip="% of Price = average of each day's (High-Low)/Close")
tblPos    = input.string("Top Right", "Table position",
     options=["Top Left", "Top Right", "Bottom Left", "Bottom Right"], group=G_DSP)
tblSize   = input.string("Normal", "Table text size", options=["Small", "Normal", "Large"], group=G_DSP)
showToday = input.bool(true, "Show today's range & % of ADR used", group=G_DSP)
hoursMode = input.string("Auto", "Trading hours per day", options=["Auto", "Manual"], group=G_DSP,
     tooltip="Auto measures the previous session from intraday bars (honours the chart's session/extended-hours setting). On Daily or higher charts it falls back to the manual value.")
manualHrs = input.float(6.25, "Manual trading hours", minval=0.1, maxval=24, step=0.25, group=G_DSP,
     tooltip="Default 6.25 = NSE cash session 09:15-15:30 IST")
roundPts  = input.float(5.0, "Round blended avg to nearest (Points)", minval=0.0001, group=G_DSP,
     tooltip="e.g. 5 → 112.3 becomes 110; 1 → 112")
roundPct  = input.float(0.5, "Round blended avg to nearest (%)", minval=0.0001, group=G_DSP,
     tooltip="e.g. 0.5 → 1.37% becomes 1.5%")
 
showLevels = input.bool(true, "Plot levels (Daily Open ± ADR)", group=G_LVL)
levelBasis = input.string("Blended (Rounded)", "ADR basis for levels", options=["Week", "Month", "Quarter", "Year", "Blended", "Blended (Rounded)"], group=G_LVL)
colUp      = input.color(color.new(color.green, 0), "Upper level", group=G_LVL)
colDn      = input.color(color.new(color.red, 0),   "Lower level", group=G_LVL)
 
// ─────────────────────────────────────────────
// Average daily range from the Daily timeframe.
// [1] + lookahead_on = completed days only, so values never repaint.
// ─────────────────────────────────────────────
[wPts, mPts, qPts, yPts, wPct, mPct, qPct, yPct] = request.security(
     syminfo.tickerid, "D",
     [ta.sma(high - low, daysWeek)[1],
      ta.sma(high - low, daysMonth)[1],
      ta.sma(high - low, daysQuarter)[1],
      ta.sma(high - low, daysYear)[1],
      ta.sma((high - low) / close * 100, daysWeek)[1],
      ta.sma((high - low) / close * 100, daysMonth)[1],
      ta.sma((high - low) / close * 100, daysQuarter)[1],
      ta.sma((high - low) / close * 100, daysYear)[1]],
     lookahead = barmerge.lookahead_on)
 
// ─────────────────────────────────────────────
// Today's developing range (built on the chart timeframe, no lookahead)
// ─────────────────────────────────────────────
newDay = timeframe.change("D")
var float dayOpen = na
var float dayHigh = na
var float dayLow  = na
if newDay
    dayOpen := open
    dayHigh := high
    dayLow  := low
else
    dayHigh := math.max(dayHigh, high)
    dayLow  := math.min(dayLow, low)
 
// Trading hours: sum the duration of every bar in a day; use the last completed day
var float accMs  = 0.0
var float prevMs = na
if newDay
    prevMs := accMs > 0 ? accMs : na
    accMs  := 0.0
accMs += timeframe.isintraday ? (time_close - time) : 0.0
 
autoHrs    = na(prevMs) ? accMs / 3600000.0 : prevMs / 3600000.0
useAuto    = hoursMode == "Auto" and timeframe.isintraday and autoHrs > 0
tradingHrs = useAuto ? autoHrs : manualHrs
 
todayPts = dayHigh - dayLow
todayPct = todayPts / close * 100
 
// ─────────────────────────────────────────────
// Helpers
// ─────────────────────────────────────────────
isPct = unitMode == "% of Price"
 
fmt(float v) =>
    na(v) ? "n/a" : isPct ? str.tostring(v, "#.##") + "%" : str.tostring(v, format.mintick)
 
// Blend = simple mean of the four averages (week, month, quarter, year)
blendPts = (wPts + mPts + qPts + yPts) / 4
blendPct = (wPct + mPct + qPct + yPct) / 4
 
roundTo(float v, float step) =>
    math.round(v / step) * step
 
blendPtsR = roundTo(blendPts, roundPts)
blendPctR = roundTo(blendPct, roundPct)
 
basisPts = switch levelBasis
    "Week"    => wPts
    "Month"   => mPts
    "Quarter" => qPts
    "Year"    => yPts
    "Blended" => blendPts
    =>           blendPtsR
 
// ─────────────────────────────────────────────
// Level plots
// ─────────────────────────────────────────────
upperLvl = showLevels and not newDay ? dayOpen + basisPts : na
lowerLvl = showLevels and not newDay ? dayOpen - basisPts : na
plot(upperLvl, "Open + ADR", colUp, 1, plot.style_linebr)
plot(lowerLvl, "Open - ADR", colDn, 1, plot.style_linebr)
 
// ─────────────────────────────────────────────
// Table
// ─────────────────────────────────────────────
tPos = switch tblPos
    "Top Left"     => position.top_left
    "Top Right"    => position.top_right
    "Bottom Left"  => position.bottom_left
    =>               position.bottom_right
 
tSize = switch tblSize
    "Small" => size.small
    "Large" => size.large
    =>         size.normal
 
var table t = table.new(tPos, 3, 10, border_width = 1,
     frame_color = color.new(color.gray, 60), frame_width = 1, border_color = color.new(color.gray, 70))
 
if barstate.islast
    hdrBg = color.new(color.blue, 20)
    rowBg = color.new(color.gray, 85)
    txt   = chart.fg_color
 
    table.cell(t, 0, 0, "Avg Daily Range", text_color = color.white, bgcolor = hdrBg, text_size = tSize)
    table.cell(t, 1, 0, isPct ? "% of Price" : "Points", text_color = color.white, bgcolor = hdrBg, text_size = tSize)
    table.cell(t, 2, 0, "Days", text_color = color.white, bgcolor = hdrBg, text_size = tSize)
 
    table.cell(t, 0, 1, "Week",    text_color = txt, bgcolor = rowBg, text_size = tSize, text_halign = text.align_left)
    table.cell(t, 1, 1, fmt(isPct ? wPct : wPts), text_color = txt, bgcolor = rowBg, text_size = tSize)
    table.cell(t, 2, 1, str.tostring(daysWeek), text_color = txt, bgcolor = rowBg, text_size = tSize)
 
    table.cell(t, 0, 2, "Month",   text_color = txt, bgcolor = rowBg, text_size = tSize, text_halign = text.align_left)
    table.cell(t, 1, 2, fmt(isPct ? mPct : mPts), text_color = txt, bgcolor = rowBg, text_size = tSize)
    table.cell(t, 2, 2, str.tostring(daysMonth), text_color = txt, bgcolor = rowBg, text_size = tSize)
 
    table.cell(t, 0, 3, "Quarter", text_color = txt, bgcolor = rowBg, text_size = tSize, text_halign = text.align_left)
    table.cell(t, 1, 3, fmt(isPct ? qPct : qPts), text_color = txt, bgcolor = rowBg, text_size = tSize)
    table.cell(t, 2, 3, str.tostring(daysQuarter), text_color = txt, bgcolor = rowBg, text_size = tSize)
 
    table.cell(t, 0, 4, "Year",    text_color = txt, bgcolor = rowBg, text_size = tSize, text_halign = text.align_left)
    table.cell(t, 1, 4, fmt(isPct ? yPct : yPts), text_color = txt, bgcolor = rowBg, text_size = tSize)
    table.cell(t, 2, 4, str.tostring(daysYear), text_color = txt, bgcolor = rowBg, text_size = tSize)
 
    // Blended average of all four windows + rounded value
    blendBg = color.new(color.teal, 75)
    table.cell(t, 0, 5, "Blended Avg", text_color = txt, bgcolor = blendBg, text_size = tSize, text_halign = text.align_left)
    table.cell(t, 1, 5, fmt(isPct ? blendPct : blendPts), text_color = txt, bgcolor = blendBg, text_size = tSize)
    table.cell(t, 2, 5, "W/M/Q/Y", text_color = txt, bgcolor = blendBg, text_size = tSize)
    table.cell(t, 0, 6, "Blended (Rounded)", text_color = txt, bgcolor = blendBg, text_size = tSize, text_halign = text.align_left)
    table.cell(t, 1, 6, fmt(isPct ? blendPctR : blendPtsR), text_color = txt, bgcolor = blendBg, text_size = tSize)
    table.cell(t, 2, 6, isPct ? "±" + str.tostring(roundPct) : "±" + str.tostring(roundPts), text_color = txt, bgcolor = blendBg, text_size = tSize)
 
    table.cell(t, 0, 9, "Trading Hours / Day", text_color = txt, bgcolor = blendBg, text_size = tSize, text_halign = text.align_left)
    table.cell(t, 1, 9, str.tostring(tradingHrs, "#.##") + " h", text_color = txt, bgcolor = blendBg, text_size = tSize)
    table.cell(t, 2, 9, useAuto ? "Auto" : "Manual", text_color = txt, bgcolor = blendBg, text_size = tSize)
 
    if showToday
        usedPct = basisPts > 0 ? todayPts / basisPts * 100 : na
        todBg = color.new(color.orange, 80)
        table.cell(t, 0, 7, "Today", text_color = txt, bgcolor = todBg, text_size = tSize, text_halign = text.align_left)
        table.cell(t, 1, 7, fmt(isPct ? todayPct : todayPts), text_color = txt, bgcolor = todBg, text_size = tSize)
        table.cell(t, 2, 7, "", bgcolor = todBg, text_size = tSize)
        table.cell(t, 0, 8, "% of " + levelBasis + " ADR", text_color = txt, bgcolor = todBg, text_size = tSize, text_halign = text.align_left)
        table.cell(t, 1, 8, na(usedPct) ? "n/a" : str.tostring(usedPct, "#") + "%", text_color = txt, bgcolor = todBg, text_size = tSize)
        table.cell(t, 2, 8, "", bgcolor = todBg, text_size = tSize)
    else
        table.cell(t, 0, 7, "")
        table.cell(t, 1, 7, "")
        table.cell(t, 2, 7, "")
        table.cell(t, 0, 8, "")
        table.cell(t, 1, 8, "")
        table.cell(t, 2, 8, "")

← 返回列表