# Unofficial TD Ameritrade Python API Library
## Table of Contents
- [Overview](#overview)
- [Setup](#setup)
- [Usage](#usage)
- [Support These Projects](#support-these-projects)
## Overview
Current Version: **0.1.1**
The unofficial Python API client library for TD Ameritrade allows individuals with
TD Ameritrade accounts to manage trades, pull historical and real-time data, manage
their accounts, create and modify orders all using the Python programming language.
To learn more about the TD Ameritrade API, please refer to
the [official documentation](https://developer.tdameritrade.com/apis).
## Setup
**Setup - Requirements Install:***
For this particular project, you only need to install the dependencies, to use the project. The dependencies
are listed in the `requirements.txt` file and can be installed by running the following command:
```console
pip install -r requirements.txt
```
After running that command, the dependencies should be installed.
**Setup - Local Install:**
If you are planning to make modifications to this project or you would like to access it
before it has been indexed on `PyPi`. I would recommend you either install this project
in `editable` mode or do a `local install`. For those of you, who want to make modifications
to this project. I would recommend you install the library in `editable` mode.
If you want to install the library in `editable` mode, make sure to run the `setup.py`
file, so you can install any dependencies you may need. To run the `setup.py` file,
run the following command in your terminal.
```console
pip install -e .
```
If you don't plan to make any modifications to the project but still want to use it across
your different projects, then do a local install.
```console
pip install .
```
This will install all the dependencies listed in the `setup.py` file. Once done
you can use the library wherever you want.
**Setup - PyPi Install:**
To **install** the library, run the following command from the terminal.
```console
pip install py-tda-api
```
**Setup - PyPi Upgrade:**
To **upgrade** the library, run the following command from the terminal.
```console
pip install --upgrade py-tda-api
```
## Usage
Here is a simple example of using the `td` library.
```python
from pprint import pprint
from configparser import ConfigParser
from td.credentials import TdCredentials
from td.client import TdAmeritradeClient
# Initialize the Parser.
config = ConfigParser()
# Read the file.
config.read('config/config.ini')
# Get the specified credentials.
client_id = config.get('main', 'client_id')
redirect_uri = config.get('main', 'redirect_uri')
# Intialize our `Credentials` object.
td_credentials = TdCredentials(
client_id=client_id,
redirect_uri=redirect_uri,
credential_file='config/td_credentials.json'
)
# Initalize the `TdAmeritradeClient`
td_client = TdAmeritradeClient(
credentials=td_credentials
)
# Initialize the Quotes service.
quote_service = td_client.quotes()
# Grab a single quote.
pprint(
quote_service.get_quote(instrument='AAPL')
)
# Grab multiple quotes.
pprint(
quote_service.get_quotes(instruments=['AAPL', 'SQ'])
)
```
## Support These Projects
**Patreon:**
Help support this project and future projects by donating to my [Patreon Page](https://www.patreon.com/sigmacoding). I'm
always looking to add more content for individuals like yourself, unfortuantely some of the APIs I would require me to
pay monthly fees.
**YouTube:**
If you'd like to watch more of my content, feel free to visit my YouTube channel [Sigma Coding](https://www.youtube.com/c/SigmaCoding).
**Questions:**
If you have questions please feel free to reach out to me at [coding.sigma@gmail.com](mailto:coding.sigma@gmail.com?subject=[GitHub]%20Fred%20Library)
## Authentication Workflow
**Step 1 - Start the Script:**
While in Visual Studio Code, right click anywhere in the code editor while in the file that contains your code.
The following dropdown will appear:
![Terminal Dropdown](https://raw.githubusercontent.com/areed1192/td-ameritrade-python-api/master/samples/instructions/photos/terminal_dropdown.jpg "Terminal Dropdown")
From the dropdown, click `Run Python file in Terminal`, this will start the python script.
**Step 2 - Go to Redirect URL:**
The TD Library will automatically generate the redirect URL that will navigate you to the TD website for for
you authentication. You can either copy the link and paste it into a browser manually or if you're using Visual
Studio Code you can press `CTRL + Click` to have Visual Studio Code navigate you to the URL immeditately.
![Redirect URI](https://raw.githubusercontent.com/areed1192/td-ameritrade-python-api/master/samples/instructions/photos/redirect_uri.jpg "Redirect URI")
**Step 3 - Login to the TD API:**
Once you've arrived at the login screen, you'll need to provide your credentials to authenticate the session.
Please provide your Account Username and Account Password in the userform and then press enter. As a reminder
these, are the same username/password combination you use to login to your regular TD Account.
!["TD Login](https://raw.githubusercontent.com/areed1192/td-ameritrade-python-api/master/samples/instructions/photos/td_login.jpg "TD Login")
**Step 4 - Accept the Terms:**
Accept the Terms of the API by clicking `Allow`, this will redirect you.
![TD Terms](https://raw.githubusercontent.com/areed1192/td-ameritrade-python-api/master/samples/instructions/photos/td_terms.jpg "TD Terms")
**Step 5 - Copy the Authorization Code:**
After accepting the terms, you'll be taken to the URL that you provided as your `redirect URI`. However, at
the end of that URL will be `authorization code`. To complete the authentication workflow, copy the URL as
it appears below. Don't worry if the numbers don't match, as you will have a different code.
![Auth Code](https://raw.githubusercontent.com/areed1192/td-ameritrade-python-api/master/samples/instructions/photos/auth_code.jpg "Auth Code")
**Step 6 - Paste the Authorization Code in the Terminal:**
Take the URL and copy it into the Terminal, after you have pasted it, press `Enter`. The authentication workflow
will complete and the script will start running. At this stage, we are exchanging your authorization code for
an access token. That access token is valid only for 30 minutes. However, a refresh token is also stored that
will refresh your access token when it expires.
![Paste URL](https://raw.githubusercontent.com/areed1192/td-ameritrade-python-api/master/samples/instructions/photos/paste_url.jpg "Paste URL")
After, that the script should run. Additionally, if you go to the location you specified in the `credentials_path`
arugment you will now see `td_state.json` file. This file contains all the info used during a session. Please
DO NOT DELETE THIS FILE OR ELSE YOU WILL NEED TO GO THROUGH THE STEPS ABOVE.
Raw data
{
"_id": null,
"home_page": "https://github.com/areed1192/td-ameritrade-api",
"name": "py-tda-api",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "finance,td ameritrade,api",
"author": "Alex Reed",
"author_email": "coding.sigma@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/27/be/7dfcd731375756aa376c85c246cdc042a8a457bed1a64dbd74849d8c10f6/py-tda-api-0.1.3.tar.gz",
"platform": null,
"description": "# Unofficial TD Ameritrade Python API Library\r\n\r\n## Table of Contents\r\n\r\n- [Overview](#overview)\r\n- [Setup](#setup)\r\n- [Usage](#usage)\r\n- [Support These Projects](#support-these-projects)\r\n\r\n## Overview\r\n\r\nCurrent Version: **0.1.1**\r\n\r\nThe unofficial Python API client library for TD Ameritrade allows individuals with\r\nTD Ameritrade accounts to manage trades, pull historical and real-time data, manage\r\ntheir accounts, create and modify orders all using the Python programming language.\r\n\r\nTo learn more about the TD Ameritrade API, please refer to\r\nthe [official documentation](https://developer.tdameritrade.com/apis).\r\n\r\n## Setup\r\n\r\n**Setup - Requirements Install:***\r\n\r\nFor this particular project, you only need to install the dependencies, to use the project. The dependencies\r\nare listed in the `requirements.txt` file and can be installed by running the following command:\r\n\r\n```console\r\npip install -r requirements.txt\r\n```\r\n\r\nAfter running that command, the dependencies should be installed.\r\n\r\n**Setup - Local Install:**\r\n\r\nIf you are planning to make modifications to this project or you would like to access it\r\nbefore it has been indexed on `PyPi`. I would recommend you either install this project\r\nin `editable` mode or do a `local install`. For those of you, who want to make modifications\r\nto this project. I would recommend you install the library in `editable` mode.\r\n\r\nIf you want to install the library in `editable` mode, make sure to run the `setup.py`\r\nfile, so you can install any dependencies you may need. To run the `setup.py` file,\r\nrun the following command in your terminal.\r\n\r\n```console\r\npip install -e .\r\n```\r\n\r\nIf you don't plan to make any modifications to the project but still want to use it across\r\nyour different projects, then do a local install.\r\n\r\n```console\r\npip install .\r\n```\r\n\r\nThis will install all the dependencies listed in the `setup.py` file. Once done\r\nyou can use the library wherever you want.\r\n\r\n**Setup - PyPi Install:**\r\n\r\nTo **install** the library, run the following command from the terminal.\r\n\r\n```console\r\npip install py-tda-api\r\n```\r\n\r\n**Setup - PyPi Upgrade:**\r\n\r\nTo **upgrade** the library, run the following command from the terminal.\r\n\r\n```console\r\npip install --upgrade py-tda-api\r\n```\r\n\r\n## Usage\r\n\r\nHere is a simple example of using the `td` library.\r\n\r\n```python\r\nfrom pprint import pprint\r\nfrom configparser import ConfigParser\r\nfrom td.credentials import TdCredentials\r\nfrom td.client import TdAmeritradeClient\r\n\r\n\r\n# Initialize the Parser.\r\nconfig = ConfigParser()\r\n\r\n# Read the file.\r\nconfig.read('config/config.ini')\r\n\r\n# Get the specified credentials.\r\nclient_id = config.get('main', 'client_id')\r\nredirect_uri = config.get('main', 'redirect_uri')\r\n\r\n# Intialize our `Credentials` object.\r\ntd_credentials = TdCredentials(\r\n client_id=client_id,\r\n redirect_uri=redirect_uri,\r\n credential_file='config/td_credentials.json'\r\n)\r\n\r\n# Initalize the `TdAmeritradeClient`\r\ntd_client = TdAmeritradeClient(\r\n credentials=td_credentials\r\n)\r\n\r\n# Initialize the Quotes service.\r\nquote_service = td_client.quotes()\r\n\r\n# Grab a single quote.\r\npprint(\r\n quote_service.get_quote(instrument='AAPL')\r\n)\r\n\r\n# Grab multiple quotes.\r\npprint(\r\n quote_service.get_quotes(instruments=['AAPL', 'SQ'])\r\n)\r\n```\r\n\r\n## Support These Projects\r\n\r\n**Patreon:**\r\nHelp support this project and future projects by donating to my [Patreon Page](https://www.patreon.com/sigmacoding). I'm\r\nalways looking to add more content for individuals like yourself, unfortuantely some of the APIs I would require me to\r\npay monthly fees.\r\n\r\n**YouTube:**\r\nIf you'd like to watch more of my content, feel free to visit my YouTube channel [Sigma Coding](https://www.youtube.com/c/SigmaCoding).\r\n\r\n**Questions:**\r\nIf you have questions please feel free to reach out to me at [coding.sigma@gmail.com](mailto:coding.sigma@gmail.com?subject=[GitHub]%20Fred%20Library)\r\n\r\n## Authentication Workflow\r\n\r\n**Step 1 - Start the Script:**\r\n\r\nWhile in Visual Studio Code, right click anywhere in the code editor while in the file that contains your code.\r\nThe following dropdown will appear:\r\n\r\n![Terminal Dropdown](https://raw.githubusercontent.com/areed1192/td-ameritrade-python-api/master/samples/instructions/photos/terminal_dropdown.jpg \"Terminal Dropdown\")\r\n\r\nFrom the dropdown, click `Run Python file in Terminal`, this will start the python script.\r\n\r\n**Step 2 - Go to Redirect URL:**\r\n\r\nThe TD Library will automatically generate the redirect URL that will navigate you to the TD website for for\r\nyou authentication. You can either copy the link and paste it into a browser manually or if you're using Visual\r\nStudio Code you can press `CTRL + Click` to have Visual Studio Code navigate you to the URL immeditately.\r\n\r\n![Redirect URI](https://raw.githubusercontent.com/areed1192/td-ameritrade-python-api/master/samples/instructions/photos/redirect_uri.jpg \"Redirect URI\")\r\n\r\n**Step 3 - Login to the TD API:**\r\n\r\nOnce you've arrived at the login screen, you'll need to provide your credentials to authenticate the session.\r\nPlease provide your Account Username and Account Password in the userform and then press enter. As a reminder\r\nthese, are the same username/password combination you use to login to your regular TD Account.\r\n\r\n![\"TD Login](https://raw.githubusercontent.com/areed1192/td-ameritrade-python-api/master/samples/instructions/photos/td_login.jpg \"TD Login\")\r\n\r\n**Step 4 - Accept the Terms:**\r\n\r\nAccept the Terms of the API by clicking `Allow`, this will redirect you.\r\n\r\n![TD Terms](https://raw.githubusercontent.com/areed1192/td-ameritrade-python-api/master/samples/instructions/photos/td_terms.jpg \"TD Terms\")\r\n\r\n**Step 5 - Copy the Authorization Code:**\r\n\r\nAfter accepting the terms, you'll be taken to the URL that you provided as your `redirect URI`. However, at\r\nthe end of that URL will be `authorization code`. To complete the authentication workflow, copy the URL as\r\nit appears below. Don't worry if the numbers don't match, as you will have a different code.\r\n\r\n![Auth Code](https://raw.githubusercontent.com/areed1192/td-ameritrade-python-api/master/samples/instructions/photos/auth_code.jpg \"Auth Code\")\r\n\r\n**Step 6 - Paste the Authorization Code in the Terminal:**\r\n\r\nTake the URL and copy it into the Terminal, after you have pasted it, press `Enter`. The authentication workflow\r\nwill complete and the script will start running. At this stage, we are exchanging your authorization code for\r\nan access token. That access token is valid only for 30 minutes. However, a refresh token is also stored that\r\nwill refresh your access token when it expires.\r\n\r\n![Paste URL](https://raw.githubusercontent.com/areed1192/td-ameritrade-python-api/master/samples/instructions/photos/paste_url.jpg \"Paste URL\")\r\n\r\nAfter, that the script should run. Additionally, if you go to the location you specified in the `credentials_path`\r\narugment you will now see `td_state.json` file. This file contains all the info used during a session. Please\r\nDO NOT DELETE THIS FILE OR ELSE YOU WILL NEED TO GO THROUGH THE STEPS ABOVE.\r\n",
"bugtrack_url": null,
"license": "",
"summary": "A python client lirbary for the TD Ameritrade API.",
"version": "0.1.3",
"split_keywords": [
"finance",
"td ameritrade",
"api"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2bdf3624380df519e9ddfb1f860b598c60fc4d151054cb8c879dd3813454c313",
"md5": "af604af9e43c81d3dfdbc415e0aa617a",
"sha256": "f0b956d376aff31b6419452be26f165c94eafbfa8d4316a6a2db06ecbfbdca57"
},
"downloads": -1,
"filename": "py_tda_api-0.1.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "af604af9e43c81d3dfdbc415e0aa617a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 47075,
"upload_time": "2023-03-12T03:57:06",
"upload_time_iso_8601": "2023-03-12T03:57:06.703283Z",
"url": "https://files.pythonhosted.org/packages/2b/df/3624380df519e9ddfb1f860b598c60fc4d151054cb8c879dd3813454c313/py_tda_api-0.1.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "27be7dfcd731375756aa376c85c246cdc042a8a457bed1a64dbd74849d8c10f6",
"md5": "d4c219c2798ba2a34446465b82e73e3f",
"sha256": "1b901dd2aca782317e6df5d749695665a517178df237f3b14ca013754b8ccf54"
},
"downloads": -1,
"filename": "py-tda-api-0.1.3.tar.gz",
"has_sig": false,
"md5_digest": "d4c219c2798ba2a34446465b82e73e3f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 39671,
"upload_time": "2023-03-12T03:57:08",
"upload_time_iso_8601": "2023-03-12T03:57:08.330320Z",
"url": "https://files.pythonhosted.org/packages/27/be/7dfcd731375756aa376c85c246cdc042a8a457bed1a64dbd74849d8c10f6/py-tda-api-0.1.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-03-12 03:57:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "areed1192",
"github_project": "td-ameritrade-api",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "py-tda-api"
}