data.source#
Declare a data dependency at top-level.
SYNTAX#
function source<K extends SourceKind>(kind: K, opts?: { symbol?: string; exchange?: string; interval?: string; timeframe?: string; limit?: number }): SourceHandle<K>ARGUMENTS#
kind('ohlcv' | 'oi' | 'funding' | 'liquidations' | 'orderbook') — Which kind of data to fetch. Todayohlcvis supported; the other slots are reserved and return shaped-but-empty payloads.opts({ symbol?, exchange?, interval? | timeframe?, limit? }) — Optional overrides. When omitted, the host fills from chart context (ctx.symbol,ctx.exchange,ctx.timeframe).intervalandtimeframeare aliases.
RETURNS#
SourceHandle — { kind, values, data }. For ohlcv, values exposes columnar arrays: time[], open[], high[], low[], close[], volume[] (all aligned 1:1 with the chart's candles, NaN where unavailable).
REMARKS#
Top-level only. Calling source(...) inside onBar throws — the host needs all declarations before warmup so it can pre-fetch and dedup. Two scripts declaring the same (kind, opts) share one fetch via the host registry.
EXAMPLE#
// Declare the chart's own OHLCV — the host treats empty opts as "use chart context".
const chartOhlcv = source('ohlcv');
// Pin a different timeframe for a higher-timeframe filter.
const dailyOhlcv = source('ohlcv', { interval: '1D', limit: 200 });
function onBar(i, ctx) {
// Read pre-aligned columnar arrays via the handle.
const dailyClose = dailyOhlcv.values.close[i];
if (dailyClose !== undefined) plot('daily-close', dailyClose, { color: '#fbbf24' });
}