Coverage for src/copytrading_engine/assistant/wording.py: 86%

23 statements  

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

1"""The app's words for the engine's codes, so the assistant reads and repeats what the owner sees. 

2 

3`REASONS` mirrors the app's `Reason.swift`; `test_wording.py` keeps the two in step. 

4""" 

5 

6import datetime as dt 

7from decimal import Decimal 

8 

9REASONS = { 

10 "partial_symbol": "The ticker is not a whole word in the post", 

11 "lot_unavailable": "CopyTrading holds none of these shares", 

12 "preview_expired": "The price check ran out before you confirmed", 

13 "account_facts_changed": "The account changed since you reviewed the sale", 

14 "account_changed": "The account changed since you reviewed the sale", 

15 "plan_changed": "The order would be different now", 

16 "related_manual_action": "A sale of these shares is already under way", 

17 "market_facts_unavailable": "The broker could not be reached", 

18 "preview_unavailable": "The sale could not be checked", 

19 "account_disabled": "Copying into this account is off", 

20 "account_paused": "Copying into this account is stopped", 

21 "account_blocked": "The broker blocked this account", 

22 "account_unavailable": "The account could not be reached", 

23 "outside_open_orders": "Cancel the orders open at Alpaca first", 

24 "broker_account_inactive": "Alpaca has not activated this account", 

25 "account_in_use": "Another CopyTrading on this Mac is copying into this account", 

26 "record_unreadable": "Its record on this Mac couldn't be read", 

27 "alpaca_account_shared": ( 

28 "Another account in this setup uses keys for the same Alpaca account" 

29 ), 

30 "broker_keys_rejected": "Alpaca didn't accept the API keys", 

31 "broker_unreachable": "Alpaca couldn't be reached", 

32 "accounts_unavailable": "More than one account couldn't open", 

33 "storage_unavailable": "CopyTrading couldn't open its data on this Mac", 

34 "routing_state_unavailable": "CopyTrading couldn't read its saved gurus", 

35 "processing_unavailable": "Copying stopped unexpectedly", 

36 "activation_journal_unavailable": "CopyTrading couldn't record the start", 

37 "notification_unavailable": "Alerts couldn't be sent", 

38 "source_unavailable": "Discord could not be reached", 

39 "source_unauthorized": "Discord turned down the token", 

40 "account_risk_unavailable": "Risk could not be checked", 

41 "recovery_pending": "Still checking the account after a restart", 

42 "manual_resume_required": "This account waits for its Start Copying after the restart", 

43 "stale_during_recovery": "Too old by the time the account's restart checks finished", 

44 "stale_waiting_for_resume": ( 

45 "Waited for the account's Start Copying after the restart, and got too old" 

46 ), 

47 "outside_session": "The market was closed", 

48 "overnight_halted": "Overnight trading was halted", 

49 "overnight_not_supported": "Overnight trading is not supported", 

50 "quote_unavailable": "No price quote was available", 

51 "quote_stale": "The price quote was too old", 

52 "quote_above_limit": "The price moved above your limit", 

53 "total_exposure_cap": "Your total exposure limit was reached", 

54 "symbol_exposure_cap": "Your per-stock limit was reached", 

55 "symbol_and_total_exposure_cap": "Your exposure limits were reached", 

56 "daily_loss_cap": "Your daily loss limit was reached", 

57 "daily_entry_cap": "Your daily entry limit was reached", 

58 "insufficient_cash": "Not enough cash", 

59 "insufficient_owned_shares": "No shares left to sell", 

60 "below_minimum_budget": "The order was below the minimum size", 

61 "below_minimum_quantity": "The order was below one share", 

62 "below_minimum_order_size": "Less is left than the coin's smallest order", 

63 "exits_disabled": "Copying exits is off", 

64 "unsupported_asset": "The broker does not support this symbol", 

65 "crypto_paper_only": "Crypto is copied on paper accounts only, and this account is live", 

66 "invalid_price_tick": "The price was not a valid tick", 

67 "missing_or_ambiguous_lot": "You don't hold a copied buy at that price", 

68 "wait_pending_order": "Waiting for an earlier order", 

69 "external_open_order": "Another open order is in the way", 

70 "ownership_incident": "Holdings need your review", 

71 "position_mismatch": "Holdings differ from the broker", 

72 "duplicate": "A repeat of an earlier call", 

73 "stale": "Too old to copy: older than this account copies buys. A sell is always copied.", 

74 "provider_rejected": "The model service refused the request", 

75 "provider_key_rejected": "The model service didn't accept the API key", 

76 "provider_model_not_found": "The model service has no model by that name", 

77 "out_of_order": "Arrived out of order", 

78 "review_required": "Needs your review", 

79 "ignored": "Ignored", 

80 "halted": "Copying was halted", 

81 "session_changed": "The market session changed", 

82 "processing_stopped": "Copying is paused", 

83 "not_checked": "Not checked yet", 

84 "stale_signal": "Too old to copy", 

85 "future_signal": "The post's time was ahead of this Mac's clock", 

86 "attachments_require_human_review": "The post has a picture, so it waits for you", 

87 "decode_attempts_exhausted": "The model couldn't read it after three tries", 

88 "daily_model_request_budget_exhausted": "Not read: today's 2,000 model reads were used up", 

89 "source_profile_not_configured": "No guru is set up for this poster", 

90 "evidence_validation_failed": "The model's reading didn't match the post", 

91 "invalid_model_output": "The model's answer couldn't be read", 

92 "model_output_budget_exceeded": "The model's answer was too long", 

93 "provider_timeout": "The model service didn't answer in time", 

94 "provider_unavailable": "The model service couldn't be reached", 

95 "broker_rejected": "The broker rejected the order", 

96 "sells_from_holdings": "Sells from what the account holds", 

97 "unresolved_order_incident": "An earlier order needs your review", 

98 "unresolved_account_order": "An open order at the broker needs your review", 

99 "resume_blocked_outside_order": ( 

100 "An order placed outside CopyTrading is still open. Let it fill or cancel it, then resume." 

101 ), 

102 "resume_blocked_account_inactive": "Alpaca says this account isn't active, so it can't buy.", 

103 "resume_blocked_order_unclear": ( 

104 "An order's outcome is still unclear at Alpaca. Resume once it settles." 

105 ), 

106 "resume_blocked_unreadable": ( 

107 "CopyTrading couldn't read this account's holdings just now. Try again in a moment." 

108 ), 

109 "incomplete_account_orders": "The broker's order list was incomplete", 

110 "plan_unavailable": "The order couldn't be planned", 

111 "conditional": "The guru would trade only if something happens", 

112 "suggestion": "The guru suggested it but didn't trade", 

113 "unclear": "The post's meaning wasn't clear", 

114 "chatter": "Just chat, nothing to trade", 

115 "price_range": "The guru gave a price range", 

116 "price_at_market": "The guru said to trade at the market price", 

117 "price_not_given": "The post gave no price", 

118 "approval_required": "You asked to approve every order for this account", 

119 "named_buy_not_held": "None of your buys is at the buy price the guru named", 

120 "bought_after_sell": "Every buy it would sell was posted after this sell", 

121 "already_selling": "Already selling these shares", 

122 "follow_stops_off": "Following the guru's stops is off", 

123 "stop_buy_not_filled": "The buy this stop protects did not fill", 

124 "stop_buy_not_held": "CopyTrading holds none of the buy this stop protects", 

125 "stop_buy_not_made": "The buy this stop protects was not made", 

126 "stop_not_for_coins": "Stops are not placed for coins", 

127 "stop_nothing_left": "Nothing was left to protect", 

128 "waiting_for_quote": "Waiting for a price to sell at", 

129 "sell_share_not_given": "The sell doesn't say how much", 

130 "batch_size_not_given": "The playbook doesn't say how big a batch is", 

131 "repeats_an_earlier_call": "The post repeats an earlier call of the guru's", 

132 "repeats_a_recent_call": "A re-post of a call already copied", 

133 "approved_by_owner": "You approved this call", 

134 "waiting_expired": "Too late to copy: its trading day is over", 

135 "account_ownership_pending": "An account you removed still holds what CopyTrading bought", 

136 "routing_change_pending": "Posts were still being read or sent", 

137} 

138 

139# Outcomes the app shows with its own words rather than through Reason. 

140OUTCOMES = { 

141 "order_linked": "An order was placed", 

142 "pending": "Still being processed", 

143 "done": "Done", 

144} 

145 

146# The line under a copying word: what it waits for, or a warning while it still copies. 

147COPYING_DETAILS = { 

148 "starting": "Starting", 

149 "pausing": "Pausing", 

150 "account_paused": "You paused new buys; sells still go through", 

151 "manual_resume_required": "Waiting for your Start Copying after a restart", 

152 "enabled_waiting_for_session": "Copies once the market opens", 

153 "enabled_coins_only": "Copying coins now; stocks once the market opens", 

154 "recovery_pending": "Checking the account after a restart", 

155 "reconciliation_unavailable": "The account couldn't be checked against Alpaca", 

156 "session_unavailable": "The market hours couldn't be read", 

157} 

158 

159AFTER_RESTART = { 

160 "manual": "waits for your Start Copying", 

161 "automatic": "starts copying again on its own", 

162} 

163 

164# Why the engine refused a tool, in words the assistant can pass on. 

165REFUSALS = { 

166 "access_off": "Agent access is off in Settings.", 

167 "locked": "CopyTrading is locked; the owner has to unlock it first.", 

168 "forbidden": "The assistant isn't allowed to do that.", 

169 "invalid_request": "That request wasn't valid.", 

170 "not_found": "Nothing by that name was found.", 

171 "unavailable": "The engine couldn't answer right now.", 

172 "busy": "Something else is running; try again in a moment.", 

173 "cooling_down": "That was asked moments ago; try again shortly.", 

174 "proposal_limit": "There is already a request waiting for the owner's approval.", 

175 "conflict": "Something changed meanwhile; try again.", 

176 "expired": "That has expired.", 

177 "rejected": "The owner rejected it.", 

178 "cancelled": "The owner cancelled this answer.", 

179} 

180 

181 

182def plain(code: str | None, table: dict[str, str] = REASONS) -> str | None: 

183 """The owner's words for a code; an unknown code reads as its words, never as snake_case.""" 

184 if code is None: 

185 return None 

186 return table.get(code) or REASONS.get(code) or OUTCOMES.get(code) or code.replace("_", " ") 

187 

188 

189def money(value: Decimal | None) -> str | None: 

190 """Dollars with cents and a thousands separator, signed when negative.""" 

191 if value is None: 191 ↛ 192line 191 didn't jump to line 192 because the condition on line 191 was never true

192 return None 

193 sign = "-" if value < 0 else "" 

194 return f"{sign}${abs(value):,.2f}" 

195 

196 

197def shares(value: Decimal) -> str: 

198 """A share count without trailing zeros.""" 

199 return f"{value.normalize():f}" 

200 

201 

202def when(value: dt.datetime | None) -> str | None: 

203 """A moment in the owner's local time, the way the app shows it: "Oct 8, 7:54 AM".""" 

204 if value is None: 204 ↛ 205line 204 didn't jump to line 205 because the condition on line 204 was never true

205 return None 

206 local = value.astimezone() 

207 return f"{local:%b} {local.day}, {local:%-I:%M %p}"