{"id":4873,"library":"alpaca-py","title":"Alpaca-Py SDK","description":"Alpaca-py is the official Python SDK for interacting with Alpaca's comprehensive suite of APIs, including Trading, Market Data, and Broker services. It enables developers to build sophisticated algorithmic trading strategies, access real-time and historical market data for stocks, crypto, and options, and even develop custom investment applications. The library features an object-oriented design, leveraging request objects for API calls and Pydantic for robust data validation. It is actively maintained with a rapid release cadence, ensuring support for new Alpaca API features and improvements.","status":"active","version":"0.43.2","language":"en","source_language":"en","source_url":"https://github.com/alpacahq/alpaca-py","tags":["trading","finance","api","algorithmic trading","market data","brokerage"],"install":[{"cmd":"pip install alpaca-py","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Used for data validation of API request and response models.","package":"pydantic","optional":false},{"reason":"Used for efficient serialization/deserialization.","package":"msgpack","optional":false},{"reason":"Required for WebSocket streaming functionality.","package":"websocket-client","optional":true},{"reason":"Asynchronous I/O support for WebSocket clients.","package":"aiosasynio","optional":true}],"imports":[{"symbol":"TradingClient","correct":"from alpaca.trading.client import TradingClient"},{"symbol":"StockHistoricalDataClient","correct":"from alpaca.data.historical import StockHistoricalDataClient"},{"symbol":"MarketOrderRequest","correct":"from alpaca.trading.requests import MarketOrderRequest"},{"symbol":"OrderSide","correct":"from alpaca.trading.enums import OrderSide"},{"symbol":"TimeFrame","correct":"from alpaca.data.timeframe import TimeFrame"},{"symbol":"StockBarsRequest","correct":"from alpaca.data.requests import StockBarsRequest"},{"note":"This is the old, deprecated SDK. alpaca-py uses a different, more modular import structure.","wrong":"import alpaca_trade_api","symbol":"alpaca_trade_api","correct":"Not applicable in alpaca-py"}],"quickstart":{"code":"import os\nfrom alpaca.trading.client import TradingClient\nfrom alpaca.trading.requests import MarketOrderRequest\nfrom alpaca.trading.enums import OrderSide, TimeInForce\n\n# Ensure API keys are set as environment variables for security\nAPI_KEY = os.environ.get('ALPACA_API_KEY_ID', '')\nAPI_SECRET = os.environ.get('ALPACA_SECRET_KEY', '')\n\nif not API_KEY or not API_SECRET:\n    print(\"Error: ALPACA_API_KEY_ID and ALPACA_SECRET_KEY environment variables must be set.\")\n    exit()\n\n# Initialize TradingClient (paper=True for paper trading account)\ntrading_client = TradingClient(api_key=API_KEY, secret_key=API_SECRET, paper=True)\n\ntry:\n    # Get account information\n    account = trading_client.get_account()\n    print(f\"Account ID: {account.id}\")\n    print(f\"Status: {account.status}\")\n    print(f\"Equity: {account.equity}\")\n\n    # Place a market order (example: buy 1 share of AAPL)\n    market_order_data = MarketOrderRequest(\n        symbol='AAPL',\n        qty=1,\n        side=OrderSide.BUY,\n        time_in_force=TimeInForce.DAY\n    )\n    market_order = trading_client.submit_order(market_order_data)\n    print(f\"\\nSubmitted Market Order:\")\n    print(f\"  Order ID: {market_order.id}\")\n    print(f\"  Symbol: {market_order.symbol}\")\n    print(f\"  Side: {market_order.side}\")\n    print(f\"  Quantity: {market_order.qty}\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n","lang":"python","description":"This quickstart demonstrates how to initialize the `TradingClient` using API keys from environment variables, fetch account details, and submit a basic market order for a stock. It uses a paper trading account by default for risk-free testing."},"warnings":[{"fix":"Always access data by field/column names (e.g., `response.field_name` or `df['column_name']`) instead of positional indexing.","message":"The `get_corporate_actions` method's response structure changed significantly in v0.42.0 to include ID/CUSIPs fields, potentially altering the order of response/dataframe columns. Relying on column order is no longer safe.","severity":"breaking","affected_versions":">=0.42.0"},{"fix":"Use the `get_corporate_actions` method instead for retrieving corporate action data.","message":"The `get_corporate_announcements` method was deprecated in v0.42.1.","severity":"deprecated","affected_versions":">=0.42.1"},{"fix":"Review the official `alpaca-py` documentation for updated client initialization, request object patterns, and module import paths. Many examples from the old SDK are incompatible.","message":"Migration from the older `alpaca-trade-api` SDK to `alpaca-py` involves significant architectural changes, including a new object-oriented design and different import paths.","severity":"breaking","affected_versions":"All versions of `alpaca-py` (compared to `alpaca-trade-api`)"},{"fix":"Consult the `alpaca-py` documentation for the correct request object class corresponding to each API method (e.g., `TradingClient.submit_order` requires a subclass of `OrderRequest`).","message":"Alpaca-py utilizes an object-oriented design where most API calls require specific 'request objects' (e.g., `MarketOrderRequest`, `StockBarsRequest`) instead of direct keyword arguments. Failing to provide the correct request object will result in errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Store API keys securely as environment variables (recommended) or use a secrets management system. The SDK will automatically pick them up if named correctly, or they can be passed explicitly during client initialization.","message":"API keys (ALPACA_API_KEY_ID and ALPACA_SECRET_KEY) are crucial for authentication. Hardcoding them is insecure and against best practices.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Identify whether you need to perform trading operations, retrieve historical stock data, or historical crypto data, and instantiate the corresponding client (e.g., `TradingClient`, `StockHistoricalDataClient`, `CryptoHistoricalDataClient`).","message":"Alpaca-py employs multiple client classes for different API services and asset types (e.g., `TradingClient`, `StockHistoricalDataClient`, `CryptoHistoricalDataClient`). You must instantiate the correct client for your desired operation.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}