quantaxis_rs/
transaction.rs

1use chrono::{DateTime, Offset, TimeZone, Utc};
2use qifi_rs::Trade;
3use serde::{Deserialize, Serialize};
4use serde_json::to_string;
5
6#[derive(Debug, Clone, Deserialize, Serialize)]
7pub struct QATransaction {
8    pub code: String,
9    pub amount: f64,
10    pub price: f64,
11    pub datetime: String,
12    pub order_id: String,
13    pub trade_id: String,
14    pub realorder_id: String,
15    pub account_cookie: String,
16    pub commission: f64,
17    pub tax: f64,
18    pub message: String,
19    pub frozen: f64,
20    pub direction: i32,
21}
22
23
24impl QATransaction {
25    pub fn to_json(&self) -> String {
26        let jdata = serde_json::to_string(&self).unwrap();
27        jdata
28    }
29    pub fn get_direction_or_offset(&mut self, towards: i32) -> (String, String) {
30        let rt = match towards {
31            1 => (String::from("BUY"), String::from("OPEN")),
32            2 => (String::from("BUY"), String::from("OPEN")),
33            3 => (String::from("BUY"), String::from("CLOSE")),
34            4 => (String::from("BUY"), String::from("CLOSETODAY")),
35            -1 => (String::from("SELL"), String::from("CLOSE")),
36            -2 => (String::from("SELL"), String::from("OPEN")),
37            -3 => (String::from("SELL"), String::from("CLOSE")),
38            -4 => (String::from("SELL"), String::from("CLOSETODAY")),
39            _ => (String::from(""), String::from("")),
40        };
41        rt
42    }
43
44
45    pub fn to_qifitrade(&mut self) -> Trade {
46        let (direction, offset) = self.get_direction_or_offset(self.direction);
47        let td = Utc
48            .datetime_from_str(self.datetime.as_ref(), "%Y-%m-%d %H:%M:%S")
49            .unwrap()
50            .timestamp_nanos()
51            - 28800000000000;
52
53        Trade {
54            seqno: 0,
55            user_id: self.account_cookie.clone(),
56            trade_id: self.trade_id.clone(),
57            exchange_id: "".to_string(),
58            instrument_id: self.code.clone(),
59            order_id: self.order_id.clone(),
60            exchange_trade_id: self.realorder_id.clone(),
61            direction,
62            offset,
63            volume: self.amount.clone(),
64            price: self.price.clone(),
65            trade_date_time: td,
66            commission: self.commission.clone(),
67        }
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn test_to_qifi() {
77        let mut transaction = QATransaction {
78            code: "".to_string(),
79            amount: 0.0,
80            price: 0.0,
81            datetime: "2020-01-02 00:00:00".to_string(),
82            order_id: "".to_string(),
83            trade_id: "".to_string(),
84            realorder_id: "".to_string(),
85            account_cookie: "".to_string(),
86            commission: 0.0,
87            tax: 0.0,
88            message: "".to_string(),
89            frozen: 0.0,
90            direction: 0,
91        };
92        transaction.to_qifitrade();
93    }
94}
95