本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Range Meter: DTR vs ATR + RVol - JAW Trades
//
// How much of its usual range a symbol has already moved, how much room is left, and how heavy
// the volume is compared with the same time on previous days.
//
// Range used today's range (high - low) / ATR of the previous N periods. The ATR never
// includes today, so it doesn't drift as today's own range grows.
// Room left ATR - today's range, in price.
// ATR ceiling today's low + ATR. ATR floor: today's high - ATR. Where a normal-sized day
// would run out of room in each direction.
// RVol now volume so far today / the average volume by this same time of day over the
// previous N days (intraday charts). A fair read at 10am.
// RVol day today's volume / the average daily volume of the previous N days.
// On weekly and monthly charts it switches to the week or month (WTR, MTR).
indicator("Range Meter: DTR vs ATR + RVol - JAW Trades", shorttitle = "Range Meter - JAW Trades", overlay = true, max_lines_count = 10, max_labels_count = 10)
// ---------- Colours
const color BLUE = #1E66F0
const color BLUE_HI = #7FB0FF
const color GOLD = #F5A524
const color RED = #F0505F
const color PANEL = #0B1322
const color TEXT = #F4F6FB
const color DIM = #9AA3B8
const color FAINT = #5B647C
// ---------- Inputs
string GC = "Calculation"
int atrLen = input.int(14, "ATR length", minval = 1, maxval = 200, group = GC, display = display.none)
string atrType = input.string("RMA", "ATR smoothing", options = ["RMA", "SMA", "EMA", "WMA"], group = GC, display = display.none)
int volLen = input.int(20, "Volume lookback", minval = 1, maxval = 100, group = GC, display = display.none, tooltip = "How many previous days (or weeks, months) the volume averages use.")
string sessIn = input.string("Regular session", "Range and volume from", options = ["Regular session", "Include extended hours"], group = GC, display = display.none, tooltip = "Regular session: 9:30am to 4pm ET for US stocks, like the daily candle. Premarket and after-hours highs, lows and volume are left out. Include extended hours: they count too.")
string periodIn = input.string("Auto", "Period", options = ["Auto", "Day", "Week", "Month"], group = GC, display = display.none, tooltip = "Auto: the day on intraday and daily charts, the week on weekly charts, the month on monthly charts.")
string GL = "Thresholds"
float warnPct = input.float(70, "Range used: stretched from (%)", minval = 1, step = 5, group = GL, display = display.none)
float fullPct = input.float(100, "Range used: full ATR at (%)", minval = 1, step = 5, group = GL, display = display.none)
float hotPct = input.float(150, "RVol: heavy from (%)", minval = 1, step = 10, group = GL, display = display.none)
string GA = "ATR ceiling and floor"
bool showLines = input.bool(true, "Show the ATR ceiling and floor", group = GA, display = display.none, tooltip = "Ceiling: today's low + ATR. Floor: today's high - ATR.")
color lineCol = input.color(BLUE_HI, "Colour", group = GA, inline = "l", display = display.none)
int lineW = input.int(1, "Width", minval = 1, maxval = 4, group = GA, inline = "l", display = display.none)
string lineSt = input.string("Dashed", "Style", options = ["Solid", "Dashed", "Dotted"], group = GA, inline = "l", display = display.none)
bool showLineLabels = input.bool(true, "Labels with prices", group = GA, display = display.none)
int labelOffset = input.int(8, "Label offset (bars)", minval = 1, maxval = 200, group = GA, display = display.none)
string GT = "Panel"
string tSizeIn = input.string("Normal", "Text size", options = ["Small", "Normal", "Large"], group = GT, inline = "t", display = display.none)
string tPosIn = input.string("Top right", "Position", options = ["Top right", "Middle right", "Bottom right", "Top left", "Middle left", "Bottom left", "Top center", "Bottom center"], group = GT, inline = "t", display = display.none)
bool showMeter = input.bool(true, "Meter bar next to range used", group = GT, display = display.none)
bool showDayVol = input.bool(true, "Show full day RVol next to RVol now", group = GT, display = display.none)
// ---------- Helpers
sizeOf(string s) =>
s == "Small" ? size.small : s == "Large" ? size.large : size.normal
posOf(string s) =>
switch s
"Middle right" => position.middle_right
"Bottom right" => position.bottom_right
"Top left" => position.top_left
"Middle left" => position.middle_left
"Bottom left" => position.bottom_left
"Top center" => position.top_center
"Bottom center" => position.bottom_center
=> position.top_right
lineStyleOf(string st) =>
st == "Solid" ? line.style_solid : st == "Dotted" ? line.style_dotted : line.style_dashed
pctTxt(float p) =>
na(p) ? "-" : str.tostring(math.round(p)) + "%"
usedColor(float p) =>
na(p) ? DIM : p >= fullPct ? RED : p >= warnPct ? GOLD : BLUE_HI
rvolColor(float p) =>
na(p) ? DIM : p >= hotPct ? GOLD : TEXT
meter(float p) =>
int n = na(p) ? 0 : math.min(10, math.max(0, int(math.round(p / 10))))
str.repeat("▰", n) + str.repeat("▱", 10 - n)
// The ATR and average volume of the periods BEFORE the current one
prevStats() =>
float tr1 = ta.tr(true)
float r = ta.rma(tr1, atrLen)
float s = ta.sma(tr1, atrLen)
float e = ta.ema(tr1, atrLen)
float w = ta.wma(tr1, atrLen)
float a = atrType == "SMA" ? s : atrType == "EMA" ? e : atrType == "WMA" ? w : r
float v = ta.sma(volume, volLen)
[a[1], v[1]]
// ---------- Period and its numbers
string tf = periodIn == "Day" ? "D" : periodIn == "Week" ? "W" : periodIn == "Month" ? "M" : timeframe.ismonthly ? "M" : timeframe.isweekly ? "W" : "D"
string trName = tf == "W" ? "WTR" : tf == "M" ? "MTR" : "DTR"
string pNameBase = tf == "W" ? "This week" : tf == "M" ? "This month" : "Today"
bool useReg = sessIn == "Regular session"
string srcId = useReg ? ticker.modify(syminfo.tickerid, session.regular) : ticker.modify(syminfo.tickerid, session.extended)
[pAtr, pVolAvg] = request.security(srcId, tf, prevStats(), lookahead = barmerge.lookahead_on)
[pHigh, pLow, pVol] = request.security(srcId, tf, [high, low, volume])
float rng = pHigh - pLow
float usedPct = pAtr > 0 ? rng / pAtr * 100 : na
float roomLeft = pAtr - rng
float ceilLvl = pLow + pAtr
float floorLvl = pHigh - pAtr
float rvolDay = pVolAvg > 0 ? pVol / pVolAvg * 100 : na
// ---------- RVol now: volume so far today against the average by the same time of day
type VolHist
array<float> v
var map<int, VolHist> byTime = map.new<int, VolHist>()
var float cumVol = 0.0
bool intradayDay = timeframe.isintraday and tf == "D"
bool dayChanged = timeframe.change("D")
bool newDay = bar_index == 0 or dayChanged
// Regular session: only bars from the open count, so premarket volume is left out
bool countBar = not useReg or session.ismarket
float barVol = countBar ? volume : 0.0
cumVol := newDay ? barVol : cumVol + barVol
// Before today's open (regular session), the numbers are still yesterday's
var bool openedToday = false
var int openT = na
if newDay
openedToday := false
if session.ismarket and not openedToday
openedToday := true
openT := time
bool beforeOpen = useReg and intradayDay and not openedToday
float avgAtTime = na
if intradayDay and countBar
int key = hour(time, syminfo.timezone) * 60 + minute(time, syminfo.timezone)
VolHist h = byTime.get(key)
if not na(h)
if h.v.size() > 0
avgAtTime := h.v.avg()
if barstate.isconfirmed
if na(h)
h := VolHist.new(array.new<float>())
byTime.put(key, h)
h.v.push(cumVol)
if h.v.size() > volLen
h.v.shift()
float rvolNow = avgAtTime > 0 ? cumVol / avgAtTime * 100 : na
// Values in the Data Window only (the status line stays clean)
plot(usedPct, "Range used %", display = display.data_window)
plot(rvolNow, "RVol now %", display = display.data_window)
plot(rvolDay, "RVol day %", display = display.data_window)
// ---------- Alerts
alertcondition(ta.crossover(usedPct, fullPct), "Range used reached the full ATR", "{{ticker}} has used its full ATR for the period")
alertcondition(ta.crossover(usedPct, warnPct), "Range used is stretched", "{{ticker}} has used most of its ATR for the period")
alertcondition(ta.crossover(rvolNow, hotPct), "RVol now turned heavy", "{{ticker}} volume is heavy for this time of day")
// ---------- ATR ceiling and floor (from the start of the period, last bar only)
var int pStartT = na
bool periodChanged = timeframe.change(tf)
if bar_index == 0 or periodChanged
pStartT := time
var array<line> lns = array.new<line>()
var array<label> labs = array.new<label>()
drawLevel(float y, string name) =>
int t0 = useReg and intradayDay and not na(openT) ? openT : pStartT
if not na(y) and not na(t0)
int t1 = time + labelOffset * timeframe.in_seconds() * 1000
lns.push(line.new(t0, y, t1, y, xloc.bar_time, extend.none, lineCol, lineStyleOf(lineSt), lineW))
if showLineLabels
labs.push(label.new(t1, y, name + " " + str.tostring(y, format.mintick), xloc.bar_time, yloc.price, lineCol, label.style_label_left, PANEL, sizeOf(tSizeIn)))
// ---------- Panel
var table tb = table.new(posOf(tPosIn), 2, 8, bgcolor = PANEL, frame_color = color.new(BLUE, 60), frame_width = 1, border_width = 0)
row(int r, string l, string v, color vc) =>
tb.cell(0, r, l, text_color = DIM, text_size = sizeOf(tSizeIn), text_halign = text.align_left)
tb.cell(1, r, v, text_color = vc, text_size = sizeOf(tSizeIn), text_halign = text.align_right)
if barstate.islast
for ln in lns
line.delete(ln)
lns.clear()
for lb in labs
label.delete(lb)
labs.clear()
if showLines and not beforeOpen and timeframe.in_seconds() < timeframe.in_seconds(tf)
drawLevel(ceilLvl, "ATR ceiling")
drawLevel(floorLvl, "ATR floor")
tb.clear(0, 0, 1, 7)
tb.cell(0, 0, "Range Meter", text_color = GOLD, text_size = sizeOf(tSizeIn), text_halign = text.align_left)
tb.cell(1, 0, beforeOpen ? "Yesterday" : pNameBase, text_color = FAINT, text_size = sizeOf(tSizeIn), text_halign = text.align_right)
row(1, trName + " vs ATR", str.tostring(rng, format.mintick) + " / " + str.tostring(pAtr, format.mintick), TEXT)
row(2, "Range used", (showMeter ? meter(usedPct) + " " : "") + pctTxt(usedPct), usedColor(usedPct))
row(3, "Room left", na(roomLeft) ? "-" : roomLeft > 0 ? str.tostring(roomLeft, format.mintick) : "past ATR", roomLeft > 0 ? TEXT : RED)
if intradayDay and showDayVol
row(4, "RVol now / day", (beforeOpen ? "-" : pctTxt(rvolNow)) + " / " + pctTxt(rvolDay), beforeOpen ? DIM : rvolColor(rvolNow))
else if intradayDay
row(4, "RVol now", beforeOpen ? "from the open" : pctTxt(rvolNow), beforeOpen ? FAINT : rvolColor(rvolNow))
else
row(4, "RVol", pctTxt(rvolDay), rvolColor(rvolDay))