py_alpaca_api.stock.screener

Classes

Requests

Assets

Market

Screener

Module Contents

class py_alpaca_api.stock.screener.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.screener.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.

Parameters:

symbol (str) – The symbol of the asset to retrieve.

Returns:

The AssetModel for the specified asset.

Return type:

AssetModel

Raises:

Exception – If the asset is not a US Equity (stock).

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.screener.Market(base_url: str, headers: dict[str, str])[source]
clock() py_alpaca_api.models.clock_model.ClockModel[source]

Retrieves the current market clock.

Returns:

A model containing the current market clock data.

Return type:

ClockModel

calendar(start_date: str, end_date: str) pandas.DataFrame[source]

Retrieves the market calendar for the specified date range.

Parameters:
  • start_date (str) – The start date of the calendar range in the format “YYYY-MM-DD”.

  • end_date (str) – The end date of the calendar range in the format “YYYY-MM-DD”.

Returns:

A DataFrame containing the market calendar data, with columns for the date, settlement date, open time, and close time.

Return type:

pd.DataFrame

class py_alpaca_api.stock.screener.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

set_dates()[source]

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