Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork250
Unofficial Python API client library for TD Ameritrade. This library allows for easy access of the Standard API and allows users to build data pipelines for the Streaming API.
License
areed1192/td-ameritrade-python-api
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This repo will soon be updated to include the newhttps://github.com/areed1192/td-ameritrade-apilibrary. The master branch will be overwritten and the old code will need to be updatedto reflect the new changes. If you would like to start rewriting your old code, pleaserefer to the new branch merge-new-repo or go the link above.
- Overview
- What's in the API
- Requirements
- API Key & Credentials
- Installation
- Usage
- Features
- Documentation & Resources
- Support These Projects
- Authentication Workflow
Current Version:0.3.5
The unofficial Python API client library for TD Ameritrade allows individuals withTD Ameritrade accounts to manage trades, pull historical and real-time data, managetheir accounts, create and modify orders all using the Python programming language.
To learn more about the TD Ameritrade API, please refer totheofficial documentation.
- Authentication - access tokens, refresh tokens, request authentication.
- Accounts & Trading
- Market Hours
- Instruments
- Movers
- Option Chains
- Price History
- Quotes
- Transaction History
- User Info & Preferences
- Watchlist
The following requirements must be met to use this API:
- A TD Ameritrade account, you'll need your account password and account number to use the API.
- A TD Ameritrade Developer Account
- A TD Ameritrade Developer API Key
- A Consumer ID
- A Redirect URI, sometimes called Redirect URL
- Python 3.7 or later.
Each TD Ameritrade API request requires a TD Ameritrade Developer API Key, a consumer ID,an account password, an account number, and a redirect URI. API Keys, consumer IDs, andredirect URIs are generated from the TD Ameritrade developer portal. To set up and createyour TD Ameritrade developer account, please refer totheofficial documentation.
Additionally, to authenticate yourself using this library, you will need to provide youraccount number and password for your main TD Ameritrade account.
Important: Your account number, an account password, consumer ID, and API key shouldbe kept secret.
The project can be found at PyPI, if you'd like to view the project please usethislink.
pip install td-ameritrade-python-api
To upgrade the library run the following command:
pip install --upgrade td-ameritrade-python-api
This example demonstrates how to login to the API and demonstrates sending a requestusing theget_quotes endpoint, using your API key.
Credentials:Please note, that thecredentials_path is a file path that will house the credentialslike your refresh token and access token. You must specify thecredentials_path argumentyourself so that you are aware of where the tokens will be stored. For example, if youspecify thecredentials_path asC:\Users\Public\Credentials\td_state.json it wouldstore your tokens in a JSON file located in a folder called Credentials located underthe Users profile.
# Import the clientfromtd.clientimportTDClient# Create a new session, credentials path is required.TDSession=TDClient(client_id='<CLIENT_ID>',redirect_uri='<REDIRECT_URI>',credentials_path='<PATH_TO_CREDENTIALS_FILE>')# Login to the sessionTDSession.login()# Grab real-time quotes for 'MSFT' (Microsoft)msft_quotes=TDSession.get_quotes(instruments=['MSFT'])# Grab real-time quotes for 'AMZN' (Amazon) and 'SQ' (Square)multiple_quotes=TDSession.get_quotes(instruments=['AMZN','SQ'])
Automatically will handle the authentication workflow for new users, returning users, and userswith expired tokens (refresh token or access token).
For certain requests, in a limited fashion, it will help validate your request when possible.For example, when using theget_movers endpoint, it will automatically validate that themarket you're requesting data from is one of the valid options.
Requests for saved orders, regular orders, watchlists, and option chains can be a challengingprocess that has multiple opportunities to make mistakes. This library has built-in objectsthat will allow you to quickly build your request and then validate certain portions of yourrequest when possible.
The following requirements must be met before being able to use the TD Ameritrade Python API library.
- You must have a TD Ameritrade Account.
- You must have a TD Ameritrade Developer Account. Please go to followingfolderfor instructions on how to create a Developer account.
Patreon:Help support this project and future projects by donating to myPatreon Page.I'm always looking to add more content for individuals like yourself, unfortuantely some of the APIs I would requireme to pay monthly fees.
YouTube:If you'd like to watch more of my content, feel free to visit my YouTube channelSigma Coding.
Hire Me:If you have a project, you think I can help you with feel free to reach out atcoding.sigma@gmail.com or fill out thecontract request form
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:
From the dropdown, clickRun 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 foryou authentication. You can either copy the link and paste it into a browser manually or if you're using VisualStudio Code you can pressCTRL + Click to have Visual Studio Code navigate you to the URL immeditately.
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 reminderthese, are the same username/password combination you use to login to your regular TD Account.
Step 4 - Accept the Terms:
Accept the Terms of the API by clickingAllow, this will redirect you.
Step 5 - Copy the Authorization Code:
After accepting the terms, you'll be taken to the URL that you provided as yourredirect URI. However, atthe end of that URL will beauthorization code. To complete the authentication workflow, copy the URL asit appears below. Don't worry if the numbers don't match, as you will have a different code.
Step 6 - Paste the Authorization Code in the Terminal:
Take the URL and copy it into the Terminal, after you have pasted it, pressEnter. The authentication workflowwill complete and the script will start running. At this stage, we are exchanging your authorization code foran access token. That access token is valid only for 30 minutes. However, a refresh token is also stored thatwill refresh your access token when it expires.
After, that the script should run. Additionally, if you go to the location you specified in thecredentials_patharugment you will now seetd_state.json file. This file contains all the info used during a session. PleaseDO NOT DELETE THIS FILE OR ELSE YOU WILL NEED TO GO THROUGH THE STEPS ABOVE.
About
Unofficial Python API client library for TD Ameritrade. This library allows for easy access of the Standard API and allows users to build data pipelines for the Streaming API.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Contributors9
Uh oh!
There was an error while loading.Please reload this page.





