npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

questrade

v2.1.0

Published

Questrade API

Downloads

35

Readme

Questrade API

npm package

Build status Dependency Status Known Vulnerabilities Gitter

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>');

// Connect to Questrade
const newKey = await qt.connect()
// Save newKey for next time

// Access your account here
const account = await qt.getPrimaryAccount()
const accounts = await qt.getAccounts()
const balances = await account.getBalances()

// Get Market quotes
const msft = await qt.getSymbol('MSFT')
const quote = await msft.getQuote()
const options = await msft.getOptionChain()

// ... etc. See the full documentation for all the calls you can make!

Some examples

Root API

const account = await qt.getPrimaryAccount() // Account
const markets = await qt.getMarkets()
const accounts = await qt.getAccounts() // => [Account]

Account

const balances = await account.getBalances()
const activities = await account.getActivities()
const orders = await qt.getOpenOrders() // [Order]
const orders = await qt.getOrders() // [Order]
const orders = await qt.getClosedOrders() // [Order]
const order = await qt.getOrder(orderId) // => Order
await qt.createOrder(newOrder)
await qt.updateOrder(orderId, newOrder)
await qt.removeOrder(orderId)
await qt.testOrder(order)

Symbols

const symbol = await qt.getSymbol('MSFT') // => Symbol
const symbols = await qt.getSymbols(['MSFT', 'AAPL', 'BMO']) // => [Symbol]
const symbols = qt.searchSymbols('MS') // => [Symbol]

Option Chain

// Example fetching TSLA options
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.


// ... connect to Questrade first!

// Websocket port changes by API and by symbol. So you have to get the port every time you need different data stream
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) { // BMO.TO & AAPL
  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);
    // Do what you want with the data
  });

  // CLOSING WebSocket Connections otherwise will remain open
  process.on('exit', function () {
    if (ws) {
      console.log('CLOSE WebSocket');
      ws.close();
    }
  });

  //catches ctrl+c event
  process.on('SIGINT', function () {
    if (ws) {
      console.log('CLOSE WebSocket SIGINT');
      ws.close();
    }
  });

  //catches uncaught exceptions
  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!