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

1"""The trading services the pipe server calls, as the protocols it depends on.""" 

2 

3import datetime as dt 

4from dataclasses import dataclass 

5from typing import Protocol 

6 

7from pydantic import SecretStr 

8 

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) 

71 

72 

73class TradingLifecycle(Protocol): 

74 def status(self) -> TradingStatus: ... 

75 

76 async def validate( 

77 self, configuration: TradingConfiguration, secrets: TradingSecrets 

78 ) -> TradingCapabilityReport: ... 

79 

80 async def check_connection(self, connection: ConnectionCheck) -> CapabilityCheck: ... 

81 

82 async def start( 

83 self, configuration: TradingConfiguration, secrets: TradingSecrets, activation_id: str 

84 ) -> TradingStatus: ... 

85 

86 def activation_status(self, activation_id: str) -> TradingActivationStatus: ... 

87 

88 async def pause(self) -> TradingStatus: ... 

89 

90 async def update_account_limits(self, configuration: TradingConfiguration) -> str: ... 

91 

92 def register_restore_secrets(self, values: tuple[str, ...]) -> None: ... 

93 

94 

95class OperatorReads(Protocol): 

96 async def account_overviews( 

97 self, before_account_id: str | None, limit: int 

98 ) -> AccountOverviewPage: ... 

99 

100 async def source_activity(self, before_seq: int | None, limit: int) -> SourceActivityPage: ... 

101 

102 async def guru_activity( 

103 self, guru_id: str, before_seq: int | None, limit: int 

104 ) -> SourceActivityPage: ... 

105 

106 async def guru_day_stats( 

107 self, day_start: dt.datetime, day_end: dt.datetime 

108 ) -> GuruDayStatsPage: ... 

109 

110 async def account_feed( 

111 self, account_id: str, before_seq: int | None, limit: int 

112 ) -> AccountFeedPage: ... 

113 

114 async def equity_history( 

115 self, account_id: str, window: HistoryWindow 

116 ) -> EquityHistory | None: ... 

117 

118 async def quote(self, account_id: str, symbol: str) -> Quote | None: ... 

119 

120 async def bars( 

121 self, account_id: str, symbols: tuple[str, ...] 

122 ) -> dict[str, tuple[Bar, ...]]: ... 

123 

124 async def daily_bars( 

125 self, account_id: str, symbol: str, period: DailyRange 

126 ) -> tuple[DailyBar, ...]: ... 

127 

128 async def guru_performance(self, account_id: str, guru_id: str) -> GuruPerformance: ... 

129 

130 async def list_manual_commands( 

131 self, request: ManualCommandPageRequest 

132 ) -> ManualCommandPage: ... 

133 

134 def attach_readers(self, keys: tuple[ReadKeys, ...]) -> None: ... 

135 

136 def detach_readers(self) -> None: ... 

137 

138 

139class ManualIntervention(Protocol): 

140 async def control_account(self, command: AccountControlCommand) -> AccountControlResult: ... 

141 

142 async def delete_account_record(self, account_id: str) -> str | None: ... 

143 

144 async def resolve_ownership( 

145 self, local_account_id: str, request: OwnershipResolutionRequest 

146 ) -> OwnershipResolution: ... 

147 

148 async def save_manual_correction( 

149 self, request: ManualCorrectionRequest 

150 ) -> ManualCorrectionOutcome: ... 

151 

152 async def preview_manual_order(self, request: ManualPreviewRequest) -> ManualOrderPreview: ... 

153 

154 async def preview_lot_sale(self, request: LotSalePreviewRequest) -> LotSalePreview: ... 

155 

156 async def confirm_lot_sale(self, request: LotSaleConfirmation) -> LotSaleResult: ... 

157 

158 async def confirm_manual_orders( 

159 self, requests: tuple[ManualConfirmationRequest, ...] 

160 ) -> ManualCommandsOutcome: ... 

161 

162 async def manual_command_result( 

163 self, account_id: str, command_id: str 

164 ) -> ManualCommandResult: ... 

165 

166 

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: ... 

176 

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: ... 

185 

186 

187@dataclass(frozen=True, slots=True) 

188class TradingServices: 

189 """The trading capabilities the pipe serves, one per kind of caller task.""" 

190 

191 lifecycle: TradingLifecycle 

192 operator: OperatorReads 

193 manual: ManualIntervention 

194 profiles: ProfileReview 

195 

196 @classmethod 

197 def of(cls, runtime: TradingRuntime) -> TradingServices: 

198 return cls(runtime, runtime.operator, runtime.manual, runtime.profiles)