Python Library for TDAmeritrade API

pip install tdlink==0.3


GitHub license


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:

  1. Go to TDAmeritrade developer:
  2. Login and go to "My Apps"
  3. Create an app. Use http://localhost as the callback url.
  4. Go to this url:[YourAppName]@AMER.OAUTHAP
  5. Enter your TD credentials and give premission
  6. Once you login, you will be redirected to a url of this form https://localhost/?code=[code]
  7. Copy the [code] part. Add it as a parameter to the TDAmeritrade class.
  8. 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.


From PyPI

pip install tdlink

From Source

pip install --upgrade git+git://


Connect to the API

from tdlink import TDlink

td = TDlink(app_key='TRADING123',
                code='3V7m...', # 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

          to_return=['askPrice', 'bidPrice', 'totalVolume'])

NOTE: Use this also for prices of options.

Historical Data

                    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
                    return_df=True # if False, returns a dictionary instead
Historical Data

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)
                    to_date='01/30/2020', # only expirations before this date
                    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
                    return_df=True # if False, returns a dictionary instead
Options Chain

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
Top Movers


Please email me at if you have any questions, suggestions, or ideas for improvement.