Coverage for src/copytrading_engine/host/pipe/services.py: 100%
30 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-16 16:55 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-16 16:55 +0000
1"""The trading services the pipe server calls, as the protocols it depends on."""
3import datetime as dt
4from dataclasses import dataclass
5from typing import Protocol
7from pydantic import SecretStr
9from copytrading_engine.execution.domain.lifecycle import (
10 AccountControlCommand,
11 AccountControlResult,
12)
13from copytrading_engine.execution.domain.lot_sales import (
14 LotSaleConfirmation,
15 LotSalePreview,
16 LotSalePreviewRequest,
17 LotSaleResult,
18)
19from copytrading_engine.execution.domain.manual_commands import (
20 ManualCommandPage,
21 ManualCommandPageRequest,
22 ManualCommandResult,
23 ManualCommandsOutcome,
24 ManualConfirmationRequest,
25 ManualCorrectionOutcome,
26 ManualCorrectionRequest,
27 ManualOrderPreview,
28 ManualPreviewRequest,
29)
30from copytrading_engine.execution.domain.market import (
31 Bar,
32 DailyBar,
33 DailyRange,
34 EquityHistory,
35 HistoryWindow,
36 Quote,
37)
38from copytrading_engine.execution.domain.ownership import (
39 OwnershipResolution,
40 OwnershipResolutionRequest,
41)
42from copytrading_engine.execution.domain.sizing import RouteConnection
43from copytrading_engine.execution.presentation.account_feed import AccountFeedPage
44from copytrading_engine.execution.presentation.guru_performance import GuruPerformance
45from copytrading_engine.execution.presentation.operator_views import (
46 AccountOverviewPage,
47)
48from copytrading_engine.trading.adapters.activation import TradingActivationStatus
49from copytrading_engine.trading.adapters.capabilities import (
50 CapabilityCheck,
51 TradingCapabilityReport,
52)
53from copytrading_engine.trading.application.paused_reader import ReadKeys
54from copytrading_engine.trading.domain.config import (
55 ConnectionCheck,
56 ProviderConfiguration,
57 TradingConfiguration,
58 TradingSecrets,
59)
60from copytrading_engine.trading.domain.profiles import (
61 LearnedPlaybook,
62 ProfileEvaluation,
63 ProfileRevision,
64)
65from copytrading_engine.trading.domain.status import TradingStatus
66from copytrading_engine.trading.entrypoints.runtime import TradingRuntime
67from copytrading_engine.trading.presentation.operator_models import (
68 GuruDayStatsPage,
69 SourceActivityPage,
70)
73class TradingLifecycle(Protocol):
74 def status(self) -> TradingStatus: ...
76 async def validate(
77 self, configuration: TradingConfiguration, secrets: TradingSecrets
78 ) -> TradingCapabilityReport: ...
80 async def check_connection(self, connection: ConnectionCheck) -> CapabilityCheck: ...
82 async def start(
83 self, configuration: TradingConfiguration, secrets: TradingSecrets, activation_id: str
84 ) -> TradingStatus: ...
86 def activation_status(self, activation_id: str) -> TradingActivationStatus: ...
88 async def pause(self) -> TradingStatus: ...
90 async def update_account_limits(self, configuration: TradingConfiguration) -> str: ...
92 def register_restore_secrets(self, values: tuple[str, ...]) -> None: ...
95class OperatorReads(Protocol):
96 async def account_overviews(
97 self, before_account_id: str | None, limit: int
98 ) -> AccountOverviewPage: ...
100 async def source_activity(self, before_seq: int | None, limit: int) -> SourceActivityPage: ...
102 async def guru_activity(
103 self, guru_id: str, before_seq: int | None, limit: int
104 ) -> SourceActivityPage: ...
106 async def guru_day_stats(
107 self, day_start: dt.datetime, day_end: dt.datetime
108 ) -> GuruDayStatsPage: ...
110 async def account_feed(
111 self, account_id: str, before_seq: int | None, limit: int
112 ) -> AccountFeedPage: ...
114 async def equity_history(
115 self, account_id: str, window: HistoryWindow
116 ) -> EquityHistory | None: ...
118 async def quote(self, account_id: str, symbol: str) -> Quote | None: ...
120 async def bars(
121 self, account_id: str, symbols: tuple[str, ...]
122 ) -> dict[str, tuple[Bar, ...]]: ...
124 async def daily_bars(
125 self, account_id: str, symbol: str, period: DailyRange
126 ) -> tuple[DailyBar, ...]: ...
128 async def guru_performance(self, account_id: str, guru_id: str) -> GuruPerformance: ...
130 async def list_manual_commands(
131 self, request: ManualCommandPageRequest
132 ) -> ManualCommandPage: ...
134 def attach_readers(self, keys: tuple[ReadKeys, ...]) -> None: ...
136 def detach_readers(self) -> None: ...
139class ManualIntervention(Protocol):
140 async def control_account(self, command: AccountControlCommand) -> AccountControlResult: ...
142 async def delete_account_record(self, account_id: str) -> str | None: ...
144 async def resolve_ownership(
145 self, local_account_id: str, request: OwnershipResolutionRequest
146 ) -> OwnershipResolution: ...
148 async def save_manual_correction(
149 self, request: ManualCorrectionRequest
150 ) -> ManualCorrectionOutcome: ...
152 async def preview_manual_order(self, request: ManualPreviewRequest) -> ManualOrderPreview: ...
154 async def preview_lot_sale(self, request: LotSalePreviewRequest) -> LotSalePreview: ...
156 async def confirm_lot_sale(self, request: LotSaleConfirmation) -> LotSaleResult: ...
158 async def confirm_manual_orders(
159 self, requests: tuple[ManualConfirmationRequest, ...]
160 ) -> ManualCommandsOutcome: ...
162 async def manual_command_result(
163 self, account_id: str, command_id: str
164 ) -> ManualCommandResult: ...
167class ProfileReview(Protocol):
168 async def evaluate_historical_profile(
169 self,
170 source_id: str,
171 profile: ProfileRevision,
172 provider: ProviderConfiguration,
173 provider_api_key: SecretStr,
174 destinations: list[RouteConnection],
175 ) -> ProfileEvaluation: ...
177 async def learn_playbook(
178 self,
179 channel_id: str,
180 author_id: str | None,
181 discord_token: SecretStr,
182 provider: ProviderConfiguration,
183 provider_api_key: SecretStr,
184 ) -> LearnedPlaybook: ...
187@dataclass(frozen=True, slots=True)
188class TradingServices:
189 """The trading capabilities the pipe serves, one per kind of caller task."""
191 lifecycle: TradingLifecycle
192 operator: OperatorReads
193 manual: ManualIntervention
194 profiles: ProfileReview
196 @classmethod
197 def of(cls, runtime: TradingRuntime) -> TradingServices:
198 return cls(runtime, runtime.operator, runtime.manual, runtime.profiles)