alator 0.4.1

Library for backtesting investment strategies
Documentation
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
use itertools::Itertools;
use std::{
    collections::HashMap,
    error::Error,
    fmt::{Display, Formatter},
};

use log::info;
use rotala::clock::DateTime;
use rotala::exchange::uist::{
    UistOrder, UistOrderType, UistQuote, UistTrade, UistTradeType, UistV1,
};

use crate::types::{
    CashValue, PortfolioAllocation, PortfolioHoldings, PortfolioQty, PortfolioValues, Price,
};

use super::{
    BrokerCost, BrokerEvent, BrokerOperations, BrokerState, BrokerStates, CashOperations,
    Portfolio, Quote, SendOrder,
};

type UistBrokerEvent = BrokerEvent<UistOrder>;

/// Implementation of broker that uses the [Uist](rotala::exchange::uist::UistV1) exchange.
#[derive(Debug)]
pub struct UistBroker {
    cash: CashValue,
    exchange: UistV1,
    holdings: PortfolioHoldings,
    //Kept distinct from holdings because some perf calculations may need to distinguish between
    //trades that we know are booked vs ones that we think should get booked
    pending_orders: PortfolioHoldings,
    //Used to mark last trade seen by broker when reconciling completed trades with exchange
    last_seen_trade: usize,
    latest_quotes: HashMap<String, UistQuote>,
    log: UistBrokerLog,
    trade_costs: Vec<BrokerCost>,
    broker_state: BrokerState,
}

impl Quote<UistQuote> for UistBroker {
    fn get_quote(&self, symbol: &str) -> Option<UistQuote> {
        self.latest_quotes.get(symbol).cloned()
    }

    fn get_quotes(&self) -> Option<Vec<UistQuote>> {
        if self.latest_quotes.is_empty() {
            return None;
        }

        let mut tmp = Vec::new();
        for quote in self.latest_quotes.values() {
            tmp.push(quote.clone());
        }
        Some(tmp)
    }
}

impl Portfolio<UistQuote> for UistBroker {
    fn get_trade_costs(&self) -> Vec<BrokerCost> {
        self.trade_costs.clone()
    }

    fn get_holdings(&self) -> PortfolioHoldings {
        self.holdings.clone()
    }

    fn get_cash_balance(&self) -> CashValue {
        self.cash.clone()
    }

    fn update_cash_balance(&mut self, cash: CashValue) {
        self.cash = cash;
    }

    fn get_position_cost(&self, symbol: &str) -> Option<Price> {
        self.log.cost_basis(symbol)
    }

    fn update_holdings(&mut self, symbol: &str, change: PortfolioQty) {
        //We have to take ownership for logging but it is easier just to use ref for symbol as that
        //is used throughout
        let symbol_own = symbol.to_string();
        info!(
            "BROKER: Incrementing holdings in {:?} by {:?}",
            symbol_own, change
        );
        if (*change).eq(&0.0) {
            self.holdings.remove(symbol.as_ref());
        } else {
            self.holdings.insert(symbol.as_ref(), &change);
        }
    }

    fn get_pending_orders(&self) -> PortfolioHoldings {
        self.pending_orders.clone()
    }
}

impl BrokerStates for UistBroker {
    fn get_broker_state(&self) -> BrokerState {
        self.broker_state.clone()
    }

    fn update_broker_state(&mut self, state: BrokerState) {
        self.broker_state = state;
    }
}

impl CashOperations<UistQuote> for UistBroker {}

impl BrokerOperations<UistOrder, UistQuote> for UistBroker {}

impl SendOrder<UistOrder> for UistBroker {
    fn send_order(&mut self, order: UistOrder) -> UistBrokerEvent {
        //This is an estimate of the cost based on the current price, can still end with negative
        //balance when we reconcile with actuals, may also reject valid orders at the margin
        match self.get_broker_state() {
            BrokerState::Failed => {
                info!(
                    "BROKER: Unable to send {:?} order for {:?} shares of {:?} to exchange as broker in Failed state",
                    order.get_order_type(),
                    order.get_shares(),
                    order.get_symbol()
                );
                UistBrokerEvent::OrderInvalid(order.clone())
            }
            BrokerState::Ready => {
                info!(
                    "BROKER: Attempting to send {:?} order for {:?} shares of {:?} to the exchange",
                    order.get_order_type(),
                    order.get_shares(),
                    order.get_symbol()
                );

                let quote = self.get_quote(order.get_symbol()).unwrap();
                let price = match order.get_order_type() {
                    UistOrderType::MarketBuy | UistOrderType::LimitBuy | UistOrderType::StopBuy => {
                        quote.get_ask()
                    }
                    UistOrderType::MarketSell
                    | UistOrderType::LimitSell
                    | UistOrderType::StopSell => quote.get_bid(),
                };

                if let Err(_err) =
                    self.client_has_sufficient_cash::<UistOrderType>(&order, &Price::from(price))
                {
                    info!(
                        "BROKER: Unable to send {:?} order for {:?} shares of {:?} to exchange",
                        order.get_order_type(),
                        order.get_shares(),
                        order.get_symbol()
                    );
                    return UistBrokerEvent::OrderInvalid(order.clone());
                }
                if let Err(_err) =
                    self.client_has_sufficient_holdings_for_sale::<UistOrderType>(&order)
                {
                    info!(
                        "BROKER: Unable to send {:?} order for {:?} shares of {:?} to exchange",
                        order.get_order_type(),
                        order.get_shares(),
                        order.get_symbol()
                    );
                    return UistBrokerEvent::OrderInvalid(order.clone());
                }
                if let Err(_err) = self.client_is_issuing_nonsense_order(&order) {
                    info!(
                        "BROKER: Unable to send {:?} order for {:?} shares of {:?} to exchange",
                        order.get_order_type(),
                        order.get_shares(),
                        order.get_symbol()
                    );
                    return UistBrokerEvent::OrderInvalid(order.clone());
                }

                self.exchange.insert_order(order.clone());
                //From the point of view of strategy, an order pending is the same as an order
                //executed. If the order is executed, then it is executed. If the order isn't
                //executed then the strategy must wait but all the strategy's work has been
                //done. So once we send the order, we need some way for clients to work out
                //what orders are pending and whether they need to do more work.
                let order_effect = match order.get_order_type() {
                    UistOrderType::MarketBuy | UistOrderType::LimitBuy | UistOrderType::StopBuy => {
                        order.get_shares()
                    }

                    UistOrderType::MarketSell
                    | UistOrderType::LimitSell
                    | UistOrderType::StopSell => -order.get_shares(),
                };

                if let Some(position) = self.pending_orders.get(order.get_symbol()) {
                    let existing = *position + order_effect;
                    self.pending_orders
                        .insert(order.get_symbol(), &PortfolioQty::from(existing));
                } else {
                    self.pending_orders
                        .insert(order.get_symbol(), &PortfolioQty::from(order_effect));
                }
                info!(
                    "BROKER: Successfully sent {:?} order for {:?} shares of {:?} to exchange",
                    order.get_order_type(),
                    order.get_shares(),
                    order.get_symbol()
                );
                UistBrokerEvent::OrderSentToExchange(order)
            }
        }
    }

    fn send_orders(&mut self, orders: &[UistOrder]) -> Vec<UistBrokerEvent> {
        let mut res = Vec::new();
        for o in orders {
            let trade = self.send_order(o.clone());
            res.push(trade);
        }
        res
    }
}

impl UistBroker {
    /// Called on every tick of clock to ensure that state is synchronized with other components.
    ///
    /// * Calls `check` on exchange
    /// * Updates last seen prices for exchange tick
    /// * Reconciles internal state against trades completed on current tick
    /// * Rebalances cash, which can trigger new trades if broker is in invalid state
    pub fn check(&mut self) {
        let (has_next, completed_trades, inserted_orders) = self.exchange.tick();

        //Update prices, these prices are not tradable
        for quote in &self.exchange.fetch_quotes() {
            self.latest_quotes
                .insert(quote.get_symbol().to_string(), quote.clone());
        }

        for trade in completed_trades {
            match trade.typ {
                //Force debit so we can end up with negative cash here
                UistTradeType::Buy => self.debit_force(&trade.value),
                UistTradeType::Sell => self.credit(&trade.value),
            };
            self.log.record::<UistTrade>(trade.clone());

            let curr_position = self.get_position_qty(&trade.symbol).unwrap_or_default();

            let updated = match trade.typ {
                UistTradeType::Buy => *curr_position + trade.quantity,
                UistTradeType::Sell => *curr_position - trade.quantity,
            };
            self.update_holdings(&trade.symbol, PortfolioQty::from(updated));

            //Because the order has completed, we should be able to unwrap pending_orders safetly
            //If this fails then there must be an application bug and panic is required.
            let pending = self.pending_orders.get(&trade.symbol).unwrap_or_default();

            let updated_pending = match trade.typ {
                UistTradeType::Buy => *pending - trade.quantity,
                UistTradeType::Sell => *pending + trade.quantity,
            };
            if updated_pending == 0.0 {
                self.pending_orders.remove(&trade.symbol);
            } else {
                self.pending_orders
                    .insert(&trade.symbol, &PortfolioQty::from(updated_pending));
            }

            self.last_seen_trade += 1;
        }
        //Previous step can cause negative cash balance so we have to rebalance here, this
        //is not instant so will never balance properly if the series is very volatile
        self.rebalance_cash();
    }

    pub fn cost_basis(&self, symbol: &str) -> Option<Price> {
        self.log.cost_basis(symbol)
    }

    pub fn trades_between(&self, start: &i64, stop: &i64) -> Vec<UistTrade> {
        self.log.trades_between(start, stop)
    }
}

pub struct UistBrokerBuilder {
    trade_costs: Vec<BrokerCost>,
    exchange: Option<UistV1>,
}

impl UistBrokerBuilder {
    pub fn build(&mut self) -> UistBroker {
        if self.exchange.is_none() {
            panic!("Cannot build broker without exchange");
        }

        //If we don't have quotes on first tick, we shouldn't error but we should expect every
        //`DataSource` to provide a first tick
        let mut first_quotes = HashMap::new();
        let quotes = self.exchange.as_ref().unwrap().fetch_quotes();
        for quote in &quotes {
            first_quotes.insert(quote.get_symbol().to_string(), quote.clone());
        }

        let holdings = PortfolioHoldings::new();
        let pending_orders = PortfolioHoldings::new();
        let log = UistBrokerLog::new();

        let exchange = std::mem::take(&mut self.exchange).unwrap();

        UistBroker {
            //Intialised as invalid so errors throw if client tries to run before init
            holdings,
            pending_orders,
            cash: CashValue::from(0.0),
            log,
            last_seen_trade: 0,
            exchange,
            trade_costs: self.trade_costs.clone(),
            latest_quotes: first_quotes,
            broker_state: BrokerState::Ready,
        }
    }

    pub fn with_exchange(&mut self, exchange: UistV1) -> &mut Self {
        self.exchange = Some(exchange);
        self
    }

    pub fn with_trade_costs(&mut self, trade_costs: Vec<BrokerCost>) -> &mut Self {
        self.trade_costs = trade_costs;
        self
    }

    pub fn new() -> Self {
        UistBrokerBuilder {
            trade_costs: Vec::new(),
            exchange: None,
        }
    }
}

impl Default for UistBrokerBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Clone, Debug)]
pub enum UistRecordedEvent {
    TradeCompleted(UistTrade),
}

impl From<UistTrade> for UistRecordedEvent {
    fn from(value: UistTrade) -> Self {
        UistRecordedEvent::TradeCompleted(value)
    }
}

//Records events generated by brokers. Used for internal calculations but is public for tax
//calculations.
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct UistBrokerLog {
    log: Vec<UistRecordedEvent>,
}

impl UistBrokerLog {
    pub fn record<E: Into<UistRecordedEvent>>(&mut self, event: E) {
        let brokerevent: UistRecordedEvent = event.into();
        self.log.push(brokerevent);
    }

    pub fn trades(&self) -> Vec<UistTrade> {
        let mut trades = Vec::new();
        for event in &self.log {
            let UistRecordedEvent::TradeCompleted(trade) = event;
            trades.push(trade.clone());
        }
        trades
    }

    pub fn trades_between(&self, start: &i64, stop: &i64) -> Vec<UistTrade> {
        let trades = self.trades();
        trades
            .iter()
            .filter(|v| v.date >= *DateTime::from(*start) && v.date <= *DateTime::from(*stop))
            .cloned()
            .collect_vec()
    }

    pub fn cost_basis(&self, symbol: &str) -> Option<Price> {
        let mut cum_qty = PortfolioQty::default();
        let mut cum_val = CashValue::default();
        for event in &self.log {
            let UistRecordedEvent::TradeCompleted(trade) = event;
            if trade.symbol.eq(symbol) {
                match trade.typ {
                    UistTradeType::Buy => {
                        cum_qty = PortfolioQty::from(*cum_qty + trade.quantity);
                        cum_val = CashValue::from(*cum_val + trade.value);
                    }
                    UistTradeType::Sell => {
                        cum_qty = PortfolioQty::from(*cum_qty - trade.quantity);
                        cum_val = CashValue::from(*cum_val - trade.value);
                    }
                }
                //reset the value if we are back to zero
                if (*cum_qty).eq(&0.0) {
                    cum_val = CashValue::default();
                }
            }
        }
        if (*cum_qty).eq(&0.0) {
            return None;
        }
        Some(Price::from(*cum_val / *cum_qty))
    }
}

impl UistBrokerLog {
    pub fn new() -> Self {
        UistBrokerLog { log: Vec::new() }
    }
}

impl Default for UistBrokerLog {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {

    use crate::broker::{
        BrokerCashEvent, BrokerCost, BrokerOperations, CashOperations, Portfolio, SendOrder,
    };
    use crate::types::{CashValue, PortfolioAllocation, PortfolioQty};
    use rotala::clock::{ClockBuilder, Frequency};
    use rotala::exchange::uist::{
        random_uist_generator, UistOrder, UistOrderType, UistTrade, UistTradeType, UistV1,
    };
    use rotala::input::penelope::PenelopeBuilder;

    use super::{UistBroker, UistBrokerBuilder, UistBrokerEvent, UistBrokerLog};

    fn setup() -> UistBroker {
        let mut source_builder = PenelopeBuilder::new();

        source_builder.add_quote(100.00, 101.00, 100, "ABC");
        source_builder.add_quote(10.00, 11.00, 100, "BCD");

        source_builder.add_quote(104.00, 105.00, 101, "ABC");
        source_builder.add_quote(14.00, 15.00, 101, "BCD");

        source_builder.add_quote(95.00, 96.00, 102, "ABC");
        source_builder.add_quote(10.00, 11.00, 102, "BCD");

        source_builder.add_quote(95.00, 96.00, 103, "ABC");
        source_builder.add_quote(10.00, 11.00, 103, "BCD");

        let (price_source, clock) =
            source_builder.build_with_frequency(rotala::clock::Frequency::Second);
        let uist = UistV1::new(clock, price_source, "FAKE");

        let brkr = UistBrokerBuilder::new()
            .with_exchange(uist)
            .with_trade_costs(vec![BrokerCost::PctOfValue(0.01)])
            .build();

        brkr
    }

    #[test]
    fn test_cash_deposit_withdraw() {
        let mut brkr = setup();
        brkr.deposit_cash(&100.0);

        brkr.check();

        //Test cash
        assert!(matches!(
            brkr.withdraw_cash(&50.0),
            BrokerCashEvent::WithdrawSuccess(..)
        ));
        assert!(matches!(
            brkr.withdraw_cash(&51.0),
            BrokerCashEvent::WithdrawFailure(..)
        ));
        assert!(matches!(
            brkr.deposit_cash(&50.0),
            BrokerCashEvent::DepositSuccess(..)
        ));

        //Test transactions
        assert!(matches!(
            brkr.debit(&50.0),
            BrokerCashEvent::WithdrawSuccess(..)
        ));
        assert!(matches!(
            brkr.debit(&51.0),
            BrokerCashEvent::WithdrawFailure(..)
        ));
        assert!(matches!(
            brkr.credit(&50.0),
            BrokerCashEvent::DepositSuccess(..)
        ));
    }

    #[test]
    fn test_that_buy_order_reduces_cash_and_increases_holdings() {
        let mut brkr = setup();
        brkr.deposit_cash(&100_000.0);

        let res = brkr.send_order(UistOrder::market_buy("ABC", 495.0));
        println!("{:?}", res);
        assert!(matches!(res, UistBrokerEvent::OrderSentToExchange(..)));

        brkr.check();

        let cash = brkr.get_cash_balance();
        assert!(*cash < 100_000.0);

        let qty = brkr
            .get_position_qty("ABC")
            .unwrap_or(PortfolioQty::from(0.0));
        assert_eq!(*qty.clone(), 495.00);
    }

    #[test]
    fn test_that_buy_order_larger_than_cash_fails_with_error_returned_without_panic() {
        let mut brkr = setup();
        brkr.deposit_cash(&100.0);
        //Order value is greater than cash balance
        let res = brkr.send_order(UistOrder::market_buy("ABC", 495.0));

        assert!(matches!(res, UistBrokerEvent::OrderInvalid(..)));
        brkr.check();

        let cash = brkr.get_cash_balance();
        assert!(*cash == 100.0);
    }

    #[test]
    fn test_that_sell_order_larger_than_holding_fails_with_error_returned_without_panic() {
        let mut brkr = setup();
        brkr.deposit_cash(&100_000.0);

        let res = brkr.send_order(UistOrder::market_buy("ABC", 100.0));
        assert!(matches!(res, UistBrokerEvent::OrderSentToExchange(..)));
        brkr.check();

        //Order greater than current holding
        brkr.check();

        let res = brkr.send_order(UistOrder::market_sell("ABC", 105.0));
        assert!(matches!(res, UistBrokerEvent::OrderInvalid(..)));

        //Checking that
        let qty = brkr.get_position_qty("ABC").unwrap_or_default();
        println!("{:?}", qty);
        assert!((*qty.clone()).eq(&100.0));
    }

    #[test]
    fn test_that_market_sell_increases_cash_and_decreases_holdings() {
        let mut brkr = setup();
        brkr.deposit_cash(&100_000.0);
        let res = brkr.send_order(UistOrder::market_buy("ABC", 495.0));
        assert!(matches!(res, UistBrokerEvent::OrderSentToExchange(..)));
        brkr.check();
        let cash = brkr.get_cash_balance();

        brkr.check();

        let res = brkr.send_order(UistOrder::market_sell("ABC", 295.0));
        assert!(matches!(res, UistBrokerEvent::OrderSentToExchange(..)));

        brkr.check();
        let cash0 = brkr.get_cash_balance();

        let qty = brkr.get_position_qty("ABC").unwrap_or_default();
        assert_eq!(*qty, 200.0);
        assert!(*cash0 > *cash);
    }

    #[test]
    fn test_that_valuation_updates_in_next_period() {
        let mut brkr = setup();
        brkr.deposit_cash(&100_000.0);

        brkr.send_order(UistOrder::market_buy("ABC", 495.0));
        brkr.check();

        let val = brkr.get_position_value("ABC");

        brkr.check();
        let val1 = brkr.get_position_value("ABC");
        assert_ne!(val, val1);
    }

    #[test]
    fn test_that_profit_calculation_is_accurate() {
        let mut brkr = setup();
        brkr.deposit_cash(&100_000.0);
        brkr.send_order(UistOrder::market_buy("ABC", 495.0));
        brkr.check();

        brkr.check();

        let profit = brkr.get_position_profit("ABC").unwrap();
        assert_eq!(*profit, -4950.00);
    }

    #[test]
    fn test_that_broker_build_passes_without_trade_costs() {
        let mut source_builder = PenelopeBuilder::new();
        source_builder.add_quote(100.00, 101.00, 100, "ABC");
        source_builder.add_quote(104.00, 105.00, 101, "ABC");
        source_builder.add_quote(95.00, 96.00, 102, "ABC");

        let (price_source, clock) =
            source_builder.build_with_frequency(rotala::clock::Frequency::Second);
        let uist = UistV1::new(clock, price_source, "FAKE");

        let _brkr = UistBrokerBuilder::new()
            .with_exchange(uist)
            .with_trade_costs(vec![BrokerCost::PctOfValue(0.01)])
            .build();
    }

    #[test]
    fn test_that_broker_uses_last_value_if_it_fails_to_find_quote() {
        //If the broker cannot find a quote in the current period for a stock, it automatically
        //uses a value of zero. This is a problem because the current time could a weekend or
        //bank holiday, and if the broker is attempting to value the portfolio on that day
        //they will ask for a quote, not find one, and then use a value of zero which is
        //incorrect.
        let mut source_builder = PenelopeBuilder::new();
        source_builder.add_quote(100.00, 101.00, 100, "ABC");
        source_builder.add_quote(10.00, 11.00, 100, "BCD");

        //Trades execute here
        source_builder.add_quote(100.00, 101.00, 101, "ABC");
        source_builder.add_quote(10.00, 11.00, 101, "BCD");

        //We are missing a quote for BCD on 101, but the broker should return the last seen value
        source_builder.add_quote(104.00, 105.00, 102, "ABC");

        //And when we check the next date, it updates correctly
        source_builder.add_quote(104.00, 105.00, 103, "ABC");
        source_builder.add_quote(12.00, 13.00, 103, "BCD");

        let (price_source, clock) =
            source_builder.build_with_frequency(rotala::clock::Frequency::Second);
        let uist = UistV1::new(clock, price_source, "FAKE");

        let mut brkr = UistBrokerBuilder::new()
            .with_exchange(uist)
            .with_trade_costs(vec![BrokerCost::PctOfValue(0.01)])
            .build();

        brkr.deposit_cash(&100_000.0);

        brkr.send_order(UistOrder::market_buy("ABC", 100.0));
        brkr.send_order(UistOrder::market_buy("BCD", 100.0));

        brkr.check();

        //Missing live quote for BCD
        brkr.check();
        let value = brkr
            .get_position_value("BCD")
            .unwrap_or(CashValue::from(0.0));
        println!("{:?}", value);
        //We test against the bid price, which gives us the value exclusive of the price paid at ask
        assert!(*value == 10.0 * 100.0);

        //BCD has quote again
        brkr.check();

        let value1 = brkr
            .get_position_value("BCD")
            .unwrap_or(CashValue::from(0.0));
        println!("{:?}", value1);
        assert!(*value1 == 12.0 * 100.0);
    }

    #[test]
    fn test_that_broker_handles_negative_cash_balance_due_to_volatility() {
        //Because orders sent to the exchange are not executed instantaneously it is possible for a
        //broker to issue an order for a stock, the price to fall/rise before the trade gets
        //executed, and the broker end up with more/less cash than expected.
        //
        //For example, if orders are issued for 100% of the portfolio then if prices rises then we
        //can end up with negative balances.

        let mut source_builder = PenelopeBuilder::new();
        source_builder.add_quote(100.00, 101.00, 100, "ABC");
        source_builder.add_quote(150.00, 151.00, 101, "ABC");
        source_builder.add_quote(150.00, 151.00, 102, "ABC");

        let (price_source, clock) =
            source_builder.build_with_frequency(rotala::clock::Frequency::Second);
        let uist = UistV1::new(clock, price_source, "FAKE");

        let mut brkr = UistBrokerBuilder::new()
            .with_exchange(uist)
            .with_trade_costs(vec![BrokerCost::PctOfValue(0.01)])
            .build();

        brkr.deposit_cash(&100_000.0);
        //Because the price of ABC rises after this order is sent, we will end up with a negative
        //cash balance after the order is executed
        brkr.send_order(UistOrder::market_buy("ABC", 700.0));

        //Trades execute
        brkr.check();

        let cash = brkr.get_cash_balance();
        assert!(*cash < 0.0);

        //Broker rebalances to raise cash
        brkr.check();
        let cash1 = brkr.get_cash_balance();
        assert!(*cash1 > 0.0);
    }

    #[test]
    fn test_that_broker_stops_when_liquidation_fails() {
        let mut source_builder = PenelopeBuilder::new();
        source_builder.add_quote(100.00, 101.00, 100, "ABC");
        //Price doubles over one tick so that the broker is trading on information that has become
        //very inaccurate
        source_builder.add_quote(200.00, 201.00, 101, "ABC");
        source_builder.add_quote(200.00, 201.00, 101, "ABC");

        let (price_source, clock) =
            source_builder.build_with_frequency(rotala::clock::Frequency::Second);
        let uist = UistV1::new(clock, price_source, "FAKE");

        let mut brkr = UistBrokerBuilder::new()
            .with_exchange(uist)
            .with_trade_costs(vec![BrokerCost::PctOfValue(0.01)])
            .build();

        brkr.deposit_cash(&100_000.0);
        //This will use all the available cash balance, the market price doubles so the broker ends
        //up with a shortfall of -100_000.

        brkr.send_order(UistOrder::market_buy("ABC", 990.0));

        brkr.check();
        brkr.check();

        let cash = brkr.get_cash_balance();
        assert!(*cash < 0.0);

        let res = brkr.send_order(UistOrder::market_buy("ABC", 100.0));
        assert!(matches!(res, UistBrokerEvent::OrderInvalid { .. }));

        assert!(matches!(
            brkr.deposit_cash(&100_000.0),
            BrokerCashEvent::OperationFailure { .. }
        ));
        assert!(matches!(
            brkr.withdraw_cash(&100_000.0),
            BrokerCashEvent::OperationFailure { .. }
        ));
    }

    #[test]
    fn test_that_holdings_updates_correctly() {
        let mut brkr = setup();
        brkr.deposit_cash(&100_000.0);
        let res = brkr.send_order(UistOrder::market_buy("ABC", 50.0));
        assert!(matches!(res, UistBrokerEvent::OrderSentToExchange(..)));
        assert_eq!(
            *brkr
                .get_holdings_with_pending()
                .get("ABC")
                .unwrap_or_default(),
            50.0
        );
        brkr.check();
        assert_eq!(*brkr.get_holdings().get("ABC").unwrap_or_default(), 50.0);

        let res = brkr.send_order(UistOrder::market_sell("ABC", 10.0));
        assert!(matches!(res, UistBrokerEvent::OrderSentToExchange(..)));
        assert_eq!(
            *brkr
                .get_holdings_with_pending()
                .get("ABC")
                .unwrap_or_default(),
            40.0
        );
        brkr.check();
        assert_eq!(*brkr.get_holdings().get("ABC").unwrap_or_default(), 40.0);

        let res = brkr.send_order(UistOrder::market_buy("ABC", 50.0));
        assert!(matches!(res, UistBrokerEvent::OrderSentToExchange(..)));
        assert_eq!(
            *brkr
                .get_holdings_with_pending()
                .get("ABC")
                .unwrap_or_default(),
            90.0
        );
        brkr.check();
        assert_eq!(*brkr.get_holdings().get("ABC").unwrap_or_default(), 90.0)
    }

    fn setup_log() -> UistBrokerLog {
        let mut rec = UistBrokerLog::new();

        let t1 = UistTrade::new("ABC", 100.0, 10.00, 100, UistTradeType::Buy);
        let t2 = UistTrade::new("ABC", 500.0, 90.00, 101, UistTradeType::Buy);
        let t3 = UistTrade::new("BCD", 100.0, 100.0, 102, UistTradeType::Buy);
        let t4 = UistTrade::new("BCD", 500.0, 100.00, 103, UistTradeType::Sell);
        let t5 = UistTrade::new("BCD", 50.0, 50.00, 104, UistTradeType::Buy);

        rec.record(t1);
        rec.record(t2);
        rec.record(t3);
        rec.record(t4);
        rec.record(t5);
        rec
    }

    #[test]
    fn test_that_log_filters_trades_between_dates() {
        let log = setup_log();
        let between = log.trades_between(&102.into(), &104.into());
        assert!(between.len() == 3);
    }

    #[test]
    fn test_that_log_calculates_the_cost_basis() {
        let log = setup_log();
        let abc_cost = log.cost_basis("ABC").unwrap();
        let bcd_cost = log.cost_basis("BCD").unwrap();

        assert_eq!(*abc_cost, 6.0);
        assert_eq!(*bcd_cost, 1.0);
    }

    #[test]
    fn diff_direction_correct_if_need_to_buy() {
        let (uist, clock) = random_uist_generator(100);
        let mut brkr = UistBrokerBuilder::new()
            .with_trade_costs(vec![BrokerCost::flat(1.0)])
            .with_exchange(uist)
            .build();

        let mut weights = PortfolioAllocation::new();
        weights.insert("ABC", 1.0);

        brkr.deposit_cash(&100_000.0);
        brkr.check();

        let orders = brkr.diff_brkr_against_target_weights(&weights);

        println!("{:?}", orders);
        let first = orders.first().unwrap();
        assert!(matches!(
            first.get_order_type(),
            UistOrderType::MarketBuy { .. }
        ));
    }

    #[test]
    fn diff_direction_correct_if_need_to_sell() {
        //This is connected to the previous test, if the above fails then this will never pass.
        //However, if the above passes this could still fail.

        let (uist, clock) = random_uist_generator(100);
        let mut brkr = UistBrokerBuilder::new()
            .with_trade_costs(vec![BrokerCost::flat(1.0)])
            .with_exchange(uist)
            .build();

        let mut weights = PortfolioAllocation::new();
        weights.insert("ABC", 1.0);

        brkr.deposit_cash(&100_000.0);
        let orders = brkr.diff_brkr_against_target_weights(&weights);
        brkr.send_orders(&orders);

        brkr.check();

        brkr.check();

        let mut weights1 = PortfolioAllocation::new();
        //This weight needs to very small because it is possible for the data generator to generate
        //a price that drops significantly meaning that rebalancing requires a buy not a sell. This
        //is unlikely but seems to happen eventually.
        weights1.insert("ABC", 0.01);
        let orders1 = brkr.diff_brkr_against_target_weights(&weights1);

        println!("{:?}", orders1);
        let first = orders1.first().unwrap();
        assert!(matches!(
            first.get_order_type(),
            UistOrderType::MarketSell { .. }
        ));
    }

    #[test]
    fn diff_continues_if_security_missing() {
        //In this scenario, the user has inserted incorrect information but this scenario can also occur if there is no quote
        //for a given security on a certain date. We are interested in the latter case, not the former but it is more
        //difficult to test for the latter, and the code should be the same.
        let (uist, clock) = random_uist_generator(100);
        let mut brkr = UistBrokerBuilder::new()
            .with_trade_costs(vec![BrokerCost::flat(1.0)])
            .with_exchange(uist)
            .build();

        let mut weights = PortfolioAllocation::new();
        weights.insert("ABC", 0.5);
        //There is no quote for this security in the underlying data, code should make the assumption (that doesn't apply here)
        //that there is some quote for this security at a later date and continues to generate order for ABC without throwing
        //error
        weights.insert("XYZ", 0.5);

        brkr.deposit_cash(&100_000.0);
        brkr.check();
        let orders = brkr.diff_brkr_against_target_weights(&weights);
        assert!(orders.len() == 1);
    }

    #[test]
    #[should_panic]
    fn diff_panics_if_brkr_has_no_cash() {
        //If we get to a point where the client is diffing without cash, we can assume that no further operations are possible
        //and we should panic
        let (uist, clock) = random_uist_generator(100);
        let mut brkr = UistBrokerBuilder::new()
            .with_trade_costs(vec![BrokerCost::flat(1.0)])
            .with_exchange(uist)
            .build();

        let mut weights = PortfolioAllocation::new();
        weights.insert("ABC", 1.0);

        brkr.check();
        brkr.diff_brkr_against_target_weights(&weights);
    }

    #[test]
    fn can_estimate_trade_costs_of_proposed_trade() {
        let pershare = BrokerCost::per_share(0.1);
        let flat = BrokerCost::flat(10.0);
        let pct = BrokerCost::pct_of_value(0.01);

        let res = pershare.trade_impact(&1000.0, &1.0, true);
        assert!((*res.1).eq(&1.1));

        let res = pershare.trade_impact(&1000.0, &1.0, false);
        assert!((*res.1).eq(&0.9));

        let res = flat.trade_impact(&1000.0, &1.0, true);
        assert!((*res.0).eq(&990.00));

        let res = pct.trade_impact(&100.0, &1.0, true);
        assert!((*res.0).eq(&99.0));

        let costs = vec![pershare, flat];
        let initial = BrokerCost::trade_impact_total(&costs, &1000.0, &1.0, true);
        assert!((*initial.0).eq(&990.00));
        assert!((*initial.1).eq(&1.1));
    }

    #[test]
    fn diff_handles_sent_but_unexecuted_orders() {
        //It is possible for the client to issue orders for infinitely increasing numbers of shares
        //if there is a gap between orders being issued and executed. For example, if we are
        //missing price data the client could think we need 100 shares, that order doesn't get
        //executed on the next tick, and the client then issues orders for another 100 shares.
        //
        //This is not possible without earlier price data either. If there is no price data then
        //the diff will be unable to work out how many shares are required. So the test case is
        //some price but no price for the execution period.
        let mut source_builder = PenelopeBuilder::new();
        source_builder.add_quote(100.00, 100.00, 100, "ABC");
        source_builder.add_quote(100.00, 100.00, 101, "ABC");
        source_builder.add_quote(100.00, 100.00, 103, "ABC");

        let (price_source, clock) =
            source_builder.build_with_frequency(rotala::clock::Frequency::Second);
        let uist = UistV1::new(clock, price_source, "FAKE");

        let mut brkr = UistBrokerBuilder::new().with_exchange(uist).build();

        brkr.deposit_cash(&100_000.0);

        //No price for security so we haven't diffed correctly
        brkr.check();

        brkr.check();

        let mut target_weights = PortfolioAllocation::new();
        target_weights.insert("ABC", 0.9);

        let orders = brkr.diff_brkr_against_target_weights(&target_weights);
        brkr.send_orders(&orders);

        brkr.check();

        let orders1 = brkr.diff_brkr_against_target_weights(&target_weights);

        brkr.send_orders(&orders1);
        brkr.check();

        dbg!(brkr.get_position_qty("ABC"));
        //If the logic isn't correct the orders will have doubled up to 1800
        assert_eq!(*brkr.get_position_qty("ABC").unwrap(), 900.0);
    }

    #[tokio::test]
    async fn diff_handles_case_when_existing_order_requires_sell_to_rebalance() {
        //Tests similar scenario to previous test but for the situation in which the price is
        //missing, and we try to rebalance by buying but the pending order is for a significantly
        //greater amount of shares than we now need (e.g. we have a price of X, we miss a price,
        //and then it drops 20%).
        let mut source_builder = PenelopeBuilder::new();
        source_builder.add_quote(100.00, 100.00, 100, "ABC");
        source_builder.add_quote(75.00, 75.00, 103, "ABC");
        source_builder.add_quote(75.00, 75.00, 104, "ABC");

        let (price_source, clock) =
            source_builder.build_with_frequency(rotala::clock::Frequency::Second);
        let uist = UistV1::new(clock, price_source, "FAKE");

        let mut brkr = UistBrokerBuilder::new().with_exchange(uist).build();

        brkr.deposit_cash(&100_000.0);

        let mut target_weights = PortfolioAllocation::new();
        target_weights.insert("ABC", 0.9);
        let orders = brkr.diff_brkr_against_target_weights(&target_weights);
        println!("{:?}", orders);

        brkr.send_orders(&orders);

        //No price for security so we haven't diffed correctly
        brkr.check();

        brkr.check();

        brkr.check();

        let orders1 = brkr.diff_brkr_against_target_weights(&target_weights);
        println!("{:?}", orders1);

        brkr.send_orders(&orders1);

        brkr.check();

        println!("{:?}", brkr.get_holdings());
        //If the logic isn't correct then the order will be for less shares than is actually
        //required by the newest price
        assert_eq!(*brkr.get_position_qty("ABC").unwrap(), 1200.0);
    }
}