1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use chrono::{DateTime, Offset, TimeZone, Utc};
use qifi_rs::Trade;
use serde::{Deserialize, Serialize};
use serde_json::to_string;

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct QATransaction {
    pub code: String,
    pub amount: f64,
    pub price: f64,
    pub datetime: String,
    pub order_id: String,
    pub trade_id: String,
    pub realorder_id: String,
    pub account_cookie: String,
    pub commission: f64,
    pub tax: f64,
    pub message: String,
    pub frozen: f64,
    pub direction: i32,
}


impl QATransaction {
    pub fn to_json(&self) -> String {
        let jdata = serde_json::to_string(&self).unwrap();
        jdata
    }
    pub fn get_direction_or_offset(&mut self, towards: i32) -> (String, String) {
        let rt = match towards {
            1 => (String::from("BUY"), String::from("OPEN")),
            2 => (String::from("BUY"), String::from("OPEN")),
            3 => (String::from("BUY"), String::from("CLOSE")),
            4 => (String::from("BUY"), String::from("CLOSETODAY")),
            -1 => (String::from("SELL"), String::from("CLOSE")),
            -2 => (String::from("SELL"), String::from("OPEN")),
            -3 => (String::from("SELL"), String::from("CLOSE")),
            -4 => (String::from("SELL"), String::from("CLOSETODAY")),
            _ => (String::from(""), String::from("")),
        };
        rt
    }


    pub fn to_qifitrade(&mut self) -> Trade {
        let (direction, offset) = self.get_direction_or_offset(self.direction);
        let td = Utc
            .datetime_from_str(self.datetime.as_ref(), "%Y-%m-%d %H:%M:%S")
            .unwrap()
            .timestamp_nanos()
            - 28800000000000;

        Trade {
            seqno: 0,
            user_id: self.account_cookie.clone(),
            trade_id: self.trade_id.clone(),
            exchange_id: "".to_string(),
            instrument_id: self.code.clone(),
            order_id: self.order_id.clone(),
            exchange_trade_id: self.realorder_id.clone(),
            direction,
            offset,
            volume: self.amount.clone(),
            price: self.price.clone(),
            trade_date_time: td,
            commission: self.commission.clone(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_to_qifi() {
        let mut transaction = QATransaction {
            code: "".to_string(),
            amount: 0.0,
            price: 0.0,
            datetime: "2020-01-02 00:00:00".to_string(),
            order_id: "".to_string(),
            trade_id: "".to_string(),
            realorder_id: "".to_string(),
            account_cookie: "".to_string(),
            commission: 0.0,
            tax: 0.0,
            message: "".to_string(),
            frozen: 0.0,
            direction: 0,
        };
        transaction.to_qifitrade();
    }
}