1use dashmap::DashMap;
4
5pub type AccKey = u64;
7
8#[derive(Debug, Clone)]
10pub struct CachedTrdAcc {
11 pub acc_id: u64,
12 pub trd_env: i32,
13 pub trd_market_auth_list: Vec<i32>,
14 pub acc_type: Option<i32>,
15 pub card_num: Option<String>,
16 pub security_firm: Option<i32>,
17 pub sim_acc_type: Option<i32>,
18 pub uni_card_num: Option<String>,
19 pub acc_status: Option<i32>,
20 pub acc_role: Option<i32>,
21 pub jp_acc_type: Vec<i32>,
22 pub owner_uid: Option<u64>,
25 pub opr_uid: Option<u64>,
27 pub mixed_state: Option<i32>,
29 pub ira_type: Option<i32>,
31 pub grant_state: Option<i32>,
33 pub kouza_type: Option<i32>,
35 pub trd_market: Option<i32>,
37 pub association_acc_id: Option<u64>,
39 pub acc_flag: Option<i32>,
41 pub order_index: usize,
43 pub sort_key: u64,
45}
46
47#[derive(Debug, Clone, Default)]
49pub struct CachedFunds {
50 pub power: f64, pub total_assets: f64, pub cash: f64, pub market_val: f64, pub frozen_cash: f64, pub debt_cash: f64, pub avl_withdrawal_cash: f64, pub currency: Option<i32>, pub available_funds: Option<f64>, pub unrealized_pl: Option<f64>, pub realized_pl: Option<f64>, pub risk_level: Option<i32>, pub initial_margin: Option<f64>, pub maintenance_margin: Option<f64>, pub max_power_short: Option<f64>, pub net_cash_power: Option<f64>, pub long_mv: Option<f64>, pub short_mv: Option<f64>, pub pending_asset: Option<f64>, pub max_withdrawal: Option<f64>, pub risk_status: Option<i32>, pub margin_call_margin: Option<f64>, pub securities_assets: Option<f64>, pub fund_assets: Option<f64>, pub bond_assets: Option<f64>, pub cash_info_list: Vec<CachedCashInfo>,
77 pub market_info_list: Vec<CachedMarketInfo>,
79}
80
81#[derive(Debug, Clone, Default)]
83pub struct CachedCashInfo {
84 pub currency: i32,
85 pub cash: f64,
86 pub available_balance: f64,
87 pub net_cash_power: f64,
88}
89
90#[derive(Debug, Clone, Default)]
92pub struct CachedMarketInfo {
93 pub trd_market: i32,
94 pub assets: f64,
95}
96
97#[derive(Debug, Clone, Default)]
99pub struct CachedPosition {
100 pub position_id: u64,
101 pub position_side: i32, pub code: String,
103 pub name: String,
104 pub qty: f64,
105 pub can_sell_qty: f64,
106 pub price: f64, pub cost_price: f64, pub val: f64, pub pl_val: f64, pub pl_ratio: Option<f64>, pub sec_market: Option<i32>, pub td_pl_val: Option<f64>, pub td_trd_val: Option<f64>, pub td_buy_val: Option<f64>, pub td_buy_qty: Option<f64>, pub td_sell_val: Option<f64>, pub td_sell_qty: Option<f64>, pub unrealized_pl: Option<f64>, pub realized_pl: Option<f64>, pub currency: Option<i32>, pub trd_market: Option<i32>, pub diluted_cost_price: Option<f64>, pub average_cost_price: Option<f64>, pub average_pl_ratio: Option<f64>, }
126
127#[derive(Debug, Clone, Default)]
129pub struct CachedOrder {
130 pub order_id: u64,
131 pub order_id_ex: String, pub code: String,
133 pub name: String,
134 pub trd_side: i32,
135 pub order_type: i32,
136 pub order_status: i32,
137 pub qty: f64,
138 pub price: f64,
139 pub fill_qty: f64,
140 pub fill_avg_price: f64,
141 pub create_time: String,
142 pub update_time: String,
143 pub last_err_msg: Option<String>, pub sec_market: Option<i32>, pub create_timestamp: Option<f64>, pub update_timestamp: Option<f64>, pub remark: Option<String>, pub time_in_force: Option<i32>, pub fill_outside_rth: Option<bool>, pub aux_price: Option<f64>, pub trail_type: Option<i32>, pub trail_value: Option<f64>, pub trail_spread: Option<f64>, pub currency: Option<i32>, pub trd_market: Option<i32>, }
157
158pub struct TrdCache {
160 pub accounts: DashMap<AccKey, CachedTrdAcc>,
162 pub funds: DashMap<AccKey, CachedFunds>,
164 pub positions: DashMap<AccKey, Vec<CachedPosition>>,
166 pub orders: DashMap<AccKey, Vec<CachedOrder>>,
168 pub ciphers: DashMap<AccKey, Vec<u8>>,
170}
171
172impl TrdCache {
173 pub fn get_cipher(&self, acc_id: u64) -> Option<Vec<u8>> {
174 self.ciphers.get(&acc_id).map(|v| v.clone())
175 }
176
177 pub fn set_cipher(&self, acc_id: u64, cipher: Vec<u8>) {
178 self.ciphers.insert(acc_id, cipher);
179 }
180
181 pub fn new() -> Self {
182 Self {
183 accounts: DashMap::new(),
184 funds: DashMap::new(),
185 positions: DashMap::new(),
186 orders: DashMap::new(),
187 ciphers: DashMap::new(),
188 }
189 }
190
191 pub fn set_accounts(&self, accounts: Vec<CachedTrdAcc>) {
192 self.accounts.clear();
193 for (idx, mut acc) in accounts.into_iter().enumerate() {
194 acc.order_index = idx;
195 self.accounts.insert(acc.acc_id, acc);
196 }
197 }
198
199 pub fn get_accounts(&self) -> Vec<CachedTrdAcc> {
200 self.accounts.iter().map(|e| e.value().clone()).collect()
201 }
202
203 pub fn update_funds(&self, acc_id: u64, funds: CachedFunds) {
204 self.funds.insert(acc_id, funds);
205 }
206
207 pub fn update_positions(&self, acc_id: u64, positions: Vec<CachedPosition>) {
208 self.positions.insert(acc_id, positions);
209 }
210
211 pub fn update_orders(&self, acc_id: u64, orders: Vec<CachedOrder>) {
212 self.orders.insert(acc_id, orders);
213 }
214
215 pub fn upsert_order(&self, acc_id: u64, order: CachedOrder) {
217 let mut entry = self.orders.entry(acc_id).or_default();
218 if let Some(existing) = entry.iter_mut().find(|o| o.order_id == order.order_id) {
219 *existing = order;
220 } else {
221 entry.push(order);
222 }
223 }
224}
225
226impl Default for TrdCache {
227 fn default() -> Self {
228 Self::new()
229 }
230}