Put this at the top of any .js file that will use the client functions.
const tdaclient = require('tda-api-client');Note: This is a file of code samples. For documentation, refer to the official website https://developer.tdameritrade.com or look in the apidocsarchive/ folder.
- ACCOUNTS
- AUTHENTICATION
- INSTRUMENTS
- MARKET HOURS
- MOVERS
- OPTIONS CHAIN
- ORDERS
- PRICE HISTORY
- QUOTES
- SAVED ORDERS
- TRANSACTIONS
- USER INFO
- WATCHLISTS
const configGetAcct = {
accountId: 1,
fields: 'positions,orders'
};
const getAcctResult = await tdaclient.accounts.getAccount(configGetAcct);const configGetAccts = {
fields: 'positions,orders'
};
const getAcctsResult = await tdaclient.accounts.getAccounts(configGetAccts);const authConfig = await tdaclient.authentication.getAuthentication();const authConfig = await tdaclient.authentication.refreshAuthentication();const configGetInst = {
cusip: 345370860,
apikey: ''
};
const getInstrumentResult = await tdaclient.instruments.getInstrument(configGetInst);const configGetAcct = {
symbol: 'F',
projection: tdaclient.instruments.PROJECTION_TYPE.SYMBOL_SEARCH,
apikey: ''
};
const searchInstrumentResult = await tdaclient.instruments.searchInstruments(configSearchInst);const configGetMktHours = {
market: tdaclient.markethours.MARKETS.OPTION,
date: '2020-09-10',
apikey: ''
};
const getSingleMarketHoursResult = await tdaclient.markethours.getSingleMarketHours(configGetMktHours);const configGetMultiMktHours = {
markets: 'EQUITY,OPTION,FUTURE,BOND,FOREX',
date: '2020-09-10',
apikey: ''
};
const getMultiMarketHoursResult = await tdaclient.markethours.getMultipleMarketHours(configGetMultiMktHours);const moversConfig = {
index: tdaclient.movers.INDEX.SPX,
direction: tdaclient.movers.DIRECTION.UP,
change: tdaclient.movers.CHANGE.PERCENT,
apikey: ''
};
const getMoversResult = await tdaclient.movers.getMovers(moversConfig);const optionsConfig = {
symbol: 'SPY',
contractType: tdaclient.optionchain.CONTRACT_TYPE.ALL,
strikeCount: 60,
includeQuotes: 'TRUE',
strategy: tdaclient.optionchain.STRATEGY.SINGLE,
range: tdaclient.optionchain.RANGE.ALL,
fromDate: '2020-09-01',
toDate: '2020-09-10',
expMonth: tdaclient.optionchain.EXPIRATION_MONTH.SEP,
optionType: tdaclient.optionchain.OPTION_TYPE.STANDARD
};
const getOptionChainResult = await tdaclient.optionchain.getOptionChain(optionsConfig);const cancelOrderConfig = {
accountId: 1,
orderId: 321
};
const cancelOrderResult = await tdaclient.orders.cancelOrder(cancelOrderConfig);const getOrderConfig = {
accountId: 1,
orderId: 321
};
const getOrderResult = await tdaclient.orders.getOrder(getOrderConfig);const getOrdersConfig = {
accountId: 1,
maxResults: 10,
fromEnteredTime: '', //yyyy-MM-dd
toEnteredTime: '', //yyyy-MM-dd
status: tdaclient.orders.ORDER_STATUS.QUEUED
};
const getOrdersResult = await tdaclient.orders.getOrdersByAccount(getOrdersConfig);const getOrdersQueryConfig = {
accountId: '', //1, blank is fine
maxResults: 10,
fromEnteredTime: '', //yyyy-MM-dd
toEnteredTime: '', //yyyy-MM-dd
status: tdaclient.orders.ORDER_STATUS.QUEUED
};
const getOrdersQueryResult = await tdaclient.orders.getOrdersByQuery(getOrdersQueryConfig);const replaceOrderConfig = {
orderJSON: {
"orderType": "MARKET",
"session": "NORMAL",
"duration": "DAY",
"orderStrategyType": "SINGLE",
"orderLegCollection": [
{
"instruction": "Buy",
"quantity": 11,
"instrument": {
"symbol": "F",
"assetType": "EQUITY"
}
}
]
},
accountId: 1,
orderId: 321
};
const replaceOrderResult = await tdaclient.orders.replaceOrder(replaceOrderConfig);const placeOrderConfig = {
orderJSON: {
"orderType": "MARKET",
"session": "NORMAL",
"duration": "DAY",
"orderStrategyType": "SINGLE",
"orderLegCollection": [
{
"instruction": "Buy",
"quantity": 9,
"instrument": {
"symbol": "F",
"assetType": "EQUITY"
}
}
]
},
accountId: 1
};
const placeOrderResult = await tdaclient.orders.placeOrder(placeOrderConfig);const priceHistoryConfig = {
startDate: new Date('2019-09-01 07:00:00').getTime(),
endDate: new Date('2020-09-01 16:00:00').getTime(),
periodType: tdaclient.pricehistory.PERIOD_TYPE.YEAR,
frequencyType: tdaclient.pricehistory.FREQUENCY_TYPE.YEAR.WEEKLY,
frequency: tdaclient.pricehistory.FREQUENCY.WEEKLY.ONE,
symbol: 'F',
getExtendedHours: 'true'
};
const getPriceHistoryResult = await tdaclient.pricehistory.getPriceHistory(priceHistoryConfig);const getQuoteConfig = {
symbol: 'F',
apikey: ''
};
const getQuoteResult = await tdaclient.quotes.getQuote(getQuoteConfig);const getQuotesConfig = {
symbol: 'F,T,O',
apikey: ''
};
const getQuotesResult = await tdaclient.quotes.getQuotes(getQuotesConfig);const createSavedOrderConfig = {
orderJSON: {
"orderType": "MARKET",
"session": "NORMAL",
"duration": "DAY",
"orderStrategyType": "SINGLE",
"orderLegCollection": [
{
"orderLegType": "EQUITY",
"legId": 0,
"instruction": "BUY",
"quantity": 10,
"quantityType": "SHARES",
"instrument": {
"symbol": "F",
"assetType": "EQUITY"
}
}
]
},
accountId: 1
};
const createSavedOrderResult = await tdaclient.savedorders.createSavedOrder(createSavedOrderConfig);const deleteSavedOrderConfig = {
accountId: 1,
savedOrderId: 123
};
const deleteSavedOrderResult = await tdaclient.savedorders.deleteSavedOrder(deleteSavedOrderConfig);const getSavedOrderConfig = {
accountId: 1,
savedOrderId: 123
};
const getSavedOrderResult = await tdaclient.savedorders.getSavedOrderById(getSavedOrderConfig);const getSavedOrdersConfig = {
accountId: 1
};
const getSavedOrdersResult = await tdaclient.savedorders.getSavedOrders(getSavedOrdersConfig);const replaceSavedOrderConfig = {
orderJSON: {
"orderType": "MARKET",
"session": "NORMAL",
"duration": "DAY",
"orderStrategyType": "SINGLE",
"orderLegCollection": [
{
"orderLegType": "EQUITY",
"legId": 0,
"instruction": "BUY",
"quantity": 15,
"quantityType": "SHARES",
"instrument": {
"symbol": "F",
"assetType": "EQUITY"
}
}
]
},
accountId: 1,
savedOrderId: 123
};
const replaceSavedOrderResult = await tdaclient.savedorders.replaceSavedOrder(replaceSavedOrderConfig);const getTransactionConfig = {
accountId: 1,
transactionId: 321
};
const getTransactionResult = await tdaclient.transactions.getTransaction(getTransactionConfig);const getTransactionsConfig = {
accountId: 1,
symbol: '',
startDate: '2020-08-25',
endDate: '2020-09-01',
type: tdaclient.transactions.TRANSACTION_TYPE.ALL
};
const getTransactionsResult = await tdaclient.transactions.getTransactions(getTransactionsConfig);const getUserPrefConfig = {
accountId: 1
};
const getUserPrefResult = await tdaclient.userinfo.getUserPreferences(getUserPrefConfig);const getStreamerKeysConfig = {
accountIds: '1,2'
};
const getStreamerKeysResult = await tdaclient.userinfo.getStreamerSubKeys(getStreamerKeysConfig);const updateUserPrefsConfig = {
accountId: 1,
preferencesJSON: {
"expressTrading":false,
"defaultEquityOrderLegInstruction":"NONE",
"defaultEquityOrderType":"LIMIT",
"defaultEquityOrderPriceLinkType":"NONE",
"defaultEquityOrderDuration":"DAY",
"defaultEquityOrderMarketSession":"NORMAL",
"defaultEquityQuantity":0,
"mutualFundTaxLotMethod":"FIFO",
"optionTaxLotMethod":"FIFO",
"equityTaxLotMethod":"FIFO",
"defaultAdvancedToolLaunch":"NONE",
"authTokenTimeout":"FIFTY_FIVE_MINUTES"
}
};
const updateUserPrefResult = await tdaclient.userinfo.updateUserPreferences(updateUserPrefsConfig);const getUserPrinConfig = {
fields: 'streamerSubscriptionKeys,streamerConnectionInfo,preferences,surrogateIds'
};
const getUserPrinResult = await tdaclient.userinfo.getUserPrincipals(getUserPrinConfig);const updateWatchlistConfig = {
accountId: 1,
watchlistId: 147,
watchlistJSON: {
"name": "testwatchlist3update",
"watchlistItems": [
{
"quantity": 1,
"averagePrice": 2,
"commission": 3,
"purchasedDate": "2020-08-21",
"instrument": {
"symbol": "AMZN",
"assetType": 'EQUITY'
}
}
]
}
};
const updateWatchlistResult = await tdaclient.watchlists.updateWatchlist(updateWatchlistConfig);const replaceWatchlistConfig = {
accountId: 1,
watchlistId: 321,
watchlistJSON: {
"name": "testwatchlist2",
"watchlistItems": [
{
"quantity": 1,
"averagePrice": 2,
"commission": 3,
"purchasedDate": "2020-08-21",
"instrument": {
"symbol": "FB",
"assetType": 'EQUITY'
}
}
]
}
};
const replaceWatchlistResult = await tdaclient.watchlists.replaceWatchlist(replaceWatchlistConfig);const getWatchlistsSingleAcctConfig = {
accountId: 1
};
const getWatchlistsSingleAcctResult = await tdaclient.watchlists.getWatchlistsSingleAcct(getWatchlistsSingleAcctConfig);const getWatchlistsAllAcctsResult = await tdaclient.watchlists.getWatchlistsMultiAcct({});const getSingleWatchlistConfig = {
accountId: 1,
watchlistId: 321
};
const getSingleWatchlistResult = await tdaclient.watchlists.getWatchlist(getSingleWatchlistConfig);const deleteWatchlistConfig = {
accountId: 1,
watchlistId: 433
};
const deleteWatchlistResult = await tdaclient.watchlists.deleteWatchlist(deleteWatchlistConfig);const createWatchlistConfig = {
accountId: 1,
watchlistJSON: {
"name": "test_watchlist",
"watchlistItems": [
{
"quantity": 1,
"averagePrice": 2,
"commission": 3,
"purchasedDate": "2020-08-21",
"instrument": {
"symbol": "T",
"assetType": 'EQUITY'
}
}
]
}
};
const createWatchlistResult = await tdaclient.watchlists.createWatchlist(createWatchlistConfig);