Skip to main content

futu_cache/trd_cache/types/
funds.rs

1/// **v1.4.106 Finding A** (codex source audit 2026-05-01): funds cache currency-aware key.
2///
3/// 对齐 C++ `INNData_Trd_Acc.cpp::m_mapAccFund`:
4///   `m_mapAccFund: NN_AssetKey -> NN_TrdCurrency -> Ndt_Trd_AccFund`
5///
6/// Universal/Futures 账户对**不同 currency** 有独立 funds snapshot, 之前 Rust
7/// 用 `DashMap<AccKey, CachedFunds>` (1 acc_id → 1 snapshot) 会被 backend
8/// pushed snapshots **互相覆盖** — 用户传 `currency=USD` 拿到的可能是 stale
9/// CAD 数据, 客户端无法察觉.
10///
11/// 字段语义:
12/// - `acc_id`: 账户 (主 key)
13/// - `asset_category`: `Trd_Common.proto::AssetCategory` enum (0=Default 等),
14///   对齐 C++ NN_AssetKey 子集. 若 client 传 `c2s.asset_category=None`, 用 0.
15/// - `currency`: `Some(c)` 表示 per-currency snapshot (Futures/Universal 路径);
16///   `None` 表示 legacy 单币种账户 native (无 per-currency 概念). C++ 等价于
17///   "first available currency" snapshot.
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19pub struct FundsCacheKey {
20    pub acc_id: u64,
21    pub asset_category: i32,
22    pub currency: Option<i32>,
23}
24
25impl FundsCacheKey {
26    /// Legacy single-account snapshot key (acc_id only, no per-currency / no
27    /// per-asset_category dimension). 用于 SingleCurrency 账户 / 无 currency
28    /// context 的 cache write.
29    #[must_use]
30    pub const fn legacy(acc_id: u64) -> Self {
31        Self {
32            acc_id,
33            asset_category: 0,
34            currency: None,
35        }
36    }
37
38    /// Per-currency snapshot key (Universal/Futures 路径).
39    #[must_use]
40    pub const fn per_currency(acc_id: u64, currency: i32) -> Self {
41        Self {
42            acc_id,
43            asset_category: 0,
44            currency: Some(currency),
45        }
46    }
47
48    /// Per-asset-category + per-currency snapshot key (full path, asset_category
49    /// 非 0 时用).
50    #[must_use]
51    pub const fn full(acc_id: u64, asset_category: i32, currency: Option<i32>) -> Self {
52        Self {
53            acc_id,
54            asset_category,
55            currency,
56        }
57    }
58}
59
60/// 缓存的资金 (对齐 C++ Ndt_Trd_AccFund 全字段)
61#[derive(Debug, Clone, Default)]
62pub struct CachedFunds {
63    pub power: f64,                      // 最大做多购买力
64    pub total_assets: f64,               // 资产净值
65    pub cash: f64,                       // 现金
66    pub market_val: f64,                 // 证券市值
67    pub frozen_cash: f64,                // 冻结资金
68    pub debt_cash: f64,                  // 欠款金额
69    pub avl_withdrawal_cash: f64,        // 可提金额
70    pub currency: Option<i32>,           // 货币类型
71    pub available_funds: Option<f64>,    // 可用资金 (期货)
72    pub unrealized_pl: Option<f64>,      // 未实现盈亏 (期货)
73    pub realized_pl: Option<f64>,        // 已实现盈亏 (期货)
74    pub risk_level: Option<i32>,         // 风险等级
75    pub initial_margin: Option<f64>,     // 初始保证金
76    pub maintenance_margin: Option<f64>, // 维持保证金
77    pub max_power_short: Option<f64>,    // 最大做空购买力
78    pub net_cash_power: Option<f64>,     // 现金购买力
79    pub long_mv: Option<f64>,            // 多头市值
80    pub short_mv: Option<f64>,           // 空头市值
81    pub pending_asset: Option<f64>,      // 在途资产
82    pub max_withdrawal: Option<f64>,     // 最大可提
83    pub risk_status: Option<i32>,        // 风险状态码
84    pub margin_call_margin: Option<f64>, // margin call 保证金
85    pub securities_assets: Option<f64>,  // 证券资产
86    pub fund_assets: Option<f64>,        // 基金资产
87    pub bond_assets: Option<f64>,        // 债券资产
88    pub crypto_mv: Option<f64>,          // 数字货币市值
89    pub exposure_level: Option<i32>,     // 数字货币风险等级
90    pub exposure_limit: Option<f64>,     // 数字货币持仓限额
91    pub used_limit: Option<f64>,         // 数字货币已用限额
92    pub remaining_limit: Option<f64>,    // 数字货币剩余额度
93
94    // v1.4.98 T1-4 (mobile-source-audit Phase 2): US PDT (Pattern Day
95    // Trader) 6 字段. proto/Trd_Common.proto:377-382 字段 24-29.
96    // 仅富途证券(美国)账户适用. mobile App 账户首页"日内交易"卡片直接显示.
97    // futu-trd::Funds 已读 5 字段 (缺 beginning_dtbp), CachedFunds 之前
98    // 6 字段全漏 → cache-only path silent drop.
99    /// 是否 PDT 账户 (Pattern Day Trader, 仅 US)
100    pub is_pdt: Option<bool>,
101    /// 剩余日内交易次数 (string 表示, mobile UI 直接显示)
102    pub pdt_seq: Option<String>,
103    /// 初始日内交易购买力 (DTBP)
104    pub beginning_dtbp: Option<f64>,
105    /// 剩余日内交易购买力 (DTBP)
106    pub remaining_dtbp: Option<f64>,
107    /// 日内交易待缴金额 (DT Call)
108    pub dt_call_amount: Option<f64>,
109    /// 日内交易限制状态 (DTStatus enum)
110    pub dt_status: Option<i32>,
111
112    /// 分币种现金信息: (currency, cash, avl_withdrawal, net_cash_power)
113    pub cash_info_list: Vec<CachedCashInfo>,
114    /// 分市场资产信息: (trd_market, assets)
115    pub market_info_list: Vec<CachedMarketInfo>,
116}
117
118/// 分币种现金信息
119#[derive(Debug, Clone, Default)]
120pub struct CachedCashInfo {
121    /// 币种(对齐 proto `TrdCommon.Currency`)
122    pub currency: i32,
123    /// 该币种现金
124    pub cash: f64,
125    /// 该币种可用余额
126    pub available_balance: f64,
127    /// 该币种净购买力(无杠杆)
128    pub net_cash_power: f64,
129}
130
131/// 分市场资产信息
132#[derive(Debug, Clone, Default)]
133pub struct CachedMarketInfo {
134    /// 所属交易市场(对齐 proto `TrdCommon.TrdMarket`)
135    pub trd_market: i32,
136    /// 该市场资产总值
137    pub assets: f64,
138}