A Python application that uses the schwab-py library to stream real-time market data from Charles Schwab's API.
- Real-time Market Data: Stream live quotes, order book data, and chart information
- Multiple Data Types: Support for equity quotes, NASDAQ/NYSE order books, and OHLCV chart data
- Easy Configuration: Simple environment-based configuration
- Automatic Account Detection: Automatically retrieves your account ID from the Schwab API
- Flexible Symbol Selection: Stream data for any valid stock symbols
- Robust Error Handling: Comprehensive error handling and logging
- Python 3.8 or higher
- Charles Schwab Developer Account
- Valid Schwab API credentials
git clone https://github.com/merplerps/schwab-streaming-client.git
cd schwab-streaming-clientpython3 setup_schwab_streaming.pyUpdate the .env file with your actual Schwab API credentials:
SCHWAB_API_KEY=your_actual_api_key
SCHWAB_APP_SECRET=your_actual_app_secret
SCHWAB_REDIRECT_URI=https://127.0.0.1:8182
# SCHWAB_ACCOUNT_ID=your_actual_account_id # Optional - auto-retrievedNote: The SCHWAB_ACCOUNT_ID is now optional! The client will automatically retrieve your account ID from the Schwab API after authentication.
# On macOS/Linux
source activate_schwab.sh
# On Windows
activate_schwab.bat# Stream default symbols (AAPL, GOOGL, MSFT, TSLA, SPY)
python schwab_streaming.py
# Stream custom symbols
python schwab_streaming.py NVDA,AMD,INTCThe first time you run the script, you'll need to complete OAuth authentication:
- The script will open your browser to the Schwab OAuth URL
- Log in to your Schwab account
- Authorize the application
- The script will automatically capture the authorization code
- A
schwab_token.jsonfile will be created for future use
The client supports multiple types of real-time data:
- Real-time bid/ask prices
- Last trade information
- Volume and timestamp data
- NASDAQ Level 2 order book
- NYSE Level 2 order book
- Bid/ask depth information
- Open, High, Low, Close prices
- Volume data
- Time-based intervals
| Variable | Description | Required |
|---|---|---|
SCHWAB_API_KEY |
Your Schwab API key | Yes |
SCHWAB_APP_SECRET |
Your Schwab app secret | Yes |
SCHWAB_ACCOUNT_ID |
Your Schwab account ID | No (auto-retrieved) |
SCHWAB_REDIRECT_URI |
OAuth redirect URI | No (default: https://127.0.0.1:8182) |
You can modify the streaming behavior in schwab_streaming.py:
- Default symbols: Change the
symbolslist in themain()function - Stream duration: Modify the
durationvariable (set toNonefor indefinite streaming) - Data handlers: Customize the message handlers for different data types
# Stream default symbols for 60 seconds
python schwab_streaming.py
# Stream custom symbols for 60 seconds
python schwab_streaming.py AAPL,GOOGL,MSFT# Automatically get your account ID
python get_account_id.py# Try the demo with mock data
python demo_streaming.py# Test your configuration
python simple_streaming_test.py
# Test account ID retrieval
python test_account_id_script.py
# Test installation
python test_installation.pyschwab-streaming-client/
βββ schwab_streaming.py # Main streaming client
βββ get_account_id.py # Account ID retrieval script
βββ demo_streaming.py # Demo with mock data
βββ setup_schwab_streaming.py # Setup and installation
βββ simple_streaming_test.py # Configuration testing
βββ test_installation.py # Installation verification
βββ requirements.txt # Python dependencies
βββ .env # Environment variables
βββ activate_schwab.sh # Environment activation (Linux/Mac)
βββ activate_schwab.bat # Environment activation (Windows)
βββ README.md # This file
βββ README_schwab_streaming.md # Detailed documentation
βββ FIND_ACCOUNT_ID.md # Account ID lookup guide
βββ ACCOUNT_ID_SCRIPT_GUIDE.md # Account ID script guide
βββ ACCOUNT_ID_AUTO_RETRIEVAL.md # Auto-retrieval documentation
-
Authentication Errors
- Ensure your API credentials are correct
- Check that the OAuth redirect URI matches your app configuration
- Delete
schwab_token.jsonand re-authenticate if needed
-
No Data Received
- Verify you're running during market hours
- Check that the symbols are valid and tradeable
- Ensure your account has streaming data permissions
-
Connection Errors
- Check your internet connection
- Verify Schwab API service status
- Try restarting the application
Enable debug logging by modifying the script:
import logging
logging.basicConfig(level=logging.DEBUG)- Schwab API has rate limits for streaming data
- Some data streams may not be available for all symbols
- Market hours restrictions apply to real-time data
- Never commit your
.envfile orschwab_token.jsonto version control - Keep your API credentials secure
- Use environment variables in production deployments
schwab-py: Official Schwab API clientpython-dotenv: Environment variable managementasyncio: Asynchronous programming supportwebsockets: WebSocket communicationhttpx: HTTP client librarypydantic: Data validation
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is for educational and personal use. Please comply with Schwab's API terms of service.
For issues related to:
- Schwab API: Contact Schwab Developer Support
- schwab-py library: Check the official documentation
- This project: Create an issue in the repository
- schwab-py - Official Schwab API client
- Charles Schwab - API provider
- Python community for excellent libraries