futu_backend/trade_query/orders/
helpers.rs1use futu_cache::trd_cache::TrdCache;
5
6use super::super::*;
7use crate::proto_internal::odr_sys_cmn;
8
9pub fn is_valid_trd_market(trd_env: i32, market: i32) -> bool {
10 if trd_env == 0 {
11 return true;
13 }
14 matches!(market, 1 | 2 | 4 | 5 | 6 | 8 | 15 | 111 | 112)
15}
16
17pub fn backend_deal_status_to_ftapi(is_cancelled: bool, is_corrected: bool) -> i32 {
26 if is_cancelled {
27 1 } else if is_corrected {
29 2 } else {
31 0 }
33}
34
35pub fn parse_backend_decimal(value: &Option<String>) -> f64 {
36 value
37 .as_ref()
38 .and_then(|v| v.parse::<f64>().ok())
39 .unwrap_or(0.0)
40}
41
42pub fn backend_order_fill_qty_price_for_ftapi(fill: &odr_sys_cmn::OrderFill) -> (f64, f64) {
50 let use_original = fill.is_cancelled.unwrap_or(false)
51 && fill.original_price.is_some()
52 && fill.original_qty.is_some();
53 let qty = if use_original {
54 parse_backend_decimal(&fill.original_qty)
55 } else {
56 parse_backend_decimal(&fill.qty)
57 };
58 let price = if use_original {
59 parse_backend_decimal(&fill.original_price)
60 } else {
61 parse_backend_decimal(&fill.price)
62 };
63 (qty, price)
64}
65
66pub fn convert_trd_market_c2s(client_market: i32) -> u32 {
73 match client_market {
74 111 => 11, 112 => 12, 113 => 13, 114 => 14, 123 => 23, 124 => 24, m if m >= 0 => m as u32,
82 _ => 0,
83 }
84}
85
86pub fn derive_acc_query_context(trd_cache: &TrdCache, acc_id: u64) -> (i32, Vec<i32>) {
93 if let Some(entry) = trd_cache.accounts.get(&acc_id) {
94 let acc = entry.value();
95 return (acc.trd_env, acc.trd_market_auth_list.clone());
96 }
97 (0, Vec::new())
98}
99
100pub fn derive_backend_market_list(trd_env: i32, enabled_markets: &[i32]) -> Vec<u32> {
103 enabled_markets
104 .iter()
105 .copied()
106 .filter(|m| is_valid_trd_market(trd_env, *m))
107 .map(convert_trd_market_c2s)
108 .collect()
109}
110
111pub fn order_fill_list_cmd_for_env(trd_env: i32) -> u16 {
112 if trd_env == 1 {
113 trade_query_command(TradeQueryOperation::Fills).real_cmd
114 } else {
115 trade_query_command(TradeQueryOperation::Fills).sim_cmd
116 }
117}
118
119pub fn order_fill_info_cmd_for_env(trd_env: i32) -> u16 {
120 if trd_env == 1 {
121 trade_query_command(TradeQueryOperation::FillInfo).real_cmd
122 } else {
123 trade_query_command(TradeQueryOperation::FillInfo).sim_cmd
124 }
125}
126
127pub fn history_order_list_cmd_for_env(trd_env: i32) -> u16 {
128 if trd_env == 1 {
129 trade_query_command(TradeQueryOperation::HistoryOrders).real_cmd
130 } else {
131 trade_query_command(TradeQueryOperation::HistoryOrders).sim_cmd
132 }
133}
134
135pub fn history_order_fill_list_cmd_for_env(trd_env: i32) -> u16 {
136 if trd_env == 1 {
137 trade_query_command(TradeQueryOperation::HistoryFills).real_cmd
138 } else {
139 trade_query_command(TradeQueryOperation::HistoryFills).sim_cmd
140 }
141}