Python Library for TDAmeritrade API
All the dates and times are in UTC.
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.
pip install tdlink
pip install --upgrade git+git://github.com/ysriram1/tdlink
from tdlink import TDlink
td = TDlink(app_key='TRADING123',
redirect_uri='http://localhost',
code='3V7m...', # follow instructions to get the code
return_raw_response=False # will return formatted df if False, else returns HTTP response
)
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.
td.get_current_quote(symbol='QQQ',
to_return=['askPrice', 'bidPrice', 'totalVolume'])
NOTE: Use this also for prices of options.
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
)
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
)
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
)
Please email me at ysriram@umich.edu if you have any questions, suggestions, or ideas for improvement.