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 Utils instance. 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 Utils instance.

Get the Most Recent Order

For successfully placed orders, schwab.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 = ...  # 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(). Return None 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 hash is not equal to the account hash set in this Utils object.