Utilities
This section describes miscellaneous utility methods provided by schwab-py.
All utilities are presented under the Utils class:
- class schwab.utils.Utils(client, account_hash)
Helper for placing orders on equities. Provides easy-to-use implementations for common tasks such as market and limit orders.
- __init__(client, account_hash)
Creates a new
Utilsinstance. For convenience, this object assumes the user wants to work with a single account hash at a time.
- set_account_hash(account_hash)
Set the account hash used by this
Utilsinstance.
Extract an order ID from a placed order
For successfully placed orders, 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 = ... # Fetched from account_information
r = client.place_order(account_hash, order)
assert r.status_code == httpx.codes.OK, r.raise_for_status()
order_id = Utils(client, account_hash).extract_order_id(r)
assert order_id is not None
- Utils.extract_order_id(place_order_response)
Attempts to extract the order hash from a response object returned by
Client.place_order(). ReturnNoneif 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 hash is not equal to the account hash set in this
Utilsobject.