mifi_rs/
front.rs

1use serde::{Deserialize, Serialize};
2use crate::base::Handler;
3use regex::Regex;
4use super::market_preset::{MarketPreset};
5// use std::intrinsics::forget;
6// use std::fs::read_to_string;
7
8#[derive(Deserialize, Clone, Serialize, Debug)]
9pub struct HqTrendSlice {
10    pub price: f64,
11    pub open: f64,
12    pub high: f64,
13    pub low: f64,
14    pub vol: f64,
15    pub amount: f64,
16    pub time: String,
17    pub avprice: f64,
18    pub increase: f64,
19    pub risefall: f64,
20    pub code: String,
21    pub close: f64,
22}
23
24#[derive(Deserialize, Clone, Serialize, Debug)]
25pub struct HqTrend {
26    pub name: String,
27    pub symbol: String,
28    pub time: String,
29    pub date: String,
30    pub price: f64,
31    pub open: f64,
32    pub yclose: f64,
33    pub high: f64,
34    pub low: f64,
35    pub vol: f64,
36    pub amount: f64,
37    pub minutecount: f64,
38    pub minute: Vec<HqTrendSlice>,
39}
40
41#[derive(Deserialize, Clone, Serialize, Debug)]
42pub struct HqKlineFormat {
43    pub data: Vec<Vec<f64>>,
44    pub symbol: String,
45    pub name: String,
46    pub end: f64,
47    pub start: f64,
48    pub count: f64,
49    pub ticket: f64,
50    pub version: String,
51    pub message: String,
52    pub code: f64,
53    pub servertime: String,
54}
55
56impl HqTrend {
57    /// 将任意初始的图转换为k线图所需要的格式
58    pub fn as_kline_format(&self) -> HqKlineFormat {
59        let mut data: Vec<Vec<f64>> = vec![];
60        let length = self.minute.len();
61        data.push(vec![self.covert_timestr_to_data_f64(self.minute[0].time.as_str()), self.yclose.clone(),
62                       self.minute[0].high.clone(), self.minute[0].low.clone(), self.minute[0].close.clone(), self.minute[0].vol, self.minute[0].amount.clone()]);
63        // 拿到第一个bar 直接送入最新的收盘价格
64        let mut close_pre = self.minute[0].close.clone();
65        for c in &self.minute[1..length] {
66            let temp = vec![self.covert_timestr_to_data_f64(c.time.as_str()), close_pre.clone(), c.open.clone(), c.high.clone(), c.low.clone(), c.close.clone(), c.vol.clone(), c.amount.clone()];
67            data.push(temp);
68            close_pre = c.close.clone();
69        };
70        HqKlineFormat {
71            data,
72            symbol: self.symbol.clone(),
73            name: self.name.clone(),
74            end: 0.0,
75            start: 0.0,
76            count: self.minutecount.clone(),
77            ticket: 0.0,
78            version: "2".to_string(),
79            message: "".to_string(),
80            code: 0.0,
81            servertime: self.time.clone(),
82        }
83    }
84    pub fn covert_timestr_to_data_f64(&self, timestr: &str) -> f64 {
85        let x: Vec<&str> = timestr.split(" ").collect::<Vec<&str>>();
86        let tt = x[0].replace("-", "");
87        assert_eq!(tt.len(), 8);
88        tt.parse::<f64>().unwrap()
89    }
90}
91
92
93impl Handler for HqTrendSlice {
94    fn get_datetime(&self) -> String {
95        self.time.clone().to_string()
96    }
97
98    fn get_code(&self) -> String {
99        self.code.to_string()
100    }
101
102    fn get_date(&self) -> String {
103        unimplemented!()
104    }
105
106    fn get_open(&self) -> f64 {
107        self.open.clone()
108    }
109
110    fn get_close(&self) -> f64 {
111        self.close.clone()
112    }
113
114    fn get_high(&self) -> f64 {
115        self.high.clone()
116    }
117
118    fn get_low(&self) -> f64 {
119        self.low.clone()
120    }
121
122    fn get_vol(&self) -> f64 {
123        self.vol.clone()
124    }
125
126    fn get_amount(&self) -> f64 {
127        self.amount.clone()
128    }
129}
130
131#[derive(Serialize, Deserialize, Debug)]
132pub struct Kline {
133    pub code: String,
134    pub datetime: String,
135    pub open: f64,
136    pub high: f64,
137    pub low: f64,
138    pub close: f64,
139    pub vol: f64,
140}
141
142impl Clone for Kline {
143    fn clone(&self) -> Self {
144        Kline {
145            code: self.code.clone(),
146            datetime: self.datetime.clone(),
147            open: self.open.clone(),
148            high: self.high.clone(),
149            low: self.low.clone(),
150            close: self.close.clone(),
151            vol: self.vol.clone(),
152        }
153    }
154}
155
156impl Default for Kline {
157    fn default() -> Self {
158        Kline {
159            code: "".to_string(),
160            datetime: "1900-01-01 00:00:00.1".to_string(),
161            open: 0.0,
162            high: 0.0,
163            low: 0.0,
164            close: 0.0,
165            vol: 0.0,
166        }
167    }
168}
169
170impl Handler for Kline {
171    fn get_datetime(&self) -> String {
172        self.datetime.clone()
173    }
174
175    fn get_code(&self) -> String {
176        self.code.clone()
177    }
178
179    fn get_date(&self) -> String {
180        self.datetime.clone()
181    }
182
183    fn get_open(&self) -> f64 {
184        self.open.clone()
185    }
186
187    fn get_close(&self) -> f64 {
188        self.close.clone()
189    }
190
191    fn get_high(&self) -> f64 {
192        self.high.clone()
193    }
194
195    fn get_low(&self) -> f64 {
196        self.low.clone()
197    }
198
199    fn get_vol(&self) -> f64 {
200        self.vol.clone()
201    }
202
203    fn get_amount(&self) -> f64 {
204        0 as f64
205    }
206
207    fn set_datetime(&mut self, datetime: String) {
208        self.datetime = datetime;
209    }
210
211    fn set_code(&mut self, code: String) {
212        self.code = code;
213    }
214
215    fn set_open(&mut self, open: f64) {
216        self.open = open;
217    }
218
219    fn set_close(&mut self, close: f64) {
220        self.close = close;
221    }
222
223    fn set_high(&mut self, high: f64) {
224        self.high = high;
225    }
226
227    fn set_low(&mut self, low: f64) {
228        self.low = low
229    }
230
231    fn set_vol(&mut self, vol: f64) {
232        self.vol = vol
233    }
234}
235
236#[derive(Serialize, Deserialize, Debug)]
237pub struct Contract {
238    pub code: String,
239    pub name: String,
240}
241
242impl Contract {
243    pub fn to_symbol(&self) -> String {
244        let re = Regex::new(r"[a-zA-z]+").unwrap();
245        let mut market = MarketPreset::new();
246        let mk = market.get(self.code.as_ref());
247        if re.find(self.code.as_str()).is_some() {
248            return format!("future_{}_{}", mk.exchange, self.code);
249        } else {
250            let exchange = if self.code.starts_with("6") { "SH" } else { "SZ" };
251            return format!("stock_{}_{}", exchange, self.code);
252        }
253    }
254}
255
256mod tests {
257    use super::*;
258
259    #[test]
260    fn it_works() {
261        let x = r#"{
262        "code":"600000",
263        "name":"上海浦東發展銀行"
264        }"#;
265        let n: Contract = serde_json::from_str(x).unwrap();
266        println!("{}", n.to_symbol());
267
268        let c = r#"{
269        "code":"RB2010",
270        "name":"螺纹钢2010"
271        }"#;
272        let m: Contract = serde_json::from_str(c).unwrap();
273        println!("{}", m.to_symbol());
274    }
275}