py_alpaca_api.stock.trades ========================== .. py:module:: py_alpaca_api.stock.trades Exceptions ---------- .. autoapisummary:: py_alpaca_api.stock.trades.APIRequestError py_alpaca_api.stock.trades.ValidationError Classes ------- .. autoapisummary:: py_alpaca_api.stock.trades.Requests py_alpaca_api.stock.trades.TradeModel py_alpaca_api.stock.trades.TradesResponse py_alpaca_api.stock.trades.Trades Functions --------- .. autoapisummary:: py_alpaca_api.stock.trades.trade_class_from_dict Module Contents --------------- .. py:exception:: APIRequestError(status_code: int | None = None, message: str = '') Bases: :py:obj:`PyAlpacaAPIError` Raised when an API request fails. .. py:exception:: ValidationError Bases: :py:obj:`PyAlpacaAPIError` Raised when input validation fails. .. py:class:: Requests .. py:method:: 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) Execute HTTP request with retry logic. :param method: A string representing the HTTP method to be used in the request. :param url: A string representing the URL to send the request to. :param headers: An optional dictionary containing the headers for the request. :param params: An optional dictionary containing the query parameters for the request. :param json: An optional dictionary containing the JSON payload for the request. :param 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. .. py:class:: TradeModel Model for individual stock trade data. .. py:attribute:: timestamp :type: str .. py:attribute:: symbol :type: str .. py:attribute:: exchange :type: str .. py:attribute:: price :type: float .. py:attribute:: size :type: int .. py:attribute:: conditions :type: list[str] | None .. py:attribute:: id :type: int .. py:attribute:: tape :type: str .. py:class:: TradesResponse Response model for trades endpoint with pagination. .. py:attribute:: trades :type: list[TradeModel] .. py:attribute:: symbol :type: str .. py:attribute:: next_page_token :type: str | None :value: None .. py:function:: trade_class_from_dict(data: dict[str, Any], symbol: str | None = None) -> TradeModel Create TradeModel from API response dictionary. :param data: Dictionary containing trade data from API :param symbol: Optional symbol to use if not in data :returns: TradeModel instance .. py:class:: Trades(headers: dict[str, str]) .. py:method:: get_trades(symbol: str, start: str, end: str, limit: int = 1000, feed: Literal['iex', 'sip', 'otc'] | None = None, page_token: str | None = None, asof: str | None = None) -> py_alpaca_api.models.trade_model.TradesResponse Retrieve historical trades for a symbol. :param symbol: The stock symbol to retrieve trades for :param start: Start time in RFC-3339 format (YYYY-MM-DDTHH:MM:SSZ) :param end: End time in RFC-3339 format (YYYY-MM-DDTHH:MM:SSZ) :param limit: Number of trades to return (1-10000, default 1000) :param feed: Data feed to use (iex, sip, otc) :param page_token: Token for pagination :param asof: As-of time for historical data in RFC-3339 format :returns: TradesResponse with list of trades and pagination token :raises ValidationError: If parameters are invalid :raises APIRequestError: If the API request fails .. py:method:: get_latest_trade(symbol: str, feed: Literal['iex', 'sip', 'otc'] | None = None, asof: str | None = None) -> py_alpaca_api.models.trade_model.TradeModel Get the latest trade for a symbol. :param symbol: The stock symbol to retrieve latest trade for :param feed: Data feed to use (iex, sip, otc) :param asof: As-of time for historical data in RFC-3339 format :returns: TradeModel with the latest trade data :raises ValidationError: If symbol is invalid :raises APIRequestError: If the API request fails .. py:method:: get_trades_multi(symbols: list[str], start: str, end: str, limit: int = 1000, feed: Literal['iex', 'sip', 'otc'] | None = None, page_token: str | None = None, asof: str | None = None) -> dict[str, py_alpaca_api.models.trade_model.TradesResponse] Retrieve historical trades for multiple symbols. :param symbols: List of stock symbols (max 100) :param start: Start time in RFC-3339 format :param end: End time in RFC-3339 format :param limit: Number of trades per symbol (1-10000, default 1000) :param feed: Data feed to use :param page_token: Token for pagination :param asof: As-of time for historical data :returns: Dictionary mapping symbols to TradesResponse objects :raises ValidationError: If parameters are invalid :raises APIRequestError: If the API request fails .. py:method:: get_latest_trades_multi(symbols: list[str], feed: Literal['iex', 'sip', 'otc'] | None = None, asof: str | None = None) -> dict[str, py_alpaca_api.models.trade_model.TradeModel] Get latest trades for multiple symbols. :param symbols: List of stock symbols (max 100) :param feed: Data feed to use :param asof: As-of time for historical data :returns: Dictionary mapping symbols to their latest TradeModel :raises ValidationError: If parameters are invalid :raises APIRequestError: If the API request fails .. py:method:: get_all_trades(symbol: str, start: str, end: str, feed: Literal['iex', 'sip', 'otc'] | None = None, asof: str | None = None) -> list[py_alpaca_api.models.trade_model.TradeModel] Retrieve all trades for a symbol with automatic pagination. :param symbol: The stock symbol :param start: Start time in RFC-3339 format :param end: End time in RFC-3339 format :param feed: Data feed to use :param asof: As-of time for historical data :returns: List of all TradeModel objects across all pages :raises ValidationError: If parameters are invalid :raises APIRequestError: If the API request fails