Spaces:
Sleeping
Sleeping
deveshc23
fix: Add Python 3.10 StrEnum backward compatibility and set python_version 3.11 in README
b277794 Download src/drift_quant/domain.py from deveshc23/drift-quant: direct link, hf CLI and curl.
- Browser
- Download file 4.77 kB
-
https://huggingface.co/spaces/deveshc23/drift-quant/resolve/main/src/drift_quant/domain.py
- Command line
-
hf download hf://spaces/deveshc23/drift-quant/src/drift_quant/domain.py
-
curl -L -o domain.py https://huggingface.co/spaces/deveshc23/drift-quant/resolve/main/src/drift_quant/domain.py
4.77 kB
| """Immutable domain contracts shared by future platform modules.""" | |
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| from datetime import datetime | |
| from decimal import Decimal | |
| from uuid import UUID | |
| try: | |
| from enum import StrEnum | |
| except ImportError: | |
| from enum import Enum | |
| class StrEnum(str, Enum): # noqa: UP042 | |
| def __str__(self) -> str: | |
| return str(self.value) | |
| class DomainValidationError(ValueError): | |
| """Raised when a core financial or lifecycle invariant is violated.""" | |
| class DeploymentEnvironment(StrEnum): | |
| PAPER = "paper" | |
| LIVE = "live" | |
| class StrategyLifecycle(StrEnum): | |
| PROPOSED = "proposed" | |
| IMPLEMENTED = "implemented" | |
| VALIDATED = "validated" | |
| PAPER = "paper" | |
| APPROVED = "approved" | |
| LIVE = "live" | |
| SUSPENDED = "suspended" | |
| REJECTED = "rejected" | |
| RETIRED = "retired" | |
| _ALLOWED_TRANSITIONS: dict[StrategyLifecycle, frozenset[StrategyLifecycle]] = { | |
| StrategyLifecycle.PROPOSED: frozenset( | |
| {StrategyLifecycle.IMPLEMENTED, StrategyLifecycle.REJECTED} | |
| ), | |
| StrategyLifecycle.IMPLEMENTED: frozenset( | |
| {StrategyLifecycle.VALIDATED, StrategyLifecycle.REJECTED} | |
| ), | |
| StrategyLifecycle.VALIDATED: frozenset({StrategyLifecycle.PAPER, StrategyLifecycle.REJECTED}), | |
| StrategyLifecycle.PAPER: frozenset( | |
| {StrategyLifecycle.APPROVED, StrategyLifecycle.SUSPENDED, StrategyLifecycle.RETIRED} | |
| ), | |
| StrategyLifecycle.APPROVED: frozenset( | |
| {StrategyLifecycle.LIVE, StrategyLifecycle.SUSPENDED, StrategyLifecycle.RETIRED} | |
| ), | |
| StrategyLifecycle.LIVE: frozenset({StrategyLifecycle.SUSPENDED, StrategyLifecycle.RETIRED}), | |
| StrategyLifecycle.SUSPENDED: frozenset({StrategyLifecycle.PAPER, StrategyLifecycle.RETIRED}), | |
| StrategyLifecycle.REJECTED: frozenset(), | |
| StrategyLifecycle.RETIRED: frozenset(), | |
| } | |
| def validate_transition(source: StrategyLifecycle, target: StrategyLifecycle) -> None: | |
| """Require a documented lifecycle transition.""" | |
| if target not in _ALLOWED_TRANSITIONS[source]: | |
| raise DomainValidationError(f"Transition from {source} to {target} is not allowed.") | |
| class StrategyVersion: | |
| strategy_id: UUID | |
| revision: str | |
| configuration_digest: str | |
| lifecycle: StrategyLifecycle | |
| def __post_init__(self) -> None: | |
| if not self.revision.strip(): | |
| raise DomainValidationError("Strategy revision must not be blank.") | |
| if len(self.configuration_digest) < 16: | |
| raise DomainValidationError("Configuration digest must be at least 16 characters.") | |
| class Signal: | |
| strategy_version: StrategyVersion | |
| instrument_id: str | |
| generated_at: datetime | |
| expires_at: datetime | |
| target_weight: Decimal | |
| def __post_init__(self) -> None: | |
| if not self.instrument_id.strip(): | |
| raise DomainValidationError("Instrument ID must not be blank.") | |
| if self.generated_at.tzinfo is None or self.expires_at.tzinfo is None: | |
| raise DomainValidationError("Signal timestamps must be timezone-aware.") | |
| if self.expires_at <= self.generated_at: | |
| raise DomainValidationError("Signal expiry must be after generation time.") | |
| if not Decimal("-1") <= self.target_weight <= Decimal("1"): | |
| raise DomainValidationError("Target weight must be between -1 and 1.") | |
| class OrderSide(StrEnum): | |
| BUY = "buy" | |
| SELL = "sell" | |
| class OrderIntent: | |
| client_order_id: UUID | |
| strategy_version: StrategyVersion | |
| instrument_id: str | |
| side: OrderSide | |
| quantity: Decimal | |
| limit_price: Decimal | None | |
| created_at: datetime | |
| environment: DeploymentEnvironment | |
| def __post_init__(self) -> None: | |
| if not self.instrument_id.strip(): | |
| raise DomainValidationError("Instrument ID must not be blank.") | |
| if self.quantity <= Decimal("0"): | |
| raise DomainValidationError("Order quantity must be positive.") | |
| if self.limit_price is not None and self.limit_price <= Decimal("0"): | |
| raise DomainValidationError("Limit price must be positive.") | |
| if self.created_at.tzinfo is None: | |
| raise DomainValidationError("Order timestamp must be timezone-aware.") | |
| class RiskDecisionType(StrEnum): | |
| ALLOW = "allow" | |
| DENY = "deny" | |
| ERROR = "error" | |
| class RiskDecision: | |
| order_id: UUID | |
| decision: RiskDecisionType | |
| policy_revision: str | |
| reason_code: str | |
| def __post_init__(self) -> None: | |
| if not self.policy_revision.strip() or not self.reason_code.strip(): | |
| raise DomainValidationError("Risk decisions require a policy revision and reason code.") | |