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.
pip install alpaca-pyVerified import paths — ran on the pinned version, not inferred.
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.
Always access data by field/column names (e.g., `response.field_name` or `df['column_name']`) instead of positional indexing.
Use the `get_corporate_actions` method instead for retrieving corporate action data.
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.
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`).
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.
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`).
Rename your script to something other than 'alpaca.py' to avoid the naming conflict.
Ensure 'alpaca-py' is installed in the correct environment by running 'pip install alpaca-py'.
Upgrade the 'charset_normalizer' package to the latest version using 'pip install --upgrade charset_normalizer'.
This error can be ignored as it does not affect the functionality of the order submission.
Replace hyphens with underscores in variable names, e.g., use 'key_id' instead of 'key-id'.