RVol intraday
unknown
plain_text
9 days ago
2.7 kB
38
Indexable
//@version=5 indicator("Time-Matched Volume Average", shorttitle="TimeVolAvg", overlay=false) // === Inputs === length = input.int(10, title="Lookback Days") above_avg_color = input.color(color.green, title="Above Average Color") below_avg_color = input.color(color.red, title="Below Average Color") avg_line_color = input.color(color.blue, title="Matched Avg Line Color") // === Constants === maxBarsPerSession = 500 // max bars expected per day (adjust for smaller timeframes) // === Helper: detect new session === isNewDay() => t = time('D') na(t[1]) and not na(t) or t[1] < t // === Tracking bar index per day === var int barIndexToday = na if isNewDay() barIndexToday := 0 else barIndexToday += 1 // === Volume history storage (1D array simulating 2D [day][bar]) === var flatVolArray = array.new_float() var int currentDayIndex = 0 // When new day starts, increment day index if isNewDay() currentDayIndex += 1 if currentDayIndex > length currentDayIndex := 0 // wrap around to reuse slots // Save current bar's volume at flattened index storeIndex = currentDayIndex * maxBarsPerSession + barIndexToday if array.size(flatVolArray) <= storeIndex // extend the array as needed for i = array.size(flatVolArray) to storeIndex array.push(flatVolArray, na) array.set(flatVolArray, storeIndex, volume) else array.set(flatVolArray, storeIndex, volume) // === Calculate average volume at same bar index across prior days === float matched_avg = na int count = 0 float sum = 0.0 if barIndexToday < maxBarsPerSession for i = 0 to length - 1 if i == currentDayIndex continue // skip today's data idx = i * maxBarsPerSession + barIndexToday if idx < array.size(flatVolArray) v = array.get(flatVolArray, idx) if not na(v) sum += v count += 1 matched_avg := count > 0 ? sum / count : na else matched_avg := na // === Fallback for non-intraday charts === is_intraday = not (timeframe.isdaily or timeframe.isweekly or timeframe.ismonthly) matched_avg := is_intraday ? matched_avg : ta.sma(volume, length) // === Plotting === centered_vol = volume - matched_avg volColor = volume > matched_avg ? above_avg_color : below_avg_color plot(volume, title="Volume", style=plot.style_columns, color=volColor) plot(matched_avg, title="Time-Matched Avg", color=avg_line_color, linewidth=2) hline(0, title="Baseline", color=color.gray, linestyle=hline.style_dashed) plot(centered_vol, title="Volume - Matched Avg", color=color.purple, linewidth=1)
Editor is loading...
Leave a Comment