Skip to content

Advanced Features

levi edited this page Nov 6, 2025 · 2 revisions

Advanced Features

Table of Contents

  1. Multi-Chart Support
  2. Theme Customization and Styling
  3. Layout Management and Responsive Design
  4. Performance Optimization Techniques
  5. Server Integration and Web Deployment
  6. Advanced Configuration and Runtime Modifications

Multi-Chart Support

PyTradingView provides comprehensive multi-chart support, enabling users to manage multiple chart instances within a single dashboard. The system automatically handles chart lifecycle events through the indicator engine's runtime mixin, which subscribes to layout change events and reinitializes all charts when the layout is modified. Each chart instance is assigned a unique identifier (e.g., "chart_0", "chart_1") and maintains its own context with associated chart object references.

The framework supports synchronization of various chart properties across multiple instances. Users can enable cross-chart synchronization for date ranges, intervals, symbols, and time settings through dedicated WatchedValue properties. When synchronization is enabled, changes to one chart are automatically propagated to all other charts in the layout. The system also supports synchronized indicator management, where default indicators can be automatically activated on all charts based on configuration settings.

Chart activation events are handled through the activeChart and activeChartIndex methods, allowing applications to respond to user interactions when switching between different chart instances. The framework provides methods to query the current number of charts (chartsCount) and access specific chart instances by index. When layout changes occur, the system automatically cleans up resources from previous chart configurations to prevent memory leaks.

Theme Customization and Styling

PyTradingView offers extensive theme customization capabilities through the changeTheme method and applyOverrides functionality. The changeTheme method allows dynamic switching between predefined themes (such as "dark" or "light") with optional configuration parameters and callback functions to handle theme transition completion. This method integrates with the frontend's theme system to ensure consistent visual presentation across all chart elements.

For fine-grained styling control, the applyOverrides method enables modification of specific chart properties without requiring a full reload. Users can override visual properties such as candlestick colors, background settings, and series styling by providing a dictionary of property paths and values. For example, the main series candlestick up and down colors can be customized using property paths like "mainSeriesProperties.candleStyle.upColor". The applyStudiesOverrides method extends this capability to indicators, allowing customization of study-specific visual properties and input parameters.

The framework also provides access to custom themes through the customThemes API, enabling the addition, removal, and management of custom color schemes. These customization options can be applied at runtime, allowing users to modify the appearance of charts based on user preferences or application state without disrupting the user experience.

flowchart TD
A["Theme Customization"] --> B["changeTheme Method"]
A --> C["applyOverrides Method"]
A --> D["applyStudiesOverrides Method"]
A --> E["Custom Themes API"]
B --> F["Switch between predefined themes"]
C --> G["Override specific chart properties"]
D --> H["Customize indicator styling"]
E --> I["Manage custom color schemes"]
Loading

Diagram sources

Layout Management and Responsive Design

The layout management system in PyTradingView provides comprehensive control over dashboard configurations and responsive design. The layout method returns the current chart layout type as a string (e.g., "2h" for two charts side by side), while setLayout allows programmatic changes to the layout configuration. Users can precisely control the size distribution of charts within a multi-chart layout using setLayoutSizes, which accepts a size configuration object and supports excluding the operation from the undo stack.

For complex dashboard configurations, the framework supports saving and loading chart layouts with extended metadata. The load method accepts a state object and optional extended data for preserving custom configuration. Layout names can be retrieved and managed through the layoutName method, enabling users to organize and recall different dashboard arrangements. The system also provides methods to reset layout sizes to default values (resetLayoutSizes) and unload invisible charts to conserve memory (unloadUnusedCharts).

Responsive design considerations are addressed through dynamic layout adaptation and resource management. When layout changes are detected, the system automatically reinitializes all charts and sets up appropriate event listeners for each instance. The framework handles resource cleanup during layout transitions to prevent memory leaks and ensure optimal performance across different screen sizes and device types.

Performance Optimization Techniques

PyTradingView incorporates several performance optimization techniques to ensure smooth operation with complex chart configurations. Data batching is implicitly handled through the efficient communication bridge between Python and JavaScript components, minimizing the overhead of cross-environment calls. The framework uses asynchronous method invocation and event batching to reduce the frequency of expensive operations and improve overall responsiveness.

Efficient event handling is achieved through the use of subscription-based patterns and proper resource cleanup. The system automatically manages event listeners for chart instances, ensuring that subscriptions are properly cleaned up when charts are removed or layouts are changed. The TVSubscribeManager handles subscription lifecycle management, preventing memory leaks from orphaned event listeners. The framework also provides methods like clearUndoHistory to reset state when appropriate, reducing memory footprint in long-running applications.

Resource cleanup is a critical aspect of performance optimization, particularly in multi-chart scenarios. The indicator engine's runtime mixin includes comprehensive cleanup functionality that deactivates all indicators, clears drawings, and invokes destroy callbacks when chart contexts are removed. The unloadUnusedCharts method explicitly releases memory by unloading invisible charts in multi-chart layouts, which is particularly beneficial for mobile devices and systems with limited resources. The resetCache method provides a way to clear cached data for all symbols, helping to manage memory usage in applications with large datasets.

Server Integration and Web Deployment

The server component in app.py provides web-based deployment capabilities through FastAPI integration. The server implementation includes a start_server function that initializes an HTTP server using uvicorn, with configurable ports for both the Node.js and Python components. The server implements automatic restart logic with a maximum retry limit, enhancing reliability in production environments. The FastAPI application is configured with CORS middleware to allow cross-origin requests, supporting integration with web frontends hosted on different domains.

The server architecture follows a client-server model where the Python backend communicates with a JavaScript frontend through a WebSocket-based bridge system. This bidirectional communication enables real-time updates and interactive chart manipulation. The server handles incoming requests and routes them to the appropriate Python components, which in turn can push updates back to the client. This architecture supports both standalone applications and integration into larger web platforms, providing flexibility for different deployment scenarios.

The integration with FastAPI enables standard web deployment practices, including RESTful endpoints, middleware support, and production-ready server features. The logging configuration provides visibility into server operations, with error handling and restart mechanisms to maintain service availability. This server component serves as the foundation for web-based trading applications, allowing developers to deploy interactive charting dashboards with advanced analytical capabilities.

graph TD
A["Client Application"] --> B["FastAPI Server"]
B --> C["Python Backend"]
C --> D["TradingView Widget"]
D --> E["JavaScript Frontend"]
E --> B
B --> F["WebSocket Bridge"]
F --> C
C --> G["Indicator Engine"]
G --> H["Data Feed"]
H --> C
style B fill:#4CAF50,stroke:#388E3C
Loading

Diagram sources

Advanced Configuration and Runtime Modifications

PyTradingView supports advanced configuration options and runtime modifications that can be applied without page reloads. The configuration system allows dynamic changes to chart properties, indicator settings, and visual themes through programmatic APIs. Users can modify chart symbols and intervals using the setSymbol method, which accepts callback functions to handle completion events. The applyTradingCustomization method enables runtime modification of trading-related visual elements such as order and position lines.

Runtime modifications extend to indicator management, where indicators can be dynamically activated or deactivated on specific charts or across all charts. The indicator engine's manager mixin provides methods to activate indicators to individual charts or all charts simultaneously, with proper context management. Configuration changes to indicators automatically trigger recalculation, ensuring that visual representations are updated to reflect the new settings.

The framework also supports dynamic UI modifications, such as creating and removing toolbar buttons and dropdown menus at runtime. These changes can be tied to application state or user preferences, enabling highly customizable interfaces. The event system allows registration of callbacks for various user interactions, including keyboard shortcuts, context menu selections, and chart events, providing a comprehensive mechanism for responding to user actions without requiring page refreshes.

Clone this wiki locally