This directory contains example scripts demonstrating how to use TvDatafeed.
- Install TvDatafeed:
pip install -e ..- (Optional) Set up environment variables for authentication:
export TV_USERNAME="your_username"
export TV_PASSWORD="your_password"Or create a .env file in the project root:
TV_USERNAME=your_username
TV_PASSWORD=your_password
Demonstrates fundamental features:
- Connecting to TradingView (with and without authentication)
- Downloading historical data
- Working with DataFrame results
- Basic data analysis
- Saving data to CSV
Run:
python basic_usage.pyWhat you'll learn:
- How to initialize TvDatafeed
- How to fetch historical bars
- How to work with the returned DataFrame
- Basic data manipulation with pandas
Shows how to use real-time data monitoring:
- Creating live data feeds for multiple symbols
- Setting up callbacks for new data
- Multiple consumers for the same symbol
- Proper cleanup
Run:
python live_feed.pyRequirements:
- TradingView account credentials (authenticated access required)
What you'll learn:
- How to create SEIS (Symbol-Exchange-Interval Set)
- How to register callbacks
- How to monitor multiple symbols simultaneously
- How to properly clean up resources
Demonstrates robust error handling:
- Authentication errors
- Invalid symbols
- Timeout handling
- Symbol search
Run:
python error_handling.pyWhat you'll learn:
- How to handle different types of errors
- How to use symbol search to find correct symbol names
- Best practices for error handling
- How to use logging for debugging
Demonstrates 2FA/TOTP authentication (NEW in PR #30):
- TOTP secret key for automatic code generation (recommended)
- Manual 6-digit code input
- Environment variable configuration
- Production-ready security patterns
Run:
python 2fa_authentication.pyRequirements:
- TradingView account with 2FA enabled
- TOTP secret key (from TradingView 2FA setup)
- Optional:
pyotppackage for automatic code generation
What you'll learn:
- How to authenticate with 2FA/TOTP
- How to get your TOTP secret key from TradingView
- Secure credential management with environment variables
- Error handling for 2FA scenarios
Demonstrates fetching data by date range instead of n_bars (NEW in PR #69):
- Query by start_date and end_date
- Timezone-aware data handling
- Validation of date ranges
- Use cases for backtesting
Run:
python date_range_search.pyWhat you'll learn:
- How to use start_date and end_date parameters
- Difference between n_bars and date range queries
- How to access timezone metadata in DataFrame
- Date validation rules and constraints
Demonstrates verbose logging control (NEW in PR #37):
- Quiet mode for production (verbose=False)
- Verbose mode for development (verbose=True)
- Environment variable configuration (TV_VERBOSE)
- Logging level control
Run:
python quiet_mode.pyWhat you'll learn:
- How to control log verbosity
- Difference between verbose and quiet modes
- Production vs development logging patterns
Demonstrates handling CAPTCHA authentication requirements:
- Extracting auth token from browser
- Using pre-obtained tokens
- Secure token storage
Run:
python captcha_workaround.pyWhat you'll learn:
- How to handle CaptchaRequiredError
- How to extract auth token from browser cookies
- Secure token management
Comprehensive guide for symbol formatting:
- EXCHANGE:SYMBOL format
- Symbol search usage
- Common exchange symbols
- Troubleshooting symbol issues
Run:
python symbol_format_guide.pyWhat you'll learn:
- Correct symbol format for TradingView
- How to find symbols for different markets
- Best practices for symbol usage
For examples requiring authentication, you can:
Option 1: Environment variables (recommended)
export TV_USERNAME="your_username"
export TV_PASSWORD="your_password"
python live_feed.pyOption 2: .env file
Create a .env file in the project root:
TV_USERNAME=your_username
TV_PASSWORD=your_password
Option 3: Direct in code (not recommended for production)
tv = TvDatafeed(username="your_username", password="your_password")If you're unsure of the exact symbol name, use symbol search:
from tvDatafeed import TvDatafeed
tv = TvDatafeed()
results = tv.search_symbol('BTC', 'BINANCE')
for result in results:
print(f"{result['symbol']} - {result['description']}")from tvDatafeed import Interval
# Minutes
Interval.in_1_minute
Interval.in_3_minute
Interval.in_5_minute
Interval.in_15_minute
Interval.in_30_minute
Interval.in_45_minute
# Hours
Interval.in_1_hour
Interval.in_2_hour
Interval.in_3_hour
Interval.in_4_hour
# Other
Interval.in_daily
Interval.in_weekly
Interval.in_monthly- Crypto:
BINANCE,COINBASE,KRAKEN,BITFINEX - US Stocks:
NASDAQ,NYSE,AMEX - Indian Markets:
NSE,BSE - Commodities:
MCX,NYMEX,COMEX - Forex:
FX,OANDA
"No data returned"
- Check symbol name with
search_symbol() - Verify exchange is correct
- Try with authentication
"Authentication failed"
- Verify username and password
- If 2FA is enabled, see
2fa_authentication.pyfor setup instructions - Ensure credentials are set in environment variables
- For CAPTCHA issues, see
captcha_workaround.py
"TwoFactorRequiredError"
- Your account has 2FA enabled
- Provide
totp_secret(recommended) ortotp_codeparameter - See
2fa_authentication.pyfor detailed examples
"Timeout errors"
- Check your internet connection
- Increase timeout:
TvDatafeed(ws_timeout=30.0) - TradingView might be experiencing issues
- Try again later
For more details, see the main README.md in the project root.