toTradingViewDatafeed
Adapt a CandleSource into a TradingView Charting Library datafeed.
A pure translation layer: resolution codes, unix-second period params, and the library’s callback style on one side; the SDK’s resolution vocabulary, millisecond timestamps, and promise/Unwatch style on the other. It holds no framework state and imports nothing from TradingView — the datafeed contract is modeled structurally, so the returned object satisfies the library’s own IBasicDataFeed type without the SDK depending on the licensed package.
import { createBinanceCandleSource, toTradingViewDatafeed } from "@symmio/trading-core";
const datafeed = toTradingViewDatafeed(createBinanceCandleSource());
new TradingView.widget({ datafeed, symbol: "BTCUSDT", interval: "60" });Parameters
sourceCandleSourceThe candle source to read from.
options.exchangestringoptionalExchange label shown in the chart’s symbol header. Defaults to the source’s id.
options.sessionstringoptionalSession string for the instrument. Defaults to "24x7" — correct for crypto perps, which never close.
options.timezonestringoptionalTimezone the chart renders bar times in. Defaults to "Etc/UTC", matching the UTC-aligned bar boundaries every
source returns.
Returns
TradingViewDatafeedA datafeed object to pass straight to widget({datafeed}) — onReady, searchSymbols, resolveSymbol, getBars,
subscribeBars, and unsubscribeBars implemented over the source.
Behavior worth knowing
- Symbol search is reported as unsupported. A SYMMIO app already has its market list (
useMarkets), and driving symbol selection from that is both more accurate and less surprising than a second search surface here. - Full market names are preserved.
TOKEN::Ab..Cd_SFLOWreaches the source unchanged. An exchange prefix such asPrice Service:TOKEN::Ab..Cd_SFLOWis removed without splitting the market’s::suffix. Use complete market names, not tickers, when names can collide. countBackwins overfrom. In the library’s contractcountBackis the exact number of bars the chart needs ending atto; the adapter widens the window to at least that many bars so a shortfromcannot starve the chart.- Reconnects invalidate the chart’s cache. When the source signals a reset, the adapter calls the library’s
onResetCacheNeeded, so it refetches history instead of splicing the live bar onto a series with a hole in it. has_empty_barsisfalse— the chart leaves real gaps rather than inventing flat bars where the venue reported none.- Live-streaming markets report
data_status: "streaming"; a source withoutwatchCandlesreports"endofday".
The datafeed owns its live subscriptions, keyed by the library’s listener guid. Keep one instance alive per widget —
in React, use useTradingViewDatafeed, which memoizes it.
Lowcap history and corrections
Pass createLowcapCandleSource to the same adapter. Market names containing :: are preserved. Sparse windows do not indicate exhaustion: on deployments without has_older, the adapter forwards noData: false and TradingView may request additional empty windows.
onResetCacheNeededCallback is forwarded when the source reconnects or signals a correction. The widget integration must also call its chart’s resetData() when reloading corrected history; the adapter does not own the widget. See TradingView’s reset guidance .
The adapter does not use the source’s optional correction range: TradingView still receives its normal full cache-reset notification. Incremental SDK consumers can forward that range to refreshCandleHistory or useCandleHistory.refresh to limit which windows are fetched.
Resolution mapping
TradingView encodes intraday resolutions as bare minute counts ("60" is one hour), seconds with an S suffix, and days/weeks/months with a letter. Two helpers translate:
import { fromTradingViewResolution, toTradingViewResolution } from "@symmio/trading-core";
fromTradingViewResolution("60"); // "1h" — undefined when the code is unrecognized
toTradingViewResolution("1h"); // "60"
fromTradingViewResolution("10"); // "10m"
toTradingViewResolution("10m"); // "10"The bare-letter forms ("D", "W", "M") are accepted alongside the numbered ones on the way in, because the library emits both depending on how the chart was configured; only one spelling per resolution is advertised on the way out.
Types
| Type | What it is |
|---|---|
TradingViewDatafeed | The datafeed object widget({ datafeed }) expects. |
TradingViewDatafeedConfiguration | Capabilities reported from onReady. |
TradingViewSymbolInfo | Symbol metadata returned from resolveSymbol. |
TradingViewBar | A bar in TradingView’s shape — time is unix milliseconds. |
TradingViewPeriodParams | The range and bar count the library asks getBars for (unix seconds). |
TradingViewHistoryMetadata | { noData: boolean } accompanying a getBars result. |
These are structural types declared by the SDK — the Charting Library is a licensed artifact a consumer self-hosts, so the SDK cannot depend on its types without forcing every consumer to vendor it.
Related
useTradingViewDatafeed— the memoized React wrapper.- Majors chart guide — wiring the widget end to end.
- Resolutions — the SDK’s resolution vocabulary.