Package Exports
- tdaclient
- tdaclient/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (tdaclient) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
TD Ameritrade Client for Nodejs
Overview
A client that knows how to call the TD-Ameritrade Restful API written for nodejs. The library is written in Typescript. This means you can use your favorite IDE to do code complete. This allows you to focus on writing your trading strategy instead of looking at the TDA documentation.
Features
- Auto fetch refresh token when access token expires and updates credentials.
- Credentials can be fetched and stored using these providers
- Local cache
- Local file
- Customizable (e.g connect to datastore such as S3 or DynamoDB)
- Get user account information
- Execute Trades
- Get watchlist
Install
$ yarn add tdaclient Getting a TD Ameritrade Access Token
To gain access to the TDA APIs, you will need to get an access token. You cannot simply use your username and password to call the APIs. What you'll need to do is use your login to get an access token.
You can get an access token by following this tutorial found on the official TDA API documentation.
How to use it
Instantiate TdaClient object
You will need an access token in order to connect to TDA. There is no way around this. For info as to how to get your own access token and client id, refer to the office TDA documentation
import {TdaClient} from "tdaclient";
const tdaClient = TdaClient.from({
access_token: "MY-ACCESS-TOKEN",
client_id: "MY-CLIENT-ID",
REFRESH_TOKEN: "MY-REFRESH-TOKEN" // OPTIONAL VALUE, ENABLES AUTO REFRESH OF ACCESS TOKEN
});
const accounts = await tdaClient.getAccount();
console.log(accounts[0]);
Place An Order
import {TdaClient} from "tdaclient";
import {
AssetType,
DurationType,
InstructionType, Order, OrdersConfig,
OrderStrategyType,
OrderType,
SessionType
} from "tdaclient/dist/models/order";
// ...
const order = {
orderType: OrderType.LIMIT,
price: 100.0,
session: SessionType.NORMAL,
duration: DurationType.DAY,
orderStrategyType: OrderStrategyType.SINGLE,
orderLegCollection: [
{
instruction: InstructionType.BUY,
quantity: 1,
instrument: {
symbol: 'SPY',
assetType: AssetType.EQUITY,
},
},
],
} as Order;
const orderConfig = {
accountId,
order,
} as OrdersConfig;
const placeOrdersResponse = await placeOrder(orderConfig);
const orderId = placeOrdersResponse.orderId;
console.log(orderId);Setup if you want to contribute to this package
For more info refer to this page
- Create a file calls
credentials.jsonand put it in the package root directory. - Put the following information in that file you just created:
{ "client_id": "example-client-id", "redirect_uri": "example-redirect-uri" }
- Run this command and open the output URL from the console.
node generateAuthUrl.js - Login and click allow.
- You will then see a blank page load. Take a look at the URL bar on your browser.
- You will see a URL query string with a value for
code. - Copy this value.
- Open up the developer's console on the browser and type this in:
decodeURIComponent("MY_CODE") - Keep this decoded code. This is your access_token.
- You will see a URL query string with a value for
- Go to this page to get the access token and
refresh token
- Fill in the form with the follow values:
- grant_type: authorization_code
- access_type: offline
- code: [The value that you receive from following step]
- client_id: [Your app consumer key]
- redirect_uri: [Your app redirect uri]
- Fill in the form with the follow values:
- Create a file in this directory (at the same level as this README.md) and name it
credentials.json - Paste the response from that page to the file create in step 3. Also add the client_id and redirect_uri attributes at
the end.
{ "access_token": "example-token", "refresh_token": "example-refresh-token", "scope": "PlaceTrades AccountAccess MoveMoney", "expires_in": 1800, // 30 minutes "refresh_token_expires_in": 7776000, // 90 days "token_type": "Bearer", "client_id": "example-client-id", "redirect_uri": "example-redirect-uri" }
- Now you can run the command
yarn testand it will run the integration tests - The test will keep updating the access_token so you can keep running the tests. If you happen to not run the tests within 90 days, then you will have to manually login and repeat these steps.
Publishing to npm
This command will update the package version and then publish to the public npm repo
$ npm version [patch | minor major]
$ npm publishAppendix
Refer to this page for more info on TDA's APIs
License
tdaclient is MIT licensed.