futu_cache/qot_cache/
kline.rs1use futu_core::qot_stock_key::QotSecurityKey;
2
3use super::QotCache;
4
5#[derive(Debug, Clone)]
7pub struct CachedKLine {
8 pub time: String,
9 pub open_price: f64,
10 pub high_price: f64,
11 pub low_price: f64,
12 pub close_price: f64,
13 pub volume: i64,
14 pub turnover: f64,
15}
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22pub struct KlineDims {
23 pub rehab: i32,
24 pub kl_type: i32,
25 pub session: i32,
26}
27
28impl KlineDims {
29 #[must_use]
30 pub const fn new(rehab: i32, kl_type: i32, session: i32) -> Self {
31 Self {
32 rehab,
33 kl_type,
34 session,
35 }
36 }
37}
38
39impl QotCache {
40 pub fn make_kline_key_by_dims(sec_key: &str, dims: KlineDims) -> String {
56 format!(
57 "{sec_key}:r{}:k{}:s{}",
58 dims.rehab, dims.kl_type, dims.session
59 )
60 }
61
62 pub fn make_kline_key(sec_key: &str, rehab: i32, kl_type: i32, session: i32) -> String {
63 Self::make_kline_key_by_dims(sec_key, KlineDims::new(rehab, kl_type, session))
64 }
65
66 pub fn update_klines_by_dims(&self, sec_key: &str, dims: KlineDims, klines: Vec<CachedKLine>) {
68 let cache_key = Self::make_kline_key_by_dims(sec_key, dims);
69 self.klines.insert(cache_key, klines);
70 }
71
72 pub fn update_klines(
73 &self,
74 sec_key: &str,
75 rehab: i32,
76 kl_type: i32,
77 session: i32,
78 klines: Vec<CachedKLine>,
79 ) {
80 self.update_klines_by_dims(sec_key, KlineDims::new(rehab, kl_type, session), klines);
81 }
82
83 pub fn get_klines_by_dims(&self, sec_key: &str, dims: KlineDims) -> Option<Vec<CachedKLine>> {
85 let cache_key = Self::make_kline_key_by_dims(sec_key, dims);
86 self.klines.get(&cache_key).map(|v| v.clone())
87 }
88
89 pub fn get_klines(
90 &self,
91 sec_key: &str,
92 rehab: i32,
93 kl_type: i32,
94 session: i32,
95 ) -> Option<Vec<CachedKLine>> {
96 self.get_klines_by_dims(sec_key, KlineDims::new(rehab, kl_type, session))
97 }
98
99 pub fn update_klines_broker_by_dims(
105 &self,
106 key: &QotSecurityKey,
107 dims: KlineDims,
108 klines: Vec<CachedKLine>,
109 ) {
110 let cache_key = Self::make_kline_key_by_dims(&key.cache_key(), dims);
111 self.klines.insert(cache_key, klines);
112 }
113
114 pub fn update_klines_broker(
115 &self,
116 key: &QotSecurityKey,
117 rehab: i32,
118 kl_type: i32,
119 session: i32,
120 klines: Vec<CachedKLine>,
121 ) {
122 self.update_klines_broker_by_dims(key, KlineDims::new(rehab, kl_type, session), klines);
123 }
124
125 pub fn get_klines_broker_by_dims(
127 &self,
128 key: &QotSecurityKey,
129 dims: KlineDims,
130 ) -> Option<Vec<CachedKLine>> {
131 let cache_key = Self::make_kline_key_by_dims(&key.cache_key(), dims);
132 self.klines.get(&cache_key).map(|v| v.clone())
133 }
134
135 pub fn get_klines_broker(
136 &self,
137 key: &QotSecurityKey,
138 rehab: i32,
139 kl_type: i32,
140 session: i32,
141 ) -> Option<Vec<CachedKLine>> {
142 self.get_klines_broker_by_dims(key, KlineDims::new(rehab, kl_type, session))
143 }
144}