Utilities¶
This section describes miscellaneous utility methods provided by tda-api
.
All utilities are presented under the Utils
class:
-
class
tda.utils.
Utils
(client, account_id)¶ Helper for placing orders on equities. Provides easy-to-use implementations for common tasks such as market and limit orders.
-
__init__
(client, account_id)¶ Creates a new
Utils
instance. For convenience, this object assumes the user wants to work with a single account ID at a time.
-
set_account_id
(account_id)¶ Set the account ID used by this
Utils
instance.
-
Get the Most Recent Order¶
For successfully placed orders, tda.client.Client.place_order()
returns
the ID of the newly created order, encoded in the r.headers['Location']
header. This method inspects the response and extracts the order ID from the
contents, if it’s there. This order ID can then be used to monitor or modify the
order as described in the Client documentation. Example
usage:
# Assume client and order already exist and are valid
account_id = 123456
r = client.place_order(account_id, order)
assert r.status_code == httpx.codes.OK, r.raise_for_status()
order_id = Utils(client, account_id).extract_order_id(r)
assert order_id is not None
-
Utils.
extract_order_id
(place_order_response)¶ Attempts to extract the order ID from a response object returned by
Client.place_order()
. ReturnNone
if the order location is not contained in the response.Parameters: place_order_response – Order response as returned by Client.place_order()
. Note this method requires that the order was successful.Raises: ValueError – if the order was not succesful or if the order’s account ID is not equal to the account ID set in this Utils
object.