(Will be) fully featured TD Ameritrade API for MATLAB.
T = TDAmeritrade;
T.tickers={'AAPL','INTC','TSLA'};
data = T.getPriceHistory;data is a structure array containing time series of
- Open
- Close
- High
- Low
- Volume
Currently, only daily information is available, but future work will support all available frequencies.
You can plot this using:
plot(data(1).Time,data(1).Close)
and you should get something like this:
q = T.getQuote;returns a StockQuote array. The stock quote for AAPL can look like:
StockQuote with properties:
AssetType: 'EQUITY'
CUSIP: '037833100'
Symbol: 'AAPL'
Name: 'Apple Inc. - Common Stock'
ClosePrice: 124.7600
LastPrice: 122.1900
DividendYield: 0.6600
f = T.getFundamental;
returns a structure array with various fundamental information (e.g., quickRatio, marketCap, etc.)
This information is cached on disk, so subsequent calls will load from the cache and will not ping TDA's servers. If you want to force a fetch from the server, use:
f = T.getFundamental('UseCache',false);
o = T.getOptionChain('Type','Call');
returns the full option chain for the symbols requested. Results are returned as an array of OptionContract.
transactions = T.getTransactions;
Using OAuth2, get a list of transactions from a specified account. You need to tell it what account to read from (see below).
transactions is a table that could look like this:
Symbol Quantity Price Date
_______________ ________ ______ ____________________
MOON -500 127 19-Mar-2021 13:18:12
FTFT -60 18.151 19-Mar-2021 14:38:06
TSLA -14 15.17 11-Mar-2021 14:15:19
GME 14 18 11-Mar-2021 15:10:11
NVDA 5 125 03-Mar-2021 14:19:00
GME -1 122.6 02-Mar-2021 14:21:40
GME 700 123.14 22-Feb-2021 14:44:30
GME -700 125 22-Feb-2021 14:31:28
positions = T.getPositions();
returns a table with positions.
Download this repo and a dependency, and add them to your MATLAB path:
git clone https://github.com/sg-s/srinivas.gs_mtools/
git clone https://github.com/sg-s/tdameritrade-matlab-api/
git clone https://github.com/sg-s/silo/You will need a TD Ameritrade trading account and a developer account to use this.
There are two levels of using this API, based on whether you want basic, delayed information (for which you can make unauthenticated requests), or realtime/private information (such as accessing your transactions).
TD Ameritrade has smartly made it easy to get basic information, and made it hard (but secure) to access more sensitive information.
- Go to https://developer.tdameritrade.com/
- Register for a new account
- Create a new app, register it, and copy the "customer key"
- This is your API key. Set it using:
T = TDAmeritrade;
T.set('APIKey','YOURAPIKEY');
You only need to do this if you want to do things like:
- read out account information
- get realtime data
- place trades, etc.
You will have to go through the process of getting access tokens via OAuth (beyond the scope of this document). Once you have it,
% set the refresh token
% This API will use the refresh token to get
% a new access token and will use that
T.set('RefreshToken','YOUR_REFRESH_TOKEN');
% also tell it what account you want to read from
T.set('AccountID','YOUR_ACCOUNT_ID');- EPS incorrectly returned as 0 for companies that lose money (this is a bug in TDAmeritrade's reporting, so cannot be fixed)
- Authenticated requests using OAuth use the system
curl, rather than builtin MATLAB tools. If you don't havecurlinstalled on your computer, this won't work.
- Realtime quotes (using authenticated requests)
Variable resolution historical data- Get account balances & orders
- Place orders
- Cancel orders
- Get new refresh token using access token
- Instrument Search using keywords and regex
- Get mover information
- Watchlist operations
GPL v3.
