|
| 1 | +# thetadata-api-python |
| 2 | + |
| 3 | +A simple, easy-to-use, unofficial Python wrapper for the [ThetaData REST API](https://http-docs.thetadata.us/docs/theta-data-rest-api-v2/4g9ms9h4009k0-getting-started). |
| 4 | + |
| 5 | +> #### Bonus: New ThetaData customers can get 30% off their first month with code **PYTHON4TRADERS** |
| 6 | + |
| 7 | +## Context |
| 8 | +The original [thetadata-python](https://github.com/ThetaData-API/thetadata-python) library is deprecated and it's preferred to use the REST API directly. |
| 9 | + |
| 10 | +Their documentation provides Python examples for the REST API that are useful and comprehensive and you'll find these on every endpoint page ([example](https://http-docs.thetadata.us/docs/theta-data-rest-api-v2/a38vp739baoch-quote-snapshot)). This library is a simple wrapper on those examples into neat classes and functions that anyone can download and use. It provides 2 additional conveniences: |
| 11 | + |
| 12 | +1. Integration with pandas, so data is returned to your program directly in a DataFrame. |
| 13 | +2. A CLI (command-line-interface) wrapper for downloading data directly without writing any code yourself |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +> ### Make sure [ThetaTerminal](https://http-docs.thetadata.us/docs/theta-data-rest-api-v2/4g9ms9h4009k0-getting-started#what-is-theta-terminal-and-why-do-i-need-it) is running - nothing will work without it! |
| 18 | + |
| 19 | +From Python: |
| 20 | + |
| 21 | +``` |
| 22 | +stocks_snapshot = ThetaDataStocksSnapshot(log_level="INFO", output_dir="./output") |
| 23 | +quotes_df = stocks_snapshot.get_quotes("AAPL") |
| 24 | +print(quotes_df.head()) |
| 25 | +``` |
| 26 | + |
| 27 | +Several code examples are available [here](https://github.com/pythonfortraders/thetadata-api-python/tree/main/examples). |
| 28 | + |
| 29 | +From the command line: |
| 30 | + |
| 31 | +``` |
| 32 | +(pft) ➜ cli git:(main) python thetadata_cli.py |
| 33 | + |
| 34 | + Usage: thetadata_cli.py [OPTIONS] COMMAND [ARGS]... |
| 35 | + |
| 36 | +╭─ Options ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ |
| 37 | +│ --install-completion Install completion for the current shell. │ |
| 38 | +│ --show-completion Show completion for the current shell, to copy it or customize the installation. │ |
| 39 | +│ --help Show this message and exit. │ |
| 40 | +╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ |
| 41 | +╭─ Commands ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ |
| 42 | +│ options │ |
| 43 | +│ stocks │ |
| 44 | +╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ |
| 45 | +``` |
| 46 | + |
| 47 | +Subcommands nest downwards naturally. For example, let's say you want to get historical OHLC data for a stock: |
| 48 | +``` |
| 49 | +(pft) ➜ cli git:(main) python thetadata_cli.py stocks historical ohlc AAPL 20240101 20240201 |
| 50 | +⠸ Loading data...Data retrieved successfully |
| 51 | +``` |
| 52 | +This will save the data as a local CSV named `ohlc_AAPL_20240101_20240201.csv`. |
| 53 | + |
| 54 | +### More Resources |
| 55 | + |
| 56 | +If you want to learn more about working with market data in Python, here are some resources for you: |
| 57 | +* [Free Algo Trading Academy](https://www.skool.com/algo-trading-academy-4983) |
| 58 | +* [Free Tutorial Videos](https://www.youtube.com/@PythonforTraders) |
| 59 | +* [Course: Financial Data Mastery](https://skool.com/pythonfortraders) |
| 60 | + * Learn to acquire, augment, analyze, and automate the way you work with financial data. |
| 61 | +* [Project: Securities Master Database and ETL Pipeline](https://skool.com/pythonfortraders): |
| 62 | + * Build a securities database and data pipeline that you can apply and adopt for your own market data needs right away. |
0 commit comments