Cumulative Relative Volume (RVol)

ionaryu · study · 113 行 · 点赞 10 · TradingView 原页

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

Pine Script

//@version=6
// Cumulative Relative Volume (RVol)
// Compares the volume traded so far in the current session against the average
// volume traded at the same bar of the session over the previous N sessions.
// On daily and higher timeframes it falls back to volume / moving average.
indicator("Cumulative Relative Volume (RVol)", shorttitle="Cum RVol", overlay=false, max_bars_back=2000)
 
// =============================================================================
// 1. Inputs
// =============================================================================
group_disp = 'Display Settings'
mode_select = input.string('RVol Ratio (Histogram + Line)', 'Display Mode', options=['Volume Comparison (Histogram)', 'RVol Ratio (Histogram + Line)'], group=group_disp)
 
group_calc = 'Calculation'
is_intraday = timeframe.isintraday
// Rows x columns must stay under Pine's 100,000-element matrix limit (1440 x 50 = 72,000).
len_lookback = input.int(10, 'Lookback Days', minval=2, maxval=50, group=group_calc)
 
// =============================================================================
// 2. Engine
// =============================================================================
// One matrix row per bar of the day, one column per session (FIFO):
// column 0 holds the oldest session, column len_lookback - 1 the newest.
// 1440 rows cover a whole day on intraday timeframes of 1 minute or slower.
int max_bars = 1440
 
var int bar_count = 0
var matrix<float> history = matrix.new<float>(max_bars, len_lookback, na)
 
// Declared outside the branches so ta.sma() runs on every bar.
float daily_avg_vol = ta.sma(volume, len_lookback)
 
float cur_cum_vol = 0.0
float daily_rvol = 0.0
float avg_past_cum = 0.0
 
if is_intraday
    if timeframe.change("D")
        bar_count := 0
        cur_cum_vol := volume
    else
        bar_count := bar_count + 1
        cur_cum_vol := cur_cum_vol[1] + volume
 
    int idx = math.min(bar_count, max_bars - 1)
 
    float sum_past = 0.0
    int count = 0
    for i = 0 to len_lookback - 1
        float val = matrix.get(history, idx, i)
        if not na(val)
            sum_past := sum_past + val
            count := count + 1
 
    avg_past_cum := count > 0 ? sum_past / count : cur_cum_vol
    daily_rvol := cur_cum_vol / math.max(avg_past_cum, 1.0)
 
    if barstate.isconfirmed
        for i = 0 to len_lookback - 2
            matrix.set(history, idx, i, matrix.get(history, idx, i + 1))
        matrix.set(history, idx, len_lookback - 1, cur_cum_vol)
else
    cur_cum_vol := volume
    avg_past_cum := daily_avg_vol
    daily_rvol := volume / math.max(daily_avg_vol, 1.0)
 
// =============================================================================
// 3. Visualization
// =============================================================================
 
is_mode_raw = mode_select == 'Volume Comparison (Histogram)'
is_mode_rvol = mode_select == 'RVol Ratio (Histogram + Line)'
 
// MODE A: running volume against the average running volume
color col_raw = cur_cum_vol >= avg_past_cum ? color.new(#00bcd4, 40) : color.new(#ff5252, 40)
plot(is_mode_raw ? cur_cum_vol : na, "Today Volume", style=plot.style_columns, color=col_raw)
plot(is_mode_raw ? avg_past_cum : na, "Average Volume", color=color.gray, linewidth=2)
 
// MODE B: RVol histogram and trend line
// Histogram marks the bar-to-bar momentum of the ratio.
bool is_rising = daily_rvol >= daily_rvol[1]
color col_momentum = if is_rising
    color.new(#00e676, 50)                                                          // rising
else
    daily_rvol >= 1.0 ? color.new(#78909c, 50) : color.new(#ff5252, 50)              // fading above / below 1.0x
 
plot(is_mode_rvol ? daily_rvol : na, "RVol Momentum", style=plot.style_columns, color=col_momentum)
 
// Trend line colour splits at the 1.0x level.
color col_line_split = daily_rvol >= 1.0 ? #00e676 : #ff5252
plot(is_mode_rvol ? daily_rvol : na, "RVol Trend", color=col_line_split, linewidth=2)
 
// 1.0x reference. hline() accepts only an input-level price and cannot be hidden per mode,
// so the baseline is plotted here and stays empty in Mode A.
plot(is_mode_rvol ? 1.0 : na, "1.0x Baseline", color=color.gray, linewidth=1)
 
// Info table
var table info = table.new(position.top_right, 2, 2, bgcolor=color.new(color.black, 60))
if barstate.islast
    table.cell(info, 0, 0, "Current RVol", text_color=color.white, text_size=size.small)
    table.cell(info, 1, 0, str.tostring(daily_rvol, "#.##") + "x", text_color=daily_rvol >= 1.0 ? color.green : color.red, text_size=size.small)
 
    if is_mode_raw
        table.cell(info, 0, 1, "Vs Avg Vol", text_color=color.gray, text_size=size.small)
        float diff = cur_cum_vol - avg_past_cum
        string diff_str = (diff >= 0 ? "+" : "") + str.tostring(diff, format.volume)
        color val_color = diff >= 0 ? color.new(#00bcd4, 0) : color.new(#ff9800, 0)
        table.cell(info, 1, 1, diff_str, text_color=val_color, text_size=size.small)
    else
        table.cell(info, 0, 1, "Momentum", text_color=color.gray, text_size=size.small)
        string mom_txt = is_rising ? "RISING ↗" : "FALLING ↘"
        color mom_col = is_rising ? color.green : (daily_rvol >= 1.0 ? color.gray : color.red)
        table.cell(info, 1, 1, mom_txt, text_color=mom_col, text_size=size.small)

← 返回列表