Package com.jquestrade
Class Questrade
java.lang.Object
com.jquestrade.Questrade
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 Summary
ConstructorsConstructorDescriptionCreates an instance of theQuestradeAPI
wrapper, whose methods can be used to access the Questrade API.Creates an instance of theQuestradeAPI
wrapper, whose methods can be used to access the Questrade API. -
Method Summary
Modifier and TypeMethodDescriptionactivate()
Starts up the API connection by exchanging the refresh token for an access token.Account[]
Get all of the accounts for the associated Questrade account.Activity[]
getActivities
(String accountNumber, ZonedDateTime startTime, ZonedDateTime endTime) Get all of the activities of an account in a given time period.Returns anAuthorization
object which contains the access token, api server, access token expiry time, new refresh token, and the access token type (which is always Bearer).getBalances
(String accountNumber) Get the balances for the given account.Candle[]
getCandles
(int symbolId, ZonedDateTime startTime, ZonedDateTime endTime, Candle.Interval interval) Returns historical market data in the form of OHLC candlesticks for a specified symbol.getExecutions
(String accountNumber, ZonedDateTime startTime, ZonedDateTime endTime) Get all of the executions of an account in a given time period.A string representation of the this object's last HTTP request.Market[]
Retrieves information about supported markets.Order[]
Get all of the orders of an account in a given time period, using one or more order IDs.Order[]
Get all of the orders of an account in a given time period, using one or more order IDs.Order[]
getOrders
(String accountNumber, ZonedDateTime startTime, ZonedDateTime endTime) Get all of the orders of an account in a given time period.Order[]
getOrders
(String accountNumber, ZonedDateTime startTime, ZonedDateTime endTime, Order.OrderState orderState) Get all of the orders of an account in a given time period.Position[]
getPositions
(String accountNumber) Get all of the current positions for a given account.Quote[]
getQuote
(int[] ids) Retrieves a single Level 1 market data quote for one or more symbols.Quote[]
getQuote
(int id, int... ids) Retrieves a single Level 1 market data quote for one or more symbols.getSymbol
(int[] ids) Returns detailed information for one or more specific symbols using their internal unique identifiers.getSymbol
(int id, int... ids) Returns detailed information for one or more specific symbols using their internal unique identifiers.Returns detailed information for one or more specific symbols using their symbol names.Returns detailed information for one or more specific symbols using their symbol names.getTime()
Returns the current server time in ISO format and Eastern time zone (EST).void
Forcefully refreshes the authorization (which includes the access token) with the refresh token saved within the object.void
retrieveAccessToken
(String refreshToken) Manually refresh the authorization (which includes the access token) with a given refresh token.void
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.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.Symbol[]
searchSymbol
(String prefix) Returns a search for a symbol containing basic information.Symbol[]
searchSymbol
(String prefix, int offset) Returns a search for a symbol containing basic information.
* Example: If theprefix
is"BMO"
, the result set will contain basic information for"BMO"
,"BMO.PRJ.TO"
, etc (anything with"BMO"
in it).setAuthRelay
(Consumer<Authorization> authRelayFunction) Sets the authorization relay function, which is user-created method to which anAuthorization
object is relayed to.
-
Constructor Details
-
Questrade
Creates an instance of theQuestradeAPI
wrapper, whose methods can be used to access the Questrade API. To use the object to access the Questrade API, you must call theactivate()
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
Creates an instance of theQuestradeAPI
wrapper, whose methods can be used to access the Questrade API. To use the object to access the Questrade API, you must call theactivate()
method on the object, otherwise API methods will not work. This constructor is intended for using cached data to create aQuestradeAPI
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
A string representation of the this object's last HTTP request.- Returns:
- A string representation of the this object's last HTTP request.
-
activate
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 anAuthorization
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
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
Manually refresh the authorization (which includes the access token) with a given refresh token. Calling this function will save the resultingAuthorization
object to be relayed to authorization relay function (if set using thesetAuthRelay(Consumer)
method).
For reference, an access token usually expires in 1800 seconds (30 minutes). This value can be retrieved by usinggetAuthorization()
then theAuthorization.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
Forcefully refreshes the authorization (which includes the access token) with the refresh token saved within the object. Calling this function will save the resultingAuthorization
object to be relayed to authorization relay function (if set using thesetAuthRelay(Consumer)
method).
For reference, an access token usually expires in 1800 seconds (30 minutes). This value can be retrieved by usinggetAuthorization()
then theAuthorization.getAccessTokenExpiry()
method.- Throws:
RefreshTokenException
- If the refresh token is invalid.StatusCodeException
- If an error occurs when contacting the Questrade API.
-
getAuthorization
Returns anAuthorization
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 benull
ifactivate()
has not been called yet.
-
setAuthRelay
Sets the authorization relay function, which is user-created method to which anAuthorization
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 theAuthorization
object to a method that saved the new refresh token in a text file, so that the refresh token could be used when creating anotherQuestradeAPI
object.
Example usage:
QuestradeAPI q = new QuestradeAPI(token);
q.setAuthRelay(auth -> saveToFile(auth));
- Parameters:
authRelayFunction
- Should be avoid
function to which theAuthorization
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
Get the balances for the given account.- Parameters:
accountNumber
- The account number to get the balances for. To get an account number, callgetAccounts()
to get aAccount[]
, then callAccount.getNumber()
on some index.- Returns:
- A
Balances
object, from which the followingBalance
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
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
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
Get all of the orders of an account in a given time period, using one or more order IDs. Is equivalent togetOrders(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 theOrder
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 togetOrders(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 (anOrder
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 theOrder
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 thegetOrders(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 theOrder
s 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. SeeOrder.OrderState
for all possible values.- Returns:
- An
Order[]
array containing all of theOrder
s 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
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 correspondingPosition
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 theCandle
s 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
Retrieves information about supported markets.- Returns:
- An
Market[]
array containing all of the availableMarket
s. - Throws:
RefreshTokenException
- If the refresh token is invalid.StatusCodeException
- If an error occurs when contacting the Questrade API.- See Also:
-
searchSymbol
Returns a search for a symbol containing basic information. This method is the same as callingsearchSymbol(String, 0)
Example: If theprefix
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
Returns a search for a symbol containing basic information.
* Example: If theprefix
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 of1
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
Returns detailed information for one or more specific symbols using their internal unique identifiers. Is equivalent togetSymbol(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
Returns detailed information for one or more specific symbols using their internal unique identifiers. Is equivalent togetSymbol(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
Returns detailed information for one or more specific symbols using their symbol names. Does the same thing asgetSymbol(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
Returns detailed information for one or more specific symbols using their symbol names. Does the same thing asgetSymbol(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
Retrieves a single Level 1 market data quote for one or more symbols. Equivalent to callinggetQuote(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
Retrieves a single Level 1 market data quote for one or more symbols. Equivalent to callinggetQuote(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:
-