本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator(
"Volume vs. 10-Day Same-Time Average",
shorttitle="Volume 10D Avg",
overlay=false)
//description="Each volume bar is compared with the average volume of the 10 most recent eligible bars that started at the same time of day. The dot marks that average. Use Include weekends to include or exclude Saturday and Sunday from the intraday average. Bright green: volume is above average and the bar closed up. Bright red: volume is above average and the bar closed down. Light gray: volume is at or below average and the bar closed up. Dark gray: volume is at or below average and the bar closed down. Bar direction is estimated from close versus open; it does not represent actual bid/ask volume. On daily and higher timeframes, the dot uses the average of the previous 10 chart bars; the weekend option applies to intraday charts."
// Settings
includeWeekends = input.bool(true, "Include weekends (Saturday and Sunday)")
// Colors
brightGreen = color.rgb(0, 255, 40)
brightRed = color.rgb(255, 0, 25)
lightGray = color.rgb(195, 195, 195)
darkGray = color.rgb(95, 95, 95)
// Store up to 20 recent observations per time slot.
// This allows the script to find 10 weekday observations when weekends are excluded.
var matrix<float> volumeHistory = matrix.new<float>(1440, 20, na)
var matrix<int> dayHistory = matrix.new<int>(1440, 20, na)
var array<int> nextColumn = array.new_int(1440, 0)
// Bar's start time in the exchange's timezone.
timeSlot = hour(time, syminfo.timezone) * 60 + minute(time, syminfo.timezone)
// Calculate the average from the 10 most recent eligible observations
// at this same time of day.
float referenceAverage = na
if timeframe.isintraday
slotNextColumn = array.get(nextColumn, timeSlot)
float totalVolume = 0.0
int eligibleSamples = 0
// Read newest observations first from the circular buffer.
for offset = 1 to 20
column = (slotNextColumn - offset + 20) % 20
storedVolume = matrix.get(volumeHistory, timeSlot, column)
storedDay = matrix.get(dayHistory, timeSlot, column)
isWeekend = storedDay == dayofweek.saturday or storedDay == dayofweek.sunday
isEligible = includeWeekends or not isWeekend
if not na(storedVolume) and isEligible and eligibleSamples < 10
totalVolume += storedVolume
eligibleSamples += 1
if eligibleSamples > 0
referenceAverage := totalVolume / eligibleSamples
else
// Daily and higher timeframes use the previous 10 chart bars.
referenceAverage := ta.sma(volume[1], 10)
// Estimate which side won from the candle direction.
buyersWon = close >= open
volumeAboveAverage = not na(referenceAverage) and volume > referenceAverage
volumeColor = volumeAboveAverage
? (buyersWon ? brightGreen : brightRed)
: (buyersWon ? lightGray : darkGray)
// Plot volume bars and the average dot.
plot(volume, title="Volume", style=plot.style_columns, color=volumeColor)
plot(
referenceAverage,
title="10-Day Same-Time Average",
color=color.new(color.white, 15),
style=plot.style_circles,
linewidth=2
)
// Store the current volume after comparison, once the bar closes.
if timeframe.isintraday and barstate.isconfirmed
columnToWrite = array.get(nextColumn, timeSlot)
matrix.set(volumeHistory, timeSlot, columnToWrite, volume)
matrix.set(dayHistory, timeSlot, columnToWrite, dayofweek)
array.set(nextColumn, timeSlot, (columnToWrite + 1) % 20)