![](https://static.wixstatic.com/media/e5cb35_870f34314cc94c77af9df284ba651b58~mv2.png/v1/fill/w_380,h_133,al_c,q_85,enc_auto/e5cb35_870f34314cc94c77af9df284ba651b58~mv2.png)
With the TD Ameritrade API, analyzing stock market data has never been so easy. With just a little set up and a few lines of code, users have access to a whole host of stocks and options data.
Generate a Consumer Key
In order to use the TD Ameritrade API, we need a consumer key. This can be found by accessing TD’s developer website, creating an account, and then requesting a token. Once API access has been granted, you will be provided a consumer key:
![](https://static.wixstatic.com/media/e5cb35_afd57da67e2b47fd89c374425c0268c4~mv2.png/v1/fill/w_980,h_341,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/e5cb35_afd57da67e2b47fd89c374425c0268c4~mv2.png)
Now that we have authentication completed, we can move on to writing our Python code:
Setup
First, let’s import our required packages. I use requests for the API requests and JSON for parsing the data.
import requests
import json
Next, we need to pass our consumer key as a variable:
![](https://static.wixstatic.com/media/e5cb35_57783b634f2f4718bae7707febff0265~mv2.png/v1/fill/w_980,h_35,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/e5cb35_57783b634f2f4718bae7707febff0265~mv2.png)
Now let’s connect to the API. I will demonstrate this with a few different endpoints:
Stock Quote
If you are looking to get a quote of an equity, the endpoint is the following:
To get information regarding American Airlines’ stock, we can feed “AAL” into the {stock_ticker} placeholder.
full_url = endpoint.format(stock_ticker='AAL')page = requests.get(url=full_url,
params={'apikey' : td_consumer_key})content = json.loads(page.content)
We are returned with current stock data about American Airlines, including current price, 52-week range, volatility, and volume.
![](https://static.wixstatic.com/media/e5cb35_30ff6409520343b88bf4e25f4144f657~mv2.png/v1/fill/w_980,h_727,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/e5cb35_30ff6409520343b88bf4e25f4144f657~mv2.png)
2. Historical Price
We can also look at a stock’s historical share price. The code looks very similar to the quotes endpoint, with pricehistory replacing quotes in the endpoint:
endpoint = 'https://api.tdameritrade.com/v1/marketdata/{stock_ticker}/pricehistory'full_url = endpoint.format(stock_ticker='AAL')page = requests.get(url=full_url,
params={'apikey' : td_consumer_key})content = json.loads(page.content)
We are returned with a long list of candles, corresponding to different timestamps:
![](https://static.wixstatic.com/media/e5cb35_be820460f09049bbba80a426ca65dec9~mv2.png/v1/fill/w_980,h_412,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/e5cb35_be820460f09049bbba80a426ca65dec9~mv2.png)
The pricehistory endpoint has many parameters, corresponding to different periods and frequencies.
![](https://static.wixstatic.com/media/e5cb35_8a8570fe061b486090e05a2a5c0d7691~mv2.png/v1/fill/w_942,h_1362,al_c,q_90,enc_auto/e5cb35_8a8570fe061b486090e05a2a5c0d7691~mv2.png)
For example, to retrieve weekly historical data for the past year of AAL share prices, we can use the same endpoint as above but with four new parameters added:
endpoint = 'https://api.tdameritrade.com/v1/marketdata/{stock_ticker}/pricehistory?periodType={periodType}&period={period}&frequencyType={frequencyType}&frequency={frequency}'full_url = endpoint.format(stock_ticker='AAL',periodType='year',period=1,frequencyType='weekly',frequency=1)page = requests.get(url=full_url,
params={'apikey' : td_consumer_key})content = json.loads(page.content)
This will return us with one year’s worth of candles:
![](https://static.wixstatic.com/media/e5cb35_898179ad1d62468b9398099ba01b991b~mv2.png/v1/fill/w_980,h_324,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/e5cb35_898179ad1d62468b9398099ba01b991b~mv2.png)
3. Fundamentals Data
We can also gather fundamentals data, via the instruments endpoint:
base_url = 'https://api.tdameritrade.com/v1/instruments?&symbol={stock_ticker}&projection={projection}'endpoint = base_url.format(stock_ticker = 'AAL',
projection = 'fundamental')page = requests.get(url=endpoint,
params={'apikey' : td_consumer_key})content = json.loads(page.content)
We are returned with a whole host of information, ranging from p/e ratio to debt ratio to CUSIP number.
![](https://static.wixstatic.com/media/e5cb35_8c217b930ef748f7b4752c8f2ac5bc9a~mv2.png/v1/fill/w_980,h_736,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/e5cb35_8c217b930ef748f7b4752c8f2ac5bc9a~mv2.png)
This endpoint allows for different types of “projections,” described below:
![](https://static.wixstatic.com/media/e5cb35_7d76b2efee004e67b93dff570db0c79c~mv2.png/v1/fill/w_980,h_990,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/e5cb35_7d76b2efee004e67b93dff570db0c79c~mv2.png)
4. Options Data
We can also gather options data with the TD Ameritrade API:
base_url = 'https://api.tdameritrade.com/v1/marketdata/chains?&symbol={stock_ticker}'endpoint = base_url.format(stock_ticker = 'AAL')page = requests.get(url=endpoint,
params={'apikey' : td_consumer_key})content = json.loads(page.content)
![](https://static.wixstatic.com/media/e5cb35_397a9252603d47f081ad3403cfe395ef~mv2.png/v1/fill/w_980,h_561,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/e5cb35_397a9252603d47f081ad3403cfe395ef~mv2.png)
The above API call will return a long list of options contracts (740 in this case!). It’s better to be more specific for this sort of request.
Let’s look specifically for put options with a November 20, 2020 expiration date:
base_url = 'https://api.tdameritrade.com/v1/marketdata/chains?&symbol={stock_ticker}&contractType={contractType}&fromDate={date}&toDate={date}'endpoint = base_url.format(stock_ticker = 'AAL',
contractType = 'PUT',
date='2020-11-20')page = requests.get(url=endpoint,
params={'apikey' : td_consumer_key})content = json.loads(page.content)
Here, we add a few more parameters: fromDate and toDate, which are both set to “2020–11–20,” and contractType, set to “PUT.”
We are now returned with just 22 options contracts:
![](https://static.wixstatic.com/media/e5cb35_1274d9d421174e5f94e0bd5729c4c7ca~mv2.png/v1/fill/w_980,h_573,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/e5cb35_1274d9d421174e5f94e0bd5729c4c7ca~mv2.png)
If you want to set a specific strike price, you can do so with the {strike} parameter:
base_url = 'https://api.tdameritrade.com/v1/marketdata/chains?&symbol={stock_ticker}\
&contractType={contract_type}&strike={strike}&fromDate={date}&toDate={date}'endpoint = base_url.format(stock_ticker = 'AAL',
contract_type = 'PUT',
strike = 9,
date='2020-06-19')page = requests.get(url=endpoint,
params={'apikey' : td_consumer_key})content = json.loads(page.content)
![](https://static.wixstatic.com/media/e5cb35_674d67f1b1f24e8d9779d05fce8ebadb~mv2.png/v1/fill/w_980,h_622,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/e5cb35_674d67f1b1f24e8d9779d05fce8ebadb~mv2.png)
— -
There are other interesting API endpoints available, such as transaction history (if you use TD Ameritrade to trade) and “movers,” which tracks high/low performing stocks within different index (i.e. top 10 upward stocks today within DJI index). More info can be found in TD Ameritrade’s documentation.
This blog post was originally published on Medium on May 27, 2020.
Great Post.
Can you also get historical price of a particular option strike with this API?
great article, I am trying to get fundamental data under point 3. for more than one symbol, how do I do that?
@stefangouyet - Great article. Do you know if the TD Ameritrade option API returns data for index products - SPX, NDX, RUT.