Coverage for src/copytrading_engine/execution/domain/sizing.py: 88%

28 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-16 16:55 +0000

1"""Immutable destination sizing terms carried with each delivered source signal.""" 

2 

3from decimal import Decimal 

4from typing import Literal 

5 

6from pydantic import BaseModel, ConfigDict, Field, model_validator 

7 

8from copytrading_engine.shared.signals import StockSignal 

9 

10 

11class RouteConnection(BaseModel): 

12 """One guru copying into one account (ADR-0007). The guru's full position is the account's 

13 maximum per stock; a call buys its share of it.""" 

14 

15 model_config = ConfigDict(frozen=True, extra="forbid", hide_input_in_errors=True) 

16 

17 account_id: str = Field(pattern=r"^[a-zA-Z0-9_-]{1,64}$") 

18 full_position_usd: Decimal = Field(gt=0, decimal_places=2, allow_inf_nan=False) 

19 

20 

21# A guru re-posting a call within this many minutes is the same call (the default per guru). 

22REPEAT_WINDOW_MINUTES = 10 

23MAX_REPEAT_WINDOW_MINUTES = 1440 

24 

25 

26class DestinationTerms(BaseModel): 

27 model_config = ConfigDict(frozen=True, extra="forbid", hide_input_in_errors=True) 

28 

29 connection: RouteConnection 

30 environment: Literal["paper", "live"] 

31 configuration_revision: str = Field(pattern=r"^[0-9a-f]{64}$") 

32 guru_id: str | None = Field(default=None, pattern=r"^[a-zA-Z0-9_-]{1,64}$") 

33 profile_revision: str | None = Field(default=None, pattern=r"^[0-9a-f]{64}$") 

34 # The guru's repeat window; None copies every post, even an identical one. 

35 repeat_window_minutes: int | None = Field( 

36 default=REPEAT_WINDOW_MINUTES, ge=1, le=MAX_REPEAT_WINDOW_MINUTES 

37 ) 

38 

39 @model_validator(mode="after") 

40 def validate_profile_reference(self) -> DestinationTerms: 

41 if (self.guru_id is None) != (self.profile_revision is None): 41 ↛ 42line 41 didn't jump to line 42 because the condition on line 41 was never true

42 raise ValueError("Guru identity and profile revision must be recorded together") 

43 return self 

44 

45 

46class DestinationSignal(BaseModel): 

47 model_config = ConfigDict(frozen=True, extra="forbid", hide_input_in_errors=True) 

48 

49 signal: StockSignal 

50 terms: DestinationTerms 

51 

52 @model_validator(mode="after") 

53 def profile_matches_signal(self) -> DestinationSignal: 

54 if (self.signal.guru_id, self.signal.profile_revision) != ( 54 ↛ 58line 54 didn't jump to line 58 because the condition on line 54 was never true

55 self.terms.guru_id, 

56 self.terms.profile_revision, 

57 ): 

58 raise ValueError("Accepted destination evidence must retain its profile revision") 

59 return self