- Notifications
You must be signed in to change notification settings - Fork37
The unofficial Python API client library for the Charles Schwab API. This library allows for easy access of the Standard REST API and Streaming API.
License
areed1192/td-ameritrade-api
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Current Version:0.1.1
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.
Setup - Requirements Install:*
For this particular project, you only need to install the dependencies, to use the project. The dependenciesare listed in therequirements.txt
file and can be installed by running the following command:
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 itbefore it has been indexed onPyPi
. I would recommend you either install this projectineditable
mode or do alocal install
. For those of you, who want to make modificationsto this project. I would recommend you install the library ineditable
mode.
If you want to install the library ineditable
mode, make sure to run thesetup.py
file, so you can install any dependencies you may need. To run thesetup.py
file,run the following command in your terminal.
pip install -e .
If you don't plan to make any modifications to the project but still want to use it acrossyour different projects, then do a local install.
pip install .
This will install all the dependencies listed in thesetup.py
file. Once doneyou can use the library wherever you want.
Setup - PyPi Install:
Toinstall the library, run the following command from the terminal.
pip install py-tda-api
Setup - PyPi Upgrade:
Toupgrade the library, run the following command from the terminal.
pip install --upgrade py-tda-api
Here is a simple example of using thetd
library.
frompprintimportpprintfromconfigparserimportConfigParserfromtd.credentialsimportTdCredentialsfromtd.clientimportTdAmeritradeClient# 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']))
Patreon:Help support this project and future projects by donating to myPatreon Page. I'malways looking to add more content for individuals like yourself, unfortuantely some of the APIs I would require me topay monthly fees.
YouTube:If you'd like to watch more of my content, feel free to visit my YouTube channelSigma Coding.
Questions:If you have questions please feel free to reach out to me atcoding.sigma@gmail.com
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_path
arugment 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.