-
Notifications
You must be signed in to change notification settings - Fork 1
Datafeed Configuration
- Introduction
- TVDatafeedConfiguration Parameters
- Configuration Initialization in BADatafeed
- onReady Callback Implementation
- Best Practices for Resolution Support
- Exchange Metadata Definition
- Common Configuration Issues
- Performance Implications of Optional Features
The TVDatafeedConfiguration class serves as the central configuration mechanism for custom datafeed implementations in the PyTradingView library. This configuration object, passed through the onReady callback, defines the capabilities and supported options of a datafeed implementation. Proper configuration is essential for ensuring compatibility with the TradingView charting library and enabling expected functionality. The configuration includes parameters for supported resolutions, exchange metadata, symbol types, and optional features like marks and time support. This document provides comprehensive guidance on configuring custom datafeeds, with practical examples from the BADatafeed implementation.
The TVDatafeedConfiguration class accepts several parameters that define the datafeed's capabilities and supported options. Each parameter serves a specific purpose in configuring the datafeed for proper integration with the TradingView charting library.
The exchanges parameter accepts a list of TVExchange objects, each containing a value, name, and description. These exchanges are used for filtering symbols during search operations and provide metadata about available trading venues. The supported_resolutions parameter defines the time intervals supported by the datafeed, specified as a list of ResolutionString values such as "1", "5", "15", "60", "1D", "1W", and "1M". This parameter is critical for chart rendering, as it determines which timeframes users can select.
The units parameter allows for unit conversion support, accepting a dictionary mapping unit categories to lists of TVUnit objects. The currency_codes parameter specifies supported currency codes for conversion purposes. Optional boolean flags include supports_marks, supports_time, and supports_timescale_marks, which enable or disable specific chart features. The symbols_types parameter accepts a list of TVDatafeedSymbolType objects for filtering symbols by type during search operations, while symbols_grouping provides a dictionary for organizing symbols into logical groups.
The BADatafeed implementation demonstrates proper initialization of the TVDatafeedConfiguration object within the class constructor. The configuration is stored as a private instance variable _configuration and initialized with specific parameters that reflect the datafeed's capabilities.
classDiagram
class TVDatafeedConfiguration {
+List[TVExchange] exchanges
+List[ResolutionString] supported_resolutions
+Dict[str, List[TVUnit]] units
+List currency_codes
+bool supports_marks
+bool supports_time
+bool supports_timescale_marks
+List[TVDatafeedSymbolType] symbols_types
+Dict[str, str] symbols_grouping
}
class TVExchange {
+str value
+str name
+str desc
}
class TVDatafeedSymbolType {
+str name
+str value
}
TVDatafeedConfiguration --> TVExchange : "contains"
TVDatafeedConfiguration --> TVDatafeedSymbolType : "contains"
The onReady callback is a critical component of the datafeed configuration process, serving as the mechanism through which the TVDatafeedConfiguration object is delivered to the TradingView charting library. In the BADatafeed implementation, the onReady method checks if the configuration object exists and, if so, passes it to the provided callback function.
sequenceDiagram
participant Chart as "TradingView Chart"
participant Datafeed as "BADatafeed"
Chart->>Datafeed : onReady(callback)
Datafeed->>Datafeed : Check if _configuration exists
alt Configuration exists
Datafeed->>Chart : callback(configuration)
else Configuration missing
Datafeed->>Chart : No action
end
Defining appropriate resolution support is crucial for ensuring proper chart functionality and user experience. The supported_resolutions parameter should accurately reflect the time intervals available from the underlying data source. In the BADatafeed example, the configuration includes both intraday resolutions ("1", "5", "15", "30", "60", "240") and longer-term intervals ("1D", "1W", "1M").
When configuring resolution support, it's important to ensure consistency between the TVDatafeedConfiguration's supported_resolutions and the TVLibrarySymbolInfo's supported_resolutions. Mismatches between these values can lead to symbol resolution failures or chart rendering issues. For example, if a datafeed claims to support "15" minute bars in its configuration but a specific symbol's TVLibrarySymbolInfo only lists "1", "5", and "60" as supported resolutions, users may encounter errors when attempting to view the 15-minute chart for that symbol.
Exchange metadata is defined using the TVExchange class, which requires three parameters: value, name, and description. The value parameter serves as the identifier used in API calls, particularly in the searchSymbols method, while the name provides a user-friendly display name and the description offers additional context about the exchange.
In the BADatafeed implementation, two exchanges are defined: Binance with value "BINANCE" and OKEx with value "OKEX". These definitions enable users to filter symbol searches by exchange and provide meaningful metadata within the TradingView interface. Proper exchange definition is essential for symbol resolution, as incorrect exchange values can lead to failed symbol lookups and chart initialization problems.
Several common configuration issues can impact datafeed functionality. The most critical is missing or incomplete supported_resolutions in the TVDatafeedConfiguration, which can prevent chart rendering entirely. If this parameter is not properly defined, the TradingView chart may fail to initialize or display blank charts, as the library cannot determine which timeframes are available.
Another frequent issue is incorrect exchange definitions, where the exchange value in TVExchange does not match the exchange value expected by the symbol resolution system. This mismatch can cause symbol resolution failures, preventing users from loading charts for specific instruments. Additionally, inconsistencies between the datafeed-level supported_resolutions and symbol-specific supported_resolutions in TVLibrarySymbolInfo can lead to confusing user experiences, where some symbols appear to support timeframes that are not actually available.
Enabling optional features such as marks and timescale marks has direct performance implications that should be carefully considered. The supports_marks and supports_timescale_marks parameters, when set to true, require the implementation of additional methods (getMarks and getTimescaleMarks) that are called frequently during chart interaction.
These methods can become performance bottlenecks if they involve expensive operations such as database queries or complex calculations. For high-frequency trading applications or datafeeds serving many concurrent users, the overhead of processing mark requests can significantly impact server performance. The supports_time parameter, when enabled, requires implementation of the getServerTime method, which adds another endpoint that must be maintained and can contribute to server load.
In the BADatafeed example, these optional features are disabled (supports_marks=False, supports_timescale_marks=False) which represents a performance-conscious decision for a basic implementation. Production systems should evaluate whether these features are necessary for their use case and implement efficient caching mechanisms if they are enabled.