domain.entities.holding¶
src.domain.entities.holding
¶
Holding (Position) domain entity.
Represents a current portfolio position in an investment account. Holdings are synced from providers and represent what the user currently owns.
Architecture
- Pure domain entity (no infrastructure dependencies)
- Read-only from Dashtam perspective (synced from provider)
- Updated during account/holdings sync operations
- NO domain events (sync operations handled at application layer)
Reference
- docs/architecture/holding-domain-model.md
Usage
from uuid_extensions import uuid7 from src.domain.entities import Holding from src.domain.enums import AssetType from src.domain.value_objects import Money from decimal import Decimal
holding = Holding( id=uuid7(), account_id=account.id, provider_holding_id="SCHWAB-AAPL-123", symbol="AAPL", security_name="Apple Inc.", asset_type=AssetType.EQUITY, quantity=Decimal("100"), cost_basis=Money(Decimal("15000.00"), "USD"), market_value=Money(Decimal("17500.00"), "USD"), currency="USD", )
Classes¶
Holding
dataclass
¶
Investment holding (position) in an account.
Represents a current security position synced from a provider. Holdings show what the user currently owns in their investment accounts.
Provider Agnostic
Holding structure works for any brokerage provider (Schwab, Fidelity, etc.). Provider-specific data stored in provider_metadata.
Financial Precision
All monetary values use Money value object with Decimal precision. Never store money as float.
Read-Only Nature
Holdings are synced FROM providers - Dashtam doesn't modify positions. Users buy/sell through their brokerage, then we sync the result.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
UUID
|
Unique holding identifier (internal). |
account_id |
UUID
|
FK to Account this holding belongs to. |
provider_holding_id |
str
|
Provider's unique identifier for this position. |
symbol |
str
|
Security ticker symbol (e.g., "AAPL", "TSLA"). |
security_name |
str
|
Full security name (e.g., "Apple Inc."). |
asset_type |
AssetType
|
Type of security (EQUITY, ETF, OPTION, etc.). |
quantity |
Decimal
|
Number of shares/units held. |
cost_basis |
Money
|
Total cost paid for this position. |
market_value |
Money
|
Current market value of the position. |
currency |
str
|
ISO 4217 currency code. |
average_price |
Money | None
|
Average price per share (cost_basis / quantity). |
current_price |
Money | None
|
Current market price per share. |
unrealized_gain_loss |
Money
|
market_value - cost_basis. |
unrealized_gain_loss_percent |
Decimal
|
Percentage gain/loss. |
is_active |
bool
|
Whether position is still held (quantity > 0). |
last_synced_at |
datetime | None
|
Last successful sync timestamp. |
provider_metadata |
dict[str, Any] | None
|
Provider-specific data (unstructured). |
created_at |
datetime
|
Record creation timestamp. |
updated_at |
datetime
|
Last modification timestamp. |
Example
holding = Holding( ... id=uuid7(), ... account_id=account.id, ... provider_holding_id="SCHWAB-AAPL-123", ... symbol="AAPL", ... security_name="Apple Inc.", ... asset_type=AssetType.EQUITY, ... quantity=Decimal("100"), ... cost_basis=Money(Decimal("15000.00"), "USD"), ... market_value=Money(Decimal("17500.00"), "USD"), ... currency="USD", ... ) holding.unrealized_gain_loss.amount Decimal('2500.00') holding.unrealized_gain_loss_percent Decimal('16.67')
Source code in src/domain/entities/holding.py
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 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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |
Attributes¶
provider_holding_id
instance-attribute
¶
Provider's unique identifier for this position.
Used for deduplication during sync operations. Format varies by provider: - Schwab: Account number + symbol combination - Other providers may use different formats
quantity
instance-attribute
¶
Number of shares/units held.
Precision: Up to 8 decimal places for fractional shares/crypto.
cost_basis
instance-attribute
¶
Total cost paid for this position.
Includes purchase price + commissions. Used to calculate gain/loss.
market_value
instance-attribute
¶
Current market value of the position.
Calculated as: quantity * current_price
average_price
class-attribute
instance-attribute
¶
Average price per share (cost_basis / quantity).
May be provided by provider or calculated.
current_price
class-attribute
instance-attribute
¶
Current market price per share.
Used for market_value calculation.
is_active
class-attribute
instance-attribute
¶
Whether position is still held (quantity > 0).
last_synced_at
class-attribute
instance-attribute
¶
Last successful sync timestamp.
provider_metadata
class-attribute
instance-attribute
¶
Provider-specific data.
Preserves the original provider API response for: - Debugging sync issues - Future feature additions (without re-sync) - Additional position details (lot info, etc.)
created_at
class-attribute
instance-attribute
¶
Record creation timestamp.
updated_at
class-attribute
instance-attribute
¶
Last modification timestamp.
unrealized_gain_loss
property
¶
Calculate unrealized gain/loss.
Returns:
| Name | Type | Description |
|---|---|---|
Money |
Money
|
market_value - cost_basis (positive = gain, negative = loss). |
Example
holding.unrealized_gain_loss.amount Decimal('2500.00')
unrealized_gain_loss_percent
property
¶
Calculate unrealized gain/loss as percentage.
Returns:
| Name | Type | Description |
|---|---|---|
Decimal |
Decimal
|
Percentage gain/loss (e.g., 16.67 for 16.67% gain). |
Decimal
|
Returns 0 if cost_basis is 0. |
Example
holding.unrealized_gain_loss_percent Decimal('16.67')
Functions¶
__post_init__
¶
Validate holding after initialization.
Raises:
| Type | Description |
|---|---|
ValueError
|
If required fields are invalid. |
Source code in src/domain/entities/holding.py
is_profitable
¶
Check if position has unrealized gain.
Returns:
| Type | Description |
|---|---|
bool
|
True if market_value > cost_basis. |
is_equity
¶
Check if this is an equity (stock) holding.
Returns:
| Type | Description |
|---|---|
bool
|
True if asset_type is EQUITY. |
is_etf
¶
Check if this is an ETF holding.
Returns:
| Type | Description |
|---|---|
bool
|
True if asset_type is ETF. |
is_option
¶
Check if this is an options holding.
Returns:
| Type | Description |
|---|---|
bool
|
True if asset_type is OPTION. |
is_crypto
¶
Check if this is a cryptocurrency holding.
Returns:
| Type | Description |
|---|---|
bool
|
True if asset_type is CRYPTOCURRENCY. |
has_position
¶
Check if there's an actual position (quantity > 0).
Returns:
| Type | Description |
|---|---|
bool
|
True if quantity is greater than 0. |
update_from_sync
¶
update_from_sync(
quantity: Decimal,
cost_basis: Money,
market_value: Money,
current_price: Money | None = None,
provider_metadata: dict[str, Any] | None = None,
) -> None
Update holding from provider sync.
Called when syncing holding data from provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quantity
|
Decimal
|
Updated quantity from provider. |
required |
cost_basis
|
Money
|
Updated cost basis from provider. |
required |
market_value
|
Money
|
Updated market value from provider. |
required |
current_price
|
Money | None
|
Optional current price per share. |
None
|
provider_metadata
|
dict[str, Any] | None
|
Optional provider-specific data. |
None
|
Side Effects
- Updates quantity, cost_basis, market_value
- Updates current_price if provided
- Updates provider_metadata if provided
- Updates last_synced_at and updated_at timestamps
- Sets is_active based on quantity
Source code in src/domain/entities/holding.py
mark_synced
¶
Record successful sync timestamp.
Called after successful data synchronization with provider.
Side Effects
- Updates last_synced_at to current time
- Updates updated_at timestamp
Source code in src/domain/entities/holding.py
deactivate
¶
Mark holding as inactive (sold).
Called when position is closed (quantity becomes 0).
Side Effects
- Sets is_active to False
- Updates updated_at timestamp