本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator('Relative Volume', shorttitle = 'RVol', overlay = false, timeframe = "", timeframe_gaps = true)
// Relative volume = volume of the current bar / typical volume of the same time of day.
// Segmenting by time of day removes the intraday volume curve (the open and the close
// are always busier than midday), so one threshold works across the whole session.
// =============================================================================
// 1. Inputs
// =============================================================================
string GROUP_CORE = 'Core Settings'
// A 1-minute chart builds the widest grid: 1,440 slots x 60 days = 86,400 elements,
// just under the 100,000-element collection limit.
int len_lookback = input.int(10, 'RVol Lookback (Days)', minval = 5, maxval = 60, group = GROUP_CORE, tooltip = 'Number of past days compared against the same time slot. Their median is the baseline, so one past spike cannot move it.')
float thres_rvol = input.float(2.0, 'High RVol Threshold', step = 0.1, group = GROUP_CORE)
float thres_ext = input.float(5.0, 'Extreme RVol Threshold', step = 0.5, group = GROUP_CORE)
// =============================================================================
// 2. Engine: time-segmented median RVol
// =============================================================================
bool is_intraday = timeframe.isintraday
int bar_secs = timeframe.in_seconds()
// Bars shorter than one minute would need one slot per second (86,400 rows on a
// 1-second chart), which is more than a Pine collection can hold.
bool engine_on = is_intraday and bar_secs >= 60
// Slots a bar start can land in: the latest start of a day is 86,399 seconds.
int slot_count = engine_on ? int(86399 / bar_secs) + 1 : 1
// Rows = time slots of the day, columns = past days of that slot. Starts as na.
var matrix<float> vol_history = matrix.new<float>(slot_count, len_lookback)
// Slot of the current bar. Clock time is used, so a bar meets the same slot on
// every earlier day regardless of where the session starts.
get_slot() =>
int secs_of_day = hour(time) * 3600 + minute(time) * 60
int(secs_of_day / bar_secs)
float rvol = na
if engine_on
int slot = get_slot()
// Column 0 holds a value only once the slot has len_lookback samples, so it
// marks the end of the warm-up. RVol stays na before that.
if not na(matrix.get(vol_history, slot, 0))
float base_vol = array.median(matrix.row(vol_history, slot))
rvol := volume / math.max(base_vol, 1.0)
// FIFO on the closed bar: the oldest sample leaves, this bar's volume enters.
if barstate.isconfirmed
for col = 0 to len_lookback - 2
matrix.set(vol_history, slot, col, matrix.get(vol_history, slot, col + 1))
matrix.set(vol_history, slot, len_lookback - 1, volume)
// Daily and higher timeframes have no time of day to segment, so the baseline is
// a plain rolling median over the same number of bars.
else if not is_intraday
rvol := volume / math.max(ta.median(volume, len_lookback), 1.0)
// =============================================================================
// 3. Plot
// =============================================================================
color col_rvol = rvol >= thres_ext ? color.new(#9c27b0, 10) : // extreme
rvol >= thres_rvol ? color.new(#ffeb3b, 10) : // high
rvol >= 1.0 ? color.new(#00bcd4, 40) : // normal
color.new(color.gray, 70) // quiet
plot(rvol, 'RVol', style = plot.style_columns, color = col_rvol)