py_alpaca_api.stock.metadata

Exceptions

APIRequestError

Raised when an API request fails.

ValidationError

Raised when input validation fails.

Classes

Requests

Metadata

Market metadata API for condition codes and exchange codes.

Module Contents

exception py_alpaca_api.stock.metadata.APIRequestError(status_code: int | None = None, message: str = '')[source]

Bases: PyAlpacaAPIError

Raised when an API request fails.

exception py_alpaca_api.stock.metadata.ValidationError[source]

Bases: PyAlpacaAPIError

Raised when input validation fails.

class py_alpaca_api.stock.metadata.Requests[source]
request(method: str, url: str, headers: dict[str, str] | None = None, params: dict[str, str | bool | float | int] | None = None, json: dict[str, Any] | None = None, raw_response: bool = False)[source]

Execute HTTP request with retry logic.

Parameters:
  • method – A string representing the HTTP method to be used in the request.

  • url – A string representing the URL to send the request to.

  • headers – An optional dictionary containing the headers for the request.

  • params – An optional dictionary containing the query parameters for the request.

  • json – An optional dictionary containing the JSON payload for the request.

  • raw_response – If True, return the raw response object without status checks. Defaults to False.

Returns:

The response object returned by the server.

Raises:

APIRequestError – If the response status code is not one of the acceptable statuses (200, 204, 207) and raw_response is False.

class py_alpaca_api.stock.metadata.Metadata(headers: dict[str, str])[source]

Market metadata API for condition codes and exchange codes.

get_exchange_codes(use_cache: bool = True) dict[str, str][source]

Get the mapping between exchange codes and exchange names.

Parameters:

use_cache – Whether to use cached data if available. Defaults to True.

Returns:

Dictionary mapping exchange codes to exchange names.

Raises:

APIRequestError – If the API request fails.

get_condition_codes(ticktype: str = 'trade', tape: str = 'A', use_cache: bool = True) dict[str, str][source]

Get the mapping between condition codes and condition names.

Parameters:
  • ticktype – Type of conditions to retrieve (“trade” or “quote”). Defaults to “trade”.

  • tape – Market tape (“A” for NYSE, “B” for NASDAQ, “C” for other). Defaults to “A”.

  • use_cache – Whether to use cached data if available. Defaults to True.

Returns:

Dictionary mapping condition codes to condition descriptions.

Raises:
get_all_condition_codes(use_cache: bool = True) dict[str, dict[str, dict[str, str]]][source]

Get all condition codes for all tick types and tapes.

Parameters:

use_cache – Whether to use cached data if available. Defaults to True.

Returns:

{
“trade”: {

“A”: {condition_code: description, …}, “B”: {condition_code: description, …}, “C”: {condition_code: description, …}

}, “quote”: {

”A”: {condition_code: description, …}, “B”: {condition_code: description, …}, “C”: {condition_code: description, …}

}

}

Return type:

Nested dictionary with structure

Raises:

APIRequestError – If any API request fails.

clear_cache() None[source]

Clear all cached metadata.

This forces the next request to fetch fresh data from the API.

lookup_exchange(code: str) str | None[source]

Look up an exchange name by its code.

Parameters:

code – The exchange code to look up.

Returns:

The exchange name if found, None otherwise.

lookup_condition(code: str, ticktype: str = 'trade', tape: str = 'A') str | None[source]

Look up a condition description by its code.

Parameters:
  • code – The condition code to look up.

  • ticktype – Type of condition (“trade” or “quote”). Defaults to “trade”.

  • tape – Market tape (“A”, “B”, or “C”). Defaults to “A”.

Returns:

The condition description if found, None otherwise.