rustrade-execution 0.16.0

Stream private account data from financial venues, and execute (live or mock) orders.
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
//! An instrument's order rules (tick, lot step, minimums and maximums) and the check of an order
//! against them before it is sent.
//!
//! A venue publishes, per instrument, the grid a price and a quantity must sit on and the range
//! each must fall in. A client that loads those rules refuses an order that breaks them without
//! sending it, so the caller gets a typed error that says which value and which limit
//! ([`OrderError::InvalidPrecision`] for the grid, [`OrderError::OutOfBounds`] for the range)
//! rather than a venue's text after a round trip. It never rounds: rounding would place a
//! different order from the one asked for.
//!
//! A pass is not a guarantee. The check covers the rules a client has loaded, from the last time
//! it loaded them, and only the ones that can be read off the request. The venue has the last say.

use crate::{
    error::{Bound, BoundsViolation, OrderError, OrderField, PrecisionLimit, PrecisionViolation},
    order::OrderKind,
};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

/// Whether a client checks an order against its instrument's [`OrderRules`] before sending it.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum PreSendRules {
    /// Refuse an order that breaks a rule, unsent. The default: a refusal costs no request, and
    /// carries the limit that was broken.
    #[default]
    Enforce,
    /// Send every order and leave the rules to the venue. Choose it when the loaded rules could
    /// be wrong, for example because the venue changed them since they were loaded.
    Off,
}

/// The grid and range of one kind of value: a price, a quantity or a notional.
///
/// Each part is optional. A venue that publishes a zero for one means the rule is off, and
/// [`ValueRule::new`] reads zero that way.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, Deserialize, Serialize)]
pub struct ValueRule {
    /// The smallest value allowed.
    pub min: Option<Decimal>,
    /// The largest value allowed.
    pub max: Option<Decimal>,
    /// The value must be a whole multiple of this, counted from zero.
    pub step: Option<Decimal>,
}

impl ValueRule {
    /// A rule from the venue's `min`, `max` and `step`, each off when zero or negative.
    pub fn new(min: Decimal, max: Decimal, step: Decimal) -> Self {
        let on = |value: Decimal| (value > Decimal::ZERO).then_some(value);
        Self {
            min: on(min),
            max: on(max),
            step: on(step),
        }
    }

    /// Whether every part is off.
    pub fn is_off(&self) -> bool {
        self.min.is_none() && self.max.is_none() && self.step.is_none()
    }

    /// `Ok` when `value` is on the grid and in the range, else the first rule it breaks, grid
    /// before range.
    fn check<AssetKey, InstrumentKey>(
        &self,
        field: OrderField,
        value: Decimal,
    ) -> Result<(), OrderError<AssetKey, InstrumentKey>> {
        // A step set to zero by hand, not through `new`, is no grid: `%` would panic on it.
        if let Some(step) = self.step.filter(|step| *step > Decimal::ZERO)
            && !(value % step).is_zero()
        {
            return Err(OrderError::InvalidPrecision(PrecisionViolation::new(
                field,
                value,
                PrecisionLimit::NotOnGrid { step },
            )));
        }
        if let Some(min) = self.min.filter(|min| value < *min) {
            return Err(OrderError::OutOfBounds(BoundsViolation::new(
                field,
                Bound::Min,
                min,
                value,
            )));
        }
        if let Some(max) = self.max.filter(|max| value > *max) {
            return Err(OrderError::OutOfBounds(BoundsViolation::new(
                field,
                Bound::Max,
                max,
                value,
            )));
        }
        Ok(())
    }
}

/// One instrument's order rules.
///
/// Venue-agnostic: a client fills it from its venue's rules and calls [`check`](Self::check).
/// [`Default`] is no rules at all, which passes every order.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, Deserialize, Serialize)]
pub struct OrderRules {
    /// The grid and range of a limit, stop and trigger price, and of a trailing order's
    /// activation price.
    pub price: ValueRule,
    /// The grid and range of every order's quantity.
    pub quantity: ValueRule,
    /// The grid and range of a market order's quantity, on top of [`quantity`](Self::quantity).
    pub market_quantity: ValueRule,
    /// The range of the notional, a price times a quantity. Its `step` is not used.
    pub notional: ValueRule,
}

impl OrderRules {
    /// Rules with `price`, `quantity`, `market_quantity` and `notional` as given.
    pub fn new(
        price: ValueRule,
        quantity: ValueRule,
        market_quantity: ValueRule,
        notional: ValueRule,
    ) -> Self {
        Self {
            price,
            quantity,
            market_quantity,
            notional,
        }
    }

    /// Whether these rules check nothing.
    pub fn is_off(&self) -> bool {
        self.price.is_off()
            && self.quantity.is_off()
            && self.market_quantity.is_off()
            && self.notional.is_off()
    }

    /// Check an order of `kind`, with its limit `price` and `quantity`, against the rules.
    ///
    /// Returns the first rule the order breaks, or `Ok` if it breaks none, checking in this order:
    /// - **Prices**, the grid and then the range, each on its own value: the limit `price` of a
    ///   limit, stop-limit, take-profit-limit or fixed-limit trailing order; the trigger price of
    ///   a stop, stop-limit, take-profit or take-profit-limit order; and a trailing order's
    ///   activation price when it has one. A market order has no price to check.
    /// - **Quantity**, against [`quantity`](Self::quantity) and, for a market order, then
    ///   [`market_quantity`](Self::market_quantity).
    /// - **Notional**, `price × quantity`, only where the price is the order's own limit: a limit,
    ///   stop-limit, take-profit-limit or fixed-limit trailing order. Never a market, plain stop
    ///   or trailing-stop order: the venue prices them at a reference price this check cannot
    ///   know. An order with no `price` skips it.
    ///
    /// A closing order is checked like any other: a venue that enforces a minimum notional
    /// enforces it on a close too, so an exemption would move the refusal to the venue.
    ///
    /// # Errors
    /// [`OrderError::InvalidPrecision`] with [`PrecisionLimit::NotOnGrid`] for a value off its
    /// grid, [`OrderError::OutOfBounds`] for one outside its range.
    pub fn check<AssetKey, InstrumentKey>(
        &self,
        kind: OrderKind,
        price: Option<Decimal>,
        quantity: Decimal,
    ) -> Result<(), OrderError<AssetKey, InstrumentKey>> {
        // A match with no wildcard, so that a new `OrderKind` fails to compile until it is
        // classified here.
        let (limit_price, trigger_price, has_own_limit) = match kind {
            OrderKind::Market => (None, None, false),
            OrderKind::Limit => (price, None, true),
            OrderKind::Stop { trigger_price } | OrderKind::TakeProfit { trigger_price } => {
                (None, Some(trigger_price), false)
            }
            OrderKind::StopLimit { trigger_price }
            | OrderKind::TakeProfitLimit { trigger_price } => (price, Some(trigger_price), true),
            OrderKind::TrailingStop {
                activation_price, ..
            } => (None, activation_price, false),
            OrderKind::TrailingStopFixedLimit {
                activation_price, ..
            } => (price, activation_price, true),
            // The venue prices it from its stop and offset: there is no price to read.
            OrderKind::TrailingStopLimit { .. } => (None, None, false),
        };

        if let Some(price) = limit_price {
            self.price.check(OrderField::Price, price)?;
        }
        if let Some(trigger) = trigger_price {
            self.price.check(OrderField::TriggerPrice, trigger)?;
        }
        self.quantity.check(OrderField::Quantity, quantity)?;
        if matches!(kind, OrderKind::Market) {
            self.market_quantity.check(OrderField::Quantity, quantity)?;
        }
        if let Some(price) = limit_price.filter(|_| has_own_limit) {
            // A product beyond `Decimal`'s range is beyond any maximum too.
            let notional = price.saturating_mul(quantity);
            self.notional.check(OrderField::Notional, notional)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::order::TrailingOffsetType;
    use rust_decimal_macros::dec;

    type Error = OrderError<(), ()>;

    /// BTCUSDT-like: tick 0.01, lot 0.001 in 0.001..=100, market lot up to 10, notional 5..=9e6.
    fn rules() -> OrderRules {
        OrderRules::new(
            ValueRule::new(dec!(0.01), dec!(1000000), dec!(0.01)),
            ValueRule::new(dec!(0.001), dec!(100), dec!(0.001)),
            ValueRule::new(dec!(0), dec!(10), dec!(0)),
            ValueRule::new(dec!(5), dec!(9000000), dec!(0)),
        )
    }

    fn check(kind: OrderKind, price: Option<Decimal>, quantity: Decimal) -> Result<(), Error> {
        rules().check(kind, price, quantity)
    }

    fn bounds(field: OrderField, bound: Bound, limit: Decimal, value: Decimal) -> Error {
        OrderError::OutOfBounds(BoundsViolation::new(field, bound, limit, value))
    }

    fn off_grid(field: OrderField, value: Decimal, step: Decimal) -> Error {
        OrderError::InvalidPrecision(PrecisionViolation::new(
            field,
            value,
            PrecisionLimit::NotOnGrid { step },
        ))
    }

    #[test]
    fn zero_or_negative_means_the_part_is_off() {
        let rule = ValueRule::new(dec!(0), dec!(-1), dec!(0));
        assert!(rule.is_off());
        assert!(OrderRules::default().is_off());
        assert!(!rules().is_off());
        // One part on is enough for the rules to be on.
        let on = ValueRule::new(dec!(1), dec!(0), dec!(0));
        let off = ValueRule::default();
        assert!(!OrderRules::new(on, off, off, off).is_off());
        assert!(!OrderRules::new(off, on, off, off).is_off());
        assert!(!OrderRules::new(off, off, on, off).is_off());
        assert!(!OrderRules::new(off, off, off, on).is_off());
        assert_eq!(
            OrderRules::default().check::<(), ()>(OrderKind::Limit, Some(dec!(0.0001)), dec!(7.77)),
            Ok(())
        );
    }

    #[test]
    fn a_step_of_zero_set_by_hand_is_no_grid_not_a_panic() {
        let mut rules = OrderRules::default();
        rules.quantity.step = Some(Decimal::ZERO);
        assert_eq!(
            rules.check::<(), ()>(OrderKind::Market, None, dec!(1.5)),
            Ok(())
        );
    }

    #[test]
    fn a_limit_order_on_the_grid_and_in_range_passes() {
        assert_eq!(
            check(OrderKind::Limit, Some(dec!(50000.12)), dec!(0.5)),
            Ok(())
        );
    }

    #[test]
    fn a_price_off_the_tick_grid_is_refused_with_the_step() {
        assert_eq!(
            check(OrderKind::Limit, Some(dec!(50000.123)), dec!(0.5)),
            Err(off_grid(OrderField::Price, dec!(50000.123), dec!(0.01)))
        );
    }

    #[test]
    fn a_quantity_off_the_lot_grid_is_refused_with_the_step() {
        assert_eq!(
            check(OrderKind::Limit, Some(dec!(50000)), dec!(0.0015)),
            Err(off_grid(OrderField::Quantity, dec!(0.0015), dec!(0.001)))
        );
    }

    #[test]
    fn a_grid_is_counted_from_zero_not_from_the_minimum() {
        // 0.003 is 1.5 steps of 0.002: off a grid counted from zero, though a minimum of 0.002
        // would put an origin-at-minimum grid's next point at 0.004 as well.
        let rules = OrderRules::new(
            ValueRule::default(),
            ValueRule::new(dec!(0.002), dec!(0), dec!(0.002)),
            ValueRule::default(),
            ValueRule::default(),
        );
        assert_eq!(
            rules.check::<(), ()>(OrderKind::Market, None, dec!(0.003)),
            Err(off_grid(OrderField::Quantity, dec!(0.003), dec!(0.002)))
        );
        assert_eq!(
            rules.check::<(), ()>(OrderKind::Market, None, dec!(0.004)),
            Ok(())
        );
    }

    #[test]
    fn a_price_beyond_either_bound_is_refused_with_the_limit() {
        // On the grid, but beyond a bound.
        let rules = OrderRules::new(
            ValueRule::new(dec!(1), dec!(10), dec!(0.5)),
            ValueRule::default(),
            ValueRule::default(),
            ValueRule::default(),
        );
        assert_eq!(
            rules.check::<(), ()>(OrderKind::Limit, Some(dec!(0.5)), dec!(1)),
            Err(bounds(OrderField::Price, Bound::Min, dec!(1), dec!(0.5)))
        );
        assert_eq!(
            rules.check::<(), ()>(OrderKind::Limit, Some(dec!(10.5)), dec!(1)),
            Err(bounds(OrderField::Price, Bound::Max, dec!(10), dec!(10.5)))
        );
        assert_eq!(
            rules.check::<(), ()>(OrderKind::Limit, Some(dec!(1)), dec!(1)),
            Ok(())
        );
        assert_eq!(
            rules.check::<(), ()>(OrderKind::Limit, Some(dec!(10)), dec!(1)),
            Ok(())
        );
    }

    #[test]
    fn a_quantity_beyond_either_bound_is_refused_and_the_bounds_are_inclusive() {
        assert_eq!(
            check(OrderKind::Limit, Some(dec!(50000)), dec!(100.001)),
            Err(bounds(
                OrderField::Quantity,
                Bound::Max,
                dec!(100),
                dec!(100.001)
            ))
        );
        assert_eq!(
            check(OrderKind::Limit, Some(dec!(50000)), dec!(0)),
            Err(bounds(
                OrderField::Quantity,
                Bound::Min,
                dec!(0.001),
                dec!(0)
            ))
        );
        assert_eq!(
            check(OrderKind::Limit, Some(dec!(50000)), dec!(100)),
            Ok(())
        );
        assert_eq!(
            check(OrderKind::Limit, Some(dec!(50000)), dec!(0.001)),
            Ok(())
        );
    }

    #[test]
    fn the_market_quantity_rule_applies_to_a_market_order_only() {
        assert_eq!(
            check(OrderKind::Market, None, dec!(10.5)),
            Err(bounds(
                OrderField::Quantity,
                Bound::Max,
                dec!(10),
                dec!(10.5)
            ))
        );
        assert_eq!(
            check(OrderKind::Limit, Some(dec!(50000)), dec!(10.5)),
            Ok(())
        );
    }

    #[test]
    fn a_market_order_also_obeys_the_ordinary_quantity_rule() {
        assert_eq!(
            check(OrderKind::Market, None, dec!(0.0005)),
            Err(off_grid(OrderField::Quantity, dec!(0.0005), dec!(0.001)))
        );
    }

    #[test]
    fn the_notional_of_a_limit_order_is_its_price_times_its_quantity() {
        assert_eq!(
            check(OrderKind::Limit, Some(dec!(1000)), dec!(0.004)),
            Err(bounds(OrderField::Notional, Bound::Min, dec!(5), dec!(4)))
        );
        assert_eq!(
            check(OrderKind::Limit, Some(dec!(1000)), dec!(0.005)),
            Ok(())
        );
        assert_eq!(
            check(OrderKind::Limit, Some(dec!(1000000)), dec!(10)),
            Err(bounds(
                OrderField::Notional,
                Bound::Max,
                dec!(9000000),
                dec!(10000000)
            ))
        );
    }

    #[test]
    fn a_notional_beyond_the_decimal_range_is_above_the_maximum_not_a_panic() {
        let rules = OrderRules::new(
            ValueRule::default(),
            ValueRule::default(),
            ValueRule::default(),
            ValueRule::new(dec!(0), dec!(1), dec!(0)),
        );
        assert!(matches!(
            rules.check::<(), ()>(OrderKind::Limit, Some(Decimal::MAX), Decimal::MAX),
            Err(OrderError::OutOfBounds(v)) if v.bound == Bound::Max
        ));
    }

    #[test]
    fn a_stop_limit_checks_the_price_the_trigger_and_the_notional() {
        let kind = |trigger_price| OrderKind::StopLimit { trigger_price };
        assert_eq!(
            check(kind(dec!(100.001)), Some(dec!(100)), dec!(1)),
            Err(off_grid(
                OrderField::TriggerPrice,
                dec!(100.001),
                dec!(0.01)
            ))
        );
        assert_eq!(
            check(kind(dec!(100)), Some(dec!(100.001)), dec!(1)),
            Err(off_grid(OrderField::Price, dec!(100.001), dec!(0.01)))
        );
        assert_eq!(
            check(kind(dec!(100)), Some(dec!(1)), dec!(1)),
            Err(bounds(OrderField::Notional, Bound::Min, dec!(5), dec!(1)))
        );
        assert_eq!(check(kind(dec!(100)), Some(dec!(100)), dec!(1)), Ok(()));
    }

    #[test]
    fn a_take_profit_limit_is_checked_like_a_stop_limit() {
        assert_eq!(
            check(
                OrderKind::TakeProfitLimit {
                    trigger_price: dec!(100.001)
                },
                Some(dec!(100)),
                dec!(1)
            ),
            Err(off_grid(
                OrderField::TriggerPrice,
                dec!(100.001),
                dec!(0.01)
            ))
        );
    }

    #[test]
    fn a_plain_stop_checks_its_trigger_and_never_a_notional() {
        // A 1 USDT order would break the minimum notional if the trigger priced it.
        for kind in [
            OrderKind::Stop {
                trigger_price: dec!(1),
            },
            OrderKind::TakeProfit {
                trigger_price: dec!(1),
            },
        ] {
            assert_eq!(check(kind, None, dec!(1)), Ok(()), "{kind:?}");
        }
        assert_eq!(
            check(
                OrderKind::Stop {
                    trigger_price: dec!(1.001)
                },
                None,
                dec!(1)
            ),
            Err(off_grid(OrderField::TriggerPrice, dec!(1.001), dec!(0.01)))
        );
    }

    #[test]
    fn a_market_order_never_checks_a_notional_or_a_price() {
        // A request price on a market order is ignored.
        assert_eq!(
            check(OrderKind::Market, Some(dec!(0.001)), dec!(0.001)),
            Ok(())
        );
    }

    #[test]
    fn a_trailing_stop_checks_its_activation_price_only() {
        let kind = |activation_price| OrderKind::TrailingStop {
            offset: dec!(50),
            offset_type: TrailingOffsetType::BasisPoints,
            activation_price,
        };
        assert_eq!(check(kind(None), None, dec!(1)), Ok(()));
        assert_eq!(check(kind(Some(dec!(1))), None, dec!(1)), Ok(()));
        assert_eq!(
            check(kind(Some(dec!(1.001))), None, dec!(1)),
            Err(off_grid(OrderField::TriggerPrice, dec!(1.001), dec!(0.01)))
        );
    }

    #[test]
    fn a_fixed_limit_trailing_stop_checks_its_limit_activation_and_notional() {
        let kind = |activation_price| OrderKind::TrailingStopFixedLimit {
            offset: dec!(50),
            offset_type: TrailingOffsetType::BasisPoints,
            activation_price,
        };
        assert_eq!(
            check(kind(None), Some(dec!(100.001)), dec!(1)),
            Err(off_grid(OrderField::Price, dec!(100.001), dec!(0.01)))
        );
        assert_eq!(
            check(kind(Some(dec!(1.001))), Some(dec!(100)), dec!(1)),
            Err(off_grid(OrderField::TriggerPrice, dec!(1.001), dec!(0.01)))
        );
        assert_eq!(
            check(kind(None), Some(dec!(1)), dec!(1)),
            Err(bounds(OrderField::Notional, Bound::Min, dec!(5), dec!(1)))
        );
        assert_eq!(check(kind(None), Some(dec!(100)), dec!(1)), Ok(()));
    }

    #[test]
    fn a_limit_order_without_a_price_skips_the_price_and_notional_checks() {
        assert_eq!(check(OrderKind::Limit, None, dec!(1)), Ok(()));
    }

    #[test]
    fn a_grid_break_is_reported_before_a_range_break_of_the_same_value() {
        // 0.0005 is below the minimum quantity and off the grid: the grid is reported.
        assert_eq!(
            check(OrderKind::Limit, Some(dec!(50000)), dec!(0.0005)),
            Err(off_grid(OrderField::Quantity, dec!(0.0005), dec!(0.001)))
        );
    }

    #[test]
    fn a_pre_send_rules_deserializes_from_snake_case_and_defaults_to_enforce() {
        assert_eq!(PreSendRules::default(), PreSendRules::Enforce);
        assert_eq!(
            serde_json::from_str::<PreSendRules>("\"off\"").ok(),
            Some(PreSendRules::Off)
        );
    }

    #[test]
    fn the_refusals_name_the_value_and_the_limit() {
        let text = |result: Result<(), Error>| match result {
            Err(OrderError::OutOfBounds(violation)) => violation.to_string(),
            Err(OrderError::InvalidPrecision(violation)) => violation.to_string(),
            other => panic!("expected a refusal, got {other:?}"),
        };
        assert_eq!(
            text(check(OrderKind::Limit, Some(dec!(1000)), dec!(0.004))),
            "notional 4.000 is below the minimum 5"
        );
        assert_eq!(
            text(check(OrderKind::Limit, Some(dec!(1000)), dec!(101))),
            "quantity 101 is above the maximum 100"
        );
        assert_eq!(
            text(check(OrderKind::Limit, Some(dec!(1000.001)), dec!(1))),
            "price 1000.001 is not a multiple of 0.01"
        );
    }
}