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 high-performance financial chart that updates in realtime using JavaScript and the SciChart.js library. It connects to the Binance Exchange for both historical candle and live trade data, updating candlestick charts dynamically while highlighting significant trades.
The implementation establishes WebSocket connections via the websocket-ts library to fetch candle and trade streams from Binance. These streams are merged using RxJS operators such as combineLatest and scan, a technique explained in detail in Ultimate Guide to RxJS Join Operators. The streaming data is processed and transformed into updated candlestick data with custom logic, and the chart is rendered using SciChart.js components such as the FastCandlestickRenderableSeries as described in The Candlestick Series type. In addition, moving average filters are applied to the data series to smooth trends, as covered in the Moving Average Filter Documentation. For WebSocket communication, best practices outlined in Writing WebSocket client applications have been followed.
The example offers several advanced features including realtime chart updates, dynamic switching between candlestick and OHLC series, and the visualization of large trades as bubbles. Custom palette providers dynamically color volume bars based on trade direction, which leverages the capabilities discussed in The PaletteProvider API. Axis animations are also implemented to smoothly adjust the visible range as new data arrives, ensuring an engaging user experience.
Developers can use this example as a blueprint for integrating live exchange data with SciChart.js in a JavaScript environment. The solution demonstrates the effective use of RxJS for managing realtime data subscriptions and handling stream merging to maintain performance. By employing WebSocket connections for direct market data integration and utilizing SciChart.js for rendering high-performance charts, the implementation adheres to best practices in both realtime data processing and graphical rendering. This approach is ideal for developers looking to build scalable, responsive financial applications with minimal overhead.












