Class Questrade

java.lang.Object
com.jquestrade.Questrade

public class Questrade extends Object
The Questrade class represents an Questrade API client. It contains methods for easily accessing the Questrade API, such as retrieving the balances, positions, orders, market data, etc. It will also automatically retrieve a new access token if the current access token expires during runtime.
See Also:
  • Constructor Details

    • Questrade

      public Questrade(String refreshToken)
      Creates an instance of the QuestradeAPI wrapper, whose methods can be used to access the Questrade API. To use the object to access the Questrade API, you must call the activate() method on the object, otherwise API methods will not work.
      Parameters:
      refreshToken - The refresh token that is used to gain access to the Questrade API.
      See Also:
    • Questrade

      public Questrade(String refreshToken, String accessToken, String apiServer)
      Creates an instance of the QuestradeAPI wrapper, whose methods can be used to access the Questrade API. To use the object to access the Questrade API, you must call the activate() method on the object, otherwise API methods will not work. This constructor is intended for using cached data to create a QuestradeAPI object, to save time and bandwidth since an access token can be used for 30 minutes after generation. Calling this constructor on its own will not make an API request to Questrade.
      Parameters:
      refreshToken - The refresh token that is used to gain access to the Questrade API.
      accessToken - The previously-generated access token. This will be used until it expires, after which the refreshToken will be used to get a new authorization.
      apiServer - The API server address associated with the accessToken.
      See Also:
  • Method Details

    • getLastRequest

      public String getLastRequest()
      A string representation of the this object's last HTTP request.
      Returns:
      A string representation of the this object's last HTTP request.
    • activate

      public Questrade activate() throws RefreshTokenException
      Starts up the API connection by exchanging the refresh token for an access token. You must call this method before calling any methods that perform API requests. This will create an Authorization for this object. Calling this method more than one time has no effect.
      Returns:
      A reference to the calling object, for optional method chaining.
      Example: QuestradeApi q = new QuestradeAPI(token).activate();
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
    • revokeAuthorization

      public void revokeAuthorization(String refreshToken)
      Consumes the refresh token and any access tokens associated with it, invalidating them so that the user will have to go and generate a new refresh token from Questrade. This method simply exchanges the refresh token for new tokens, but throws away the response, essentially invalidating the old ones.
      Parameters:
      refreshToken - The refresh token to consume
    • revokeAuthorization

      public void revokeAuthorization()
      Consumes this object's refresh and access token, invalidating them so that the user will have to go and generate a new refresh token from Questrade. This method simply exchanges the refresh token for new tokens, but throws away the response, essentially invalidating the old ones.
    • retrieveAccessToken

      public void retrieveAccessToken(String refreshToken) throws RefreshTokenException
      Manually refresh the authorization (which includes the access token) with a given refresh token. Calling this function will save the resulting Authorization object to be relayed to authorization relay function (if set using the setAuthRelay(Consumer) method).

      For reference, an access token usually expires in 1800 seconds (30 minutes). This value can be retrieved by using getAuthorization() then the Authorization.getAccessTokenExpiry() method.
      Parameters:
      refreshToken - The refresh token to be used to refresh the authorization.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
    • retrieveAccessToken

      public void retrieveAccessToken() throws RefreshTokenException
      Forcefully refreshes the authorization (which includes the access token) with the refresh token saved within the object. Calling this function will save the resulting Authorization object to be relayed to authorization relay function (if set using the setAuthRelay(Consumer) method).

      For reference, an access token usually expires in 1800 seconds (30 minutes). This value can be retrieved by using getAuthorization() then the Authorization.getAccessTokenExpiry() method.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
    • getAuthorization

      public Authorization getAuthorization()
      Returns an Authorization object which contains the access token, api server, access token expiry time, new refresh token, and the access token type (which is always Bearer).

      Returns:
      The current Authorization object. Will be null if activate() has not been called yet.
    • setAuthRelay

      public Questrade setAuthRelay(Consumer<Authorization> authRelayFunction)
      Sets the authorization relay function, which is user-created method to which an Authorization object is relayed to. This is useful for when you want to save the new refresh token instead of having to manually generate a new one on the Questrade website every time you use your application. For example, you could relay the Authorization object to a method that saved the new refresh token in a text file, so that the refresh token could be used when creating another QuestradeAPI object.

      Example usage:
      QuestradeAPI q = new QuestradeAPI(token);
      q.setAuthRelay(auth -> saveToFile(auth));
      Parameters:
      authRelayFunction - Should be a void function to which the Authorization will be forwarded to. This can be written as a lambda function.
      Returns:
      A reference to the calling object, for optional method chaining.
      Example: QuestradeApi q = new QuestradeAPI(token).setAuthRelay(function);
    • getBalances

      public Balances getBalances(String accountNumber) throws RefreshTokenException
      Get the balances for the given account.
      Parameters:
      accountNumber - The account number to get the balances for. To get an account number, call getAccounts() to get a Account[], then call Account.getNumber() on some index.
      Returns:
      A Balances object, from which the following Balance objects can be gotten: perCurrencyBalances, combinedBalances, sodPerCurrencyBalances, sodCombinedBalances.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getAccounts

      public Account[] getAccounts() throws RefreshTokenException
      Get all of the accounts for the associated Questrade account.
      Returns:
      An Account[] array, containing all of the accounts.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API
      See Also:
    • getTime

      public ZonedDateTime getTime() throws RefreshTokenException
      Returns the current server time in ISO format and Eastern time zone (EST).
      Returns:
      A ZonedDateTime object representing the current server time.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getActivities

      public Activity[] getActivities(String accountNumber, ZonedDateTime startTime, ZonedDateTime endTime) throws RefreshTokenException
      Get all of the activities of an account in a given time period. A maximum of 30 days of data can be requested at a time.
      Parameters:
      accountNumber - The account for which to get the activities for.
      startTime - The beginning of the time period to get the activities for.
      endTime - The end of the time period to get the activities for. This cannot be more than 31 days after the startTime argument.
      Returns:
      An Activity[] array representing all the activities in the given time period.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      ArgumentException - If the request arguments are invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getExecutions

      public Execution[] getExecutions(String accountNumber, ZonedDateTime startTime, ZonedDateTime endTime) throws RefreshTokenException
      Get all of the executions of an account in a given time period. A maximum of 30 days of data can be requested at a time.
      Parameters:
      accountNumber - The account for which to get the executions for.
      startTime - The beginning of the time period to get the executions for.
      endTime - The end of the time period to get the executions for.
      Returns:
      An Activity[] array representing all the executions in the given time period.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      ArgumentException - If the request arguments are invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getOrders

      public Order[] getOrders(String accountNumber, int[] orderIds) throws RefreshTokenException
      Get all of the orders of an account in a given time period, using one or more order IDs. Is equivalent to getOrders(String, int, int[])
      Parameters:
      accountNumber - The account to get the order information for.
      orderIds - An array containing all of the orders IDs to get information for.
      Returns:
      An Order[] array containing all of the Order objects corresponding to the given order IDs.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getOrders

      public Order[] getOrders(String accountNumber, int orderId, int... orderIds) throws RefreshTokenException
      Get all of the orders of an account in a given time period, using one or more order IDs. Is equivalent to getOrders(String, int[])

      Example usages:
      getOrders(accountNumber, 11111111)
      getOrders(accountNumber, 11111111, 22222222)
      getOrders(accountNumber, orderNum1, orderNum2, orderNum3)
      Parameters:
      accountNumber - The account to get the order information for.
      orderId - The order ID to get the order info for (an Order object contain all of an order's information).
      orderIds - Optional parameter for if you want to add more order IDs to the request.
      Returns:
      An Order[] array containing all of the Order objects corresponding to the given order IDs.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      ArgumentException - If the request arguments are invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getOrders

      public Order[] getOrders(String accountNumber, ZonedDateTime startTime, ZonedDateTime endTime) throws RefreshTokenException
      Get all of the orders of an account in a given time period. A maximum of 30 days of data can be requested at a time. This will get all orders in the time period, whether still open or closed. To specify the state of the filter, use the getOrders(String, ZonedDateTime, ZonedDateTime, OrderState) method.
      Parameters:
      accountNumber - The account for which to get the orders for.
      startTime - The beginning of the time period to get the orders for.
      endTime - The end of the time period to get the orders for.
      Returns:
      An Order[] array containing all of the Orders created in the time period.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getOrders

      public Order[] getOrders(String accountNumber, ZonedDateTime startTime, ZonedDateTime endTime, Order.OrderState orderState) throws RefreshTokenException
      Get all of the orders of an account in a given time period. A maximum of 30 days of data can be requested at a time. You can specify the state of the orders to retrieve using the stateFilter parameter (All, Open, or Closed).
      Parameters:
      accountNumber - The account for which to get the orders for.
      startTime - The beginning of the time period to get the orders for.
      endTime - The end of the time period to get the orders for.
      orderState - The state of the order. See Order.OrderState for all possible values.
      Returns:
      An Order[] array containing all of the Orders created in the time period.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      ArgumentException - If the request arguments are invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getPositions

      public Position[] getPositions(String accountNumber) throws RefreshTokenException
      Get all of the current positions for a given account.
      Parameters:
      accountNumber - The account to get the positions for.
      Returns:
      A Position[] array containing all of the corresponding Position objects.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      ArgumentException - If the request arguments are invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getCandles

      public Candle[] getCandles(int symbolId, ZonedDateTime startTime, ZonedDateTime endTime, Candle.Interval interval) throws RefreshTokenException
      Returns historical market data in the form of OHLC candlesticks for a specified symbol. This call is limited to returning 2,000 candlesticks in a single response.
      Parameters:
      symbolId - The internal symbol identifier.
      startTime - The beginning of the time period to get the candles for.
      endTime - The end of the time period to get the candles for.
      interval - The time between the candles.
      Returns:
      An Candle[] array containing all of the Candles within in the given time period.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      ArgumentException - If the request arguments are invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getMarkets

      public Market[] getMarkets() throws RefreshTokenException
      Retrieves information about supported markets.
      Returns:
      An Market[] array containing all of the available Markets.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • searchSymbol

      public Symbol[] searchSymbol(String prefix) throws RefreshTokenException
      Returns a search for a symbol containing basic information. This method is the same as calling searchSymbol(String, 0)

      Example: If the prefix is "BMO", the result set will contain basic information for "BMO", "BMO.PRJ.TO", etc (anything with "BMO" in it).
      Parameters:
      prefix - The prefix of a symbol or any word in the description. Example: "AAPL" is a valid parameter.
      Returns:
      A {code Symbol[]} object containing basic information about the symbol(s).
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • searchSymbol

      public Symbol[] searchSymbol(String prefix, int offset) throws RefreshTokenException
      Returns a search for a symbol containing basic information.

      * Example: If the prefix is "BMO", the result set will contain basic information for "BMO", "BMO.PRJ.TO", etc (anything with "BMO" in it).
      Parameters:
      prefix - The prefix of a symbol or any word in the description. Example: "AAPL" is a valid parameter.
      offset - Offset in number of records from the beginning of a result set. Example: If there would normally be 5 results in the resulting array, an offset of 1 would return 4 results (the 2nd to the 5th).
      Returns:
      A {code Symbol[]} object containing basic information about the symbol(s).
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getSymbol

      public SymbolInfo[] getSymbol(int id, int... ids) throws RefreshTokenException
      Returns detailed information for one or more specific symbols using their internal unique identifiers. Is equivalent to getSymbol(int[])
      Parameters:
      id - The internal unique identifier for a symbol.
      ids - Optional parameter for if you want to get information for multiple symbols in the same request.
      Returns:
      A SymbolInfo[] object containing information about the symbol(s).
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getSymbol

      public SymbolInfo[] getSymbol(int[] ids) throws RefreshTokenException
      Returns detailed information for one or more specific symbols using their internal unique identifiers. Is equivalent to getSymbol(int, int[])
      Parameters:
      ids - The internal unique identifiers for one or more symbols.
      Returns:
      A SymbolInfo[] object containing information about the symbol(s).
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getSymbol

      public SymbolInfo[] getSymbol(String name, String... names) throws RefreshTokenException
      Returns detailed information for one or more specific symbols using their symbol names. Does the same thing as getSymbol(String[])
      Parameters:
      name - The name of the symbol. (Eg: "MSFT")
      names - Optional parameter for if you want to get information for multiple symbols in the same request.
      Returns:
      A SymbolInfo[] object containing information about the symbol(s).
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getSymbol

      public SymbolInfo[] getSymbol(String[] names) throws RefreshTokenException
      Returns detailed information for one or more specific symbols using their symbol names. Does the same thing as getSymbol(String, String[])
      Parameters:
      names - The names of the symbols. (Eg: "MSFT", "AAPL")
      Returns:
      A SymbolInfo[] object containing information about the symbol(s).
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getQuote

      public Quote[] getQuote(int id, int... ids) throws RefreshTokenException
      Retrieves a single Level 1 market data quote for one or more symbols. Equivalent to calling getQuote(int[])

      IMPORTANT NOTE: The Questrade user needs to be subscribed to a real-time data package, to receive market quotes in real-time, otherwise call to get quote is considered snap quote and limit per market can be quickly reached. Without real-time data package, once limit is reached, the response will return delayed data. (Please check "delay" parameter in response always)
      Parameters:
      id - The internal identifer of a symbol.
      ids - Optional parameter for adding more symbols to the same request.
      Returns:
      A Quote[] array, each index containing the quote for each requested symbol.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also:
    • getQuote

      public Quote[] getQuote(int[] ids) throws RefreshTokenException
      Retrieves a single Level 1 market data quote for one or more symbols. Equivalent to calling getQuote(int, int[])

      IMPORTANT NOTE: The Questrade user needs to be subscribed to a real-time data package, to receive market quotes in real-time, otherwise call to get quote is considered snap quote and limit per market can be quickly reached. Without real-time data package, once limit is reached, the response will return delayed data. (Please check "delay" parameter in response always)
      Parameters:
      ids - The internal identifers of the symbols.
      Returns:
      A Quote[] array, each index containing the quote for each requested symbol.
      Throws:
      RefreshTokenException - If the refresh token is invalid.
      StatusCodeException - If an error occurs when contacting the Questrade API.
      See Also: