Spaces:
Sleeping
Sleeping
Download scripts/schwab_level2_stream.py from Arrechenash/stocks: direct link, hf CLI and curl.
- Browser
- Download file 1.62 kB
-
https://huggingface.co/spaces/Arrechenash/stocks/resolve/main/scripts/schwab_level2_stream.py
- Command line
-
hf download hf://spaces/Arrechenash/stocks/scripts/schwab_level2_stream.py
-
curl -L -o schwab_level2_stream.py https://huggingface.co/spaces/Arrechenash/stocks/resolve/main/scripts/schwab_level2_stream.py
1.62 kB
| #!/usr/bin/env python3 | |
| """Schwab Level 2 (book + tape) stream — load credentials from .env. | |
| Usage: | |
| uv run scripts/schwab_level2_stream.py [SYMBOL ...] | |
| Requires a .env with SCHWAB_API_KEY / SCHWAB_APP_SECRET and an authenticated | |
| token (run the app's OAuth flow once). Subscribes to the Level 2 book and | |
| Time & Sales for the given symbols (default AMD) and prints every message. | |
| Press Enter to stop. | |
| """ | |
| import asyncio | |
| import logging | |
| import os | |
| import sys | |
| from core.infra.env import load_env | |
| logging.basicConfig(level=logging.INFO, format="%(message)s") | |
| logger = logging.getLogger(__name__) | |
| async def _run(symbols: list[str]) -> int: | |
| load_env() | |
| # Import after load_env(): modules.schwab.client reads its credentials at | |
| # import time. | |
| from modules.schwab.streaming import SchwabStreamingProvider | |
| if not os.getenv("SCHWAB_API_KEY") or not os.getenv("SCHWAB_APP_SECRET"): | |
| logger.error("SCHWAB_API_KEY and SCHWAB_APP_SECRET must be set in .env") | |
| return 1 | |
| provider = SchwabStreamingProvider() | |
| provider.on_book(print) | |
| provider.on_timesale(print) | |
| await provider.start() | |
| await provider.subscribe_book(symbols) | |
| await provider.subscribe_timesale(symbols) | |
| try: | |
| await asyncio.to_thread(input, f"Streaming book + tape for {', '.join(symbols)}. Press Enter to stop...\n") | |
| except (EOFError, KeyboardInterrupt): | |
| pass | |
| finally: | |
| await provider.stop() | |
| return 0 | |
| def main() -> None: | |
| symbols = [s.upper() for s in sys.argv[1:]] or ["AMD"] | |
| sys.exit(asyncio.run(_run(symbols))) | |
| if __name__ == "__main__": | |
| main() | |