py_alpaca_api.stock.predictor ============================= .. py:module:: py_alpaca_api.stock.predictor Attributes ---------- .. autoapisummary:: py_alpaca_api.stock.predictor.yesterday py_alpaca_api.stock.predictor.four_years_ago py_alpaca_api.stock.predictor.logger Classes ------- .. autoapisummary:: py_alpaca_api.stock.predictor.History py_alpaca_api.stock.predictor.Screener py_alpaca_api.stock.predictor.Predictor Module Contents --------------- .. py:class:: History(data_url: str, headers: dict[str, str], asset: py_alpaca_api.stock.assets.Assets) .. py:attribute:: BATCH_SIZE :value: 200 .. py:method:: check_if_stock(symbol: str) -> py_alpaca_api.models.asset_model.AssetModel Check if the asset corresponding to the symbol is a stock. :param symbol: The symbol of the asset to be checked. :type symbol: str :returns: The asset information for the given symbol. :rtype: AssetModel :raises ValueError: If there is an error getting the asset information or if the asset is not a stock. .. py:method:: 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 Retrieves historical stock data for one or more symbols within a specified date range and timeframe. :param symbol: The stock symbol(s) to fetch data for. Can be a single symbol string or list of symbols. :param start: The start date for historical data in the format "YYYY-MM-DD". :param end: The end date for historical data in the format "YYYY-MM-DD". :param timeframe: The timeframe for the historical data. Default is "1d". :param feed: The data feed source. Default is "sip". :param currency: The currency for historical data. Default is "USD". :param limit: The number of data points to fetch per symbol. Default is 1000. :param sort: The sort order for the data. Default is "asc". :param 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. .. py:method:: preprocess_multi_data(symbols_data: dict[str, list[collections.defaultdict]]) -> pandas.DataFrame :staticmethod: Preprocess data for multiple symbols. :param symbols_data: A dictionary mapping symbols to their bar data. :returns: A pandas DataFrame containing the preprocessed historical stock data for all symbols. .. py:method:: preprocess_data(symbol_data: list[collections.defaultdict], symbol: str) -> pandas.DataFrame :staticmethod: Prepross data Preprocesses the given symbol data by converting it to a pandas DataFrame and performing various data transformations. :param symbol_data: A list of defaultdict objects representing the JSON response data. :param symbol: A string representing the symbol or ticker for the stock data. :returns: A pandas DataFrame containing the preprocessed historical stock data. .. py:method:: get_historical_data(symbols: list[str], url: str, params: dict, is_single: bool) -> dict[str, list[collections.defaultdict]] Retrieves historical data for given symbol(s). :param symbols: List of symbols for which to retrieve historical data. :param url: The URL to send the request to. :param params: Additional parameters to include in the request. :param is_single: Whether this is a single-symbol request. :returns: A dictionary mapping symbols to their historical data. :rtype: dict[str, list[defaultdict]] .. py:method:: get_latest_bars(symbols: str | list[str], feed: str = 'iex', currency: str = 'USD') -> pandas.DataFrame | dict[str, pandas.DataFrame] Get the latest bars for one or more symbols. The latest bars endpoint returns the most recent minute bar for each requested symbol. :param symbols: Symbol(s) to get latest bars for. Can be a string for single symbol or list of strings for multiple symbols. :param feed: The data feed to use ("iex", "sip", or "otc"). Defaults to "iex". :param 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. :rtype: For single symbol :raises ValueError: If feed is invalid or symbols is empty. :raises Exception: If the API request fails or returns no data. .. py:class:: Screener(data_url: str, headers: dict[str, str], asset: py_alpaca_api.stock.assets.Assets, market: py_alpaca_api.trading.market.Market) .. py:method:: 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 Filter stocks based on given parameters. :param price_greater_than: The minimum price threshold for the stocks. :param 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. :param volume_greater_than: The minimum volume threshold for the stocks. :param trade_count_greater_than: The minimum trade count threshold for the stocks. :param total_returned: The number of stocks to return. :param 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. .. py:method:: 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 Returns a filtered DataFrame of stocks that meet the specified conditions for losers. :param price_greater_than: The minimum price threshold for stocks to be considered losers. Default is 5.0. :type price_greater_than: float :param change_less_than: The maximum change threshold for stocks to be considered losers. Default is -2.0. :type change_less_than: float :param volume_greater_than: The minimum volume threshold for stocks to be considered losers. Default is :type volume_greater_than: int :param 20000.: :param trade_count_greater_than: The minimum trade count threshold for stocks to be considered losers. Default is 2000. :type trade_count_greater_than: int :param total_losers_returned: The maximum number of losers to be returned. Default is 100. :type total_losers_returned: int :returns: A filtered DataFrame containing stocks that meet the specified conditions for losers. :rtype: pd.DataFrame .. py:method:: 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 :param price_greater_than: The minimum price threshold for the stocks to be included in the gainers list. :type price_greater_than: float :param Default is 5.0.: :param change_greater_than: The minimum change (in percentage) threshold for the stocks to be included in :type change_greater_than: float :param the gainers list.: :param Default is 2.0.: :param volume_greater_than: The minimum volume threshold for the stocks to be included in the gainers list. Default is 20000. :type volume_greater_than: int :param trade_count_greater_than: The minimum trade count threshold for the stocks to be included in the :type trade_count_greater_than: int :param gainers list. Default is 2000.: :param total_gainers_returned: The maximum number of gainers to be returned. Default is 100. :type total_gainers_returned: int :returns: A Pandas DataFrame containing the stocks that satisfy the criteria for being gainers. :rtype: pd.DataFrame .. py:method:: set_dates() Sets the dates for the screener. This method retrieves the last two trading dates from the market calendar and assigns them to the `yesterday` and `day_before_yesterday` attributes. :returns: None .. py:data:: yesterday .. py:data:: four_years_ago .. py:data:: logger .. py:class:: Predictor(history: py_alpaca_api.stock.history.History, screener: py_alpaca_api.stock.screener.Screener) .. py:method:: get_stock_data(symbol: str, timeframe: str = '1d', start: str = four_years_ago, end: str = yesterday) -> pandas.DataFrame Retrieves historical stock data for a given symbol within a specified timeframe. :param symbol: The stock symbol to retrieve data for. :type symbol: str :param timeframe: The timeframe for the data. Defaults to "1d". :type timeframe: str, optional :param start: The start date for the data. Defaults to four_years_ago. :type start: str, optional :param end: The end date for the data. Defaults to yesterday. :type end: str, optional :returns: A DataFrame containing the historical stock data with columns "ds" (date) and "y" (vwap). :rtype: pd.DataFrame .. py:method:: train_prophet_model(data) :staticmethod: Trains a Prophet model using the provided data. :param data: The input data used for training the model. :returns: The trained Prophet model. .. py:method:: generate_forecast(model, future_periods=14) :staticmethod: Generates a forecast using the specified model for a given number of future periods. :param model: The model used for forecasting. :param future_periods: The number of future periods to forecast. :returns: The forecasted value for the next two weeks. .. py:method:: get_losers_to_gainers(gain_ratio: float = 10.0, losers_to_scan: int = 200, future_periods: int = 5) -> list