Skip to main content

rustrade_execution/exchange/mock/
request.rs

1use crate::{
2    UnindexedAccountSnapshot,
3    balance::AssetBalance,
4    order::{
5        Order,
6        request::{OrderRequestCancel, OrderRequestOpen, UnindexedOrderResponseCancel},
7        state::{Open, UnindexedOrderState},
8    },
9    trade::Trade,
10};
11use chrono::{DateTime, Utc};
12use rustrade_instrument::{
13    asset::name::AssetNameExchange, exchange::ExchangeId, instrument::name::InstrumentNameExchange,
14};
15use tokio::sync::oneshot;
16
17#[derive(Debug)]
18pub struct MockExchangeRequest {
19    pub time_request: DateTime<Utc>,
20    pub kind: MockExchangeRequestKind,
21}
22
23impl MockExchangeRequest {
24    pub fn new(time_request: DateTime<Utc>, kind: MockExchangeRequestKind) -> Self {
25        Self { time_request, kind }
26    }
27
28    pub fn fetch_account_snapshot(
29        time_request: DateTime<Utc>,
30        response_tx: oneshot::Sender<UnindexedAccountSnapshot>,
31    ) -> Self {
32        Self::new(
33            time_request,
34            MockExchangeRequestKind::FetchAccountSnapshot { response_tx },
35        )
36    }
37
38    pub fn fetch_balances(
39        time_request: DateTime<Utc>,
40        assets: Vec<AssetNameExchange>,
41        response_tx: oneshot::Sender<Vec<AssetBalance<AssetNameExchange>>>,
42    ) -> Self {
43        Self::new(
44            time_request,
45            MockExchangeRequestKind::FetchBalances {
46                response_tx,
47                assets,
48            },
49        )
50    }
51
52    pub fn fetch_orders_open(
53        time_request: DateTime<Utc>,
54        instruments: Vec<InstrumentNameExchange>,
55        response_tx: oneshot::Sender<Vec<Order<ExchangeId, InstrumentNameExchange, Open>>>,
56    ) -> Self {
57        Self::new(
58            time_request,
59            MockExchangeRequestKind::FetchOrdersOpen {
60                response_tx,
61                instruments,
62            },
63        )
64    }
65
66    pub fn fetch_trades(
67        time_request: DateTime<Utc>,
68        response_tx: oneshot::Sender<Vec<Trade<AssetNameExchange, InstrumentNameExchange>>>,
69        time_since: DateTime<Utc>,
70    ) -> Self {
71        Self::new(
72            time_request,
73            MockExchangeRequestKind::FetchTrades {
74                response_tx,
75                time_since,
76            },
77        )
78    }
79
80    pub fn cancel_order(
81        time_request: DateTime<Utc>,
82        response_tx: oneshot::Sender<UnindexedOrderResponseCancel>,
83        request: OrderRequestCancel<ExchangeId, InstrumentNameExchange>,
84    ) -> Self {
85        Self::new(
86            time_request,
87            MockExchangeRequestKind::CancelOrder {
88                response_tx,
89                request,
90            },
91        )
92    }
93
94    pub fn open_order(
95        time_request: DateTime<Utc>,
96        response_tx: oneshot::Sender<
97            Order<ExchangeId, InstrumentNameExchange, UnindexedOrderState>,
98        >,
99        request: OrderRequestOpen<ExchangeId, InstrumentNameExchange>,
100    ) -> Self {
101        Self::new(
102            time_request,
103            MockExchangeRequestKind::OpenOrder {
104                response_tx,
105                request,
106            },
107        )
108    }
109}
110
111#[derive(Debug)]
112pub enum MockExchangeRequestKind {
113    FetchAccountSnapshot {
114        response_tx: oneshot::Sender<UnindexedAccountSnapshot>,
115    },
116    FetchBalances {
117        assets: Vec<AssetNameExchange>,
118        response_tx: oneshot::Sender<Vec<AssetBalance<AssetNameExchange>>>,
119    },
120    FetchOrdersOpen {
121        instruments: Vec<InstrumentNameExchange>,
122        response_tx: oneshot::Sender<Vec<Order<ExchangeId, InstrumentNameExchange, Open>>>,
123    },
124    FetchTrades {
125        response_tx: oneshot::Sender<Vec<Trade<AssetNameExchange, InstrumentNameExchange>>>,
126        time_since: DateTime<Utc>,
127    },
128    CancelOrder {
129        response_tx: oneshot::Sender<UnindexedOrderResponseCancel>,
130        request: OrderRequestCancel<ExchangeId, InstrumentNameExchange>,
131    },
132    OpenOrder {
133        response_tx:
134            oneshot::Sender<Order<ExchangeId, InstrumentNameExchange, UnindexedOrderState>>,
135        /// The order to open, carrying on
136        /// [`RequestOpen::market`](crate::order::request::RequestOpen::market) the market its
137        /// sender observed when it decided to open it.
138        ///
139        /// That snapshot is the venue's only price source for a Market order, which carries no
140        /// limit price of its own. It is not repeated as a field of its own here: two copies of
141        /// the same snapshot on one message could disagree, and nothing could arbitrate.
142        request: OrderRequestOpen<ExchangeId, InstrumentNameExchange>,
143    },
144}