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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
use async_std::{
    channel::{bounded, Receiver, Sender},
    sync::RwLock,
    task,
};
use chrono::NaiveDateTime;
use uuid::Uuid;

use crate::{
    fixapi::FixApi,
    messages::{
        NewOrderSingleReq, OrderCancelReplaceReq, OrderCancelReq, OrderMassStatusReq, PositionsReq,
        ResponseMessage, SecurityListReq,
    },
    parse_func::{self, parse_execution_report},
    types::{
        ConnectionHandler, Error, ExecutionReport, Field, OrderType, PositionReport, Side,
        SymbolInformation, TradeDataHandler,
    },
};

use std::{
    collections::VecDeque,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc,
    },
    time::{Duration, Instant},
};

#[derive(Debug)]
struct TimeoutItem<T> {
    item: T,
    expiry: Instant,
    consumed: AtomicBool,
}

impl<T> TimeoutItem<T> {
    fn new(item: T, lifetime: Duration) -> Self {
        TimeoutItem {
            item,
            expiry: Instant::now() + lifetime,
            consumed: AtomicBool::new(false),
        }
    }
}

pub struct TradeClient {
    internal: FixApi,

    trade_data_handler: Option<Arc<dyn TradeDataHandler + Send + Sync>>,

    queue: Arc<RwLock<VecDeque<TimeoutItem<ResponseMessage>>>>,

    signal: Sender<()>,
    receiver: Receiver<()>,

    // for waiting response in fetch methods.
    timeout: u64,
}

impl TradeClient {
    pub fn new(
        host: String,
        login: String,
        password: String,
        sender_comp_id: String,
        heartbeat_interval: Option<u32>,
    ) -> Self {
        let (tx, rx) = bounded(1);
        Self {
            internal: FixApi::new(
                crate::types::SubID::TRADE,
                host,
                login,
                password,
                sender_comp_id,
                heartbeat_interval,
            ),
            trade_data_handler: None,
            queue: Arc::new(RwLock::new(VecDeque::new())),

            signal: tx,
            receiver: rx,

            timeout: 5000, //
        }
    }

    pub fn register_trade_handler_arc<T: TradeDataHandler + Send + Sync + 'static>(
        &mut self,
        handler: Arc<T>,
    ) {
        self.trade_data_handler = Some(handler);
    }

    pub fn register_trade_handler<T: TradeDataHandler + Send + Sync + 'static>(
        &mut self,
        handler: T,
    ) {
        self.trade_data_handler = Some(Arc::new(handler));
    }

    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.register_internal_handler();
        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()
    }

    fn register_internal_handler(&mut self) {
        let queue = self.queue.clone();
        let handler = self.trade_data_handler.clone();
        let signal = self.signal.clone();
        let trade_callback = move |res: ResponseMessage| {
            let signal = signal.clone();
            let handler = handler.clone();
            let queue = queue.clone();
            let lifetime = Duration::from_millis(5000);
            task::spawn(async move {
                match res.get_message_type() {
                    "8" => {
                        if res
                            .get_field_value(Field::ExecType)
                            .map(|v| v.as_str() != "I")
                            .unwrap_or(true)
                        {
                            match parse_execution_report(res.clone()) {
                                Ok(report) => {
                                    if let Some(handler) = handler {
                                        handler.on_execution_report(report).await;
                                    }
                                }
                                Err(_err) => {
                                    // IGNORE
                                }
                            }
                        }
                    }
                    _ => {}
                }

                queue
                    .write()
                    .await
                    .push_back(TimeoutItem::new(res, lifetime));

                // check timeout
                let now = Instant::now();
                loop {
                    let expiry = queue.read().await.front().map(|v| v.expiry).unwrap_or(now);
                    if expiry < now {
                        // pop old item
                        queue.write().await.pop_front();
                    } else {
                        break;
                    }
                }

                signal.try_send(()).ok();
                // signal.send(()).await.ok();
            });
        };

        self.internal.register_trade_callback(trade_callback);
    }

    fn create_unique_id(&self) -> String {
        Uuid::new_v4().to_string()
    }

    async fn wait_notifier(&self, receiver: Receiver<()>, dur: u64) -> Result<(), Error> {
        if !self.is_connected() {
            return Err(Error::NotConnected);
        }
        async_std::future::timeout(Duration::from_millis(dur), receiver.recv())
            .await
            .map_err(|_| Error::TimeoutError)?
            .map_err(|e| e.into())
    }

    async fn fetch_response(
        &self,
        arg: Vec<(&str, Field, String)>,
    ) -> Result<ResponseMessage, Error> {
        // setup for timeout
        let now = Instant::now();
        let mut remain = self.timeout;

        loop {
            let _ = self.wait_notifier(self.receiver.clone(), remain).await?;
            // match self.wait_notifier(receiver, remain).await {
            let mut res = None;
            let q = self.queue.read().await;
            for v in q.iter().rev() {
                let mut b = false;
                let consumed = v.consumed.load(Ordering::Relaxed);
                if consumed {
                    continue;
                }

                for (msg_type, field, value) in arg.iter() {
                    if v.item.matching_field_value(msg_type, *field, value) {
                        b = true;
                        res = Some(v.item.clone());
                        v.consumed.store(true, Ordering::Relaxed);
                        break;
                    }
                }
                if b {
                    break;
                }
            }

            match res {
                Some(res) => {
                    return Ok(res);
                }
                None => {
                    // check remaining time.
                    let past = (Instant::now() - now).as_millis() as u64;
                    if past < self.timeout {
                        // continue.
                        remain = self.timeout - past;

                        // check if there is more waiting receiver.
                        if self.receiver.receiver_count() > 1 {
                            self.signal.try_send(()).ok();
                        }
                        continue;
                    } else {
                        return Err(Error::TimeoutError);
                    }
                }
            }
        }
    }

    fn check_connection(&self) -> Result<(), Error> {
        if self.is_connected() {
            Ok(())
        } else {
            Err(Error::NotConnected)
        }
    }

    /// 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> {
        self.check_connection()?;
        let security_req_id = self.create_unique_id();
        let req = SecurityListReq::new(security_req_id.clone(), 0, None);
        self.internal.send_message(req).await?;
        match self
            .fetch_response(vec![("y", Field::SecurityReqID, security_req_id)])
            .await
        {
            Ok(res) => parse_func::parse_security_list(&res),
            Err(err) => Err(err),
        }
    }

    pub async fn fetch_positions(&self) -> Result<Vec<PositionReport>, Error> {
        self.check_connection()?;
        let pos_req_id = self.create_unique_id();
        let req = PositionsReq::new(pos_req_id.clone(), None);
        self.internal.send_message(req).await?;

        let mut result = Vec::new();

        loop {
            match self
                .fetch_response(vec![("AP", Field::PosReqID, pos_req_id.clone())])
                .await
            {
                Ok(res) => {
                    if res.get_message_type() == "AP"
                        && res
                            .get_field_value(Field::PosReqResult)
                            .map_or(false, |v| v.as_str() == "0")
                    {
                        let no_pos = res
                            .get_field_value(Field::TotalNumPosReports)
                            .unwrap_or("0".into())
                            .parse::<usize>()
                            .unwrap();
                        result.push(res);
                        if no_pos <= result.len() {
                            return parse_func::parse_positions(result);
                        } else {
                            continue;
                        }
                    } else {
                        return parse_func::parse_positions(vec![res]);
                    }
                }
                Err(err) => {
                    return Err(err);
                }
            }
        }
    }

    pub async fn fetch_all_order_status(
        &self,
        issue_data: Option<NaiveDateTime>,
    ) -> Result<Vec<ExecutionReport>, Error> {
        self.check_connection()?;
        let mass_status_req_id = self.create_unique_id();
        // FIXME if mass_status_req_id is not 7, then return 'j' but response does not include the mass_status_req_id
        let req = OrderMassStatusReq::new(mass_status_req_id.clone(), 7, issue_data);
        self.internal.send_message(req).await?;

        let mut result = Vec::new();

        loop {
            match self
                .fetch_response(vec![
                    ("8", Field::MassStatusReqID, mass_status_req_id.clone()),
                    ("j", Field::BusinessRejectRefID, mass_status_req_id.clone()),
                ])
                .await
            {
                Ok(res) => {
                    return match res.get_message_type() {
                        "j" => Ok(Vec::new()),
                        "8" => {
                            let no_report = res
                                .get_field_value(Field::TotNumReports)
                                .unwrap_or("0".into())
                                .parse::<usize>()
                                .unwrap();

                            result.push(res);

                            if no_report <= result.len() {
                                parse_func::parse_order_mass_status(result)
                            } else {
                                continue;
                            }
                        }
                        _ => Err(Error::UnknownError),
                    };
                }
                Err(err) => {
                    return Err(err);
                }
            }
        }
    }

    async fn new_order(&self, req: NewOrderSingleReq) -> Result<ExecutionReport, Error> {
        self.check_connection()?;
        let cl_ord_id = req.cl_ord_id.clone();

        self.internal.send_message(req).await?;
        match self
            .fetch_response(vec![
                ("8", Field::ClOrdId, cl_ord_id.clone()),
                ("j", Field::BusinessRejectRefID, cl_ord_id.clone()),
            ])
            .await
        {
            Ok(res) => match res.get_message_type() {
                "j" => Err(Error::OrderFailed(
                    res.get_field_value(Field::Text).unwrap_or("Unknown".into()),
                )),
                "8" => parse_func::parse_execution_report(res),
                _ => Err(Error::UnknownError),
            },
            Err(err) => Err(err),
        }
    }

    pub async fn new_market_order(
        &self,
        symbol: u32,
        side: Side,
        order_qty: f64,
        cl_ord_id: Option<String>,
        custom_ord_label: Option<String>,
    ) -> Result<ExecutionReport, Error> {
        let req = NewOrderSingleReq::new(
            cl_ord_id.unwrap_or(self.create_unique_id()),
            symbol,
            side,
            None,
            order_qty,
            OrderType::MARKET,
            None,
            None,
            None,
            None,
            custom_ord_label,
        );
        self.new_order(req).await
    }

    pub async fn new_limit_order(
        &self,
        symbol: u32,
        side: Side,
        price: f64,
        order_qty: f64,
        cl_ord_id: Option<String>,
        expire_time: Option<NaiveDateTime>,
        custom_ord_label: Option<String>,
    ) -> Result<ExecutionReport, Error> {
        let req = NewOrderSingleReq::new(
            cl_ord_id.unwrap_or(self.create_unique_id()),
            symbol,
            side,
            None,
            order_qty,
            OrderType::LIMIT,
            Some(price),
            None,
            expire_time,
            None,
            custom_ord_label,
        );

        self.new_order(req).await
    }

    pub async fn new_stop_order(
        &self,
        symbol: u32,
        side: Side,
        stop_px: f64,
        order_qty: f64,
        cl_ord_id: Option<String>,
        expire_time: Option<NaiveDateTime>,
        custom_ord_label: Option<String>,
    ) -> Result<ExecutionReport, Error> {
        let req = NewOrderSingleReq::new(
            cl_ord_id.unwrap_or(self.create_unique_id()),
            symbol,
            side,
            None,
            order_qty,
            OrderType::STOP,
            None,
            Some(stop_px),
            expire_time,
            None,
            custom_ord_label,
        );

        self.new_order(req).await
    }
    pub async fn close_position(
        &self,
        pos_report: PositionReport,
    ) -> Result<ExecutionReport, Error> {
        self.adjust_position_size(
            pos_report.position_id,
            pos_report.symbol_id,
            if pos_report.long_qty == 0.0 {
                pos_report.short_qty
            } else {
                pos_report.long_qty
            },
            if pos_report.long_qty == 0.0 {
                Side::BUY
            } else {
                Side::SELL
            },
        )
        .await
    }

    /// Adjusts the size of a position.
    ///
    /// This method takes a position id, symbol_id, a side (buy or sell), and a lot size.
    /// If the position exists, it adjusts the size of the position by adding or subtracting the given lot size.
    /// If the side is 'buy', the lot size is added to the position.
    /// If the side is 'sell', the lot size is subtracted from the position.
    pub async fn adjust_position_size(
        &self,
        pos_id: String,
        symbol_id: u32,
        lot: f64,
        side: Side,
    ) -> Result<ExecutionReport, Error> {
        let req = NewOrderSingleReq::new(
            self.create_unique_id(),
            symbol_id,
            side,
            None,
            lot,
            OrderType::MARKET,
            None,
            None,
            None,
            Some(pos_id),
            None,
        );

        self.new_order(req).await
    }

    /// Replace order request
    ///
    /// # Arguments
    ///
    /// * `orig_cl_ord_id` - A unique identifier for the order, which is going to be canceled, allocated by the client.
    /// * `order_id` - Unique ID of an order, returned by the server.
    /// ...
    ///
    ///  Either `orig_cl_ord_id` or `order_id` must be passed to this function. If both are `None`, the function will return an error.
    pub async fn replace_order(
        &self,
        org_cl_ord_id: Option<String>,
        order_id: Option<String>,
        order_qty: f64,
        price: Option<f64>,
        stop_px: Option<f64>,
        expire_time: Option<NaiveDateTime>,
    ) -> Result<ExecutionReport, Error> {
        if org_cl_ord_id.is_none() && order_id.is_none() {
            return Err(Error::MissingArgumentError);
        }
        self.check_connection()?;
        let orgid = match org_cl_ord_id.clone() {
            Some(v) => v,
            None => order_id.clone().unwrap(),
        };
        let oid = match order_id.clone() {
            Some(v) => v,
            None => org_cl_ord_id.clone().unwrap(),
        };
        let cl_ord_id = self.create_unique_id();
        let req = OrderCancelReplaceReq::new(
            orgid,
            Some(oid),
            cl_ord_id.clone(),
            order_qty,
            price,
            stop_px,
            expire_time,
        );
        self.internal.send_message(req).await?;
        match self
            .fetch_response(vec![
                if org_cl_ord_id.is_some() {
                    ("8", Field::ClOrdId, org_cl_ord_id.unwrap())
                } else {
                    ("8", Field::OrderID, order_id.unwrap())
                },
                ("j", Field::BusinessRejectRefID, cl_ord_id.clone()),
            ])
            .await
        {
            Ok(res) => {
                match res.get_message_type() {
                    "j" => {
                        // failed
                        Err(Error::OrderFailed(
                            res.get_field_value(Field::Text)
                                .unwrap_or("Unknown error".into()),
                        )
                        .into())
                    }
                    _ => {
                        // "8" Success
                        parse_func::parse_execution_report(res)
                    }
                }
            }
            Err(err) => Err(err),
        }
    }

    /// Order cancel reqeuest
    ///
    /// # Arguments
    ///
    /// * `orig_cl_ord_id` - A unique identifier for the order, which is going to be canceled, allocated by the client.
    /// * `order_id` - Unique ID of an order, returned by the server.
    ///
    ///  Either `orig_cl_ord_id` or `order_id` must be passed to this function. If both are `None`, the function will return an error.
    pub async fn cancel_order(
        &self,
        org_cl_ord_id: Option<String>,
        order_id: Option<String>,
    ) -> Result<ExecutionReport, Error> {
        if org_cl_ord_id.is_none() && order_id.is_none() {
            return Err(Error::MissingArgumentError);
        }
        self.check_connection()?;

        let orgid = match org_cl_ord_id.clone() {
            Some(v) => v,
            None => order_id.clone().unwrap(),
        };
        let oid = match order_id {
            Some(v) => v,
            None => org_cl_ord_id.unwrap(),
        };

        let cl_ord_id = self.create_unique_id();
        let req = OrderCancelReq::new(orgid, Some(oid), cl_ord_id.clone());
        self.internal.send_message(req).await?;
        match self
            .fetch_response(vec![
                ("8", Field::ClOrdId, cl_ord_id.clone()),
                ("j", Field::BusinessRejectRefID, cl_ord_id.clone()),
                ("9", Field::ClOrdId, cl_ord_id.clone()),
            ])
            .await
        {
            Ok(res) => {
                match res.get_message_type() {
                    "j" => {
                        // failed
                        Err(Error::OrderFailed(
                            res.get_field_value(Field::Text)
                                .unwrap_or("Unknown error".into()),
                        )
                        .into())
                    }
                    "9" => {
                        // cancel rejected
                        Err(Error::OrderCancelRejected(
                            res.get_field_value(Field::Text)
                                .unwrap_or("Unknown error".into()),
                        )
                        .into())
                    }
                    _ => {
                        // "8" Success
                        parse_func::parse_execution_report(res)
                    }
                }
            }
            Err(err) => Err(err),
        }
    }
}