Skip to main content
This recipe shows you how to render a live price chart. A live chart needs candle history to draw and updates to the bar that is still open. The OHLCV endpoint provides the history, and the spot.candles stream provides the live updates.

Series parameters

Use the same three values for the history request and the subscription. Otherwise, the stream updates do not line up with the bars you drew.

1. Subscribe and load history

Subscribe first and wait for the ack before you read history, so that no update is missed between the two. See Initial snapshot.

2. Update the open bar

Each stream update contains the current state of one bucket, and the open bucket is sent again each time a new trade executes. Replace the bar with the same time, or append a new one.

Missing candles

Buckets with no trades are not sent, either in history or on the stream, so a quiet token has gaps between bars. Most charting libraries handle gaps. If yours needs a bar for every bucket, fill each gap with a flat bar at the previous close and zero volume.

Load older history

When the user scrolls left, request the bars before the oldest bar you hold. to is exclusive, so the oldest bar is not repeated.
An empty response means that you have reached the start of the token’s history.

TradingView datafeeds

The OHLCV endpoint follows the TradingView datafeed contract. In getBars, map periodParams.countBack to countBack and periodParams.to (seconds) to to in milliseconds. In subscribeBars, subscribe to spot.candles and pass each update to the onTick callback.

Changing series

To change the timeframe or metric, unsubscribe using the subscriptionId from the ack, subscribe with the new values, and reload history.