Python Library for TDAmeritrade API
Project description
tdlink
Python Library for TDAmeritrade API
All the dates and times are in UTC.
Authentication
NOTE: Please make sure you obtain a code or refresh token from TDAmeritrade before using this library.
Follow these steps to get a code:
- Go to TDAmeritrade developer: https://developer.tdameritrade.com/
- Login and go to "My Apps"
- Create an app. Use http://localhost as the callback url.
- Go to this url: https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=http://localhost&client_id=[YourAppName]@AMER.OAUTHAP
- Enter your TD credentials and give premission
- Once you login, you will be redirected to a url of this form https://localhost/?code=[code]
- Copy the [code] part. Add it as a parameter to the TDAmeritrade class.
- The TDAmeritrade class should now work!
NOTE: The code you obtain is only valid for a limited period of time. If expired, follow steps again to get a new code.
Installation
From PyPI
pip install tdlink
From Source
pip install --upgrade git+git://github.com/ysriram1/tdlink
Examples
Connect to the API
from tdlink import TDlink
td = TDlink(app_key='TRADING123',
redirect_uri='http://localhost',
code=code, # follow instructions to get the code
return_raw_response=False # will return formatted df if False, else returns HTTP response
)
Get the refresh and access tokens
td.refresh_token # returns refresh token
td.access_token # returns the access token
Note: You can save the refresh token for creating a future session instead of using a code.
Get Data from API
Current Quote
```python td.get_current_quote(symbol='QQQ', to_return=['askPrice', 'bidPrice', 'totalVolume']) ```NOTE: Use this also for prices of options.
Historical Data
td.get_historical_prices(symbol='QQQ',
period_type='day', # 'day'
period=2, # day: 1, 2, 3, 4, 5, 10* month: 1*, 2, 3, 6 year: 1*, 2, 3, 5, 10, 15, 20 ytd: 1*
frequency_type='minute', # day: minute* month: daily, weekly* year: daily, weekly, monthly* ytd: daily, weekly*
frequency=5, # minute: 1*, 5, 10, 15, 30 daily: 1* weekly: 1* monthly: 1*
start_date='06/03/2019', # mm/dd/yyyy
start_time=None, # hh:mm:ss
end_date='06/04/2019',
end_time=None,
extended_hours=True,
return_df=True # if False, returns a dictionary instead
)
Options Chain
td.get_options_chain(symbol='QQQ', # symbol of underlying security
strike=None, # mention a specific strike
from_date='01/01/2020', # only expirations after this date (mm/dd/yyyy)
from_time=None,
to_date='01/30/2020', # only expirations before this date
to_time=None,
expiry_month='ALL', # option expiry month ALL or JAN
kind='OTM', # ITM, NTM, OTM, SAK (Strikes Above Market), ALL etc.
include_quotes=False, # if FALSE only returns the option names (use get_current_quote() to get value)
contract_type='CALL', # CALL, PUT
strikes=5, # number of strikes above or below at-the-money price
strategy='SINGLE',
return_df=True # if False, returns a dictionary instead
)
Top Movers of an Index
td.get_movers_for_index(symbol = 'DJI', # has to be an index DJI, SPX.X etc
direction = None, # 'up' or 'down'. if None, returns both
change = None # 'value' or 'percent'. if None, defaults to percentage
)
Support
Please email me at ysriram@umich.edu if you have any questions, suggestions, or ideas for improvement.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tdlink-0.3.tar.gz
.
File metadata
- Download URL: tdlink-0.3.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 588bd0194ec3139710deff6c802ed03d1ac9878ec3f7fc539eac34c2f5ea517a |
|
MD5 | b52d13edab2ee6472bb532a6f2b3d543 |
|
BLAKE2b-256 | ca2bc7734cce9a255334a62663f2586e54aefb6f649102c90ea7ab87cad31337 |
File details
Details for the file tdlink-0.3-py3-none-any.whl
.
File metadata
- Download URL: tdlink-0.3-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 578ae986f6f77ff6fad4858cd1463d050b3d0fdd3dc45f1b6fb2d885cc3e8a3c |
|
MD5 | 6acc5ba4ce79c184765324b3b6a5f289 |
|
BLAKE2b-256 | 56b35ef3e3cba8c8522af18cd0e47f43a33ea52bfa1f5d7cffa2e2658f9ceaa9 |