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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use std::sync::Arc;

use crate::{
    fixapi::FixApi,
    messages::{OrderMassStatusReq, PositionsReq, ResponseMessage, SecurityListReq},
    types::{ConnectionHandler, Error, ExecutionReport, Field, PositionReport, SymbolInformation},
};

pub struct TradeClient {
    internal: FixApi,
}

fn parse_security_list(res: ResponseMessage) -> Result<Vec<SymbolInformation>, Error> {
    let sec_list = res.get_repeating_groups(Field::NoRelatedSym, Field::Symbol, None);
    let mut result = Vec::new();
    for symbol in sec_list.into_iter() {
        if symbol.len() < 3 {
            continue;
        }
        result.push(SymbolInformation {
            name: symbol
                .get(&Field::SymbolName)
                .ok_or(Error::FieldNotFoundError(Field::SymbolName))?
                .clone(),
            id: symbol
                .get(&Field::Symbol)
                .ok_or(Error::FieldNotFoundError(Field::Symbol))?
                .parse::<u32>()
                .unwrap(),
            digits: symbol
                .get(&Field::SymbolDigits)
                .ok_or(Error::FieldNotFoundError(Field::SymbolDigits))?
                .parse::<u32>()
                .unwrap(),
        });
    }
    Ok(result)
}

fn parse_positions(res: ResponseMessage) -> Result<Vec<PositionReport>, Error> {
    let pos_list = res.get_repeating_groups(Field::TotalNumPosReports, Field::PosReqResult, None);
    let mut result = Vec::new();
    for pos in pos_list.into_iter() {
        let pos_req_result = pos.get(&Field::PosReqResult).unwrap();
        if pos_req_result == "2" {
            continue;
        }
        println!("{:?}", pos);
    }

    Ok(result)
}

fn parse_order_mass(res: ResponseMessage) -> Result<Vec<ExecutionReport>, Error> {
    let mut result = Vec::new();

    Ok(result)
}

impl TradeClient {
    pub fn new(
        host: String,
        login: String,
        password: String,
        broker: String,
        heartbeat_interval: Option<u32>,
    ) -> Self {
        Self {
            internal: FixApi::new(
                crate::types::SubID::TRADE,
                host,
                login,
                password,
                broker,
                heartbeat_interval,
            ),
        }
    }

    pub fn register_connection_handler<T: ConnectionHandler + Send + Sync + 'static>(
        &mut self,
        handler: T,
    ) {
        self.internal.register_connection_handler(handler);
    }

    pub fn register_connection_handler_arc<T: ConnectionHandler + Send + Sync + 'static>(
        &mut self,
        handler: Arc<T>,
    ) {
        self.internal.register_connection_handler_arc(handler);
    }

    pub async fn connect(&mut self) -> Result<(), Error> {
        self.internal.connect().await?;
        self.internal.logon().await
    }

    pub async fn disconnect(&mut self) -> Result<(), Error> {
        self.internal.disconnect().await
    }

    pub fn is_connected(&self) -> bool {
        self.internal.is_connected()
    }

    async fn fetch_response(&self, seq_num: u32) -> Result<ResponseMessage, Error> {
        while let Ok(()) = self.internal.wait_notifier().await {
            match self.internal.check_req_accepted(seq_num).await {
                Ok(res) => {
                    log::debug!("in res {:?}", seq_num);
                    return Ok(res);
                }
                Err(Error::NoResponse(_)) => {
                    log::debug!(" no reponse{:?}", seq_num);
                    if let Err(err) = self.internal.trigger.send(()).await {
                        return Err(Error::TriggerError(err));
                    }
                }
                Err(err) => {
                    log::debug!("err {:?}", seq_num);
                    return Err(err);
                }
            }
        }
        Err(Error::NoResponse(seq_num))
    }

    /// Fetch the security list from the server.
    ///
    ///
    /// This is asn asynchronous method that sends a request to the server and waits for the
    /// response. It returns a result containing the data if the request succesful, or an error if
    /// it fails.
    pub async fn fetch_security_list(&self) -> Result<Vec<SymbolInformation>, Error> {
        let req = SecurityListReq::new("1".into(), 0, None);
        let seq_num = self.internal.send_message(req).await?;
        println!("{:?}", seq_num);
        parse_security_list(self.fetch_response(seq_num).await?)
    }

    pub async fn fetch_positions(&self) -> Result<Vec<PositionReport>, Error> {
        let req = PositionsReq::new(uuid::Uuid::new_v4().to_string(), None);
        let seq_num = self.internal.send_message(req).await?;
        let res = self.fetch_response(seq_num).await?;
        parse_positions(res)
    }

    pub async fn fetch_all_orders(&self) -> Result<Vec<ExecutionReport>, Error> {
        let req = OrderMassStatusReq::new(uuid::Uuid::new_v4().to_string(), 7, None);
        let seq_num = self.internal.send_message(req).await?;
        let res = self.fetch_response(seq_num).await?;
        parse_order_mass(res)
    }

    pub async fn new_market_order(&self) -> Result<(), Error> {
        unimplemented!()
    }

    pub async fn new_limit_order(&self) -> Result<(), Error> {
        unimplemented!()
    }

    pub async fn replace_order(&self) -> Result<(), Error> {
        unimplemented!()
    }

    pub async fn close_position(&self) -> Result<(), Error> {
        unimplemented!()
    }

    pub async fn close_all_position(&self) -> Result<(), Error> {
        unimplemented!()
    }

    pub async fn cancel_order(&self) -> Result<(), Error> {
        unimplemented!()
    }

    pub async fn cancel_all_position(&self) -> Result<(), Error> {
        unimplemented!()
    }
}