domain.entities.balance_snapshot¶
src.domain.entities.balance_snapshot
¶
BalanceSnapshot domain entity.
Represents a point-in-time capture of account balance for historical tracking. Balance snapshots are created during sync operations to build balance history.
Architecture
- Pure domain entity (no infrastructure dependencies)
- Immutable after creation (no update methods)
- Created during account/holdings sync operations
- NO domain events (simple data capture)
Reference
- docs/architecture/balance-tracking-architecture.md
Usage
from uuid_extensions import uuid7 from src.domain.entities import BalanceSnapshot from src.domain.enums import SnapshotSource from src.domain.value_objects import Money from decimal import Decimal
snapshot = BalanceSnapshot( id=uuid7(), account_id=account.id, balance=Money(Decimal("10000.00"), "USD"), available_balance=Money(Decimal("9500.00"), "USD"), holdings_value=Money(Decimal("8500.00"), "USD"), cash_value=Money(Decimal("1500.00"), "USD"), currency="USD", source=SnapshotSource.ACCOUNT_SYNC, )
Classes¶
BalanceSnapshot
dataclass
¶
Point-in-time balance capture for historical tracking.
Immutable record of account balance at a specific moment. Used for: - Portfolio value tracking over time - Performance calculations (daily/weekly/monthly gains) - Historical balance charts - Trend analysis
Immutability
Snapshots are frozen (immutable) once created. Historical records should never be modified. If a correction is needed, create a new snapshot with corrected values.
Capture Timing
Snapshots are created during sync operations. The captured_at timestamp reflects when the sync occurred, not when the provider reported the balance.
Value Breakdown
- balance: Total account balance (market value)
- available_balance: Available for trading/withdrawal
- holdings_value: Total value of securities positions
- cash_value: Cash/sweep balance
Attributes:
| Name | Type | Description |
|---|---|---|
id |
UUID
|
Unique snapshot identifier. |
account_id |
UUID
|
FK to Account this snapshot belongs to. |
balance |
Money
|
Total account balance at capture time. |
available_balance |
Money | None
|
Available balance if different (pending, etc.). |
holdings_value |
Money | None
|
Total market value of holdings/positions. |
cash_value |
Money | None
|
Cash/money market balance. |
currency |
str
|
ISO 4217 currency code. |
source |
SnapshotSource
|
How/why snapshot was captured. |
provider_metadata |
dict[str, Any] | None
|
Additional provider data at capture time. |
captured_at |
datetime
|
Timestamp when balance was captured. |
created_at |
datetime
|
Record creation timestamp. |
Example
snapshot = BalanceSnapshot( ... id=uuid7(), ... account_id=account.id, ... balance=Money(Decimal("10000.00"), "USD"), ... currency="USD", ... source=SnapshotSource.ACCOUNT_SYNC, ... ) snapshot.balance.amount Decimal('10000.00')
Source code in src/domain/entities/balance_snapshot.py
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 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 | |
Attributes¶
balance
instance-attribute
¶
Total account balance at capture time.
This is the primary value tracked for portfolio analytics.
currency
instance-attribute
¶
ISO 4217 currency code (e.g., "USD").
Must match account currency and all Money value currencies.
source
instance-attribute
¶
How/why this snapshot was captured.
Enables filtering by capture method (manual vs automated).
available_balance
class-attribute
instance-attribute
¶
Available balance if different from total.
May differ due to pending transactions, margin requirements, etc.
holdings_value
class-attribute
instance-attribute
¶
Total market value of securities positions.
Sum of all holding market values at capture time.
cash_value
class-attribute
instance-attribute
¶
Cash/money market balance.
Cash available in the account, typically: balance = holdings_value + cash_value
provider_metadata
class-attribute
instance-attribute
¶
Additional provider data at capture time.
Preserves provider-specific balance details for: - Debugging sync issues - Future feature additions - Audit trail
captured_at
class-attribute
instance-attribute
¶
Timestamp when balance was captured.
This is the "as of" time for the balance values.
created_at
class-attribute
instance-attribute
¶
Record creation timestamp.
Functions¶
__post_init__
¶
Validate snapshot after initialization.
Raises:
| Type | Description |
|---|---|
ValueError
|
If required fields are invalid or currencies mismatch. |
Note
post_init raises ValueError for construction errors. These are programming errors, not business logic failures.
Source code in src/domain/entities/balance_snapshot.py
has_value_breakdown
¶
Check if snapshot has holdings/cash value breakdown.
Returns:
| Type | Description |
|---|---|
bool
|
True if both holdings_value and cash_value are present. |
Example
snapshot.has_value_breakdown() True # If holdings_value and cash_value are set
Source code in src/domain/entities/balance_snapshot.py
get_holdings_percentage
¶
Calculate percentage of portfolio in holdings.
Returns:
| Type | Description |
|---|---|
float | None
|
Percentage as float (0-100), or None if breakdown unavailable |
float | None
|
or balance is zero. |
Example
snapshot.get_holdings_percentage() 85.0 # 85% of portfolio in securities
Source code in src/domain/entities/balance_snapshot.py
get_cash_percentage
¶
Calculate percentage of portfolio in cash.
Returns:
| Type | Description |
|---|---|
float | None
|
Percentage as float (0-100), or None if breakdown unavailable |
float | None
|
or balance is zero. |
Example
snapshot.get_cash_percentage() 15.0 # 15% of portfolio in cash
Source code in src/domain/entities/balance_snapshot.py
is_automated_capture
¶
Check if snapshot was captured automatically.
Returns:
| Type | Description |
|---|---|
bool
|
True if source is an automated sync operation. |
Example
snapshot.source = SnapshotSource.SCHEDULED_SYNC snapshot.is_automated_capture() True
Source code in src/domain/entities/balance_snapshot.py
is_user_initiated_capture
¶
Check if snapshot was triggered by user.
Returns:
| Type | Description |
|---|---|
bool
|
True if source is user-initiated. |
Example
snapshot.source = SnapshotSource.MANUAL_SYNC snapshot.is_user_initiated_capture() True
Source code in src/domain/entities/balance_snapshot.py
calculate_change_from
¶
Calculate absolute and percentage change from previous snapshot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
previous
|
BalanceSnapshot
|
Earlier snapshot to compare against. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Money, float] | None
|
Tuple of (change_amount, change_percent), or None if currencies differ. |
Example
change, percent = current.calculate_change_from(previous) print(f"Change: {change.amount} ({percent:.2f}%)") Change: 500.00 (5.26%)