Aggregated CVD
One cumulative volume-delta line summed across venues, with volatility bands and sign coloring, on whichever coin the chart shows.
Cumulative volume delta tracks the running difference between aggressive buying and aggressive selling. On a single venue it tells a partial story, because flow splits across exchanges. Coin legs read each venue's market for the chart's coin, so the line sums the flow of every venue you pick and follows the chart when you switch coins.
The wrun indicator
param("band_len", 60, { min: 10, max: 500, description: "Bars in the volatility band" });
param.multi("venues", ["Binance perp", "Bybit perp", "Binance", "Coinbase"], ["Binance perp", "Bybit perp", "Binance", "Coinbase"], { label: "Venues" });
param.choice("unit", ["USD", "Coin"], "USD", { label: "Unit" });
// The chart's own candles are the grid every other input lines up on.
input("close", ohlcv.close);
// One buy and one sell input per venue, each on that venue's market for the chart's coin.
input("bnf_buy", trades.volume, { exchange: "BINANCE_FUTURES", follow: "coin", side: "BUY", currency: "@unit", missing: "zero", when: "venues", when_bit: 0 });
input("bnf_sell", trades.volume, { exchange: "BINANCE_FUTURES", follow: "coin", side: "SELL", currency: "@unit", missing: "zero", when: "venues", when_bit: 0 });
input("bybit_buy", trades.volume, { exchange: "BYBIT", follow: "coin", side: "BUY", currency: "@unit", missing: "zero", when: "venues", when_bit: 1 });
input("bybit_sell", trades.volume, { exchange: "BYBIT", follow: "coin", side: "SELL", currency: "@unit", missing: "zero", when: "venues", when_bit: 1 });
input("bn_buy", trades.volume, { exchange: "BINANCE", follow: "coin", side: "BUY", currency: "@unit", missing: "zero", when: "venues", when_bit: 2 });
input("bn_sell", trades.volume, { exchange: "BINANCE", follow: "coin", side: "SELL", currency: "@unit", missing: "zero", when: "venues", when_bit: 2 });
input("cb_buy", trades.volume, { exchange: "COINBASE", follow: "coin", side: "BUY", currency: "@unit", missing: "zero", when: "venues", when_bit: 3 });
input("cb_sell", trades.volume, { exchange: "COINBASE", follow: "coin", side: "SELL", currency: "@unit", missing: "zero", when: "venues", when_bit: 3 });
output("cvd", line, lower, {
width: 2,
color_by: "sign",
colors: ["#ff5b7f", "#22d3a5"],
description: "Cumulative volume delta over the picked venues",
});
output("upper_band", line, lower, { color: "#22d3a5", opacity: 0.35, description: "CVD plus one standard deviation" });
output("lower_band", line, lower, { color: "#ff5b7f", opacity: 0.35, description: "CVD minus one standard deviation" });
output("sign", none, lower);
let stdev = new Stdev(60);
let cvd: f64 = 0.0;
let picked: i32 = 0;
function onStart(): void {
stdev = new Stdev(i32(p_band_len()));
picked = i32(p_venues());
}
// A venue's net flow while it is picked; an unpicked venue, or one that lists no market for the coin, adds 0.
function net(bit: i32, buy: f64, sell: f64): f64 {
return (picked & (1 << bit)) != 0 ? buy - sell : 0.0;
}
function onBar(): void {
// A module-level variable is the accumulator: it survives from bar to bar.
cvd += net(0, in_bnf_buy(), in_bnf_sell()) + net(1, in_bybit_buy(), in_bybit_sell());
cvd += net(2, in_bn_buy(), in_bn_sell()) + net(3, in_cb_buy(), in_cb_sell());
const band = stdev.update(cvd);
const width = isNaN(band) ? 0.0 : band;
out_cvd(cvd);
out_upper_band(cvd + width);
out_lower_band(cvd - width);
out_sign(cvd >= 0.0 ? 1.0 : 0.0);
}How it works
The timeline spine. input("close", ohlcv.close) is never read by the code. It is the first input, and the first input defines the grid every other input lines up on: one row per candle, always. Without it, the timeline would depend on the trade feeds resolving, and on a market where they are missing the module would have no bars to compute. Load the chart series first, always.
Coin legs. Each venue is two trades.volume inputs, side: "BUY" and side: "SELL", with follow: "coin" beside its exchange: the chart reads that venue's main market for the chart's coin (BTCUSDT on Binance on a BTC chart, ETH-USD on Coinbase on an ETH chart) and picks again when you switch coins. A venue that lists no market for the coin reads 0 under missing: "zero", so it adds nothing.
The venue picker. venues is a multi setting, one bit per venue in its choice order. when: "venues" with when_bit ties each leg to its option, so the chart fetches only the picked venues, and net() reads the same bits, so the line is right on a host that fetches every leg anyway.
The unit. currency: "@unit" binds every leg's unit to the unit choice: dollars by default, coins when the reader picks Coin. Dollar volume adds up across venues; coin counts add up too, since every leg here trades the same coin.
Cumulative means module-level. cvd is a module-level variable and each bar adds every picked venue's net flow. That persistence is what turns a per-bar delta into a cumulative line.
Sign coloring and the band. sign is a data-only 1 or 0, and the cvd line indexes its two-entry palette with it: green when the running total is positive, red when negative. Stdev over the running total gives the one-sigma envelope; while it is warming the band width is 0, so the edges sit on the line instead of vanishing.
Design notes
- The legs follow the chart's coin, never its venue: on a BTC chart on any exchange the line sums the four venues' BTC flow. A coin the venues do not list reads
0from them, so the line stays flat. - The sign tint is
color_byover a data-only output. To shade between the bands, declare arange()between the two band outputs, which the chart draws as a filled band, or a one-bar box between them (the Anchored VWAP recipe shades its band that way). - An alert refuses a coin leg by name, so this Indicator draws on the chart only; for an alert, pin each venue's market with
symbolandexchange.
Customize it
- Another venue. Add a pair of legs with that venue's
exchangeid, an option invenuesfor it, and awhen_bitand anet()term for its bit. - Band width.
band_lensets the volatility lookback. Shorten it for a reactive envelope that hugs the line, lengthen it for a smoother, slower band. - Colors. The palette on
cvdand thecoloron each band are hex strings on the declarations; recolor there. - Alert on the line. Alerts refuse coin legs, so alert on a copy whose legs pin each venue's market with
symbolandexchange(Alerts).
Run it
- In the editor's Explorer, press New indicator and pick Blank indicator: the tab holds the
//@lang=wrun-tsline alone. - Paste the indicator block above under that line and press Run on a crypto perp or spot market.
- At the editor's Console prompt, type
last 20 cvdto read the running total over the last 20 bars.
Concepts used
- Multi-source for coin legs and the setting gate
- Data sources for
trades.volumewith asideand themissingpolicies - Execution model for the module-level accumulator
- Volume and VWAP for buy and sell volume as inputs