Questrade API
This API is an easy way to use the Questrade API immediately.
If you're looking for the old API docs, click here.
Features
- Easy to use API calls
- Supports options chain
- Supports account fetching
- Auto-fetch primary account
Getting Started
Simply start by installing the questrade library:
npm install --save questrade
You will then need to get an API key.
Important note about key management:
After that's it's really simple to use, but you WILL need to save the new API key and use it every time you try to reconnect. The API key Questrade initially gives you will no longer be valid after you call connect
.
const Questrade = require('questrade');
const qt = new Questrade('<your-api-key-here>');
const newKey = await qt.connect()
const account = await qt.getPrimaryAccount()
const accounts = await qt.getAccounts()
const balances = await account.getBalances()
const msft = await qt.getSymbol('MSFT')
const quote = await msft.getQuote()
const options = await msft.getOptionChain()
Some examples
Root API
const account = await qt.getPrimaryAccount()
const markets = await qt.getMarkets()
const accounts = await qt.getAccounts()
Account
const balances = await account.getBalances()
const activities = await account.getActivities()
const orders = await qt.getOpenOrders()
const orders = await qt.getOrders()
const orders = await qt.getClosedOrders()
const order = await qt.getOrder(orderId)
await qt.createOrder(newOrder)
await qt.updateOrder(orderId, newOrder)
await qt.removeOrder(orderId)
await qt.testOrder(order)
Symbols
const symbol = await qt.getSymbol('MSFT')
const symbols = await qt.getSymbols(['MSFT', 'AAPL', 'BMO'])
const symbols = qt.searchSymbols('MS')
Option Chain
const tsla = await qt.getSymbol('tsla')
const chain = await tsla.getOptionChain()
const jan25 = chain['2025-01-17']
const jan25600 = jan25['600']
const quote = await jan25600.getQuote()
Streaming
For those accounts that have L1 data access (either practice account or Advanced market data packages) you can stream live market data.
var getWebSocketURL = function (symbolId, cb) {
var webSocketURL;
request({
method: 'GET',
url: qt.apiUrl + '/markets/quotes?ids=' + symbolId + '&stream=true&mode=WebSocket',
auth: {
bearer: qt.accessToken
}
}, function (err, http, body) {
if (err) {
cb(err, null);
} else {
response = JSON.parse(body);
webSocketURL = qt.api_server.slice(0, -1) + ':' + response.streamPort + '/' + qt.apiVersion + '/markets/quotes?ids=' + symbolId + 'stream=true&mode=WebSocket';
cb(null, webSocketURL);
}
});
}
getWebSocketURL('9291,8049', function (err, webSocketURL) {
console.log(webSocketURL);
const WebSocket = require('ws');
const ws = new WebSocket(webSocketURL);
ws.on('open', function open() {
ws.send(qt.accessToken);
});
ws.on('message', function incoming(data) {
console.log(data);
});
process.on('exit', function () {
if (ws) {
console.log('CLOSE WebSocket');
ws.close();
}
});
process.on('SIGINT', function () {
if (ws) {
console.log('CLOSE WebSocket SIGINT');
ws.close();
}
});
process.on('uncaughtException', function () {
if (ws) {
console.log('CLOSE WebSocket');
ws.close();
}
});
});
Full Options
- clientId - The API key Questrade provided
- isDev - Whether or not to use real or fake login server (and API server). Defaults to
false
- apiVersion - Defaults to
v1
- accessToken - Optionally, instead of calling
connect
, you can use an access token directly, perhaps through an implicit OAuth flow.
Full Documentation
- getPrimaryAccount () => Account
- getAccounts () => [Account]
- getMarkets (cb)
- getQuotesById (symbolIds)
- getSymbol (ticker) => Symbol
- getSymbols (tickers) => [Symbol]
- searchSymbols (prefix, offset = 0) => [Symbol]
Account Object
- getPositions ()
- getBalances ()
- getExecutions ()
- createOrder (params)
- getOrders (params)
- getOpenOrders ()
- getClosedOrders ()
- getAllOrders ()
- getOrder (orderId, params)
- updateOrder (orderId, params)
- removeOrder (orderId)
- testOrder (params)
- createStrategy (params)
- testStrategy (params)
- getActivities ({ [startTime], [endTime] })
Symbol Object
- getOptionChain ()
- getQuote ()
- getCandles ({ [startTime], [endTime], [interval] })
Contributions
Are welcome!