questrade-ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Questrade TS

Coverage Status Travis (.com) Build Status Build Status Known Vulnerabilities Codacy Badge CII Best Practices Summary

GitHub contributors GitHub watchers GitHub forks GitHub stars GitHub pull requests GitHub issues

Getting Started

This NPM Package is an unofficial Questrade API wrapper for NodeJS with full TypeScript support.

The structure is changing and is unstable you might have to change you code for it to run after th next update until the 1.1.0 version use --save-exact questrade-ts and be aware that updating to a folowing version will result in a change in your code ...

This NodeJS wrapper is an easy way to use the Questrade API immediately. It commes with full TypeScript support.

Simply start by installing this questrade-ts library:

npm install --save-exact questrade-ts

or

npm install --save-exact questrade-ts@latest

You will need to get an API key.

After that, it is really simple to use:

TL;DR

 
  /* 'require' call may be converted to an import. */
  // import { redeemToken } from 'questrade-ts'
  const { redeemToken } = require('questrade-ts');
 
  /* You will need to create your own API key: */
  /* https://login.questrade.com/APIAccess/UserApps.aspx */
  const yourRefreshToken = 'RocgqWp_USE_YOUR_OWN_TOKEN_M3BCd0';
 
  /* inside of an async function or async IIFE */
  (async () => {
    const log = console.log
 
    const { qtApi, credentials } = await redeemToken(yourRefreshToken);
 
    /* Validate the server time as your hello world for this package */
    const serverTime = qtApi.serverTime
    log(serverTime)
 
    /* inside an async function use await qt.get.<... some properties or methods> */
    const myBalances = await qtApi.myBalances();
    const balances = await qtApi.account.getBalances();
 
    log(myBalances);
    log(balances);
 
    log(credentials);
 
    /* you can use a try/catch block to manage error instead: */
  })().catch(error=>console.error(error.message));

Structure

 
  const qtApi: IQuestradeApi = {
    currentAccount,
    myBalances,
    serverTime,
    account: {
      getActivities,
      getAllAccounts,
      getAllOrders,
      getBalances,
      getExecutions,
      getOrders,
      getOrdersByIds,
      getPositions,
    },
    market: {
      getAllMarkets,
      gtCandlesByStockId,
    },
    getQuotes: {
      byStockIds,
      byStrategies,
    },
    getOptionsQuotes: {
      byOptionsIds,
      fromFilter,
    },
    getOptionChains: {
      byStockId,
    },
    getSymbols: {
      byStockIds,
    },
    search: {
      stock,
      allStocks,
      countResults,
    },
  };

IQuestradeApi describe in TypeScript terms what the questrade-ts api look like

  interface IQuestradeApi {
    currentAccount: string;
    myBalances: IQtApiMyBalances;
    serverTime: Date | 'ERROR';
    account: IQtApiAccount;
    market: IQtApiMarket;
    getQuotes: IQtApiQuotes;
    getOptionsQuotes: IQtApiOptionsQuotes;
    getSymbols: IQtApiSymbols;
    getOptionChains: IQtApiOptionChains;
    search: IQtApiSearch;
  }

Methods signatures and parameters

  getActivities(startTime: string) =>
    (endTime: string) => Promise<IAccountActivity[]>;
 
  getAllAccounts() => Promise<IAccount[]>;
 
  getBalances() => Promise<IBalances>;
 
  getExecutions(startTime: string) => (endTime: string) => Promise<IExecution[]>;
 
  /* type DateRange<R> = (startTime: string) => (endTime: string) => R */
  getOrders(stateFilter?: string | undefined) => DateRange<Promise<IOrder[]>>;
 
  getOrdersByIds(orderId: number[]) => Promise<IOrder[]>;
 
  getPositions() => Promise<IPosition[]>;
 
  getServerTime() => Promise<Date>;
 
  getAllMarkets() => Promise<IMarket[]>;
 
  /* type DateRange<R> = (startTime: string) => (endTime: string) => R */
  getCandlesByStockId(symbolID: number) =>
    (interval?: string | undefined) => DateRange<Promise<ICandle[]>>;
 
  byStockIds(ids: number[]) => Promise<IQuote[]>;
 
  byStrategies(strategyVariantRequestData: StrategyVariantRequest) =>
    Promise<IStrategiesQuotes>;
 
  fromFilter(filters: OptionsFilters) => Promise<IOptionsQuotes>;
 
  byOptionsIds(optionIds: number[]) => Promise<IOptionsQuotes>;
 
  byStockIds(stockIds: number[]) => Promise<ISymbol[]>;
 
  byStockId(stockId: number) => Promise<IOptionChain[]>;
 
  stock(prefix: string, offset?: number | undefined) =>
    Promise<ISymbolSearchResult>;
 
  allStocks(prefix: string, offset?: number | undefined) =>
    Promise<ISymbolSearchResult[]>;
 
  countResults(prefix: string) => Promise<number>;

The qtApi.myBalances() property

Calling the property qtApi.myBalances() can give more user friendly "dot notation" acces to your balances than using the method qtApi.account.getBalances()

 
 /* qtApi.myBalances() property is of type IQtApiMyBalances = () => Promise<IMyBalances> */
 interface IMyBalances {
   perCurrency: {
     CAD: {
       startOfDay: IBalance;
       current: IBalance;
     };
     USD: {
       startOfDay: IBalance;
       current: IBalance;
     };
   };
   combined: {
     CAD: {
       startOfDay: IBalance;
       current: IBalance;
     };
     USD: {
       startOfDay: IBalance;
       current: IBalance;
     };
   };
   current: {
     perCurrency: {
       CAD: IBalance;
       USD: IBalance;
     };
     combined: {
       CAD: IBalance;
       USD: IBalance;
     };
   };
   startOfDay: {
     combined: {
       CAD: IBalance;
       USD: IBalance;
     };
     perCurrency: {
       CAD: IBalance;
       USD: IBalance;
     };
   };
   CAD: {
     perCurrency: {
       startOfDay: IBalance;
       current: IBalance;
     };
     combined: {
       startOfDay: IBalance;
       current: IBalance;
     };
   };
   USD: {
     combined: {
       startOfDay: IBalance;
       current: IBalance;
     };
     perCurrency: {
       startOfDay: IBalance;
       current: IBalance;
     };
   };
 }

Features

  • No any TypeScript Keyword
  • Token management
  • Easy to use API calls
  • Auto-select primary account

Security and Token management

Questrade's security token system requires that you save the latest refresh token that it vends you. After you create one in the user apps page, our library needs to save a key somewhere onto disk. By default, this wrapper create a folder for these keys in ./keys at your working directory,but you can change the directory location or load from a text file (with the key as its contents).

Switching Accounts

By default, when you instantiate the qtApi it will try to find and select the primary account (by fetching a list of all the accounts). If you want to change the account, simply do:

  /* Switch to account 12345678 -- All future calls will use this 8 digits account. */
  qtApi.currentAccount = '12345678';
 
  /* Must be one of the valid account number for the */
  /* user on behalf of which the API client is authorized */

No-any

This project count that forbiden keyword only twice, once in this title above, the other one is part of the tslint rule name forbiding the keword in the project.

Forbiden Keyword

Always use semicolons when using this package or using this code in other projects

Why? (airbnb/javascript): When JavaScript encounters a line break without a semicolon, it uses a set of rules called Automatic Semicolon Insertion to determine whether or not it should regard that line break as the end of a statement, and (as the name implies) place a semicolon into your code before the line break if it thinks so. ASI contains a few eccentric behaviors, though, and your code will break if JavaScript misinterprets your line break. These rules will become more complicated as new features become a part of JavaScript. Explicitly terminating your statements and configuring your linter to catch missing semicolons will help prevent you from encountering issues.

Contributions

All contributions are welcome

MIT LICENSE NPM

Copyright (c) 2019 Benjamin Vincent Kasapoglu (Luxcium)

Copyright (c) 2016-2019 Leander Lee

Permission is hereby granted, free of charge, to all person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ALL KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ALL CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Originaly based on the work of

Leander Lee

Questrade does not maintain this unofficial SDK

Refer to Questrade's Documentation to get help. Please always open a questrade-ts GitHub issue for anything you feel doesn't match the way it should be working when referring to Questrade docs.

npm

Versions

Current Tags

Version History

Package Sidebar

Install

npm i questrade-ts

Weekly Downloads

3

Version

1.0.2

License

MIT

Unpacked Size

305 kB

Total Files

395

Last publish

Collaborators

  • neb_401