polyoxide-venue 0.41.1

Shared vocabulary for the polyoxide venue clients: error classes, the status and close-code maps, the Retry-After parser, wire enums, positional decimals and Unix-millisecond time
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
//! The trading trait, its records, and the stream it reports on.

use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

use futures_core::Stream;
use rust_decimal::Decimal;

use crate::{
    ClassifiedError, ClientOrderId, Extensions, FillId, MarketKey, OrderId, Side, TradeId,
    UnixMillis,
};

/// Places, cancels and reconciles orders on one venue, and reports what
/// happens to them.
///
/// Every method returns [`ClassifiedError`], which an implementation builds
/// from its module error with `?`. Held as `Arc<DynTrading<'static>>` by code
/// that picks its venue at run time.
///
/// # Requests
///
/// An [`OrderRequest`] names an outcome or instrument key, a [`Side`], a price
/// and a size in the instrument's native quantity. The venue's own options
/// ride in its `extensions`, and the venue reads only the option types it
/// declares: [`place`](Self::place) refuses a request carrying any other, as
/// [`Class::InvalidRequest`](crate::Class::InvalidRequest), and sends nothing
/// ([`Extensions::ensure_declared`]). So does a key of another venue or
/// product, and so does a request whose `post_only`, `reduce_only` or time in
/// force the venue cannot honour: it is refused, never ignored.
///
/// # Kills
///
/// An immediate-or-cancel order that matched nothing, or a fill-or-kill order
/// that could not fill in full, is not an error: `place` returns `Ok` with
/// [`OrderStatus::Killed`], and its reason. `filled` says how much an
/// immediate-or-cancel order matched before its remainder was cancelled.
///
/// # Events
///
/// [`events`](Self::events) is fed from an unbounded queue, so a slow consumer
/// loses no acknowledgement or fill. The venue replays nothing: what happened
/// while the connection that carries fills was down is not sent again.
///
/// The stream's first item is [`TradingEvent::Resync`], unless the stream
/// cannot open, when its only item is that error. `Resync` is yielded once the
/// connection's first attempt to connect has ended, whether it went live or
/// began in an outage, so a fill between `events()` returning and the first
/// subscription is covered by a reconciliation. `Resync` comes again after
/// every reconnect of the connection that carries fills, and when the venue
/// voids a fill it reported. The consumer reconciles its inventory each
/// time through [`open_orders`](Self::open_orders) and the venue's trade history.
#[dynosaur::dynosaur(pub DynTrading = dyn(box) Trading)]
pub trait Trading: Send + Sync {
    /// Places `order` and returns it as the venue accepted it: open, filled,
    /// or killed.
    fn place(
        &self,
        order: OrderRequest,
    ) -> impl Future<Output = Result<Order, ClassifiedError>> + Send;

    /// Cancels the order `id`.
    ///
    /// Cancelling an order that has already ended, or that the venue does not
    /// know, is an error classed
    /// [`Class::VenueRefusal`](crate::Class::VenueRefusal). The order's final
    /// state arrives through [`events`](Self::events) and
    /// [`open_orders`](Self::open_orders).
    fn cancel(&self, id: &OrderId) -> impl Future<Output = Result<(), ClassifiedError>> + Send;

    /// Cancels every open order, or every open order on `key`, and returns the
    /// ids the venue cancelled. An order the venue did not cancel is left out
    /// of them and stays open, so [`open_orders`](Self::open_orders) shows it.
    fn cancel_all(
        &self,
        key: Option<&MarketKey>,
    ) -> impl Future<Output = Result<Vec<OrderId>, ClassifiedError>> + Send;

    /// Every open order, or every open order on `key`.
    fn open_orders(
        &self,
        key: Option<&MarketKey>,
    ) -> impl Future<Output = Result<Vec<Order>, ClassifiedError>> + Send;

    /// Every position the account holds.
    fn positions(&self) -> impl Future<Output = Result<Vec<Position>, ClassifiedError>> + Send;

    /// Every balance the account holds.
    fn balances(&self) -> impl Future<Output = Result<Vec<Balance>, ClassifiedError>> + Send;

    /// The account's acknowledgements and fills from now on, beginning with a
    /// [`TradingEvent::Resync`] and carrying one after every reconnect.
    ///
    /// Each call returns a stream of its own, which sees every event from its
    /// first subscription on; its leading `Resync` covers what came earlier
    /// (D3). An order may be reported (acked or filled) before the
    /// [`place`](Self::place) that created it returns. See [`TradingEvents`]
    /// for when the stream ends.
    fn events(&self) -> TradingEvents;
}

/// The stream [`Trading::events`] returns: the account's acknowledgements and
/// fills, from an unbounded queue.
///
/// It is `Send`, `Unpin` and `'static`, so a consumer can move it onto a task
/// of its own, whether it reached the venue statically or through
/// `Arc<DynTrading<'static>>`. (A trait method returning `impl Stream + 'static`
/// would lose the `'static` through the dyn wrapper, so the stream is this
/// named type.)
///
/// Its first item is [`TradingEvent::Resync`], unless the stream cannot open,
/// when its only item is that error. The venue replays nothing: after
/// every reconnect of the connection that carries fills, and when the venue
/// voids a fill it reported (a trade that failed after it matched, which the
/// stream has no event to take back), the stream yields `Resync` before any
/// later fill. What happened while the connection was down will not arrive, and
/// the consumer reconciles through [`Trading::open_orders`] and the venue's
/// trade history.
///
/// An `Err` item the venue recovered from, such as a lost connection it
/// re-established, is followed by more items. After one it could not recover
/// from, the stream ends. `None` means the feed has stopped for good.
pub struct TradingEvents(
    Pin<Box<dyn Stream<Item = Result<TradingEvent, ClassifiedError>> + Send + 'static>>,
);

impl TradingEvents {
    /// Wraps the stream a [`Trading`] implementation feeds from its queue.
    pub fn new(
        stream: impl Stream<Item = Result<TradingEvent, ClassifiedError>> + Send + 'static,
    ) -> Self {
        Self(Box::pin(stream))
    }
}

impl fmt::Debug for TradingEvents {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("TradingEvents(..)")
    }
}

impl Stream for TradingEvents {
    type Item = Result<TradingEvent, ClassifiedError>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.0.as_mut().poll_next(cx)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

/// How long an order rests.
///
/// An order that expires at a set time is a venue option.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum TimeInForce {
    /// Rests until it fills or is cancelled.
    #[default]
    GoodTilCancelled,
    /// Matches what it can at once; the remainder is cancelled.
    ImmediateOrCancel,
    /// Fills in full at once, or not at all. A venue that cancels one partway,
    /// such as for self-trade prevention, ends it
    /// [`Cancelled`](OrderStatus::Cancelled), possibly partly filled, not
    /// [`Killed`](OrderStatus::Killed).
    FillOrKill,
}

/// An order to place.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct OrderRequest {
    /// The instrument, or the outcome of an event, to trade.
    pub key: MarketKey,
    /// Buy or sell.
    pub side: Side,
    /// The limit price.
    pub price: Decimal,
    /// The size, in the instrument's native quantity.
    pub size: Decimal,
    /// How long it rests.
    pub time_in_force: TimeInForce,
    /// The caller's id for it. The venue echoes it, or the implementation
    /// keeps it for the orders it placed, as its documentation says; a venue
    /// that can do neither refuses a request that carries one.
    pub client_order_id: Option<ClientOrderId>,
    /// Rest only: refuse to take liquidity.
    pub post_only: bool,
    /// Only reduce a position, never open or grow one.
    pub reduce_only: bool,
    /// The venue's own options; one it does not declare is refused.
    pub extensions: Extensions,
}

impl OrderRequest {
    /// A good-til-cancelled order to `side` `size` of `key` at `price`, with
    /// no client id, neither flag and no extensions.
    pub fn new(key: MarketKey, side: Side, price: Decimal, size: Decimal) -> Self {
        Self {
            key,
            side,
            price,
            size,
            time_in_force: TimeInForce::default(),
            client_order_id: None,
            post_only: false,
            reduce_only: false,
            extensions: Extensions::new(),
        }
    }
}

/// Why an order was killed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum KillReason {
    /// An immediate-or-cancel order's remainder, cancelled because nothing
    /// more matched at once. `filled` says how much did.
    Unmatched,
    /// A fill-or-kill order that could not fill in full.
    Unfillable,
}

/// Where an order stands.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum OrderStatus {
    /// Resting, possibly partly filled.
    Open,
    /// Filled in full.
    Filled,
    /// Cancelled, by the caller or the venue, possibly partly filled.
    Cancelled,
    /// Killed by its time in force: a defined outcome, not an error.
    Killed {
        /// Why.
        reason: KillReason,
    },
}

/// An order as the venue reports it.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Order {
    /// The venue's id for it.
    pub id: OrderId,
    /// The instrument or outcome it trades.
    pub key: MarketKey,
    /// Buy or sell.
    pub side: Side,
    /// The limit price.
    pub price: Decimal,
    /// The size ordered, in the instrument's native quantity.
    pub size: Decimal,
    /// How much has filled.
    pub filled: Decimal,
    /// Where it stands.
    pub status: OrderStatus,
    /// How long it rests.
    pub time_in_force: Option<TimeInForce>,
    /// The caller's id for it.
    pub client_order_id: Option<ClientOrderId>,
    /// When the venue accepted it.
    pub created: Option<UnixMillis>,
    /// The venue's own facts.
    pub extensions: Extensions,
}

impl Order {
    /// The order `id` to `side` `size` of `key` at `price`, with nothing
    /// filled yet.
    pub fn new(
        id: OrderId,
        key: MarketKey,
        side: Side,
        price: Decimal,
        size: Decimal,
        status: OrderStatus,
    ) -> Self {
        Self {
            id,
            key,
            side,
            price,
            size,
            filled: Decimal::ZERO,
            status,
            time_in_force: None,
            client_order_id: None,
            created: None,
            extensions: Extensions::new(),
        }
    }
}

/// Whether a fill took or made liquidity.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Liquidity {
    /// The order rested and was matched.
    Maker,
    /// The order matched a resting one.
    Taker,
}

/// One order's part in a trade.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Fill {
    /// The venue's id for the fill.
    pub id: FillId,
    /// The order filled.
    pub order_id: OrderId,
    /// The trade it was part of.
    pub trade_id: Option<TradeId>,
    /// The instrument or outcome traded.
    pub key: MarketKey,
    /// The order's side.
    pub side: Side,
    /// The price.
    pub price: Decimal,
    /// The size, in the instrument's native quantity.
    pub size: Decimal,
    /// The quote-currency amount, where the venue reports one.
    pub notional: Option<Decimal>,
    /// The fee charged, in the quote currency unless the venue's own facts say
    /// otherwise.
    pub fee: Option<Decimal>,
    /// Whether the order made or took liquidity.
    pub liquidity: Option<Liquidity>,
    /// The caller's id for the order.
    pub client_order_id: Option<ClientOrderId>,
    /// When it filled.
    pub time: UnixMillis,
    /// The venue's own facts.
    pub extensions: Extensions,
}

impl Fill {
    /// The fill `id` of order `order_id`: `size` of `key` at `price`, at
    /// `time`.
    pub fn new(
        id: FillId,
        order_id: OrderId,
        key: MarketKey,
        side: Side,
        price: Decimal,
        size: Decimal,
        time: UnixMillis,
    ) -> Self {
        Self {
            id,
            order_id,
            trade_id: None,
            key,
            side,
            price,
            size,
            notional: None,
            fee: None,
            liquidity: None,
            client_order_id: None,
            time,
            extensions: Extensions::new(),
        }
    }
}

/// What the account holds of one instrument or outcome.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Position {
    /// The instrument or outcome.
    pub key: MarketKey,
    /// The size held, in the instrument's native quantity: positive long,
    /// negative short. A position in an event's outcome is never negative,
    /// since each outcome has its own key.
    pub size: Decimal,
    /// The average price paid.
    pub average_price: Option<Decimal>,
    /// The venue's own facts.
    pub extensions: Extensions,
}

impl Position {
    /// `size` of `key`.
    pub fn new(key: MarketKey, size: Decimal) -> Self {
        Self {
            key,
            size,
            average_price: None,
            extensions: Extensions::new(),
        }
    }
}

/// What the account holds of one asset.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Balance {
    /// The asset, as the venue names it.
    pub asset: String,
    /// The whole balance.
    pub total: Option<Decimal>,
    /// What is free to trade.
    pub available: Option<Decimal>,
    /// The venue's own facts.
    pub extensions: Extensions,
}

impl Balance {
    /// The balance of `asset`, with no amounts yet.
    pub fn new(asset: impl Into<String>) -> Self {
        Self {
            asset: asset.into(),
            total: None,
            available: None,
            extensions: Extensions::new(),
        }
    }
}

/// What [`Trading::events`] reports.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum TradingEvent {
    /// The venue accepted, changed or ended an order.
    Ack(Order),
    /// An order filled, in part or in full.
    Fill(Fill),
    /// Reconcile through [`Trading::open_orders`] and the venue's trade
    /// history: the stream cannot vouch for what it reported or missed before
    /// this point.
    ///
    /// It is the stream's first item, unless the stream cannot open, when its
    /// only item is that error; it comes again after every reconnect of the
    /// connection that carries fills, and comes when the venue voids a fill it
    /// already reported.
    Resync,
}