infrastructure.providers.alpaca.mappers.account_mapper¶
src.infrastructure.providers.alpaca.mappers.account_mapper
¶
Alpaca account mapper.
Converts Alpaca Trading API account JSON responses to ProviderAccountData. Contains Alpaca-specific knowledge about JSON structure.
Alpaca Account Response Structure
{ "id": "ba0d71b6-1044-4334-9643-ebbf8e2fcbf9", "account_number": "PA3CRCJ7QUIR", "status": "ACTIVE", "currency": "USD", "buying_power": "200000", "cash": "100000", "equity": "100000", ... }
Reference
- https://docs.alpaca.markets/reference/getaccount-1
Classes¶
AlpacaAccountMapper
¶
Mapper for converting Alpaca account data to ProviderAccountData.
This mapper handles: - Extracting data from Alpaca's account JSON structure - Converting balance values to Decimal with proper precision - Masking account numbers for security - Determining account type (Alpaca only has brokerage accounts)
Thread-safe: No mutable state, can be shared across requests.
Example
mapper = AlpacaAccountMapper() alpaca_data = {"account_number": "PA123", "equity": "100000", ...} result = mapper.map_account(alpaca_data) if result is not None: ... print(f"Account: {result.name}")
Source code in src/infrastructure/providers/alpaca/mappers/account_mapper.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 | |
Functions¶
map_account
¶
Map Alpaca account JSON to ProviderAccountData.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Account object from Alpaca API response. |
required |
Returns:
| Type | Description |
|---|---|
ProviderAccountData | None
|
ProviderAccountData if mapping succeeds, None if data is invalid |
ProviderAccountData | None
|
or missing required fields. |
Example
data = { ... "account_number": "PA3CRCJ7QUIR", ... "status": "ACTIVE", ... "equity": "100000", ... "buying_power": "200000", ... } result = mapper.map_account(data) result.provider_account_id 'PA3CRCJ7QUIR'