TradingView Data Provider (tradingview-rs & tradingview-py)
A high-performance, asynchronous TradingView data provider written in Rust (tradingview-rs) with first-class Python bindings (tradingview-py via PyO3 0.29). Inspired by TradingView-API, this project delivers institutional-grade market data streaming, historical OHLCV series, corporate fundamental metrics, and global economic calendar events with direct Polars DataFrame support.
Architecture Overview
graph TD
subgraph Python Environment
PyApp[Python Algorithmic Trading / Analytics App]
Polars[Polars / Pandas DataFrames]
AsyncIO[Python asyncio Event Loop]
end
subgraph PyO3 Native Extension [crates/tradingview-py]
TVClient[TradingViewClient]
Dispatcher[Callback Trampoline & sys.unraisablehook]
PyModels[Bar / CandleUpdate / QuoteTick / FundamentalSeries]
end
subgraph Rust Core Engine [crates/tradingview]
TokioRt[Tokio Multi-Threaded Runtime]
HistClient[HistoricalClient & Batch Runner]
WSClient[WebSocketClient & Auto-Reconnect Engine]
FundClient[Fundamental Catalog & Registry Engine]
CalClient[Economic Calendar REST Client]
end
subgraph TradingView Upstream
TVSocket[TradingView WebSocket Server]
TVHTTP[TradingView REST & Scanner APIs]
end
PyApp --> TVClient
TVClient --> PyModels
PyModels -.->|as_dataframe / to_polars| Polars
TVClient -->|Releases GIL| TokioRt
TokioRt --> HistClient
TokioRt --> WSClient
TokioRt --> FundClient
TokioRt --> CalClient
WSClient <-->|UTF-16 Framing & Heartbeat Echo| TVSocket
HistClient <--> TVSocket
FundClient <--> TVHTTP
CalClient <--> TVHTTP
WSClient --> Dispatcher
Dispatcher -->|loop.call_soon_threadsafe| AsyncIO
AsyncIO --> PyApp
Rust Core Architecture (Two-Tier Model)
graph LR
subgraph High-Level Event Pipeline
Source[DataSource: Live Quotes / Chart Series / Scanner]
Loader[DataLoader Engine]
ChannelSink[ChannelSink: Bounded mpsc]
CallbackSink[CallbackSink: Synchronous / Async]
KafkaSink[KafkaSink: RedPanda / Apache Kafka]
end
subgraph Low-Level Protocol Primitives
WS[WebSocketClient]
Session[Chart, Quote & Replay Sessions]
Parser[UTF-16 Code-Unit Packet Parser]
end
Source --> Loader
Loader --> ChannelSink
Loader --> CallbackSink
Loader --> KafkaSink
WS --> Parser
Parser --> Session
Session --> Source
Features
- Zero GIL Contention: Long-running network I/O, batch downloads, and deserialization execute in Tokio background threads with the Python GIL released.
- Direct Polars Support: Fetch historical candlestick bars, batch series, fundamental indicators, and economic calendar events directly as high-performance Polars DataFrames (
as_dataframe=True). - Dual-Mode Streaming: Consume live quotes and in-flight candlesticks through native asynchronous iterators (
async for) or synchronous callbacks (add_callback) dispatched on the asyncio event loop with exception isolation (sys.unraisablehook). - Strict Wire Parity: Accurate UTF-16 code-unit framing (
~m~<len>~m~<payload>), 1:1 heartbeat echoing, and protocol parity matching TradingView web clients. - Event-Driven Rust Pipeline: High-level
DataLoaderarchitecture connecting custom sources to Channel, Callback, and Kafka sinks with backpressure and graceful cancellation. - Historical Market Data: Single-symbol and concurrent multi-symbol batch fetching with configurable concurrency limits and per-symbol timeouts.
- Corporate Fundamentals: Date-versioned fundamental Pine study catalog (
tradingview::fundamental) querying annual, quarterly, and TTM balance sheet, income, and cash flow metrics. - Economic Calendar: Global macroeconomic event queries filtered by ISO 3166-1 country codes, timestamps, and importance levels.
- Credential & Token Authentication: Support for both session auth tokens and full credential login with optional TOTP 2FA.
Installation
Python (tradingview)
Install from PyPI:
To enable direct Polars and Pandas DataFrame conversion:
To build and install locally from source:
Rust (tradingview-rs)
Add to your Cargo.toml:
[]
= "0.3"
Feature Flags
| Feature | Default | Description |
|---|---|---|
rustls-tls |
✅ | Pure-Rust TLS backed by rustls (recommended) |
native-tls |
— | Platform-native TLS via OpenSSL / SChannel / Security Framework |
user |
✅ | User authentication support (login, TOTP 2FA, session cookies) |
Python Quick Start
1. Historical Candlesticks Directly to Polars
=
# Fetch 100 daily bars directly as a Polars DataFrame
= await
# Output columns: timestamp, open, high, low, close, volume
# Or retrieve structured HistoricalSeries with .to_polars() and .to_pandas()
= await
=
# Concurrent batch retrieval as a dictionary of DataFrames
= await
await
2. Real-Time Quotes & Candlestick Streaming
=
# 1. Quote streaming with callback & async iterator
= await
= 0
+= 1
break
await
# 2. Live in-flight 1-minute candle streaming
= await
= 0
+= 1
break
await
await
3. Fundamentals & Global Economic Calendar
=
# Query corporate revenue history directly as a Polars DataFrame
= await
# Query high-importance macroeconomic events for the US
= await
await
Rust Quick Start
1. Historical Data Retrieval (Single & Batch)
use ;
use DataServer;
use Interval;
async
2. Real-Time WebSocket Quote Streaming
use Value;
use Arc;
use signal;
use ;
use ;
;
async
3. Event-Driven Pipeline (DataLoader)
use Arc;
use DataLoader;
use CallbackSink;
use ChannelSink;
use WebSocketSource;
async
Workspace Structure
tradingview-rs/
├── Cargo.toml # Virtual workspace manifest
├── crates/
│ ├── tradingview/ # Pure Rust core library (tradingview-rs)
│ │ ├── Cargo.toml
│ │ ├── src/ # Protocol framing, WebSocket engine, loader, fundamental
│ │ ├── tests/ # Wire and integration tests
│ │ ├── examples/ # Runnable Rust examples
│ │ └── benches/ # Criterion microbenchmarks
│ └── tradingview-py/ # PyO3 0.29 CPython extension (tradingview)
│ ├── Cargo.toml # Native extension build config (abi3, tokio-runtime)
│ ├── pyproject.toml # Maturin package metadata and dependencies
│ ├── src/ # PyO3 bindings, models, callback dispatcher, streaming
│ ├── python/tradingview/ # Python package exports, PEP 561 py.typed, .pyi stubs
│ └── tests/ # Pytest async and typing validation suite
└── .github/
└── workflows/
├── ci.yml # Rust formatting, clippy, tests + Python test and type matrix
└── publish.yml # crates.io Trusted Publishing + PyPI Twine release pipeline
Development & Testing
Run all Rust and Python checks locally:
# Format & Lint Rust
# Execute Rust Workspace Tests (201 passing tests)
# Build Python Extension & Run Python Test Suite (22 passing tests)
# Type Verification
Publishing to PyPI
The release workflow .github/workflows/publish.yml is triggered automatically on tag creation (v*) or via manual dispatch:
- crates.io: Authenticates via OIDC Trusted Publishing and publishes
tradingview-rs. - PyPI: Builds source distribution and wheels with
maturin build --release, then uploads viatwineusing your configured credentials (supporting~/.pypircorPYPI_API_TOKENsecret).
License
This project is licensed under the MIT License.
Disclaimer: This library is not affiliated with, maintained, or endorsed by TradingView. Use in compliance with TradingView's Terms of Service.