A high-performance Python library for connecting to TradingView's WebSocket API with both synchronous and asynchronous support.
- Dual-Mode Support: Synchronous (
TVclient) and Asynchronous (AsyncTVclient) - Ultra-Low Latency: Optimized protocol parsing with
orjsonanduvloopsupport - Real-Time Data Streaming: Chart (OHLCV) and Quote (tick-by-tick) sessions
- Multiple Timeframes: 1min, 5min, 15min, 1hour, daily, and more
- Backward Compatible: Existing code continues to work unchanged
pip install pytradingviewpip install orjson uvloop # 2-4x faster with ultra-low latencyfrom pytradingview import TVclient
client = TVclient()
chart = client.chart
chart.set_up_chart()
chart.set_market("BINANCE:BTCUSD", {"timeframe": "1"})
def on_update(_):
if chart.get_periods:
print(f"Price: {chart.get_periods['close']}")
chart.on_update(on_update)
client.create_connection()import asyncio
from pytradingview import AsyncTVclient
async def main():
client = AsyncTVclient()
chart = client.chart
chart.set_up_chart()
chart.set_market("BINANCE:BTCUSD", {"timeframe": "1"})
async def on_update(_):
if chart.get_periods:
print(f"β‘ Price: {chart.get_periods['close']}")
chart.on_update(on_update)
await client.create_connection()
asyncio.run(main())For maximum speed, enable uvloop:
import uvloop
uvloop.install()Perfect for charting applications and technical analysis:
chart = client.chart
chart.set_up_chart()
chart.set_market("BINANCE:BTCUSD", {"timeframe": "5"}) # 5-minute candles
chart.on_update(handle_candle_update)Use cases:
- Candlestick charts
- Technical indicators (RSI, MACD, MA)
- Historical data analysis
- Multiple timeframe analysis
For real-time price feeds and trade execution:
quote = client.quote
quote.set_up_quote({'fields': 'price'})
quote.add_symbol("BINANCE:BTCUSD", handle_tick)Use cases:
- Real-time price tickers
- Time & sales panels
- Scalping strategies
- Order book displays
See the /examples directory for complete working examples:
example_app.py- Simple synchronous chart exampleasync_example.py- Async client with multiple symbolsquickstart_chart.py- Minimal async chart setup
tick_capture.py- Capture every tick with detailed change trackingdebug_raw_stream.py- Debug WebSocket messages
benchmark_quote_vs_chart.py- Compare update frequenciesanalyze_quote_data.py- Analyze tick data patterns
With optimizations enabled (orjson + uvloop):
- Protocol parsing: ~56% faster than regex-based parsing
- JSON processing: 2-4x faster with
orjson - Event loop: 2-4x faster with
uvloop - Message dispatch: < 3ms latency
# Download historical data
python -m pytradingview -d -s '2025-04-24 00:00' -e '2025-04-25 00:00' -p 'FX:EURUSD'
# Search for symbols
python -m pytradingview --search BTCUSD --max 50symbols = ["BINANCE:BTCUSD", "BINANCE:ETHUSD", "BINANCE:SOLUSD"]
for symbol in symbols:
quote.add_symbol(symbol, lambda data: print(f"{symbol}: {data}"))quote.set_up_quote({
'customFields': ['lp', 'volume', 'bid', 'ask', 'high_price', 'low_price']
})async with AsyncTVclient() as client:
chart = client.chart
# ... setup and use
# Connection automatically closed on exitMethods:
chart- Access ChartSessionquote- Access QuoteSessioncreate_connection()- Start WebSocket connectionon_connected(callback)- Connection establishedon_error(callback)- Error handleron_disconnected(callback)- Connection closed
Methods:
set_up_chart()- Initialize chartset_market(symbol, options)- Set symbol and timeframeon_update(callback)- Price update eventson_symbol_loaded(callback)- Symbol metadata loadedget_periods- Current OHLCV dataget_infos- Symbol metadata
Methods:
set_up_quote(options)- Initialize with fieldsadd_symbol(symbol, callback)- Subscribe to symboldelete()- Close session
Contributions welcome! Please open issues or PRs.
See LICENSE file.
Built on the TradingView WebSocket API. Async implementation and performance optimizations added in v0.4.0.