py_alpaca_api.stock.history
Classes
Module Contents
- class py_alpaca_api.stock.history.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.history.Assets(base_url: str, headers: dict[str, str])[source]
- get(symbol: str) py_alpaca_api.models.asset_model.AssetModel[source]
Retrieves an AssetModel for the specified symbol.
- get_all(status: str = 'active', exchange: str = '', excluded_exchanges: list[str] | None = None) pandas.DataFrame[source]
Retrieves a DataFrame of all active, fractionable, and tradable assets.
Excluding those from the OTC exchange.
- Parameters:
status (str, optional) – The status of the assets to retrieve. Defaults to “active”.
exchange (str, optional) – The exchange to filter the assets by. Defaults to an empty string, which retrieves assets from all exchanges.
excluded_exchanges (List[str], optional) – A list of exchanges to exclude from the results. Defaults to [“OTC”].
- Returns:
A DataFrame containing the retrieved assets.
- Return type:
pd.DataFrame
- class py_alpaca_api.stock.history.History(data_url: str, headers: dict[str, str], asset: py_alpaca_api.stock.assets.Assets)[source]
-
- check_if_stock(symbol: str) py_alpaca_api.models.asset_model.AssetModel[source]
Check if the asset corresponding to the symbol is a stock.
- Parameters:
symbol (str) – The symbol of the asset to be checked.
- Returns:
The asset information for the given symbol.
- Return type:
- Raises:
ValueError – If there is an error getting the asset information or if the asset is not a stock.
- get_stock_data(symbol: str | list[str], start: str, end: str, timeframe: str = '1d', feed: str = 'sip', currency: str = 'USD', limit: int = 1000, sort: str = 'asc', adjustment: str = 'raw') pandas.DataFrame[source]
Retrieves historical stock data for one or more symbols within a specified date range and timeframe.
- Parameters:
symbol – The stock symbol(s) to fetch data for. Can be a single symbol string or list of symbols.
start – The start date for historical data in the format “YYYY-MM-DD”.
end – The end date for historical data in the format “YYYY-MM-DD”.
timeframe – The timeframe for the historical data. Default is “1d”.
feed – The data feed source. Default is “sip”.
currency – The currency for historical data. Default is “USD”.
limit – The number of data points to fetch per symbol. Default is 1000.
sort – The sort order for the data. Default is “asc”.
adjustment – The adjustment for historical data. Default is “raw”.
- Returns:
A pandas DataFrame containing the historical stock data for the given symbol(s) and time range.
- Raises:
ValueError – If the given timeframe is not one of the allowed values.
- static preprocess_multi_data(symbols_data: dict[str, list[collections.defaultdict]]) pandas.DataFrame[source]
Preprocess data for multiple symbols.
- Parameters:
symbols_data – A dictionary mapping symbols to their bar data.
- Returns:
A pandas DataFrame containing the preprocessed historical stock data for all symbols.
- static preprocess_data(symbol_data: list[collections.defaultdict], symbol: str) pandas.DataFrame[source]
Prepross data Preprocesses the given symbol data by converting it to a pandas DataFrame and performing various data transformations.
- Parameters:
symbol_data – A list of defaultdict objects representing the JSON response data.
symbol – A string representing the symbol or ticker for the stock data.
- Returns:
A pandas DataFrame containing the preprocessed historical stock data.
- get_historical_data(symbols: list[str], url: str, params: dict, is_single: bool) dict[str, list[collections.defaultdict]][source]
Retrieves historical data for given symbol(s).
- Parameters:
symbols – List of symbols for which to retrieve historical data.
url – The URL to send the request to.
params – Additional parameters to include in the request.
is_single – Whether this is a single-symbol request.
- Returns:
A dictionary mapping symbols to their historical data.
- Return type:
- get_latest_bars(symbols: str | list[str], feed: str = 'iex', currency: str = 'USD') pandas.DataFrame | dict[str, pandas.DataFrame][source]
Get the latest bars for one or more symbols.
The latest bars endpoint returns the most recent minute bar for each requested symbol.
- Parameters:
symbols – Symbol(s) to get latest bars for. Can be a string for single symbol or list of strings for multiple symbols.
feed – The data feed to use (“iex”, “sip”, or “otc”). Defaults to “iex”.
currency – The currency for the returned prices. Defaults to “USD”.
- Returns:
pd.DataFrame with the latest bar data. For multiple symbols: dict mapping symbols to DataFrames with latest bar data.
- Return type:
For single symbol
- Raises:
ValueError – If feed is invalid or symbols is empty.
Exception – If the API request fails or returns no data.