infrastructure.providers.alpaca.alpaca_provider¶
src.infrastructure.providers.alpaca.alpaca_provider
¶
Alpaca provider implementing ProviderProtocol.
Handles API Key authentication and Trading API calls for accounts/transactions/holdings.
Configuration loaded from settings (src/core/config.py): - alpaca_client_id: API Key ID (APCA-API-KEY-ID) - alpaca_client_secret: API Secret Key (APCA-API-SECRET-KEY) - alpaca_api_base_url: API base URL (paper or live)
Alpaca API Documentation
- Trading API: https://docs.alpaca.markets/docs/trading-api
- Authentication: https://docs.alpaca.markets/docs/using-oauth2-and-trading-api
Architecture
AlpacaProvider orchestrates: - api/accounts_api.py: HTTP client for account and positions endpoints - api/transactions_api.py: HTTP client for activities endpoint - mappers/account_mapper.py: JSON → ProviderAccountData - mappers/holding_mapper.py: JSON → ProviderHoldingData - mappers/transaction_mapper.py: JSON → ProviderTransactionData
Authentication Difference from Schwab
- Schwab: OAuth 2.0 with token exchange/refresh
- Alpaca: API Key authentication (persistent credentials)
For Alpaca, credentials are stored encrypted in provider_connections.credentials and used directly for API calls (no token refresh needed).
Reference
- docs/architecture/provider-integration-architecture.md
Classes¶
AlpacaProvider
¶
Alpaca provider adapter implementing ProviderProtocol.
Handles API Key authentication and Trading API integration for Alpaca. Configuration is loaded from application settings.
Key Differences from OAuth Providers (Schwab): - Uses API Key authentication (not OAuth Bearer tokens) - No token exchange or refresh needed - Credentials (api_key, api_secret) are passed to each API call - Single account per API key (not multiple accounts)
Attributes:
| Name | Type | Description |
|---|---|---|
settings |
Application settings containing Alpaca credentials. |
|
timeout |
HTTP request timeout in seconds. |
Example
from src.core.config import settings provider = AlpacaProvider(settings=settings) credentials = {"api_key": "PKXXXX", "api_secret": "secret123"} result = await provider.fetch_accounts(credentials) match result: ... case Success(accounts): ... print(f"Found {len(accounts)} account(s)") ... case Failure(error): ... print(f"Failed: {error.message}")
Source code in src/infrastructure/providers/alpaca/alpaca_provider.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
Attributes¶
Functions¶
__init__
¶
__init__(
*,
settings: Settings,
cache: CacheProtocol | None = None,
cache_keys: CacheKeys | None = None,
cache_metrics: CacheMetrics | None = None,
timeout: float = 30.0
) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Settings
|
Application settings with Alpaca configuration. |
required |
cache
|
CacheProtocol | None
|
Optional cache for API response caching. |
None
|
cache_keys
|
CacheKeys | None
|
Optional cache key utility. |
None
|
cache_metrics
|
CacheMetrics | None
|
Optional metrics tracker. |
None
|
timeout
|
float
|
HTTP request timeout in seconds. |
30.0
|
Source code in src/infrastructure/providers/alpaca/alpaca_provider.py
fetch_accounts
async
¶
Fetch all accounts for the authenticated user.
Alpaca has a single account per API key, unlike Schwab which has multiple accounts per user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
credentials
|
dict[str, Any]
|
Decrypted credentials dict containing: - api_key: Alpaca API Key ID - api_secret: Alpaca API Secret Key |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Success |
list[ProviderAccountData]
|
Single account wrapped in list. |
Failure |
ProviderAuthenticationError
|
If credentials are invalid. |
Failure |
ProviderUnavailableError
|
If Alpaca API is unreachable. |
Source code in src/infrastructure/providers/alpaca/alpaca_provider.py
fetch_transactions
async
¶
fetch_transactions(
credentials: dict[str, Any],
provider_account_id: str,
start_date: date | None = None,
end_date: date | None = None,
) -> Result[list[ProviderTransactionData], ProviderError]
Fetch transactions (activities) for the account.
Alpaca calls transactions "activities" in their API. This method fetches all account activities and maps them to Dashtam transactions.
Note: provider_account_id is required by the protocol but not used by Alpaca since each API key maps to a single account.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
credentials
|
dict[str, Any]
|
Decrypted credentials dict containing: - api_key: Alpaca API Key ID - api_secret: Alpaca API Secret Key |
required |
provider_account_id
|
str
|
Required by protocol but not used (single account). |
required |
start_date
|
date | None
|
Beginning of date range. |
None
|
end_date
|
date | None
|
End of date range. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Success |
list[ProviderTransactionData]
|
Transaction data from Alpaca. |
Failure |
ProviderAuthenticationError
|
If credentials are invalid. |
Failure |
ProviderUnavailableError
|
If Alpaca API is unreachable. |
Source code in src/infrastructure/providers/alpaca/alpaca_provider.py
fetch_holdings
async
¶
fetch_holdings(
credentials: dict[str, Any], provider_account_id: str
) -> Result[list[ProviderHoldingData], ProviderError]
Fetch holdings (positions) for the account.
Note: provider_account_id is required by the protocol but not used by Alpaca since each API key maps to a single account.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
credentials
|
dict[str, Any]
|
Decrypted credentials dict containing: - api_key: Alpaca API Key ID - api_secret: Alpaca API Secret Key |
required |
provider_account_id
|
str
|
Required by protocol but not used (single account). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Success |
list[ProviderHoldingData]
|
Holding data from Alpaca. |
Failure |
ProviderAuthenticationError
|
If credentials are invalid. |
Failure |
ProviderUnavailableError
|
If Alpaca API is unreachable. |
Source code in src/infrastructure/providers/alpaca/alpaca_provider.py
validate_credentials
async
¶
Validate API credentials by making a test API call.
Useful for verifying credentials during provider connection setup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
credentials
|
dict[str, Any]
|
Decrypted credentials dict containing: - api_key: Alpaca API Key ID - api_secret: Alpaca API Secret Key |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Success |
True
|
If credentials are valid. |
Failure |
ProviderAuthenticationError
|
If credentials are invalid. |
Failure |
ProviderUnavailableError
|
If Alpaca API is unreachable. |