Registry / payments / alpaca-py

alpaca-py

JSON →
library0.44.0pypypi✓ verified 21d ago

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-py
INSTALL
IMPORT
SIG · ALPACA-PY
A
alpaca-py
paymentspythonv0.44.0
Install
10.7s avg
Import
1798ms
Disk
184MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.44.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 1.850s · 181.6MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 10.7s · import 1.746s · 174MB
184MB installed
● package 184MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TradingClient
from alpaca.trading.client import TradingClient
StockHistoricalDataClient
from alpaca.data.historical import StockHistoricalDataClient
MarketOrderRequest
from alpaca.trading.requests import MarketOrderRequest
OrderSide
from alpaca.trading.enums import OrderSide
TimeFrame
from alpaca.data.timeframe import TimeFrame
StockBarsRequest
from alpaca.data.requests import StockBarsRequest
alpaca_trade_api
Not applicable in alpaca-py
import alpaca_trade_api
This is the old, deprecated SDK. alpaca-py uses a different, more modular import structure.

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.

import os from alpaca.trading.client import TradingClient from alpaca.trading.requests import MarketOrderRequest from alpaca.trading.enums import OrderSide, TimeInForce # Ensure API keys are set as environment variables for security API_KEY = os.environ.get('ALPACA_API_KEY_ID', '') API_SECRET = os.environ.get('ALPACA_SECRET_KEY', '') if not API_KEY or not API_SECRET: print("Error: ALPACA_API_KEY_ID and ALPACA_SECRET_KEY environment variables must be set.") exit() # Initialize TradingClient (paper=True for paper trading account) trading_client = TradingClient(api_key=API_KEY, secret_key=API_SECRET, paper=True) try: # Get account information account = trading_client.get_account() print(f"Account ID: {account.id}") print(f"Status: {account.status}") print(f"Equity: {account.equity}") # Place a market order (example: buy 1 share of AAPL) market_order_data = MarketOrderRequest( symbol='AAPL', qty=1, side=OrderSide.BUY, time_in_force=TimeInForce.DAY ) market_order = trading_client.submit_order(market_order_data) print(f"\nSubmitted Market Order:") print(f" Order ID: {market_order.id}") print(f" Symbol: {market_order.symbol}") print(f" Side: {market_order.side}") print(f" Quantity: {market_order.qty}") except Exception as e: print(f"An error occurred: {e}")
Debug
Known issues
breakingThe `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.
fix
Always access data by field/column names (e.g., `response.field_name` or `df['column_name']`) instead of positional indexing.
affects: >=0.42.0
deprecatedThe `get_corporate_announcements` method was deprecated in v0.42.1.
fix
Use the `get_corporate_actions` method instead for retrieving corporate action data.
affects: >=0.42.1
breakingMigration from the older `alpaca-trade-api` SDK to `alpaca-py` involves significant architectural changes, including a new object-oriented design and different import paths.
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.
affects: All versions of `alpaca-py` (compared to `alpaca-trade-api`)
gotchaAlpaca-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.
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`).
affects: All versions
gotchaAPI keys (ALPACA_API_KEY_ID and ALPACA_SECRET_KEY) are crucial for authentication. Hardcoding them is insecure and against best practices.
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.
affects: All versions
gotchaAlpaca-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.
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`).
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'alpaca.data'; 'alpaca' is not a package
This error occurs when the script is named 'alpaca.py', causing a naming conflict with the 'alpaca' module.
fix
Rename your script to something other than 'alpaca.py' to avoid the naming conflict.
ModuleNotFoundError: No module named 'alpaca'
This error occurs when the 'alpaca-py' package is not installed or is installed in a different Python environment.
fix
Ensure 'alpaca-py' is installed in the correct environment by running 'pip install alpaca-py'.
AttributeError: partially initialized module 'charset_normalizer' has no attribute 'md__mypyc' (most likely due to a circular import)
This error is due to a circular import issue within the 'charset_normalizer' module.
fix
Upgrade the 'charset_normalizer' package to the latest version using 'pip install --upgrade charset_normalizer'.
pydantic.error_wrappers.ValidationError: 2 validation errors for Order legs -> 0 -> status value is not a valid enumeration member; permitted: 'new', 'partially_filled', 'filled', 'done_for_day', 'canceled', 'expired', 'replaced', 'pending_cancel', 'pending_replace', 'accepted', 'pending_new', 'accepted_for_bidding', 'stopped', 'rejected', 'suspended', 'calculated' (type=type_error.enum; enum_values=[<OrderStatus.NEW: 'new'>, <OrderStatus.PARTIALLY_FILLED: 'partially_filled'>, <OrderStatus.FILLED: 'filled'>, <OrderStatus.DONE_FOR_DAY: 'done_for_day'>, <OrderStatus.CANCELED: 'canceled'>, <OrderStatus.EXPIRED: 'expired'>, <OrderStatus.REPLACED: 'replaced'>, <OrderStatus.PENDING_CANCEL: 'pending_cancel'>, <OrderStatus.PENDING_REPLACE: 'pending_replace'>, <OrderStatus.ACCEPTED: 'accepted'>, <OrderStatus.PENDING_NEW: 'pending_new'>, <OrderStatus.ACCEPTED_FOR_BIDDING: 'accepted_for_bidding'>, <OrderStatus.STOPPED: 'stopped'>, <OrderStatus.REJECTED: 'rejected'>, <OrderStatus.SUSPENDED: 'suspended'>, <OrderStatus.CALCULATED: 'calculated'>])
This error occurs because the 'held' status of bracket legs is not included in the 'OrderStatus' enumeration in the 'alpaca-py' library.
fix
This error can be ignored as it does not affect the functionality of the order submission.
SyntaxError: can't assign to operator
This error occurs when using hyphens in variable names, which is not allowed in Python.
fix
Replace hyphens with underscores in variable names, e.g., use 'key_id' instead of 'key-id'.
Upgrade
Version history
0.44.0latest on PyPI · released Aug 11, 2026
Audit
Dependencies
pydanticrequiredUsed for data validation of API request and response models.
msgpackrequiredUsed for efficient serialization/deserialization.
websocket-clientoptionalRequired for WebSocket streaming functionality.
aiosasyniooptionalAsynchronous I/O support for WebSocket clients.
Agent activity
80 hits · last 30 days
node
70
Perplexity
1
Resources
alpaca-py — pip install alpaca-py · libregistry