Data
createDatafeed
Turn plain functions into a datafeed. You return data in whatever shape you have; it does the rest.
import { createDatafeed } from "@fx-charting/pro";
const datafeed = createDatafeed({
symbols, // list (or async search function) — what the symbol search shows
resolve, // optional: details of a symbol that isn't in the list
history, // required: historical bars
ticks, // live prices → candles are built for every interval
stream, // …or live bars of one interval
poll, // …or re-fetch the newest bars every N ms
});| Option | Type | What it does |
|---|---|---|
symbols | array | (query) => Promise<array> | Strings ("EURUSD") or objects { symbol, description, type, digits, spread, exchange }. Missing fields are guessed (6-letter = forex with 5 digits, JPY pairs 3, …). |
resolve | (symbol) => info | Details of a symbol not in the list (e.g. fetched from your API). |
history | (req) => Promise<bars> | See Historical data. Called again with `to` as the user scrolls back. |
ticks | (symbol, onTick, info) => unsubscribe | Push prices: onTick(1.0852) or onTick({ price, time, volume, bid, ask }). One subscription per symbol, shared by all intervals. |
stream | ({ symbol, interval }, onBar) => unsubscribe | Push the bar being formed for that interval (repeat it as it changes). |
poll | number (ms) | No push source? The newest bars are re-fetched every N ms. |
maxPages | number (5) | Extra history calls when your API returns fewer bars than asked. |
▶ Live example — history + simulated ticks
let price = 100;
const datafeed = createDatafeed({
symbols: [{ symbol: "DEMO", description: "Simulated ticks", digits: 2 }],
history: async ({ to, countBack, intervalMs }) => makeRandomBars(to, countBack, intervalMs),
ticks: (symbol, onTick) => {
const t = setInterval(() => {
price *= 1 + (Math.random() - 0.5) * 0.002;
onTick({ price, volume: 5 });
}, 300);
return () => clearInterval(t);
},
});