Live data (OpenAlgo)
The chart depends only on the DataFeed / TradeFeed interfaces, so any source fits behind
a small adapter. OpenAlgo adapters are included.
Historical bars (REST)
import { OpenAlgoDataFeed } from 'openalgo-charts';
const feed = new OpenAlgoDataFeed({ baseUrl: 'http://127.0.0.1:5000', apiKey: 'YOUR_KEY' });
const day = 86400;
const bars = await feed.getBars({
symbol: 'RELIANCE', exchange: 'NSE', interval: '1m',
from: Math.floor(Date.now() / 1000) - 7 * day,
to: Math.floor(Date.now() / 1000),
});
series.setData(bars);from / to are UTC seconds; the adapter converts them to the IST date range OpenAlgo’s
/api/v1/history expects, and normalizes returned timestamps (epoch seconds, epoch ms, or
IST strings) back to UTC seconds.
Realtime (WebSocket)
LTP / Quote / Depth come from the WS adapter. Feed its LTP ticks through a CandleBuilder
and call series.update():
import { OpenAlgoWsFeed, CandleBuilder } from 'openalgo-charts';
const ws = new OpenAlgoWsFeed({ url: 'ws://127.0.0.1:8765', apiKey: 'YOUR_KEY' });
const builder = new CandleBuilder({ intervalSec: 60, volumeMode: 'ltq-sum' });
ws.connect();
ws.onLtp((e) => {
const u = builder.onTick({ time: e.timeSec, price: e.ltp, ltq: e.ltq });
if (u) series.update(u.bar);
});
ws.subscribe('LTP', 'RELIANCE', 'NSE');The WS adapter speaks the documented OpenAlgo protocol (authenticate → numeric-mode subscribe
→ market_data), with connection and control callbacks (onState / onControl).
Real-time order updates
OpenAlgo also streams order lifecycle events — fills, partial fills,
rejections, cancellations — over the same socket (subscribe_orders). This is
an account-level stream (no symbols/modes), pushed by the broker in live mode
or by the sandbox engine in analyze mode. Use it to update on-chart order lines
instantly instead of polling the order book:
ws.onOrderUpdate((e) => {
// e: { orderId, symbol, action, quantity, price, triggerPrice?, pricetype,
// status, filledQuantity, averagePrice, rejectionReason, mode, ... }
const working = e.status === 'open' || e.status === 'trigger pending' || e.status === 'pending';
if (working) upsertOrderLine(e);
else removeOrderLine(e.orderId); // complete / cancelled / rejected
});
ws.subscribeOrders(); // replayed automatically on reconnect
// ws.unsubscribeOrders() to stopstatus is OpenAlgo’s lowercase order vocabulary (open, trigger pending,
complete, rejected, cancelled, …); mapOrderStatus (exported from the
base package) converts it to the chart’s Order['status']. Keep a slow
order-book poll as reconciliation — brokers can deliver the same transition
twice (dedupe on orderId + status + filledQuantity).
After an unexpected close it auto-reconnects with exponential backoff, then
re-authenticates and replays every active subscription. close() is treated as
intentional and never reconnects. onState reports a 'reconnecting' phase in
between. Tune or disable it:
new OpenAlgoWsFeed({
url, apiKey,
reconnect: { enabled: true, baseDelayMs: 1000, maxDelayMs: 30000, maxAttempts: Infinity },
});All-in-one live feed
OpenAlgoLiveDataFeed composes REST history + WebSocket + the candle builder so you get a
seamless history → live seam from a single object:
import { OpenAlgoLiveDataFeed } from 'openalgo-charts';
const live = new OpenAlgoLiveDataFeed({
baseUrl: 'http://127.0.0.1:5000', wsUrl: 'ws://127.0.0.1:8765', apiKey: 'YOUR_KEY',
});
const bars = await live.getBars({ symbol: 'RELIANCE', exchange: 'NSE', interval: '1m', from, to });
series.setData(bars);
// Pass seedFrom to continue the last history bar's bucket seamlessly.
// Pass cumDayVolumeSoFar if using volumeMode: 'day-delta' to diff against the right baseline.
live.subscribeBars(
{ symbol: 'RELIANCE', exchange: 'NSE', interval: '1m', from, to },
(bar) => series.update(bar),
{ seedFrom: bars[bars.length - 1] },
);Your API key lives only in your app/browser - never commit it. Verify the exact REST paths and WS message schema against your running OpenAlgo build before production use, and prefer analyzer mode while testing the trade path.
A complete, runnable example (history + WebSocket + chart trading) lives in the repository
under examples/live.