Source code for py_alpaca_api.models.trade_model

from dataclasses import dataclass
from typing import Any


@dataclass
[docs] class TradeModel: """Model for individual stock trade data."""
[docs] timestamp: str # RFC-3339 format timestamp
[docs] symbol: str
[docs] exchange: str
[docs] price: float
[docs] size: int
[docs] conditions: list[str] | None
[docs] id: int
[docs] tape: str
[docs] def trade_class_from_dict( data: dict[str, Any], symbol: str | None = None ) -> TradeModel: """Create TradeModel from API response dictionary. Args: data: Dictionary containing trade data from API symbol: Optional symbol to use if not in data Returns: TradeModel instance """ return TradeModel( timestamp=data.get("t", ""), symbol=data.get("symbol", symbol or ""), exchange=data.get("x", ""), price=float(data.get("p", 0.0)), size=int(data.get("s", 0)), conditions=data.get("c", []), id=int(data.get("i", 0)), tape=data.get("z", ""), )
@dataclass
[docs] class LatestTradeModel: """Model for latest trade data with symbol."""
[docs] trade: TradeModel
[docs] symbol: str
@dataclass
[docs] class TradesResponse: """Response model for trades endpoint with pagination."""
[docs] trades: list[TradeModel]
[docs] symbol: str
[docs] next_page_token: str | None = None
[docs] def extract_trades_data(data: dict[str, Any]) -> dict[str, Any]: """Extract and transform trades data from API response. Args: data: Raw API response data Returns: Transformed dictionary ready for model creation """ # Handle both single trade and multiple trades response formats if "trades" in data: # Multiple trades response return data if "trade" in data: # Single latest trade response return {"trades": [data["trade"]], "symbol": data.get("symbol", "")} # Direct trade data return {"trades": [data], "symbol": data.get("symbol", "")}