Datafeeds
A datafeed is how bars reach the chart. It is an object with four functions, and the chart decides when to call them. Once you know the rhythm, connecting any data source is straightforward.
The contract
Drawing diagram…
| Function | Called when | You return |
|---|---|---|
searchSymbols(search?) | you type in the symbol box | Promise<SymbolInfo[]> |
getHistoryData(symbol, period, from, to) | the chart loads, and when you scroll back | Promise<CandleData[]>, oldest first |
subscribe(symbol, period, callback) | history has loaded | nothing; call callback(bar) as prices change |
unsubscribe(symbol, period) | the symbol or period changes | nothing; stop sending |
Watch it happen
This chart runs on a datafeed that reports every call it receives. Use the buttons, or drag the chart to the right to scroll back in time, and watch the log. You will see getHistoryData asked for an earlier range, and each live update arrive as a callback.
0 history0 subscribe0 unsubscribe0 updates sent
- Waiting for the chart to call your datafeed…
Rules to remember
- Timestamps are in milliseconds, not seconds.
getHistoryDatareturns bars oldest first, inside the range it was asked for.- For live updates, the same timestamp as the last bar replaces it; a newer one adds a bar; an older one is ignored. See the simulator in Getting started, step 4.
- If history fails, return an empty array or throw: the chart shows nothing (or the saved bars, if the history cache is on).
Write your own
This is the shape of a feed backed by a REST API and a WebSocket. The addresses are placeholders.
import type { Datafeed } from 'astroneum'
let socket: WebSocket | undefined
const datafeed: Datafeed = {
searchSymbols: async (query = '') => {
const res = await fetch(`/api/symbols?q=${encodeURIComponent(query)}`)
return res.json()
},
getHistoryData: async (symbol, period, from, to) => {
const res = await fetch(`/api/bars?symbol=${symbol.ticker}&tf=${period.text}&from=${from}&to=${to}`)
const rows: Array<{ t: number, o: number, h: number, l: number, c: number, v: number }> = await res.json()
return rows.map(r => ({ timestamp: r.t, open: r.o, high: r.h, low: r.l, close: r.c, volume: r.v }))
},
subscribe: (symbol, period, callback) => {
socket = new WebSocket(`wss://example.com/stream?symbol=${symbol.ticker}&tf=${period.text}`)
socket.onmessage = (event) => {
const r = JSON.parse(event.data)
callback({ timestamp: r.t, open: r.o, high: r.h, low: r.l, close: r.c, volume: r.v })
}
},
unsubscribe: () => socket?.close(),
}The full datafeed guide on GitHub covers a REST feed, a WebSocket feed, error handling and smooth animation of ticks.
Which datafeed should I use?
Drawing diagram…
| Datafeed | What it is |
|---|---|
createStandardCryptoDatafeed() | Binance, Bitget and OKX futures, 100+ symbols, real-time WebSocket. Needs no key. |
DefaultDatafeed, WebSocketDatafeed | Polygon.io REST and WebSocket (astroneum/datafeeds/polygon). |
WebTransportDatafeed | Experimental. A documented protocol over HTTP/3 (astroneum/datafeeds/webtransport). |
createTransformedDatafeed(feed, ...) | Wraps any datafeed to serve Heikin-Ashi, Renko or range bars. |