Connecting data
Overviewβ
The library is a powerful, client-side solution for creating rich, interactive user interfaces for market data.
The library does not provide any market data. You must connect the library to your own data source or a third-party provider.
The connection between the library and your data source is managed by a crucial component called the datafeed. The datafeed is a JavaScript object that acts as a bridge, responding to the library's requests for information. When a user interacts with the chart, for example, by loading a symbol, changing the time interval, or scrolling back in time, the library calls the appropriate methods on your datafeed to fetch the necessary data.
Your responsibility is to implement the datafeed to fetch data from your backend and return it to the library in the expected format. The core datafeed responsibilities include:
- Provide configuration to the library, including supported exchanges, resolutions, and symbol types.
- Resolve symbol information, such as its trading hours, timezone, and price formatting rules.
- Supply historical bar data (OHLC).
- Stream real-time data to show the latest market activity.
- Provide real-time quote data such as bid/ask prices and volume (for Trading Platform only).
Data integration approachesβ
You have two approaches for implementing the datafeed. The best choice depends on your project's requirements and development timeline.
Datafeed APIβ
The Datafeed API is a set of JavaScript methods that you implement to create a custom datafeed. This approach offers maximum flexibility, allowing you to use any protocol (like WebSockets for real-time data) and implement custom logic for data handling. This is the recommended approach.
To begin your implementation, we recommend following our step-by-step tutorial How to connect data via datafeed API. It will guide you through creating a functional datafeed from scratch, including real-time data streaming with WebSockets.
UDF adapterβ
The library includes a pre-built UDF (Universal Datafeed) adapter, which is a ready-to-use implementation of the Datafeed API. It works by making simple HTTP requests to your backend. Your responsibility is to create a server-side service that responds in the required format.
UDF adapter does not support real-time data streaming out of the box but you can implement it.
Choose this approach if you want to get started quickly or as a reference implementation. You can use the UDF adapter source code π (restricted access) as an example of the Datafeed API implementation, copying and modifying it for your own needs.
To begin your implementation, we recommend following the UDF adapter article for step-by-step details on API requests, queries, and parameters.
Implementation exampleβ
The datafeed is a server-side component that acts as a bridge between your data and the library. While the library is compatible with any backend technology (such as Python, PHP, .NET, or Go), we provide a Node.js reference implementation to illustrate the expected data structures and API responses.
You can explore the yahoo_datafeed repository for an example of a backend service. This repository can serve as a guide even if you choose to build your backend in a different language.
Using specific protocolsβ
The library is a browser-based client-side solution and doesn't provide any pre-made data integrations with the FIX protocol or other binary/low-level protocols.
However, you can implement your backend data connection with any tools, including the FIX protocol, based on your requirements. You should integrate the FIX protocol between your backend and the data provider, then pass the data to the library via the Datafeed API.
The diagram below illustrates the data connection layers:
Connecting data from static filesβ
The library is designed to fetch data dynamically from a server and doesn't support reading files (like .csv, .txt, or .xlsx) directly from a local machine or a static URL.
You should also keep in mind that according to the license agreement you can use the library on public websites only.
If you want to use a file as a data source, you must implement a backend bridge by following these steps:
- Develop a backend application. Use any server-side language (.NET, PHP, Node.js, Python, etc.) to read the file and serve its content.
- You can transfer the data in the UDF format over HTTP(S).
- Alternatively, you can provide data in a custom format or via WebSockets, but this requires a custom Datafeed API implementation.
- Configure server access. Ensure your server is accessible to the browser. You will need a static IP or a registered domain.
- Update the widget constructor. In
index.html, point thedatafeedproperty to your new server URL instead of the default demo feed (demo_feed.tradingview.com).