TradingView data scraper with multiple output formats
Project description
๐บ TV Scraper
Fetch historical price data from TradingView in any format you need.
๐ Overview
TV Scraper is a lightweight yet powerful Python library that fetches historical OHLCV (Open, High, Low, Close, Volume) data from TradingView. Whether you need DataFrames for analysis, NumPy arrays for machine learning, or JSON for APIs โ TV Scraper has you covered.
Why TV Scraper?
- ๐ One line to get data โ
df = tv.get("BTCUSDT") - ๐จ 7 output formats โ pandas, numpy, arrays, dict, json, csv, raw tuples
- ๐งน Zero bloat โ Only requires
websocket-client - ๐ ML-ready โ Direct to TensorFlow, PyTorch, or scikit-learn
- ๐ Batch fetching โ Get multiple symbols in one call
- ๐ก๏ธ Smart defaults โ Works out of the box, fully customizable
- ๐ All TradingView markets โ Crypto, stocks, forex, indices, commodities
๐ฆ Installation
Basic Install (DataFrame support)
pip install tv_scraper[pandas]
ML Install (NumPy arrays)
pip install tv_scraper[numpy]
Full Install (everything)
pip install tv_scraper[all]
From GitHub (latest)
pip install git+https://github.com/anuragjha0001/tv_scraper.git
๐ Quick Start
from tv_scraper import TvDatafeed
# Create instance
tv = TvDatafeed()
# Get Bitcoin daily data (returns DataFrame)
df = tv.get("BTCUSDT")
print(df.head())
Output:
open high low close volume
timestamp
2026-04-05 00:00:00 83500.0 84200.0 83400.0 84000.0 125.34
2026-04-06 00:00:00 84000.0 84800.0 83900.0 84600.0 200.50
...
๐ Usage Guide
1. Basic Fetching
from tv_scraper import TvDatafeed
from datetime import datetime, timedelta
tv = TvDatafeed()
# Simple - last 30 days daily data
df = tv.get("BTCUSDT")
# With custom parameters
df = tv.get(
symbol="ETHUSDT",
exchange="BINANCE", # Default: BINANCE
interval="1H", # 1m, 5m, 15m, 1H, 4H, 1D, 1W, 1M
start="2024-01-01", # Start date
end="2024-01-31", # End date
)
# Using datetime objects
start = datetime(2024, 1, 1)
end = datetime.now()
df = tv.get("SOLUSDT", start=start, end=end, interval="4H")
2. All Output Formats
tv = TvDatafeed()
# DataFrame (default)
df = tv.get("BTCUSDT", output_format="pandas")
# NumPy structured array
arr = tv.get("BTCUSDT", output_format="numpy")
print(arr.dtype) # [('timestamp', '<i8'), ('open', '<f8'), ...]
# Separate arrays (ML-ready)
ts, o, h, l, c, v = tv.get("BTCUSDT", output_format="arrays")
# List of dictionaries (API-ready)
data = tv.get("BTCUSDT", output_format="dict")
# [{"timestamp": 1704067200, "open": 42500.5, ...}, ...]
# JSON string
json_str = tv.get("BTCUSDT", output_format="json", indent=2)
# CSV string
csv_str = tv.get("BTCUSDT", output_format="csv")
# Raw tuples (fastest)
bars = tv.get("BTCUSDT", output_format="raw")
# [(1704067200, 42500.5, 43200.0, 42400.0, 43100.0, 125.34), ...]
3. Batch Fetching (Multiple Symbols)
tv = TvDatafeed()
results = tv.get_multi([
{"symbol": "BTCUSDT", "interval": "1H", "output_format": "pandas"},
{"symbol": "ETHUSDT", "interval": "1H", "output_format": "dict"},
{"symbol": "SOLUSDT", "interval": "4H", "output_format": "json"},
])
# Access results
btc_df = results["BINANCE:BTCUSDT"]
eth_data = results["BINANCE:ETHUSDT"]
sol_json = results["BINANCE:SOLUSDT"]
4. ML Pipeline (PyTorch/TensorFlow Ready)
from tv_scraper import TvDatafeed
import numpy as np
import torch
tv = TvDatafeed()
# Get data as separate arrays
ts, opens, highs, lows, closes, volumes = tv.get(
"BTCUSDT",
interval="1H",
output_format="arrays"
)
# Feature engineering
X = np.column_stack([opens, highs, lows, volumes])
y = np.roll(closes, -1)[:-1] # Next period's close
X = X[:-1]
# Convert to PyTorch tensors
X_tensor = torch.from_numpy(X).float()
y_tensor = torch.from_numpy(y).float()
print(f"Features: {X_tensor.shape}, Target: {y_tensor.shape}")
5. API Backend (FastAPI)
from fastapi import FastAPI
from tv_scraper import TvDatafeed
app = FastAPI()
tv = TvDatafeed()
@app.get("/api/crypto/{symbol}")
def get_crypto_data(symbol: str, interval: str = "1D"):
return tv.get(
symbol.upper(),
interval=interval,
output_format="json"
)
# GET http://localhost:8000/api/crypto/BTCUSDT?interval=1H
6. Context Manager
# Connection auto-closes after use
with TvDatafeed() as tv:
df = tv.get("BTCUSDT")
# Connection automatically closed
7. Stock & Forex Markets
tv = TvDatafeed()
# Indian Stocks (NSE)
df = tv.get("RELIANCE", exchange="NSE", interval="1D")
# US Stocks (NASDAQ)
df = tv.get("AAPL", exchange="NASDAQ", interval="1H")
# Forex
df = tv.get("EURUSD", exchange="FX_IDC", interval="1D")
# Indices
df = tv.get("SPX", exchange="SP", interval="1D")
# Commodities
df = tv.get("XAUUSD", exchange="OANDA", interval="1D")
๐ Supported Timeframes
| Code | Timeframe | Bars/Day |
|---|---|---|
1m |
1 Minute | 1440 |
3m |
3 Minutes | 480 |
5m |
5 Minutes | 288 |
15m |
15 Minutes | 96 |
30m |
30 Minutes | 48 |
1H |
1 Hour | 24 |
2H |
2 Hours | 12 |
3H |
3 Hours | 8 |
4H |
4 Hours | 6 |
1D |
Daily | 1 |
1W |
Weekly | ~0.14 |
1M |
Monthly | ~0.03 |
๐ฏ Output Formats
| Format | Returns | Required | Best For |
|---|---|---|---|
pandas |
DataFrame | pandas |
Data analysis, plotting |
numpy |
Structured ndarray | numpy |
Scientific computing |
arrays |
Tuple of 6 arrays | numpy |
ML/AI pipelines |
dict |
List of dicts | None | APIs, databases |
json |
JSON string | None | HTTP responses |
csv |
CSV string | None | Excel, spreadsheets |
raw |
List of tuples | None | Custom processing |
๐ Performance Benchmarks
| Format | 1,000 bars | 10,000 bars | 100,000 bars |
|---|---|---|---|
| raw (tuple) | 0.05s | 0.20s | 1.50s |
| numpy | 0.08s | 0.30s | 2.00s |
| arrays | 0.09s | 0.35s | 2.20s |
| dict | 0.15s | 0.50s | 3.00s |
| pandas | 0.25s | 0.80s | 5.00s |
| json | 0.30s | 1.00s | 6.00s |
Benchmarks on Python 3.12, i7-13700K
๐ ๏ธ API Reference
TvDatafeed Class
TvDatafeed(
auth_token="unauthorized_user_token", # TradingView auth token
max_retries=3, # Connection retry attempts
timeout=10 # WebSocket timeout (seconds)
)
get() Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
symbol |
str | Required | Trading pair symbol |
exchange |
str | "BINANCE" |
Exchange identifier |
interval |
str | "1D" |
Timeframe interval |
start |
str/datetime | 30 days ago | Start of date range |
end |
str/datetime | now | End of date range |
output_format |
str | "pandas" |
Output format |
**format_kwargs |
dict | {} |
Extra formatter options |
Date Format Examples
# All these work:
tv.get("BTCUSDT", start="2024-01-01") # YYYY-MM-DD
tv.get("BTCUSDT", start="01-01-2024") # DD-MM-YYYY
tv.get("BTCUSDT", start="2024-01-01 12:30:00") # With time
tv.get("BTCUSDT", start=datetime(2024, 1, 1)) # datetime object
tv.get("BTCUSDT", start=1704067200) # Unix timestamp
โ Error Handling
from tv_scraper import TvDatafeed
from tv_scraper.exceptions import *
tv = TvDatafeed()
try:
df = tv.get("INVALID_SYMBOL")
except NoDataError:
print("No data returned for this symbol")
except ConnectionError:
print("Could not connect to TradingView")
except FormatError:
print("Invalid output format specified")
except InvalidSymbolError:
print("Symbol format is invalid")
Exception Hierarchy
TvScraperError (base)
โโโ ConnectionError โ WebSocket connection failures
โโโ NoDataError โ No data for symbol/range
โโโ InvalidSymbolError โ Bad symbol format
โโโ FormatError โ Invalid output format
โโโ ParseError โ Response parsing failures
๐งช Running Tests
pip install tv_scraper[dev]
pytest tests/ -v
pytest tests/ --cov=tv_scraper --cov-report=html
๐ค Contributing
We welcome contributions! See CONTRIBUTING.md for details.
- Fork the repository
- Create feature branch:
git checkout -b feature/amazing-feature - Commit changes:
git commit -m "Add amazing feature" - Push branch:
git push origin feature/amazing-feature - Open a Pull Request
๐ Changelog
See CHANGELOG.md for version history.
โ ๏ธ Disclaimer
This library is for educational and research purposes only.
- Respect TradingView's Terms of Service
- Implement appropriate rate limiting in production
- Consider using official APIs for critical applications
๐ License
MIT License โ See LICENSE for details.
๐ Star History
If you find this useful, please โญ star the repository!
๐ฌ Contact
- Author: Anurag Jha
- Email: anuragjha507@gmail.com
- GitHub: @anuragjha0001
- Issues: Report Bug
Made with โค๏ธ by Anurag Jha
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tv_scraper_py-0.1.0.tar.gz.
File metadata
- Download URL: tv_scraper_py-0.1.0.tar.gz
- Upload date:
- Size: 16.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a2088f376df192f9a8171afc1f98bb6f0fe3cedaeb66d1d5bf2e34113a0d09d
|
|
| MD5 |
10f8a2e85313c4dceddeedff471946da
|
|
| BLAKE2b-256 |
2e6266bff00545dcc4ff450b782af8aa80ab71c7e5a6a505e0256de8b94c5b80
|
File details
Details for the file tv_scraper_py-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tv_scraper_py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23d25a4140e43e959128b095f7d7966843ebb95604e1fa4e36ca52ca093e3dc8
|
|
| MD5 |
7a07e0b209a2878228c57345216afcfb
|
|
| BLAKE2b-256 |
7daed0f09fd6f01713faa96215268358b0a4de26961a574758ba6a184d49c06b
|