本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Volume and Velocity Meter", shorttitle="Vol Speed", overlay=true)
// ============================================================================
//dougz
// INPUTS
// ============================================================================
windowSec = input.int(10, "Rolling window (seconds)", minval=1, maxval=120, group="Speed", tooltip="All meters are calculated over this many seconds. Shorter = faster but jumpier.")
fullScale = input.float(0.0, "Full-scale vol/sec (0 = auto)", minval=0, group="Speed", tooltip="Volume/sec that fills the SPEED bar all the way. 0 = auto: uses the highest reading seen since the script loaded. If you enter a number, make it high (watch the peak readout first) or the bar will stay full.")
ptsFull = input.float(2.0, "Full-scale points/sec (Direction bar)", minval=0.05, step=0.25, group="Speed", tooltip="Price speed in points per second that fills the DIRECTION bar all the way. Watch the pts/s readout for a session and set this near the fastest moves you see.")
showBS = input.bool(true, "Show Buy/Sell bar", group="Display")
showDir = input.bool(true, "Show Direction bar", group="Display")
segments = input.int(20, "Meter segments", minval=5, maxval=40, group="Display")
segWidth = input.float(1.5, "Segment width (% of chart)", minval=0.2, maxval=5, step=0.1, group="Display")
segHeight = input.float(2.5, "Segment height (% of chart)", minval=1, maxval=10, step=0.5, group="Display")
posInput = input.string("Top Center", "Position", options=["Top Left", "Top Center", "Top Right", "Bottom Left", "Bottom Center", "Bottom Right"], group="Display")
colSlow = input.color(color.rgb(255, 23, 68), "Speed: slow", group="Colors")
colMid = input.color(color.rgb(255, 193, 7), "Speed: mid", group="Colors")
colFast = input.color(color.rgb(0, 200, 83), "Speed: fast", group="Colors")
colSell = input.color(color.rgb(255, 23, 68), "Selling / Down", group="Colors")
colBuy = input.color(color.rgb(0, 200, 83), "Buying / Up", group="Colors")
colEmpty = input.color(color.rgb(120, 123, 134, 92), "Empty segment", group="Colors")
// ============================================================================
// LIVE TICK TRACKING (varip = keeps values between live updates)
// ============================================================================
varip float lastVol = na
varip float lastPrice = na
varip int lastDir = 0
varip int lastBarTime = na
varip int startTime = na
varip float peakVps = 0.0
varip array<float> buyVols = array.new<float>()
varip array<float> sellVols = array.new<float>()
varip array<float> tickPrices = array.new<float>()
varip array<int> tickTimes = array.new<int>()
float vps = na
float buyVps = na
float sellVps = na
float priceMove = na
float ptsPerSec = na
if barstate.isrealtime
float dv = 0.0
if na(lastBarTime)
// first live update after load: just set the baseline
startTime := timenow
else if time != lastBarTime
// new bar opened: its volume so far is all new
dv := volume
else
dv := math.max(volume - lastVol, 0.0)
// tick rule: uptick = buying, downtick = selling, unchanged = same as last move
if not na(lastPrice)
if close > lastPrice
lastDir := 1
else if close < lastPrice
lastDir := -1
// price at the start of this update (used to measure the move over the window)
float prevPrice = na(lastPrice) ? close : lastPrice
lastVol := volume
lastPrice := close
lastBarTime := time
if dv > 0
tickTimes.push(timenow)
tickPrices.push(prevPrice)
buyVols.push(lastDir > 0 ? dv : 0.0)
sellVols.push(lastDir < 0 ? dv : 0.0)
// drop updates older than the window
int cutoff = timenow - windowSec * 1000
while tickTimes.size() > 0 and tickTimes.first() < cutoff
tickTimes.shift()
tickPrices.shift()
buyVols.shift()
sellVols.shift()
// until a full window has passed, divide by the time actually elapsed
float elapsedSec = math.max((timenow - startTime) / 1000.0, 1.0)
float divisor = math.min(elapsedSec, windowSec)
float buySum = buyVols.size() > 0 ? buyVols.sum() : 0.0
float sellSum = sellVols.size() > 0 ? sellVols.sum() : 0.0
buyVps := buySum / divisor
sellVps := sellSum / divisor
vps := buyVps + sellVps
peakVps := math.max(peakVps, vps)
priceMove := tickPrices.size() > 0 ? close - tickPrices.first() : 0.0
ptsPerSec := priceMove / divisor
// ============================================================================
// METER DISPLAY
// ============================================================================
tablePos = switch posInput
"Top Left" => position.top_left
"Top Right" => position.top_right
"Bottom Left" => position.bottom_left
"Bottom Center" => position.bottom_center
"Bottom Right" => position.bottom_right
=> position.top_center
var table meter = table.new(tablePos, segments + 2, 3, border_width=2)
// one solid color for a whole row: pale when slow, full color when fast
shade(color base, float ratio) =>
color.new(base, 75 - 75 * ratio)
if barstate.islast
bool live = barstate.isrealtime and not na(vps)
color txtCol = live ? chart.fg_color : color.gray
// ---- Row 0: SPEED (volume/sec) — fills left to right, one color: red = slow, yellow = mid, green = fast ----
float scale = fullScale > 0 ? fullScale : peakVps
float ratio = live and scale > 0 ? math.min(vps / scale, 1.0) : 0.0
int filled = int(math.round(ratio * segments))
color speedCol = ratio <= 0.5 ? color.from_gradient(ratio, 0.0, 0.5, colSlow, colMid) : color.from_gradient(ratio, 0.5, 1.0, colMid, colFast)
table.cell(meter, 0, 0, "SPEED", text_color=txtCol, text_size=size.small)
for i = 1 to segments
table.cell(meter, i, 0, "", width=segWidth, height=segHeight, bgcolor=live and i <= filled ? speedCol : colEmpty)
string speedLive = str.tostring(vps, "#,##0.0") + "/s (pk " + str.tostring(peakVps, "#,##0") + ")"
table.cell(meter, segments + 1, 0, live ? speedLive : "LIVE ONLY", text_color=txtCol, text_size=size.small, text_halign=text.align_left)
// ---- Row 1: BUY/SELL — green share from left, red share for selling ----
if showBS
float buyShare = live and vps > 0 ? buyVps / vps : 0.5
int buySegs = int(math.round(buyShare * segments))
table.cell(meter, 0, 1, "BUY/SELL", text_color=txtCol, text_size=size.small)
for i = 1 to segments
color c = not live ? colEmpty : i <= buySegs ? colBuy : colSell
table.cell(meter, i, 1, "", width=segWidth, height=segHeight, bgcolor=c)
string bsLive = str.tostring(int(math.round(buyShare * 100))) + "% buy (" + str.tostring(buyVps, "#,##0.0") + " / " + str.tostring(sellVps, "#,##0.0") + ")"
table.cell(meter, segments + 1, 1, live ? bsLive : "LIVE ONLY", text_color=txtCol, text_size=size.small, text_halign=text.align_left)
// ---- Row 2: DIRECTION — green = price up, red = price down ----
// length and darkness = PRICE speed in points/sec (short + pale = slow, long + solid = fast)
if showDir
float pps = live ? ptsPerSec : 0.0
float pm = live ? priceMove : 0.0
int dir = pps > 0 ? 1 : pps < 0 ? -1 : 0
float dirRatio = math.min(math.abs(pps) / ptsFull, 1.0)
int dirSegs = int(math.round(dirRatio * segments))
color baseCol = dir > 0 ? colBuy : dir < 0 ? colSell : color.gray
color dirCol = shade(baseCol, dirRatio)
string arrow = dir > 0 ? "▲ UP" : dir < 0 ? "▼ DOWN" : "— FLAT"
table.cell(meter, 0, 2, live ? arrow : "DIRECTION", text_color=live ? chart.fg_color : color.gray, bgcolor=live and dir != 0 ? dirCol : na, text_size=size.small)
for i = 1 to segments
table.cell(meter, i, 2, "", width=segWidth, height=segHeight, bgcolor=live and i <= dirSegs ? dirCol : colEmpty)
string ppsTxt = (pps > 0 ? "+" : "") + str.tostring(pps, "0.00") + " pts/s (" + (pm > 0 ? "+" : "") + str.tostring(pm, format.mintick) + ")"
table.cell(meter, segments + 1, 2, live ? ppsTxt : "LIVE ONLY", text_color=txtCol, text_size=size.small, text_halign=text.align_left)
// values in the Data Window only (does not affect chart scale)
plot(vps, "Vol/sec", display=display.data_window)
plot(buyVps, "Buy vol/sec", display=display.data_window)
plot(sellVps, "Sell vol/sec", display=display.data_window)
plot(ptsPerSec, "Points/sec", display=display.data_window)