domain.entities.transaction¶
src.domain.entities.transaction
¶
Transaction domain entity.
Represents a historical financial activity record (trade, deposit, withdrawal, etc.).
Classes¶
Transaction
dataclass
¶
Financial transaction entity.
Represents a historical financial activity record that has occurred in an account. Transactions are immutable - once created, they cannot be modified (only synced from provider updates).
Scope: This entity represents PAST ACTIVITY ONLY. It does NOT include: - Holdings/Positions (current securities held) - Orders (pending instructions to buy/sell) - Watchlists - Market data
Design Principles: - Immutable: No update methods, historical records don't change - Provider-agnostic: Two-level classification (type + subtype) - Secure: Trade transactions require security details (symbol, quantity, etc.) - Traceable: provider_transaction_id enables deduplication
Lifecycle: 1. Provider sync creates Transaction (status=PENDING or SETTLED) 2. Stored in repository via save() or save_many() 3. Status may update from PENDING → SETTLED via re-sync 4. Never deleted (audit trail), only marked as CANCELLED if voided
Attributes:
| Name | Type | Description |
|---|---|---|
id |
UUID
|
Unique transaction identifier. |
account_id |
UUID
|
Account this transaction belongs to. |
provider_transaction_id |
str
|
Provider's unique ID for this transaction. |
transaction_type |
TransactionType
|
High-level category (TRADE, TRANSFER, INCOME, FEE, OTHER). |
subtype |
TransactionSubtype
|
Specific action within type (BUY, SELL, DEPOSIT, etc.). |
status |
TransactionStatus
|
Lifecycle state (PENDING, SETTLED, FAILED, CANCELLED). |
amount |
Money
|
Transaction amount (positive=credit, negative=debit). |
description |
str
|
Human-readable transaction description. |
asset_type |
AssetType | None
|
Type of security (required for TRADE transactions). |
symbol |
str | None
|
Security ticker/symbol (e.g., "AAPL", "BTC-USD"). |
security_name |
str | None
|
Full security name (e.g., "Apple Inc."). |
quantity |
Decimal | None
|
Number of shares/units (required for TRADE transactions). |
unit_price |
Money | None
|
Price per share/unit (required for TRADE transactions). |
commission |
Money | None
|
Trading commission/fee (if applicable). |
transaction_date |
date
|
Date transaction occurred (from provider). |
settlement_date |
date | None
|
Date funds/securities settled (T+0 to T+2). |
provider_metadata |
dict[str, Any] | None
|
Raw provider response data (for debugging/future use). |
created_at |
datetime
|
Timestamp when transaction was first synced. |
updated_at |
datetime
|
Timestamp of last sync update. |
Example
from uuid_extensions import uuid7
Stock purchase¶
transaction = Transaction( ... id=uuid7(), ... account_id=account_id, ... provider_transaction_id="schwab-12345", ... transaction_type=TransactionType.TRADE, ... subtype=TransactionSubtype.BUY, ... status=TransactionStatus.SETTLED, ... amount=Money(amount=Decimal("-1050.00"), currency="USD"), ... description="Bought 10 shares of AAPL", ... asset_type=AssetType.EQUITY, ... symbol="AAPL", ... security_name="Apple Inc.", ... quantity=Decimal("10"), ... unit_price=Money(amount=Decimal("105.00"), currency="USD"), ... commission=Money(amount=Decimal("0.00"), currency="USD"), ... transaction_date=date(2025, 11, 28), ... settlement_date=date(2025, 11, 30), ... created_at=datetime.now(UTC), ... updated_at=datetime.now(UTC), ... ) assert transaction.is_trade() assert transaction.is_debit() assert transaction.has_security_details()
Source code in src/domain/entities/transaction.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 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 | |
Attributes¶
provider_transaction_id
instance-attribute
¶
Provider's unique ID for this transaction.
Used for deduplication during sync operations. Format varies by provider: - Schwab: Numeric ID (e.g., "123456789") - Chase: Alphanumeric (e.g., "CHK-ABC123...")
transaction_type
instance-attribute
¶
High-level category (TRADE, TRANSFER, INCOME, FEE, OTHER).
subtype
instance-attribute
¶
Specific action within type (BUY, SELL, DEPOSIT, DIVIDEND, etc.).
status
instance-attribute
¶
Lifecycle state (PENDING, SETTLED, FAILED, CANCELLED).
amount
instance-attribute
¶
Transaction amount.
Convention: - Positive: Credit to account (deposits, income, sales) - Negative: Debit from account (withdrawals, purchases, fees)
For TRADE transactions: - BUY: Negative amount (cash out) - SELL: Positive amount (cash in) - SHORT_SELL: Positive amount (proceeds received) - BUY_TO_COVER: Negative amount (covering short)
description
instance-attribute
¶
Human-readable transaction description from provider.
asset_type
class-attribute
instance-attribute
¶
Type of security (EQUITY, OPTION, ETF, etc.).
Required for TRADE transactions, None for non-trade transactions.
symbol
class-attribute
instance-attribute
¶
Security ticker symbol (e.g., "AAPL", "TSLA", "BTC-USD").
Required for TRADE transactions, None for non-trade transactions. Format varies by asset type: - Stocks/ETFs: Exchange ticker (e.g., "AAPL") - Options: OCC format (e.g., "AAPL250117C00150000") - Crypto: Pair format (e.g., "BTC-USD")
security_name
class-attribute
instance-attribute
¶
Full security name (e.g., "Apple Inc.", "Bitcoin USD").
Optional but recommended for TRADE transactions.
quantity
class-attribute
instance-attribute
¶
Number of shares/units traded.
Required for TRADE transactions, None for non-trade transactions. - Stocks/ETFs: Whole or fractional shares (e.g., 10.5) - Options: Contracts (e.g., 2) - Crypto: Fractional units (e.g., 0.00123456)
Precision: Up to 8 decimal places.
unit_price
class-attribute
instance-attribute
¶
Price per share/unit.
Required for TRADE transactions, None for non-trade transactions. - Stocks/ETFs: Price per share - Options: Price per contract - Crypto: Price per coin/token
commission
class-attribute
instance-attribute
¶
Trading commission/fee charged by broker.
Optional for TRADE transactions, None for non-trade transactions. Many modern brokers charge $0 commission.
transaction_date
instance-attribute
¶
Date the transaction occurred (from provider).
This is the "as-of" date for the transaction, not when we synced it.
settlement_date
class-attribute
instance-attribute
¶
Date funds/securities settled.
Settlement periods vary: - Stocks/ETFs: T+2 (2 business days after transaction_date) - Options: T+1 - Cash transfers: T+0 to T+3 (depends on method)
None for transactions that don't settle (e.g., adjustments).
provider_metadata
class-attribute
instance-attribute
¶
Raw provider response data.
Preserves the original provider API response for: - Debugging sync issues - Future feature additions (without re-sync) - Audit trail of provider data
Example (Schwab): { "activityId": 123456789, "type": "TRADE", "status": "EXECUTED", "subAccount": "MARGIN", ... }
created_at
instance-attribute
¶
Timestamp when transaction was first synced from provider.
Functions¶
is_trade
¶
Check if this is a trade transaction.
Returns:
| Type | Description |
|---|---|
bool
|
True if transaction_type is TRADE. |
Example
if transaction.is_trade(): ... print(f"Traded {transaction.quantity} shares of {transaction.symbol}")
Source code in src/domain/entities/transaction.py
is_transfer
¶
Check if this is a transfer transaction.
Returns:
| Type | Description |
|---|---|
bool
|
True if transaction_type is TRANSFER. |
Example
if transaction.is_transfer(): ... print(f"Transfer: {transaction.description}")
Source code in src/domain/entities/transaction.py
is_income
¶
Check if this is an income transaction.
Returns:
| Type | Description |
|---|---|
bool
|
True if transaction_type is INCOME. |
Example
if transaction.is_income(): ... print(f"Income: {transaction.amount}")
Source code in src/domain/entities/transaction.py
is_fee
¶
Check if this is a fee transaction.
Returns:
| Type | Description |
|---|---|
bool
|
True if transaction_type is FEE. |
Example
if transaction.is_fee(): ... print(f"Fee charged: {transaction.amount}")
Source code in src/domain/entities/transaction.py
is_debit
¶
Check if this transaction debits the account.
Returns:
| Type | Description |
|---|---|
bool
|
True if amount is negative (money leaving account). |
Example
if transaction.is_debit(): ... print("Cash out of account")
Source code in src/domain/entities/transaction.py
is_credit
¶
Check if this transaction credits the account.
Returns:
| Type | Description |
|---|---|
bool
|
True if amount is positive (money entering account). |
Example
if transaction.is_credit(): ... print("Cash into account")
Source code in src/domain/entities/transaction.py
is_settled
¶
Check if this transaction has settled.
Returns:
| Type | Description |
|---|---|
bool
|
True if status is SETTLED. |
Example
if transaction.is_settled(): ... # Include in balance calculations
Source code in src/domain/entities/transaction.py
has_security_details
¶
Check if this transaction has security-related fields populated.
Returns:
| Type | Description |
|---|---|
bool
|
True if symbol, quantity, and unit_price are all present. |
Example
if transaction.has_security_details(): ... cost_basis = transaction.quantity * transaction.unit_price.amount