本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// [SignalFI] Tick Speed Δvol — the Δvol column of [SignalFI] Tick Speed drawn on the price chart:
// five boxes just right of the live candle, top to bottom 1T → 10T → 100T → 1000T → WTD. Text = volume
// delta direction and %, box shade = the dashboard heat colour (hue = Δvol, opacity = speed).
// A fifth row (WTD) is the weighted composite of the four: Σ w·Δvol / Σ w over the scales that
// are on and have data. 1000T weighs most by default, so it reads calmer than the 1T/10T rows.
// Its box shade averages the speeds the same way, each capped at SPD_CAP first.
//
// The calculation core is copied unchanged from TickSpeed.pine (see its header for details):
// Δvol : volume delta in [-1, 1]. 10T/100T/1000T: contracts of up-moving candles minus
// down-moving ones, ÷ total (exact at 10T, coarser at 1000T). 1T: aggressor split —
// trades at/above the ask (or above mid) are buys, at/below the bid are sells,
// uptick/downtick rule when bid/ask are missing or tied.
// speed: tick× / vol× / Blend = √(tick×·vol×), each ÷ its own EMA baseline (1.0 = normal).
//
// Requests use a standard ticker built from prefix + ticker + session: on a Heikin Ashi (or
// other non-standard) chart, syminfo.tickerid would return HA-transformed tick data.
// Tick candles are de-duplicated by start time; the 1T data clock drives the stall rule
// (realtime tick data can arrive up to ~12 s late).
//
// History depth is limited by TradingView's tick history and the intrabar cap: on MNQ1! 1m
// (2026-09-23) 1T reached ~300 bars back, 10T ~1,000, 100T ~14,000, 1000T the whole chart.
indicator('[SignalFI] Tick Speed Δvol', shorttitle = 'TickΔvol', overlay = true, max_labels_count = 10)
// ╔════════════════════════════════╗ //
// ║ INPUTS ║ //
// ╚════════════════════════════════╝ //
grpScale = 'Tick Scales'
// Each scale is forced off on tick charts (a notice label is shown instead).
on1 = input.bool(true, '1T', group = grpScale, inline = 's1', tooltip = '1T has the shortest history (the intrabar cap is reached first). Its speed is ticks per second within the chart bar and its Δvol is the bid/ask aggressor split.') and not timeframe.isticks
w1 = input.float(0.1, 'Weight', minval = 0, step = 0.05, group = grpScale, inline = 's1')
on10 = input.bool(true, '10T', group = grpScale, inline = 's10') and not timeframe.isticks
w10 = input.float(0.2, 'Weight', minval = 0, step = 0.05, group = grpScale, inline = 's10')
m10 = input.int(5, 'Candles', minval = 1, maxval = 50, group = grpScale, inline = 's10')
on100 = input.bool(true, '100T', group = grpScale, inline = 's100') and not timeframe.isticks
w100 = input.float(0.3, 'Weight', minval = 0, step = 0.05, group = grpScale, inline = 's100')
m100 = input.int(5, 'Candles', minval = 1, maxval = 50, group = grpScale, inline = 's100')
on1k = input.bool(true, '1000T', group = grpScale, inline = 's1k') and not timeframe.isticks
w1k = input.float(0.4, 'Weight', minval = 0, step = 0.05, group = grpScale, inline = 's1k')
m1k = input.int(3, 'Candles', minval = 1, maxval = 50, group = grpScale, inline = 's1k', tooltip = 'Candles = how many closed tick candles the speed and volume delta are averaged over. Weight = contribution to the weighted WTD row.')
grpVol = 'Volume'
speedBasis = input.string('Blend', 'Speed Basis', options = ['Ticks', 'Volume', 'Blend'], group = grpVol, tooltip = 'What drives box opacity. Ticks = trade-print pace (tick×). Volume = contract pace (vol×). Blend = √(tick× · vol×): 1.0 when both are normal, high only when prints and contracts both run hot.')
grpCalc = 'Calculation'
baseLen = input.int(50, 'Speed Baseline (bars)', minval = 2, group = grpCalc, tooltip = 'EMA length, in chart bars, of each scale\'s tick rate and volume rate. ×norm = current value ÷ this baseline, so 1.0 is normal and 2.0 is twice normal.')
grpVis = 'Visuals'
upCol = input.color(#0098ff, 'Up', group = grpVis, inline = 'col')
dnCol = input.color(#ff0000, 'Down', group = grpVis, inline = 'col')
neuCol = input.color(#888888, 'Neutral', group = grpVis, inline = 'col')
grpLbl = 'Labels'
lblOffset = input.int(3, 'Offset (bars)', minval = 0, maxval = 100, group = grpLbl, tooltip = 'How many bars right of the live candle the boxes start.')
rowSpace = input.float(0.5, 'Row Spacing (ATR)', minval = 0.1, step = 0.1, group = grpLbl, tooltip = 'Vertical distance between rows, in ATR(14) of the chart. The stack is centred on the last close. Raise it if the boxes overlap when zoomed out.')
lblSizeStr = input.string('Small', 'Text Size', options = ['Tiny', 'Small', 'Normal'], group = grpLbl)
showBar = input.bool(true, 'Δvol Oscillator Bar', group = grpLbl, tooltip = 'Adds a centre-zero text bar to each label: negative Δvol fills left of │, positive fills right, 10% per cell (100% = full side). The text colour then fades from Neutral at 0% to Up/Down at 100%. Off = plain labels.')
// ╔════════════════════════════════╗ //
// ║ CALCULATIONS ║ //
// ╚════════════════════════════════╝ //
BUF_CAP = 64
STALL_GRACE = 15000 // ms; covers the observed realtime tick-data delay
DIR_DEADBAND = 0.05 // |Δvol| at or below this reads as neutral (arrow and colour)
ATR_LEN = 14
ROW_MID = 2.0 // centre row of the 5-row stack (rows 0 … 4), placed on the last close
SOLID_T = 50 // heat-box transparency below which the text switches to the chart foreground
SPD_CAP = 2.5 // speed at which f_heat is already fully opaque; caps each scale in the WTD average
BAR_N = 10 // oscillator cells per side; one cell = 100% / BAR_N
PCT_W = 6 // width of the padded value column, e.g. '▲ 100%'
V_FULL = '███' // vertical WTD bar cells (3 characters wide): full, half toward the zero line, empty, zero line
V_TRK = ' · '
// Standard-candle ticker with the chart's session (see header: Heikin Ashi charts).
tid = ticker.new(syminfo.prefix, syminfo.ticker, syminfo.session)
// Wall-clock end of the measured window: the bar close on history, "now" on the live bar.
f_now() =>
barstate.isrealtime ? math.min(timenow, time_close) : time_close
// Value ÷ its own EMA baseline (EMA that skips na). The previous baseline is used, so a
// spike is not diluted by itself. First sample reads as 1.0. Call it at global scope once per
// series: each written call owns one baseline.
f_rel(float x) =>
var float base = na
float rel = na
if not na(x)
rel := na(base) or base <= 0 ? 1.0 : x / base
float alpha = 2.0 / (baseLen + 1)
base := na(base) ? x : base + alpha * (x - base)
rel
// One N-tick scale. Buffers hold each tick candle's start time, open and volume, oldest first,
// newest = the forming candle (its volume is overwritten each time it is reported again).
// Window = the last m closed candles. nowT = data clock (see header). Returns
// [secsPerCandle, ticksPerSec, contractsPerSec, contractsPerTick, priceDir, volDelta, added].
f_scale(simple string tf, simple int n, simple int m, array<int> bt, array<float> bo, array<float> bv, int nowT) =>
[ta_, oa_, va_] = request.security_lower_tf(tid, tf, [time, open, volume], ignore_invalid_timeframe = true)
int sz = na(ta_) ? 0 : array.size(ta_)
int added = 0
if sz > 0
for j = 0 to sz - 1
int tj = array.get(ta_, j)
int k0 = array.size(bt)
if k0 == 0 or tj > array.last(bt)
array.push(bt, tj)
array.push(bo, array.get(oa_, j))
array.push(bv, nz(array.get(va_, j)))
added += 1
if array.size(bt) > BUF_CAP
array.shift(bt)
array.shift(bo)
array.shift(bv)
else if tj == array.last(bt)
array.set(bv, k0 - 1, nz(array.get(va_, j)))
float secs = na
float dir = na
float vRate = na
float perTick = na
float dVol = na
int k = array.size(bt)
if k > m
int tNew = array.get(bt, k - 1)
float span = (tNew - array.get(bt, k - 1 - m)) / 1000.0 / m
float formingSec = (nowT - tNew) / 1000.0
secs := math.max(span, formingSec)
float net = array.get(bo, k - 1) - array.get(bo, k - 1 - m)
float gross = 0.0
float vSum = 0.0
float vNet = 0.0
// candle i-1: move = open[i] - open[i-1], volume = bv[i-1] (closed, final)
for i = k - m to k - 1
float mv = array.get(bo, i) - array.get(bo, i - 1)
float vc = array.get(bv, i - 1)
gross += math.abs(mv)
vSum += vc
vNet += math.sign(mv) * vc
dir := gross > 0 ? net / gross : 0.0
vRate := vSum / m / secs
perTick := vSum / (m * n)
dVol := vSum > 0 ? vNet / vSum : 0.0
[secs, na(secs) ? na : n / secs, vRate, perTick, dir, dVol, added]
// 1T: every element is one trade. Speed = ticks per second in the chart bar; price direction =
// uptick/downtick balance (zero-change ticks ignored); Δvol = aggressor split (TradingView ta
// library rule), falling back to the tick rule. Tick-rule state is carried across chart bars.
// Also returns the data clock: newest tick time seen, or the wall clock when that tick is
// older than STALL_GRACE (or there is no 1T history).
f_ticks() =>
[tArr, cArr, vArr, bArr, aArr] = request.security_lower_tf(tid, '1T', [time, close, volume, bid, ask], ignore_invalid_timeframe = true)
int sz = na(tArr) ? 0 : array.size(tArr)
var float lastPx = na
var bool lastBuy = true
var int lastTickT = na
float secs = na
float dir = na
float vRate = na
float perTick = na
float dVol = na
if sz > 0
lastTickT := array.last(tArr)
int wall = f_now()
int nowT = na(lastTickT) ? wall : math.max(lastTickT, wall - STALL_GRACE)
if sz > 0
int ups = 0
int dns = 0
float buyV = 0.0
float sellV = 0.0
for i = 0 to sz - 1
float px = array.get(cArr, i)
float b = array.get(bArr, i)
float a = array.get(aArr, i)
bool tickUp = not na(lastPx) and px > lastPx
bool tickDn = not na(lastPx) and px < lastPx
if tickUp
ups += 1
else if tickDn
dns += 1
bool isBuy = tickUp ? true : tickDn ? false : lastBuy
if not na(b) and not na(a) and a > b
float mid = (a + b) / 2
isBuy := px >= a ? true : px <= b ? false : px > mid ? true : px < mid ? false : isBuy
if isBuy
buyV += nz(array.get(vArr, i))
else
sellV += nz(array.get(vArr, i))
lastBuy := isBuy
lastPx := px
float elapsed = math.max(nowT - time, 1000) / 1000.0
float vTot = buyV + sellV
secs := elapsed / sz
dir := ups + dns > 0 ? (ups - dns) / (ups + dns) : 0.0
vRate := vTot / elapsed
perTick := vTot / sz
dVol := vTot > 0 ? (buyV - sellV) / vTot : 0.0
[secs, na(secs) ? na : 1.0 / secs, vRate, perTick, dir, dVol, sz, nowT]
var array<int> bt10 = array.new<int>()
var array<float> bo10 = array.new<float>()
var array<float> bv10 = array.new<float>()
var array<int> bt100 = array.new<int>()
var array<float> bo100 = array.new<float>()
var array<float> bv100 = array.new<float>()
var array<int> bt1k = array.new<int>()
var array<float> bo1k = array.new<float>()
var array<float> bv1k = array.new<float>()
[s1, tr1, vr1, pt1, pd1, vd1, n1, nowT] = f_ticks()
[s10, tr10, vr10, pt10, pd10, vd10, n10] = f_scale('10T', 10, m10, bt10, bo10, bv10, nowT)
[s100, tr100, vr100, pt100, pd100, vd100, n100] = f_scale('100T', 100, m100, bt100, bo100, bv100, nowT)
[s1k, tr1k, vr1k, pt1k, pd1k, vd1k, n1k] = f_scale('1000T', 1000, m1k, bt1k, bo1k, bv1k, nowT)
// ×norm: one written f_rel call per series → one independent EMA baseline each.
tx1 = f_rel(tr1)
tx10 = f_rel(tr10)
tx100 = f_rel(tr100)
tx1k = f_rel(tr1k)
vx1 = f_rel(vr1)
vx10 = f_rel(vr10)
vx100 = f_rel(vr100)
vx1k = f_rel(vr1k)
// Selected speed per scale (Speed Basis input).
f_spd(float tx, float vx) =>
switch speedBasis
'Ticks' => tx
'Volume' => vx
=> na(tx) or na(vx) ? na : math.sqrt(tx * vx)
sp1 = f_spd(tx1, vx1)
sp10 = f_spd(tx10, vx10)
sp100 = f_spd(tx100, vx100)
sp1k = f_spd(tx1k, vx1k)
atr = ta.atr(ATR_LEN)
// Weighted composite for the WTD row: Σ w·Δvol / Σ w and Σ w·speed / Σ w over the scales that
// are on and have data, renormalised over the ones that remain (na when none do). Speed is capped
// per scale first, so one spiking 1T/10T scale cannot saturate the composite box by itself.
f_wterm(bool on, float d, float sp, float w) =>
bool ok = on and not na(d) and not na(sp)
[ok ? w * d : 0.0, ok ? w * math.min(sp, SPD_CAP) : 0.0, ok ? w : 0.0]
[wd1, ws1, wq1] = f_wterm(on1, vd1, sp1, w1)
[wd10, ws10, wq10] = f_wterm(on10, vd10, sp10, w10)
[wd100, ws100, wq100] = f_wterm(on100, vd100, sp100, w100)
[wd1k, ws1k, wq1k] = f_wterm(on1k, vd1k, sp1k, w1k)
wSum = wq1 + wq10 + wq100 + wq1k
vdW = wSum > 0 ? (wd1 + wd10 + wd100 + wd1k) / wSum : na
spW = wSum > 0 ? (ws1 + ws10 + ws100 + ws1k) / wSum : na
// ╔════════════════════════════════╗ //
// ║ LABELS ║ //
// ╚════════════════════════════════╝ //
// Hue = direction (neutral → up/down), opacity = speed (0.5× faint … 2.5× solid).
f_heat(bool on, float d, float r) =>
color c = na
if on and not na(d) and not na(r)
color hue = d >= 0 ? color.from_gradient(d, 0, 1, neuCol, upCol) : color.from_gradient(d, -1, 0, dnCol, neuCol)
c := color.new(hue, 85 - 70 * math.max(0, math.min(1, (r - 0.5) / 2.0)))
c
f_dirTxt(float d) =>
na(d) ? '—' : (d > DIR_DEADBAND ? '▲ ' : d < -DIR_DEADBAND ? '▼ ' : '• ') + str.tostring(math.abs(d) * 100, '0') + '%'
// Arrow + |Δvol|% with the number left-padded to 3 digits, so the bars line up across rows.
f_pct3(float d) =>
string p = str.tostring(math.abs(d) * 100, '0')
(d > DIR_DEADBAND ? '▲ ' : d < -DIR_DEADBAND ? '▼ ' : '• ') + str.repeat(' ', 3 - str.length(p)) + p + '%'
// Centre-zero oscillator, BAR_N cells per side, half-cell resolution: negative fills left of │,
// positive fills right. Empty cells are a '·' track so the width never changes. na = empty track.
f_bar(float d) =>
int h = na(d) ? 0 : int(math.round(math.min(1.0, math.abs(d)) * BAR_N * 2))
int full = int(math.floor(h / 2))
bool half = h % 2 == 1
int gap = BAR_N - full - (half ? 1 : 0)
string trk = str.repeat('·', BAR_N)
h == 0 ? trk + '│' + trk : d < 0 ? str.repeat('·', gap) + (half ? '▐' : '') + str.repeat('█', full) + '│' + trk : trk + '│' + str.repeat('█', full) + (half ? '▌' : '') + str.repeat('·', gap)
// Vertical counterpart of f_bar for the left-hand WTD label: one line per cell, top to bottom
// BAR_N positive cells, the zero line, BAR_N negative cells. Positive fills upward from the zero
// line, negative downward; the half cell sits next to the zero line. na = empty track.
f_vbar(float d) =>
int h = na(d) ? 0 : int(math.round(math.min(1.0, math.abs(d)) * BAR_N * 2))
int full = int(math.floor(h / 2))
bool half = h % 2 == 1
array<string> up = array.new<string>(BAR_N, V_TRK)
array<string> dn = array.new<string>(BAR_N, V_TRK)
if h > 0 and d > 0
if full > 0
for i = 0 to full - 1
array.set(up, BAR_N - 1 - i, V_FULL)
if half
array.set(up, BAR_N - 1 - full, '▄▄▄')
else if h > 0 and d < 0
if full > 0
for i = 0 to full - 1
array.set(dn, i, V_FULL)
if half
array.set(dn, full, '▀▀▀')
array.join(up, '\n') + '\n───\n' + array.join(dn, '\n')
lblSize = lblSizeStr == 'Tiny' ? size.tiny : lblSizeStr == 'Normal' ? size.normal : size.small
idleBg = color.new(neuCol, 85)
f_newLbl() =>
label.new(na, na, '', xloc = xloc.bar_index, style = label.style_label_left, textalign = text.align_left, text_font_family = font.family_monospace)
// Rows top to bottom: 0 = 1T … 3 = 1000T, 4 = weighted composite, centred on the last close.
var lbl1 = f_newLbl()
var lbl10 = f_newLbl()
var lbl100 = f_newLbl()
var lbl1k = f_newLbl()
var lblW = f_newLbl()
// Vertical WTD bar, left of the stack: its right edge meets the stack's left edge and its zero line
// (the middle line, between the name on top and the value at the bottom) sits on the last close.
var lblV = label.new(na, na, '', xloc = xloc.bar_index, style = label.style_label_right, textalign = text.align_center, text_font_family = font.family_monospace)
// Places one label in its row and styles it.
f_put(label lb, int row, string txt, color tc, color bg) =>
label.set_xy(lb, bar_index + lblOffset, close + (ROW_MID - row) * rowSpace * atr)
label.set_text(lb, txt)
label.set_textcolor(lb, tc)
label.set_color(lb, bg)
label.set_size(lb, lblSize)
// One row (a tick scale, or the WTD composite): same text, text colour and heat shade as the
// dashboard's Δvol cell. A row that is off or has no data keeps its place with a faint neutral
// box, so the stack never shifts. WTD reads 'off' only when every scale is off.
// When the box is opaque enough to carry the hue itself (fast scale), the text switches to the
// chart foreground colour — direction-coloured text on a solid box of the same hue is unreadable.
// With the oscillator bar on, the value is padded, the bar is appended and the direction colour
// becomes a gradient from Neutral (0%) to Up/Down (100%). The gradient runs on √|Δvol| so the
// common 10–40% readings already carry visible colour (linear left them almost grey).
f_row(label lb, int row, string name, bool on, float d, float sp) =>
bool ok = on and not na(d) and not na(sp)
string val = not on ? 'off' : ok ? f_dirTxt(d) : '—'
color bg = ok ? f_heat(true, d, sp) : idleBg
string txt = name + ' ' + val
color dirCol = d > DIR_DEADBAND ? upCol : d < -DIR_DEADBAND ? dnCol : neuCol
if showBar
string pv = ok ? f_pct3(d) : val + str.repeat(' ', PCT_W - str.length(val))
txt := name + ' ' + pv + ' ' + f_bar(ok ? d : na)
dirCol := d > DIR_DEADBAND ? color.from_gradient(math.sqrt(d), 0, 1, neuCol, upCol) : d < -DIR_DEADBAND ? color.from_gradient(math.sqrt(-d), 0, 1, neuCol, dnCol) : neuCol
color tc = not ok ? neuCol : color.t(bg) < SOLID_T ? chart.fg_color : dirCol
f_put(lb, row, txt, tc, bg)
// The vertical WTD label: same box shade and text colour as the WTD row with the bar on.
f_vlbl(bool on, float d, float sp) =>
bool ok = on and not na(d) and not na(sp)
string val = not on ? 'off' : ok ? f_pct3(d) : '—'
color bg = ok ? f_heat(true, d, sp) : idleBg
color dirCol = d > DIR_DEADBAND ? color.from_gradient(math.sqrt(d), 0, 1, neuCol, upCol) : d < -DIR_DEADBAND ? color.from_gradient(math.sqrt(-d), 0, 1, neuCol, dnCol) : neuCol
color tc = not ok ? neuCol : color.t(bg) < SOLID_T ? chart.fg_color : dirCol
label.set_xy(lblV, bar_index + lblOffset, close)
label.set_text(lblV, 'WTD\n' + f_vbar(ok ? d : na) + '\n' + val)
label.set_textcolor(lblV, tc)
label.set_color(lblV, bg)
label.set_size(lblV, lblSize)
if barstate.islast
if timeframe.isticks
f_put(lbl1, 0, 'Tick Speed Δvol: use a time-based chart (1m–5m)', neuCol, idleBg)
for lb in array.from(lbl10, lbl100, lbl1k, lblW, lblV)
label.set_xy(lb, bar_index, na)
else
f_row(lbl1, 0, '1T ', on1, vd1, sp1)
f_row(lbl10, 1, '10T ', on10, vd10, sp10)
f_row(lbl100, 2, '100T ', on100, vd100, sp100)
f_row(lbl1k, 3, '1000T', on1k, vd1k, sp1k)
f_row(lblW, 4, 'WTD ', on1 or on10 or on100 or on1k, vdW, spW)
f_vlbl(on1 or on10 or on100 or on1k, vdW, spW)