futu_cache/trd_cache/types.rs
1// 交易缓存数据模型。
2
3use futu_core::account_locator::AccountCardRecord;
4
5/// 账户 key: acc_id
6pub type AccKey = u64;
7
8/// 缓存的账户信息
9#[derive(Debug, Clone, Default)]
10pub struct CachedTrdAcc {
11 /// 账户 ID
12 pub acc_id: u64,
13 /// 后端/mobile native intra account id (C++ `Ndt_Trd_AccItem.nIntraAccID`).
14 ///
15 /// 公开 FTAPI `acc_id` 与 backend 查询 body 里的 `account_id` 不是同一层
16 /// 语义。需要发 backend native account_id 的 handler 应优先用这个字段,
17 /// 不要从公开 `acc_id` 低 32 位反推。
18 pub intra_acc_id: Option<u64>,
19 /// 交易环境(0=Simulate / 1=Real)
20 pub trd_env: i32,
21 /// 该账户有权限访问的交易市场列表
22 pub trd_market_auth_list: Vec<i32>,
23 /// 账户类型(Cash / Margin / Derivative / ...)
24 pub acc_type: Option<i32>,
25 /// 账户卡号(后段数字,仅用于显示识别)
26 pub card_num: Option<String>,
27 /// 账户所属 broker(FutuHK=1 / FutuUS=2 / ...)
28 pub security_firm: Option<i32>,
29 /// 模拟账户子类型
30 pub sim_acc_type: Option<i32>,
31 /// 统一卡号(跨市场账户聚合标识)
32 pub uni_card_num: Option<String>,
33 /// 账户状态码(正常 / 冻结 / ...)
34 pub acc_status: Option<i32>,
35 /// Backend raw `FTUsrTrdAcc::Account.state`.
36 ///
37 /// C++ API layer keeps this distinct from public `TrdAccStatus`:
38 /// `OPENED(1)` is returned as Active, `CLOSED(2)` is returned in the
39 /// disabled-real tail, while `OPENING(0)` is skipped by
40 /// `APIServer_Trd_GetAccList.cpp:109-115`. Do not derive this back from
41 /// `acc_status`, because both CLOSED and OPENING are non-active.
42 pub acc_open_state: Option<i32>,
43 /// 账户角色(主账户 / 子账户 / 顾问)
44 pub acc_role: Option<i32>,
45 /// Daemon-derived user-visible account label.
46 ///
47 /// Some opened business accounts are not representable by
48 /// `Trd_Common.TrdMarket` (for example crypto) or overload protocol role
49 /// values (for example equity-incentive / IPO route). The bridge derives a
50 /// label from backend account metadata and stores it here so public account
51 /// discovery does not rely on numeric market allowlists.
52 pub acc_label: Option<String>,
53 /// 日本账户附加类型标签
54 pub jp_acc_type: Vec<i32>,
55 // --- 以下为审计补全的字段 ---
56 /// 账户所有者 UID
57 pub owner_uid: Option<u64>,
58 /// 账户操作者 UID
59 pub opr_uid: Option<u64>,
60 /// 混合状态 (C++ enAccState / MixedState)
61 pub mixed_state: Option<i32>,
62 /// IRA 类型 (CA: TFSA=1, RRSP=2, SRRSP=3)
63 pub ira_type: Option<i32>,
64 /// 授权状态 (GrantState)
65 pub grant_state: Option<i32>,
66 /// 口座类型 (JP: Cash=1, Margin=2, Derivative=3)
67 pub kouza_type: Option<i32>,
68 /// 交易市场 (Account.market, 单个值)
69 pub trd_market: Option<i32>,
70 /// 关联账户 ID (基金账户绑定)
71 pub association_acc_id: Option<u64>,
72 /// 综合账户子账户标志 (0=非子账户)
73 pub acc_flag: Option<i32>,
74 /// 原始顺序索引 (用于保持后端返回的自然顺序)
75 pub order_index: usize,
76 /// C++ 排序 key: (BrokerID << 48) | (TrdMkt << 32) | IntraAccID
77 pub sort_key: u64,
78}
79
80impl AccountCardRecord for CachedTrdAcc {
81 fn acc_id(&self) -> u64 {
82 self.acc_id
83 }
84
85 fn card_num(&self) -> Option<&str> {
86 self.card_num.as_deref()
87 }
88
89 fn uni_card_num(&self) -> Option<&str> {
90 self.uni_card_num.as_deref()
91 }
92}
93
94impl CachedTrdAcc {
95 /// v1.4.108: identify crypto from bridge-derived backend metadata label.
96 ///
97 /// Account discovery must not hide opened crypto accounts, but `Trd_Common`
98 /// has no public `TrdMarket_Crypto` variant. The bridge therefore derives
99 /// `acc_label=crypto` from `FTUsrTrdAcc.AccountMarket::Crypto` /
100 /// `TradingCapability::NaCrypto`; cache consumers should read that label
101 /// rather than re-hardcoding market numbers.
102 pub fn is_crypto_account(&self) -> bool {
103 self.acc_label.as_deref() == Some("crypto")
104 }
105
106 pub fn is_encrypted(&self) -> bool {
107 self.is_crypto_account()
108 }
109
110 /// v1.4.97 J-Acc-Q3 + v1.4.98 T2-6: derived `acc_label` for
111 /// `/api/accounts` REST response.
112 ///
113 /// **Priority order**:
114 /// 1. bridge-derived backend label (`crypto`, `equity_incentive`,
115 /// `ipo_route`, ...);
116 /// 2. `"paper_trade"` — `trd_env==0 (Simulate)` (v1.4.98).
117 ///
118 /// Returns `None` for "no special label" (default Margin / regular Cash).
119 ///
120 /// **Spec**: REST-only enrichment (no proto change for gRPC clients —
121 /// gRPC 不看 proto extension field, 只看 /api/accounts REST output).
122 /// Clients should `treat unknown labels as opaque strings` for forward
123 /// compatibility (v1.5+ may add more labels per pitfall #51 v1.4.94 mod).
124 ///
125 /// Labels are opaque strings for clients. Unknown labels should be rendered
126 /// as-is rather than treated as an error.
127 pub fn derive_acc_label(&self) -> Option<&str> {
128 if let Some(label) = self.acc_label.as_deref() {
129 return Some(label);
130 }
131 if self.trd_env == 0 {
132 return Some("paper_trade");
133 }
134 None
135 }
136}
137
138/// 缓存的资金 (对齐 C++ Ndt_Trd_AccFund 全字段)
139#[derive(Debug, Clone, Default)]
140pub struct CachedFunds {
141 pub power: f64, // 最大做多购买力
142 pub total_assets: f64, // 资产净值
143 pub cash: f64, // 现金
144 pub market_val: f64, // 证券市值
145 pub frozen_cash: f64, // 冻结资金
146 pub debt_cash: f64, // 欠款金额
147 pub avl_withdrawal_cash: f64, // 可提金额
148 pub currency: Option<i32>, // 货币类型
149 pub available_funds: Option<f64>, // 可用资金 (期货)
150 pub unrealized_pl: Option<f64>, // 未实现盈亏 (期货)
151 pub realized_pl: Option<f64>, // 已实现盈亏 (期货)
152 pub risk_level: Option<i32>, // 风险等级
153 pub initial_margin: Option<f64>, // 初始保证金
154 pub maintenance_margin: Option<f64>, // 维持保证金
155 pub max_power_short: Option<f64>, // 最大做空购买力
156 pub net_cash_power: Option<f64>, // 现金购买力
157 pub long_mv: Option<f64>, // 多头市值
158 pub short_mv: Option<f64>, // 空头市值
159 pub pending_asset: Option<f64>, // 在途资产
160 pub max_withdrawal: Option<f64>, // 最大可提
161 pub risk_status: Option<i32>, // 风险状态码
162 pub margin_call_margin: Option<f64>, // margin call 保证金
163 pub securities_assets: Option<f64>, // 证券资产
164 pub fund_assets: Option<f64>, // 基金资产
165 pub bond_assets: Option<f64>, // 债券资产
166 pub crypto_mv: Option<f64>, // 数字货币市值
167 pub exposure_level: Option<i32>, // 数字货币风险等级
168 pub exposure_limit: Option<f64>, // 数字货币持仓限额
169 pub used_limit: Option<f64>, // 数字货币已用限额
170 pub remaining_limit: Option<f64>, // 数字货币剩余额度
171
172 // v1.4.98 T1-4 (mobile-source-audit Phase 2): US PDT (Pattern Day
173 // Trader) 6 字段. proto/Trd_Common.proto:377-382 字段 24-29.
174 // 仅富途证券(美国)账户适用. mobile App 账户首页"日内交易"卡片直接显示.
175 // futu-trd::Funds 已读 5 字段 (缺 beginning_dtbp), CachedFunds 之前
176 // 6 字段全漏 → cache-only path silent drop.
177 /// 是否 PDT 账户 (Pattern Day Trader, 仅 US)
178 pub is_pdt: Option<bool>,
179 /// 剩余日内交易次数 (string 表示, mobile UI 直接显示)
180 pub pdt_seq: Option<String>,
181 /// 初始日内交易购买力 (DTBP)
182 pub beginning_dtbp: Option<f64>,
183 /// 剩余日内交易购买力 (DTBP)
184 pub remaining_dtbp: Option<f64>,
185 /// 日内交易待缴金额 (DT Call)
186 pub dt_call_amount: Option<f64>,
187 /// 日内交易限制状态 (DTStatus enum)
188 pub dt_status: Option<i32>,
189
190 /// 分币种现金信息: (currency, cash, avl_withdrawal, net_cash_power)
191 pub cash_info_list: Vec<CachedCashInfo>,
192 /// 分市场资产信息: (trd_market, assets)
193 pub market_info_list: Vec<CachedMarketInfo>,
194}
195
196/// 分币种现金信息
197#[derive(Debug, Clone, Default)]
198pub struct CachedCashInfo {
199 /// 币种(对齐 proto `TrdCommon.Currency`)
200 pub currency: i32,
201 /// 该币种现金
202 pub cash: f64,
203 /// 该币种可用余额
204 pub available_balance: f64,
205 /// 该币种净购买力(无杠杆)
206 pub net_cash_power: f64,
207}
208
209/// 分市场资产信息
210#[derive(Debug, Clone, Default)]
211pub struct CachedMarketInfo {
212 /// 所属交易市场(对齐 proto `TrdCommon.TrdMarket`)
213 pub trd_market: i32,
214 /// 该市场资产总值
215 pub assets: f64,
216}
217
218/// 缓存的持仓 (对齐 C++ Ndt_Trd_AccPosition 全字段)
219#[derive(Debug, Clone, Default)]
220pub struct CachedPosition {
221 pub position_id: u64,
222 pub position_side: i32, // 0=多仓, 1=空仓
223 pub code: String,
224 pub name: String,
225 pub qty: f64,
226 pub can_sell_qty: f64,
227 pub price: f64, // 当前价
228 pub cost_price: f64, // 摊薄成本价
229 pub val: f64, // 市值
230 pub pl_val: f64, // 盈亏金额
231 pub pl_ratio: Option<f64>, // 盈亏比例
232 pub sec_market: Option<i32>, // 证券市场
233 pub td_pl_val: Option<f64>, // 今日盈亏
234 pub td_trd_val: Option<f64>, // 今日成交额
235 pub td_buy_val: Option<f64>, // 今日买入金额
236 pub td_buy_qty: Option<f64>, // 今日买入数量
237 pub td_sell_val: Option<f64>, // 今日卖出金额
238 pub td_sell_qty: Option<f64>, // 今日卖出数量
239 pub unrealized_pl: Option<f64>, // 未实现盈亏 (期货)
240 pub realized_pl: Option<f64>, // 已实现盈亏 (期货)
241 pub currency: Option<i32>, // 货币
242 pub trd_market: Option<i32>, // 交易市场
243 pub diluted_cost_price: Option<f64>, // 摊薄成本
244 pub average_cost_price: Option<f64>, // 平均成本
245 pub average_pl_ratio: Option<f64>, // 平均盈亏比例
246 /// v1.4.42 (eli v1.4.40 roadmap #2 + P2.3): 期权持仓到期日距今天数。
247 /// backend 不返,daemon 按 code 推导(option code 才有值)。
248 pub expiry_date_distance: Option<i32>,
249}
250
251/// 缓存的订单 (对齐 C++ Ndt_Trd_Order 全字段)
252#[derive(Debug, Clone, Default)]
253pub struct CachedOrder {
254 pub order_id: u64,
255 pub order_id_ex: String, // 服务端订单 ID 字符串
256 pub code: String,
257 pub name: String,
258 pub trd_side: i32,
259 pub order_type: i32,
260 pub order_status: i32,
261 pub qty: f64,
262 pub price: f64,
263 pub fill_qty: f64,
264 pub fill_avg_price: f64,
265 pub create_time: String,
266 pub update_time: String,
267 pub last_err_msg: Option<String>, // 最后错误信息
268 pub sec_market: Option<i32>, // 证券市场
269 pub create_timestamp: Option<f64>, // 创建时间戳
270 pub update_timestamp: Option<f64>, // 更新时间戳
271 pub remark: Option<String>, // 备注
272 pub time_in_force: Option<i32>, // 有效期类型
273 pub fill_outside_rth: Option<bool>, // 是否允许盘前盘后成交
274 pub aux_price: Option<f64>, // 触发价格
275 pub trail_type: Option<i32>, // 跟踪类型
276 pub trail_value: Option<f64>, // 跟踪值
277 pub trail_spread: Option<f64>, // 跟踪价差
278 pub currency: Option<i32>, // 货币
279 pub trd_market: Option<i32>, // 交易市场
280
281 /// v1.4.106 codex 0219 Finding 4 / 0226 F7: backend snapshot 字段集合.
282 ///
283 /// PlaceOrder ack 后 backend 返 `OrderNewRsp.order_id` (= `szOrderID`,
284 /// 服务端真实订单 id, alphanumeric 字符串). FTAPI `Trd_PlaceOrder.S2C.order_id`
285 /// 是这个 string 的 hash (`HashStrToU64` 结果, 见 `trade_query::hash_str_to_u64`).
286 ///
287 /// **必填语义**: ModifyOrder / CancelOrder backend req 的 `order_id` 字段
288 /// **必须**填 backend `szOrderID`, 不是 hash. 反模式 (v1.4.105 及以前):
289 /// 没有 orderIDEx 时直接 `order_id_ex.parse().unwrap_or(0)` 把 hash 当
290 /// backend id 发 — backend 拒错或匹配失败.
291 ///
292 /// 修法 (v1.4.106 Finding 1+4): cache 存 `backend_order_id` (= szOrderID)
293 /// 字段; trade-write handler 通过 `find_order_for_trade_write` lookup 拿到
294 /// `ResolvedOrderContext { backend_order_id, version, exchange, exchange_code,
295 /// security_type, ... }` 后再发 backend req.
296 pub backend_order_id: String,
297
298 /// v1.4.106 codex 0219 Finding 4: backend `Order.version` (proto field 21).
299 ///
300 /// ModifyOrder backend `OrderReplaceReq.order_version` 必填 — 让 backend
301 /// 能拒接收已经被其他客户端改过版本的旧请求. C++ `FillModifyOrderReq:736`:
302 /// `req.set_order_version((u32_t)order.nVersion)`.
303 pub order_version: i32,
304
305 /// v1.4.106 codex 0219 Finding 4: backend `Order.exchange_code` (proto field 37).
306 ///
307 /// 期货所属交易所代码 (e.g. `1` = HKEX, `2` = NYSE 之类, 取值参考
308 /// `NN_QotMarket`). ModifyOrder / CancelOrder backend 必填 (期货必填,
309 /// 股票为 0). C++ `FillModifyOrderReq:773`:
310 /// `req.set_exchange_code((u32_t)order.enMktID)`.
311 pub exchange_code: i32,
312
313 /// v1.4.106 codex 0219 Finding 4: backend `Order.exchange` (proto field 49).
314 ///
315 /// 股票所属交易所字符串 (e.g. "SEHK", "NYSE", "NASDAQ"). ModifyOrder /
316 /// CancelOrder backend 必填. C++ `FillModifyOrderReq:774`:
317 /// `req.set_exchange(order.szExchange)`.
318 pub exchange: String,
319
320 /// v1.4.106 codex 0219 Finding 4: backend `Order.security_type` (proto field 29).
321 ///
322 /// 取值参考 backend `odr_sys_cmn::SecurityType`
323 /// (1=COMMON, 2=OPTION, 4=FUTURES, 5=BOND).
324 /// CancelOrder single 必填 (`req.add_security_type(GetSecurityType(...))`,
325 /// C++ `FillCancelOrderReq:817`).
326 pub security_type: i32,
327
328 /// v1.4.98 T1-8 (mobile-source-audit): 美股盘前/盘中/盘后 session 标识.
329 /// proto/Trd_Common.proto:455 字段 27. 同 time_in_force / fill_outside_rth
330 /// 系列, 美股 RTH/Pre-Market/After-Hours order routing 显示用.
331 pub session: Option<i32>,
332
333 /// Backend `odr_sys_cmn.Order.order_trade_time_type` / C++ `order.enOrderTradeTimeType`.
334 ///
335 /// `GetMaxTrdQtys` real option IM side request (CMD5004) mirrors C++
336 /// `NNProto_Trd_MaxQty::QueryOptionIM`: when querying max qty for a
337 /// modification order, the side request forwards the cached backend order's
338 /// trade-time type if it is not UNSET.
339 pub order_trade_time_type: Option<u32>,
340
341 /// v1.4.98 T1-8 (mobile-source-audit): 日本子账户类型 (security_firm=7
342 /// FutuJP 时填充). proto/Trd_Common.proto:456 字段 28.
343 /// 8 enum 值: GENERAL/TOKUTEI/NISA_GENERAL/NISA_TSUMITATE 等
344 /// (per docs/reference/rest-api.md D6).
345 pub jp_acc_type: Option<i32>,
346
347 /// v1.4.90 S BUG-e4da-009: stub 标志。
348 ///
349 /// PlaceOrder/CancelOrder handler 成功响应后**立刻** upsert 一个 stub
350 /// `CachedOrder`(v1.4.82 A2 / `place_order.rs:427`),让 `/api/orders`
351 /// 0ms 可见。`is_stub=true` 标记此条尚未被 backend 权威列表 ack。
352 ///
353 /// 后续 backend `query_orders` 返回包含同 `order_id` 的 enriched 数据时,
354 /// 经 `merge_preserving_stubs` 合并 → `is_stub=false`。
355 ///
356 /// 历史坑:v1.4.73 A1 PlaceOrder 后 spawn refresh 直接 `orders.insert`
357 /// **整覆盖**,把刚 upsert 的 stub 抹掉(race 22ms 内即清零)。
358 /// 跨 v1.4.73 → v1.4.89 7 版未真修。本字段是根因修法的一部分。
359 pub is_stub: bool,
360
361 /// v1.4.90 S BUG-e4da-009: stub 插入时间(unix epoch ms)。
362 ///
363 /// `merge_preserving_stubs` 用于 evict 老 stub:backend 如果连续多次
364 /// 不返某 stub `order_id` 且 stub 已超过 `STUB_TTL_MS` (30s) → evict。
365 /// 防止 stub 因 backend 拒单(never appear in list)永久滞留。
366 ///
367 /// `0` 表示非 stub(与 `is_stub=false` 配套)。
368 pub stub_inserted_at_ms: u64,
369
370 /// v1.4.105 BUG-v1.4.104-001 (P0): broker 异步 confirm 标志.
371 ///
372 /// PlaceOrder backend 同步 ack (CMD 4701 result=0) 仅说明 backend 收到请求,
373 /// **不**代表 broker 真的接受订单. C++ `OnOMEvent_Reply_PlaceOrder`
374 /// (`APIServer_Trd_PlaceOrder.cpp:794`) 是**异步 event handler** — broker
375 /// 真 confirm 后才 fire `set_orderid(nOrderIDHash)`. backend `OrderNewRsp.
376 /// need_op_confirm` (proto field 6, default=true) 即表示 "真 broker confirm
377 /// 还在路上, 等 OMEvent push (notice_type 4/5/8/100)".
378 ///
379 /// **历史坑 (eli BUG-v1.4.104-001 实锤)**: v1.4.82-104 stub 上 cache 时直接
380 /// 视为已确认, 没有 broker async confirm 等待 → 三次不同订单返同 order_id_ex
381 /// 时客户端看 success → 误以为生效 → 加仓重下 → 风控 auto-cancel error 10003.
382 ///
383 /// **语义**:
384 /// - `true`: backend 已 ack 但 broker 未确认 — `is_stub=true` 时 stub 不进
385 /// `/api/orders` 响应 (filter), 等 push notice_type=4/5/8/100 confirm 后翻
386 /// `false` 才 expose. 30s 内未 confirm → cleanup task 删 stub + warn.
387 /// - `false`: backend 权威 / broker 已 confirm — 正常 expose 给 client.
388 /// query_orders 返的所有 order 都是 `false` (backend list 是 broker-confirmed
389 /// 的权威列表).
390 ///
391 /// **与 `is_stub` 关系**:
392 /// - `is_stub=true && is_pending_broker_confirm=true`: 刚 PlaceOrder ack, 还没
393 /// push confirm. **client 不可见** (Layer 4 filter).
394 /// - `is_stub=true && is_pending_broker_confirm=false`: backend `need_op_confirm=
395 /// false` 路径 (sim 账户 / 立即生效场景). client 可见.
396 /// - `is_stub=false`: backend authoritative (query_orders 返 / push merge 后).
397 /// `is_pending_broker_confirm` 必为 false. client 可见.
398 pub is_pending_broker_confirm: bool,
399}
400
401/// v1.4.106 codex 0219 Finding 1+4: trade-write resolution snapshot.
402///
403/// 用于 ModifyOrder / CancelOrder handler 把 cached order 字段一次性
404/// 拿出来构造 backend req. **不在 hot path 改 CachedOrder**, 而是返一个
405/// 只读 snapshot 防 caller mutate cache.
406///
407/// 字段 1:1 映射 backend `OrderReplaceReq` / `OrderCancelReq` 必填项 +
408/// modify validation 用到的 (`order_type` / `trd_side` / `qty` /
409/// `price` / `order_status`).
410#[derive(Debug, Clone, Default, PartialEq)]
411pub struct CachedOrderSnapshot {
412 /// = backend `szOrderID` (alphanumeric 字符串, 服务端真实 id).
413 pub backend_order_id: String,
414 /// = backend `Order.version`.
415 pub order_version: i32,
416 /// = backend `Order.exchange_code` (期货所属交易所代码, e.g. NN_QotMarket).
417 pub exchange_code: i32,
418 /// = backend `Order.exchange` (股票所属交易所字符串, e.g. "SEHK").
419 pub exchange: String,
420 /// = backend `Order.security_type` (1=COMMON, 2=OPTION, 4=FUTURES, 5=BOND).
421 pub security_type: i32,
422 /// FTAPI `OrderType` (modify validation 按原 order_type 决定 price /
423 /// aux_price / trail* 是否必填).
424 pub order_type: i32,
425 /// FTAPI `TrdSide` (1=Buy / 2=Sell / 3=SellShort / 4=BuyBack).
426 /// trailing modify 计算 sign 用.
427 pub trd_side: i32,
428 /// FTAPI `OrderStatus` (`IsNotSupportOrderOp` 检查用).
429 pub order_status: i32,
430 /// FTAPI `TrdMarket` (modify validation 中 sec_type futures 路径用).
431 pub trd_market: Option<i32>,
432 /// FTAPI `Order.qty` (历史值, 仅 modify validation 引用).
433 pub qty: f64,
434 /// FTAPI `Order.price` (历史值, 仅 modify validation 引用).
435 pub price: f64,
436 /// FTAPI `Order.code` (用于错误提示 + log 关联).
437 pub code: String,
438 /// PlaceOrder stub 标记. PlaceOrder ack 时插入的 stub `is_stub=true` +
439 /// `order_version=0`. ModifyOrder 按 C++ 本地回显订单 `nVersion=-1`
440 /// 映射为 backend wire `u32::MAX`; CancelOrder 不依赖 order_version.
441 pub is_stub: bool,
442 /// PlaceOrder stub 是否仍在等待 broker/backend 权威确认.
443 ///
444 /// Real write handlers use this to avoid reporting success against a local
445 /// optimistic echo when the authoritative order list has not accepted the
446 /// order yet.
447 pub is_pending_broker_confirm: bool,
448}
449
450impl CachedOrderSnapshot {
451 /// 从 `CachedOrder` 抽出 trade-write resolution 用到的字段子集.
452 pub fn from_order(o: &CachedOrder) -> Self {
453 Self {
454 backend_order_id: o.backend_order_id.clone(),
455 order_version: o.order_version,
456 exchange_code: o.exchange_code,
457 exchange: o.exchange.clone(),
458 security_type: o.security_type,
459 order_type: o.order_type,
460 trd_side: o.trd_side,
461 order_status: o.order_status,
462 trd_market: o.trd_market,
463 qty: o.qty,
464 price: o.price,
465 code: o.code.clone(),
466 is_stub: o.is_stub,
467 is_pending_broker_confirm: o.is_pending_broker_confirm,
468 }
469 }
470}
471
472/// v1.4.106 codex 0219 Finding 1: ResolveOrderError 区分三种 cache miss 形态.
473#[derive(Debug, Clone, PartialEq, Eq)]
474pub enum ResolveOrderError {
475 /// 同时缺 `order_id` 和 `order_id_ex`. caller 必须早期 reject.
476 InvalidInput,
477 /// `(acc_id, order_id)` 在 cache 找不到. 提示用户先刷新 `/api/orders`
478 /// 或传 orderIDEx.
479 CacheMiss,
480 /// cache 命中但 `backend_order_id` 字段空 (老版本 cache entry).
481 /// 提示用户先刷新 `/api/orders` 或传 orderIDEx.
482 MissingBackendId,
483}
484
485impl std::fmt::Display for ResolveOrderError {
486 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
487 match self {
488 Self::InvalidInput => f.write_str(
489 "modify/cancel: 必须传 order_id 或 order_id_ex (orderIDEx 优先)",
490 ),
491 Self::CacheMiss => f.write_str(
492 "modify/cancel: 订单不在本地缓存. 请先调 /api/orders 刷新, 或在请求里传 orderIDEx (= backend szOrderID).",
493 ),
494 Self::MissingBackendId => f.write_str(
495 "modify/cancel: 本地缓存缺 backend orderIDEx (老版本 daemon 写入的 cache). 请先调 /api/orders 刷新, 或在请求里传 orderIDEx.",
496 ),
497 }
498 }
499}
500
501impl std::error::Error for ResolveOrderError {}
502
503/// **v1.4.106 Finding A** (codex source audit 2026-05-01): funds cache currency-aware key.
504///
505/// 对齐 C++ `INNData_Trd_Acc.cpp::m_mapAccFund`:
506/// `m_mapAccFund: NN_AssetKey -> NN_TrdCurrency -> Ndt_Trd_AccFund`
507///
508/// Universal/Futures 账户对**不同 currency** 有独立 funds snapshot, 之前 Rust
509/// 用 `DashMap<AccKey, CachedFunds>` (1 acc_id → 1 snapshot) 会被 backend
510/// pushed snapshots **互相覆盖** — 用户传 `currency=USD` 拿到的可能是 stale
511/// CAD 数据, 客户端无法察觉.
512///
513/// 字段语义:
514/// - `acc_id`: 账户 (主 key)
515/// - `asset_category`: `Trd_Common.proto::AssetCategory` enum (0=Default 等),
516/// 对齐 C++ NN_AssetKey 子集. 若 client 传 `c2s.asset_category=None`, 用 0.
517/// - `currency`: `Some(c)` 表示 per-currency snapshot (Futures/Universal 路径);
518/// `None` 表示 legacy 单币种账户 native (无 per-currency 概念). C++ 等价于
519/// "first available currency" snapshot.
520#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
521pub struct FundsCacheKey {
522 pub acc_id: u64,
523 pub asset_category: i32,
524 pub currency: Option<i32>,
525}
526
527impl FundsCacheKey {
528 /// Legacy single-account snapshot key (acc_id only, no per-currency / no
529 /// per-asset_category dimension). 用于 SingleCurrency 账户 / 无 currency
530 /// context 的 cache write.
531 #[must_use]
532 pub const fn legacy(acc_id: u64) -> Self {
533 Self {
534 acc_id,
535 asset_category: 0,
536 currency: None,
537 }
538 }
539
540 /// Per-currency snapshot key (Universal/Futures 路径).
541 #[must_use]
542 pub const fn per_currency(acc_id: u64, currency: i32) -> Self {
543 Self {
544 acc_id,
545 asset_category: 0,
546 currency: Some(currency),
547 }
548 }
549
550 /// Per-asset-category + per-currency snapshot key (full path, asset_category
551 /// 非 0 时用).
552 #[must_use]
553 pub const fn full(acc_id: u64, asset_category: i32, currency: Option<i32>) -> Self {
554 Self {
555 acc_id,
556 asset_category,
557 currency,
558 }
559 }
560}
561
562/// **v1.4.107 PositionList asset-category key**.
563///
564/// C++ `APIServer_Trd_GetPositionList.cpp::FillPositionList` reads positions
565/// by `NN_AssetKey { accid, enCategory }`. FutuJP margin / derivative accounts
566/// therefore need independent position snapshots per asset category, just like
567/// funds. Category 0 keeps the legacy single-bucket behavior for non-JP and sim
568/// accounts.
569#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
570pub struct PositionsCacheKey {
571 pub acc_id: u64,
572 pub asset_category: i32,
573}
574
575impl PositionsCacheKey {
576 #[must_use]
577 pub const fn legacy(acc_id: u64) -> Self {
578 Self {
579 acc_id,
580 asset_category: 0,
581 }
582 }
583
584 #[must_use]
585 pub const fn scoped(acc_id: u64, asset_category: i32) -> Self {
586 Self {
587 acc_id,
588 asset_category,
589 }
590 }
591}
592
593/// v1.4.106 codex 0226 F1+F2: PlaceOrder 阶段 backend 返回 `OrderNewRsp.action`
594/// (CltAction.type == ORDER_CONFIRM=5) 时, 携带 `CltActionOrderConfirm` 二次
595/// 确认上下文 — 客户端需把这些字段透传给 backend `OrderConfirmReq` (cmd 4728).
596///
597/// 来源 (proto-internal/odr_sys_cmn.proto:883-893):
598/// ```text
599/// message CltActionOrderConfirm {
600/// optional string order_id = 1; // 订单 id (= backend 真实
601/// // szOrderID, alphanumeric)
602/// optional string title = 2; // 弹窗标题文案
603/// optional string content = 3; // 弹窗内容文案
604/// optional string confirm_button_title = 4;
605/// optional string cancel_button_title = 5;
606/// optional uint32 confirm_type = 6; // 必传给 OrderConfirmReq
607/// optional uint32 exchange_code = 7; // 期货上游交易所代码
608/// optional string exchange = 8; // 股票所属交易所
609/// }
610/// ```
611///
612/// daemon 在 PlaceOrder ack 路径里 capture 此 context, 然后在
613/// `Trd_ReconfirmOrder` 收到客户端确认请求时按 (acc_id, ftapi_order_id) 取出来
614/// 构造 backend `OrderConfirmReq`. 缺 context = backend 不需要二次确认 (e.g.
615/// HK 高买低卖未触发 / sim 路径) → ReconfirmOrder handler 早 reject loud.
616///
617/// **来源 cmd_id**: `CS_CMDID_TRADE_ORDER_CONFIRM_ORDER = 4728`
618/// (`/Users/leaf/ai-lab/o-src/moomoo/Moomoo/Include/FTTrade/TradeCmdDefine.h:132`).
619#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
620pub struct OrderConfirmContext {
621 /// backend 真实 szOrderID (alphanumeric); 写到 `OrderConfirmReq.order_id`.
622 pub backend_order_id: String,
623 /// `CltActionOrderConfirm.confirm_type` (来自 OrderConfirmType enum).
624 /// **必传** (backend 用此值校验客户端确实看到了对应弹窗).
625 pub confirm_type: u32,
626 /// `CltActionOrderConfirm.exchange_code` (期货必传).
627 pub exchange_code: u32,
628 /// `CltActionOrderConfirm.exchange` (股票必传).
629 pub exchange: String,
630 /// 弹窗 title (用于 daemon 日志, 客户端可参考).
631 pub title: String,
632 /// 弹窗 content (用于 daemon 日志, 客户端可参考).
633 pub content: String,
634 /// confirm_button / cancel_button title 透传 (UX, 不影响 backend).
635 pub confirm_button_title: String,
636 pub cancel_button_title: String,
637 /// 写入时间 (unix epoch ms), TTL 用. PlaceOrder 与 ReconfirmOrder 之间通常
638 /// <60s, 远超 60s 视为 stale → handler 拒绝.
639 pub inserted_at_ms: u64,
640}
641
642/// v1.4.106 codex 0226 F1+F2: pending OrderConfirm cache key.
643///
644/// `(acc_id, ftapi_order_id)` 而不是 `(acc_id, backend_order_id)`, 因为 FTAPI
645/// 客户端发 ReconfirmOrder 用的是 PlaceOrder 返的 `s2c.order_id` (FTAPI u64,
646/// 由 `hash_backend_id_to_u64` 派生). FTAPI 客户端**不**直接看到 backend
647/// alphanumeric szOrderID; daemon 必须按 FTAPI order_id 反查 context.
648#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
649pub struct OrderConfirmKey {
650 pub acc_id: u64,
651 /// FTAPI order_id (= `hash_backend_id_to_u64(backend.order_id)`).
652 pub ftapi_order_id: u64,
653}
654
655impl OrderConfirmKey {
656 pub fn new(acc_id: u64, ftapi_order_id: u64) -> Self {
657 Self {
658 acc_id,
659 ftapi_order_id,
660 }
661 }
662}
663
664/// v1.4.106 codex 0226 F1+F2: pending order confirm context TTL (ms).
665///
666/// PlaceOrder → ReconfirmOrder 通常 <60s (用户看到弹窗后立即点确认). 超过 60s
667/// 视为弹窗已被忽略 / 用户 abandon → daemon 不允许 reconfirm (backend 也会拒绝
668/// 因为 confirm_type 已过期). 当前选 5min (300_000 ms) 作宽松 TTL — 给真机用户
669/// 更多缓冲, 同时防内存泄漏.
670pub const ORDER_CONFIRM_CONTEXT_TTL_MS: u64 = 300_000;