py_alpaca_api.stock.predictor
Attributes
Classes
Module Contents
- class py_alpaca_api.stock.predictor.History(data_url: str, headers: dict[str, str], asset: py_alpaca_api.stock.assets.Assets)[source]
- BATCH_SIZE = 200
- 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.
- class py_alpaca_api.stock.predictor.Screener(data_url: str, headers: dict[str, str], asset: py_alpaca_api.stock.assets.Assets, market: py_alpaca_api.trading.market.Market)[source]
- filter_stocks(price_greater_than: float, change_condition: collections.abc.Callable[[pandas.DataFrame], pandas.Series], volume_greater_than: int, trade_count_greater_than: int, total_returned: int, ascending_order: bool) pandas.DataFrame[source]
Filter stocks based on given parameters.
- Parameters:
price_greater_than – The minimum price threshold for the stocks.
change_condition – A callable function that takes in a DataFrame and returns a boolean Series. This function is used to filter the stocks based on a specific change condition.
volume_greater_than – The minimum volume threshold for the stocks.
trade_count_greater_than – The minimum trade count threshold for the stocks.
total_returned – The number of stocks to return.
ascending_order – A boolean value indicating whether to sort the stocks in ascending order by change value.
- Returns:
A pandas DataFrame containing the filtered stocks.
- losers(price_greater_than: float = 5.0, change_less_than: float = -2.0, volume_greater_than: int = 20000, trade_count_greater_than: int = 2000, total_losers_returned: int = 100) pandas.DataFrame[source]
Returns a filtered DataFrame of stocks that meet the specified conditions for losers.
- Parameters:
price_greater_than (float) – The minimum price threshold for stocks to be considered losers. Default is 5.0.
change_less_than (float) – The maximum change threshold for stocks to be considered losers. Default is -2.0.
volume_greater_than (int) – The minimum volume threshold for stocks to be considered losers. Default is
20000.
trade_count_greater_than (int) – The minimum trade count threshold for stocks to be considered losers. Default is 2000.
total_losers_returned (int) – The maximum number of losers to be returned. Default is 100.
- Returns:
A filtered DataFrame containing stocks that meet the specified conditions for losers.
- Return type:
pd.DataFrame
- gainers(price_greater_than: float = 5.0, change_greater_than: float = 2.0, volume_greater_than: int = 20000, trade_count_greater_than: int = 2000, total_gainers_returned: int = 100) pandas.DataFrame[source]
- Parameters:
price_greater_than (float) – The minimum price threshold for the stocks to be included in the gainers list.
5.0. (Default is)
change_greater_than (float) – The minimum change (in percentage) threshold for the stocks to be included in
list. (the gainers)
2.0. (Default is)
volume_greater_than (int) – The minimum volume threshold for the stocks to be included in the gainers list. Default is 20000.
trade_count_greater_than (int) – The minimum trade count threshold for the stocks to be included in the
2000. (gainers list. Default is)
total_gainers_returned (int) – The maximum number of gainers to be returned. Default is 100.
- Returns:
A Pandas DataFrame containing the stocks that satisfy the criteria for being gainers.
- Return type:
pd.DataFrame
- class py_alpaca_api.stock.predictor.Predictor(history: py_alpaca_api.stock.history.History, screener: py_alpaca_api.stock.screener.Screener)[source]
- get_stock_data(symbol: str, timeframe: str = '1d', start: str = four_years_ago, end: str = yesterday) pandas.DataFrame[source]
Retrieves historical stock data for a given symbol within a specified timeframe.
- Parameters:
- Returns:
A DataFrame containing the historical stock data with columns “ds” (date) and “y” (vwap).
- Return type:
pd.DataFrame
- static train_prophet_model(data)[source]
Trains a Prophet model using the provided data.
- Parameters:
data – The input data used for training the model.
- Returns:
The trained Prophet model.
- static generate_forecast(model, future_periods=14)[source]
Generates a forecast using the specified model for a given number of future periods.
- Parameters:
model – The model used for forecasting.
future_periods – The number of future periods to forecast.
- Returns:
The forecasted value for the next two weeks.