Connects to Binance Exchange to fetch historical data on 1-minute timeframe. Subscribes to WebSocket and listens to candles & trades. Candles are updated in realtime. You can zoom, pan the example or use tooltips. Large trades > $25,000 size are plotted as bubbles.
import { createCandlestickChart, sciChartOverview } from "./createCandlestickChart";
import { binanceSocketClient, TRealtimePriceBar } from "./binanceSocketClient";
import { Observable } from "rxjs";
import { simpleBinanceRestClient, TPriceBar } from "./binanceRestClient";
import { ExampleDataProvider } from "./ExampleDataProvider";
// SCICHART EXAMPLE
export const drawExample = (dataSource: string) => async (rootElement: string | HTMLDivElement) => {
// Create the candlestick chart example. Contains Candlestick series, tooltips, volume, zooming panning behaviour and more
const { sciChartSurface, controls } = await createCandlestickChart(rootElement);
const endDate = new Date(Date.now());
const startDate = new Date();
startDate.setMinutes(endDate.getMinutes() - 300);
let priceBars: TPriceBar[];
if (dataSource !== "Random") {
priceBars = await simpleBinanceRestClient.getCandles("BTCUSDT", "1m", startDate, endDate, 500, dataSource);
// Set the candles data on the chart
controls.setData("BTC/USDT", "Bitcoin / US Dollar - 1 Minute", priceBars);
} else {
priceBars = ExampleDataProvider.getRandomCandles(300, 60000, startDate, 60);
controls.setData("Random", "Random Data - 1 Minute", priceBars);
}
const startViewportRange = new Date();
startViewportRange.setMinutes(endDate.getMinutes() - 100);
endDate.setMinutes(endDate.getMinutes() + 10);
controls.setXRange(startViewportRange, endDate);
// Susbscribe to price updates from the exchange
let obs: Observable<TRealtimePriceBar>;
if (dataSource !== "Random") {
obs = binanceSocketClient.getRealtimeCandleStream("BTCUSDT", "1m");
} else {
const lastBar = priceBars[priceBars.length - 1];
const startBar: TRealtimePriceBar = {
symbol: "Random",
close: lastBar.close,
high: lastBar.high,
low: lastBar.low,
volume: lastBar.volume,
eventTime: new Date().getTime(),
open: lastBar.open,
openTime: lastBar.date * 1000,
closeTime: (lastBar.date + 60) * 1000,
interval: "1m",
lastTradeSize: 0,
lastTradeBuyOrSell: false,
};
obs = binanceSocketClient.getRandomCandleStream(startBar, 60000);
}
const subscription = obs.subscribe((pb) => {
const priceBar = {
date: pb.openTime,
open: pb.open,
high: pb.high,
low: pb.low,
close: pb.close,
volume: pb.volume,
};
controls.onNewTrade(priceBar, pb.lastTradeSize, pb.lastTradeBuyOrSell);
});
return { sciChartSurface, subscription, controls };
};
This example demonstrates a real-time ticking stock chart implemented in React Charts component using the high performance SciChart.js library.
The implementation leverages React components along with hooks and refs to integrate <SciChartReact/> and SciChartNestedOverview. Real-time updates are achieved using RxJS operators like combineLatest and scan to merge and process live data streams from a custom WebSocket client powered by the websocket-ts package. For an in-depth discussion about real-time data streaming with RxJS in a React application, see Reactive Programming in React With RxJS.
This example showcases several advanced features, including real-time chart updates, dynamic switching between candlestick and OHLC series, and the visualization of large trades above a $25,000 threshold with custom bubble renderings. It also applies moving average filters on streaming data, as detailed in the Moving Average Filter | JavaScript Chart Documentation - SciChart. These capabilities highlight the customizability and performance strengths of SciChart.js when integrated within a React framework.
The integration follows best practices for React by encapsulating the SciChart surface and controls in reusable components and managing state through hooks. The use of <SciChartReact/> ensures a smooth React integration as demonstrated in React Charts with SciChart.js: Introducing “SciChart React”. Real-time WebSocket data is efficiently managed with RxJS, aligning with guidelines from The complete guide to WebSockets with React. Additionally, the example implements animated transitions for axis range updates, a technique explored in Axis Ranging - How to Listen to VisibleRange Changes. Together, these practices ensure optimal performance and a seamless user experience.












