architect-api 11.6.3

Architect.xyz Trading Platform API
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
use super::ExecutionVenue;
use crate::Dir;
use derive_more::Display;
use rust_decimal::Decimal;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use strum_macros::{EnumString, IntoStaticStr};

/// Information about a symbol related to its execution route.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct ExecutionInfo {
    pub execution_venue: ExecutionVenue,
    /// If the execution venue has stable symbology, this may be populated
    pub exchange_symbol: Option<String>,
    pub tick_size: TickSize,
    pub step_size: Decimal,
    pub min_order_quantity: Decimal,
    pub min_order_quantity_unit: MinOrderQuantityUnit,
    pub is_delisted: bool,
    pub initial_margin: Option<Decimal>,
    pub maintenance_margin: Option<Decimal>,
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum TickSize {
    #[schemars(title = "Simple|Decimal")]
    Simple(Decimal),
    /// List of (threshold, tick_size) pairs.  For price greater than or equal
    /// to each threshold, the tick size is the corresponding value.
    ///
    /// For example, if the thresholds are [(100, 0.01), (200, 0.02)], valid
    /// prices include:
    ///
    /// ...199.98, 199.99, 200.00, 200.02, 200.04...
    #[schemars(title = "Varying")]
    Varying { thresholds: Vec<(Decimal, Decimal)> },
}

impl TickSize {
    pub fn simple(tick_size: Decimal) -> Self {
        Self::Simple(tick_size)
    }

    /// Increment the price by `n` ticks.  Negative values decrement.
    ///
    /// This method assumes:
    ///
    /// - `price` is a valid price (i.e. on a tick and not between ticks)
    /// - `price` is not on or below the first threshold, for negative `n`
    /// - `thresholds` is non-empty, if tick size is `Varying`
    /// - `thresholds` are well-formed[1]
    ///
    /// [1] Sequential thresholds must be arithemetically adjacent; e.g.
    /// `(100, 0.3), (200, _)` is an invalid threshold sequence because
    /// no iteration of ticks can get from 100 to 200.
    pub fn increment(&self, mut price: Decimal, mut n: i32) -> Option<Decimal> {
        if n == 0 {
            return Some(price);
        } else if n < 0 {
            return self.decrement(price, -n);
        }
        // INVARIANT: n > 0
        let thresholds = match self {
            TickSize::Simple(step) => {
                return Some(price + *step * Decimal::from(n));
            }
            TickSize::Varying { thresholds } => thresholds,
        };
        if thresholds.is_empty() {
            return None;
        }
        // INVARIANT: thresholds.len() > 0
        let mut i = thresholds.len() - 1;
        let mut t_bound;
        let mut t_step;
        let mut u_bound;
        loop {
            (t_bound, t_step) = thresholds[i];
            u_bound = thresholds.get(i + 1).map(|(b, _)| *b);
            if price >= t_bound {
                break;
            }
            if i == 0 {
                // didn't find a threshold
                return None;
            }
            i -= 1;
        }
        while n > 0 {
            price += t_step;
            if u_bound.is_some_and(|u| price >= u) {
                // INVARIANT: threshold[i + 1] exists
                // move to next threshold
                i += 1;
                (_, t_step) = thresholds[i];
                u_bound = thresholds.get(i + 1).map(|(b, _)| *b);
            }
            n -= 1;
        }
        Some(price)
    }

    pub fn decrement(&self, mut price: Decimal, mut n: i32) -> Option<Decimal> {
        if n == 0 {
            return Some(price);
        } else if n < 0 {
            return self.increment(price, -n);
        }
        // INVARIANT: n > 0
        let thresholds = match self {
            TickSize::Simple(step) => {
                return Some(price - *step * Decimal::from(n));
            }
            TickSize::Varying { thresholds } => thresholds,
        };
        if thresholds.is_empty() {
            return None;
        }
        // INVARIANT: thresholds.len() > 0
        let mut i = thresholds.len() - 1;
        let mut t_bound;
        let mut t_step;
        loop {
            (t_bound, t_step) = thresholds[i];
            if price >= t_bound {
                break;
            }
            if i == 0 {
                // didn't find a threshold
                return None;
            }
            i -= 1;
        }
        while n > 0 {
            if price == t_bound {
                // on boundary, use previous threshold tick size
                if i == 0 {
                    return None;
                }
                i -= 1;
                (t_bound, t_step) = thresholds[i];
            }
            price -= t_step;
            n -= 1;
        }
        Some(price)
    }

    /// Round the price to make the price more aggressive for the given direction.
    pub fn round_aggressive(&self, price: Decimal, dir: Dir) -> Option<Decimal> {
        match dir {
            Dir::Buy => self.round_up(price),
            Dir::Sell => self.round_down(price),
        }
    }

    /// Round the price to make the price more passive for the given direction.
    pub fn round_passive(&self, price: Decimal, dir: Dir) -> Option<Decimal> {
        match dir {
            Dir::Buy => self.round_down(price),
            Dir::Sell => self.round_up(price),
        }
    }

    pub fn round_up(&self, price: Decimal) -> Option<Decimal> {
        match self {
            TickSize::Simple(tick_size) => {
                if tick_size.is_zero() {
                    return None;
                }
                let remainder = price % tick_size;
                if remainder.is_zero() {
                    // Already on a tick
                    Some(price)
                } else if remainder > Decimal::ZERO {
                    // Positive remainder: round up to next tick
                    Some(price - remainder + tick_size)
                } else {
                    // Negative remainder: already rounded up
                    Some(price - remainder)
                }
            }
            TickSize::Varying { thresholds } => {
                if thresholds.is_empty() {
                    return None;
                }

                // Find the appropriate tick size for this price
                let mut tick_size = thresholds[0].1;
                for (threshold, size) in thresholds {
                    if price >= *threshold {
                        tick_size = *size;
                    } else {
                        break;
                    }
                }

                if tick_size.is_zero() {
                    return None;
                }

                let remainder = price % tick_size;
                if remainder.is_zero() {
                    // Already on a tick
                    Some(price)
                } else if remainder > Decimal::ZERO {
                    // Positive remainder: round up to next tick
                    Some(price - remainder + tick_size)
                } else {
                    // Negative remainder: already rounded up
                    Some(price - remainder)
                }
            }
        }
    }

    pub fn round_down(&self, price: Decimal) -> Option<Decimal> {
        match self {
            TickSize::Simple(tick_size) => {
                if tick_size.is_zero() {
                    return None;
                }
                let remainder = price % tick_size;
                if remainder.is_zero() {
                    // Already on a tick
                    Some(price)
                } else if remainder > Decimal::ZERO {
                    // Positive remainder: round down
                    Some(price - remainder)
                } else {
                    // Negative remainder: need to go down one more tick
                    Some(price - remainder - tick_size)
                }
            }
            TickSize::Varying { thresholds } => {
                if thresholds.is_empty() {
                    return None;
                }

                // Find the appropriate tick size for this price
                let mut tick_size = thresholds[0].1;
                for (threshold, size) in thresholds {
                    if price >= *threshold {
                        tick_size = *size;
                    } else {
                        break;
                    }
                }

                if tick_size.is_zero() {
                    return None;
                }

                let remainder = price % tick_size;
                if remainder.is_zero() {
                    // Already on a tick
                    Some(price)
                } else if remainder > Decimal::ZERO {
                    // Positive remainder: round down
                    Some(price - remainder)
                } else {
                    // Negative remainder: need to go down one more tick
                    Some(price - remainder - tick_size)
                }
            }
        }
    }

    /// Number of ticks between `from` to `to` based on the tick size.
    ///
    /// If `from` is less than `to`, the result is positive.  If `from` is
    /// greater than `to`, the result is negative.
    ///
    /// If step size is zero, return None.
    pub fn signed_tick_distance(&self, from: Decimal, to: Decimal) -> Option<Decimal> {
        if from == to {
            return Some(Decimal::ZERO);
        } else if from > to {
            return self.signed_tick_distance(to, from).map(|d| -d);
        }
        // INVARIANT: from < to
        match self {
            TickSize::Simple(step) => {
                if step.is_zero() {
                    None
                } else {
                    Some((to - from) / *step)
                }
            }
            TickSize::Varying { thresholds } => {
                let mut ticks = Decimal::ZERO;
                let mut price = from;
                let mut i = thresholds.iter();
                let mut step = None;
                // find the first threshold including the price
                while let Some((lower, lower_step)) = i.next() {
                    if price >= *lower {
                        step = Some(*lower_step);
                        break;
                    }
                }
                let mut step = step?; // threshold doesn't include price at all, impossible to compute
                loop {
                    if step.is_zero() {
                        return None;
                    }
                    match i.next() {
                        Some((upper, next_step)) => {
                            // `to` is beyond the upper threshold, compute distance to next threshold
                            if to >= *upper {
                                ticks += (upper - price) / step;
                                price = *upper;
                                step = *next_step;
                            } else {
                                // `to` is below the upper threshold, compute distance to `to` and done
                                ticks += (to - price) / step;
                                break;
                            }
                        }
                        None => {
                            // no more thresholds, compute straightforward distance
                            ticks += (to - price) / step;
                            break;
                        }
                    }
                }
                Some(ticks)
            }
        }
    }
}

impl ExecutionInfo {
    /// Calculate the minimum quantity in base units
    /// When min_order_quantity_unit is Quote, converts using the provided price
    /// Returns Decimal::ZERO if price is needed but not provided or is zero
    pub fn min_quantity_in_base_units(&self, price: Option<Decimal>) -> Option<Decimal> {
        match self.min_order_quantity_unit {
            MinOrderQuantityUnit::Base => Some(self.min_order_quantity),
            MinOrderQuantityUnit::Quote => {
                if let Some(p) = price {
                    if p.is_zero() {
                        None
                    } else {
                        Some(self.min_order_quantity / p)
                    }
                } else {
                    // If price not provided for Quote unit, can't enforce minimum
                    None
                }
            }
        }
    }

    /// Round quantity to the nearest valid step size without checking minimum
    pub fn round_quantity(&self, quantity: Decimal) -> Decimal {
        round_quantity_with_step(quantity, self.step_size)
    }

    /// Round quantity up to the next valid step size without respecting minimum order quantity
    pub fn round_quantity_up(&self, quantity: Decimal) -> Decimal {
        round_quantity_up_with_step(quantity, self.step_size)
    }

    /// Round quantity down to the previous valid step size without respecting minimum order quantity
    pub fn round_quantity_down(&self, quantity: Decimal) -> Decimal {
        round_quantity_down_with_step(quantity, self.step_size)
    }
}

/// Round quantity to the nearest valid step size
pub fn round_quantity_with_step(quantity: Decimal, step_size: Decimal) -> Decimal {
    if step_size.is_zero() {
        return quantity;
    }
    let remainder = quantity % step_size;
    if remainder.is_zero() {
        quantity
    } else {
        let half_step = step_size / Decimal::from(2);
        if remainder >= half_step {
            // Round up
            quantity - remainder + step_size
        } else {
            // Round down
            quantity - remainder
        }
    }
}

/// Round quantity up to the next valid step size
pub fn round_quantity_up_with_step(quantity: Decimal, step_size: Decimal) -> Decimal {
    if step_size.is_zero() {
        return quantity;
    }

    let remainder = quantity % step_size;
    if remainder.is_zero() {
        quantity
    } else {
        quantity - remainder + step_size
    }
}

/// Round quantity down to the previous valid step size
pub fn round_quantity_down_with_step(quantity: Decimal, step_size: Decimal) -> Decimal {
    if step_size.is_zero() {
        return quantity;
    }

    let remainder = quantity % step_size;
    if remainder.is_zero() {
        quantity
    } else {
        quantity - remainder
    }
}

// TODO: un snake_case this
#[derive(
    Default,
    Debug,
    Display,
    Clone,
    Copy,
    EnumString,
    IntoStaticStr,
    Serialize,
    Deserialize,
    JsonSchema,
)]
#[cfg_attr(feature = "juniper", derive(juniper::GraphQLEnum))]
#[serde(tag = "unit", rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
pub enum MinOrderQuantityUnit {
    #[default]
    #[schemars(title = "Base")]
    Base,
    #[schemars(title = "Quote")]
    Quote,
}

#[cfg(feature = "postgres")]
crate::to_sql_str!(MinOrderQuantityUnit);

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

    #[test]
    fn test_round_quantity() {
        let step_size = dec!(0.1);

        assert_eq!(round_quantity_with_step(dec!(1.24), step_size), dec!(1.2));
        assert_eq!(round_quantity_with_step(dec!(1.25), step_size), dec!(1.3));
        assert_eq!(round_quantity_with_step(dec!(0.04), step_size), dec!(0.0));

        assert_eq!(round_quantity_up_with_step(dec!(1.21), step_size), dec!(1.3));
        assert_eq!(round_quantity_up_with_step(dec!(0.01), step_size), dec!(0.1));
        assert_eq!(round_quantity_up_with_step(dec!(0.0), step_size), dec!(0.0));

        assert_eq!(round_quantity_down_with_step(dec!(1.29), step_size), dec!(1.2));
        assert_eq!(round_quantity_down_with_step(dec!(0.09), step_size), dec!(0.0));
        assert_eq!(round_quantity_down_with_step(dec!(0.0), step_size), dec!(0.0));
    }

    #[test]
    fn test_tick_size_decrement() {
        let tick = TickSize::Simple(dec!(0.01));
        assert_eq!(tick.increment(dec!(100.00), -1), Some(dec!(99.99)));

        let tick_varying = TickSize::Varying {
            thresholds: vec![
                (dec!(0), dec!(0.01)),
                (dec!(100), dec!(0.05)),
                (dec!(500), dec!(0.10)),
            ],
        };

        // Test on boundary with negative increment
        assert_eq!(tick_varying.increment(dec!(100.00), -1), Some(dec!(99.99)));
        assert_eq!(tick_varying.increment(dec!(100.05), -1), Some(dec!(100)));
        assert_eq!(tick_varying.increment(dec!(500.00), -1), Some(dec!(499.95)));
    }

    #[test]
    fn test_tick_size_zero() {
        let tick = TickSize::Varying {
            thresholds: vec![
                (dec!(0), dec!(0.01)),
                (dec!(100), dec!(0.05)),
                (dec!(500), dec!(0.10)),
            ],
        };

        assert_eq!(tick.increment(dec!(100.00), 0), Some(dec!(100.00)));
        assert_eq!(tick.increment(dec!(150.00), 0), Some(dec!(150.00)));
        assert_eq!(tick.increment(dec!(50.00), 0), Some(dec!(50.00)));

        // Note: With the assumption that price is NOT below first threshold,
        // this test case is no longer valid
    }

    #[test]
    fn test_tick_size_boundary_behavior() {
        let tick = TickSize::Varying {
            thresholds: vec![
                (dec!(0), dec!(0.01)),
                (dec!(100), dec!(0.05)),
                (dec!(500), dec!(0.10)),
            ],
        };

        // On boundary going up uses new tick size
        assert_eq!(tick.increment(dec!(100.00), 1), Some(dec!(100.05)));
        assert_eq!(tick.increment(dec!(500.00), 1), Some(dec!(500.10)));

        // On boundary going down uses previous tick size
        assert_eq!(tick.increment(dec!(100.00), -1), Some(dec!(99.99)));
        assert_eq!(tick.increment(dec!(500.00), -1), Some(dec!(499.95)));
    }

    #[test]
    fn test_tick_size_multiple_crossings() {
        let tick = TickSize::Varying {
            thresholds: vec![
                (dec!(0), dec!(0.01)),
                (dec!(100), dec!(0.05)),
                (dec!(500), dec!(0.10)),
            ],
        };

        // Cross multiple thresholds going up
        let result = tick.increment(dec!(50), 12000);
        assert_eq!(result, Some(dec!(450)));

        // Test crossing multiple thresholds going down
        // With the boundary handling:
        // - 1000 ticks to get from 600 to 500
        // - Then we're at 500, one more tick at 0.05 gets us to 499.95
        // - Remaining ticks: 5100 - 1000 - 1 = 4099
        // - 4099 * 0.05 = 204.95 movement
        // - 499.95 - 204.95 = 295.00
        let result = tick.increment(dec!(600), -5100);
        assert_eq!(result, Some(dec!(295.00)));
    }

    #[test]
    fn test_round_up_simple() {
        let tick = TickSize::Simple(dec!(0.01));

        // Already on tick
        assert_eq!(tick.round_up(dec!(100.00)), Some(dec!(100.00)));

        // Between ticks - should round up
        assert_eq!(tick.round_up(dec!(100.001)), Some(dec!(100.01)));
        assert_eq!(tick.round_up(dec!(100.005)), Some(dec!(100.01)));
        assert_eq!(tick.round_up(dec!(100.009)), Some(dec!(100.01)));

        // Negative prices
        assert_eq!(tick.round_up(dec!(-99.995)), Some(dec!(-99.99)));
        assert_eq!(tick.round_up(dec!(-100.00)), Some(dec!(-100.00)));

        // Zero tick size
        let zero_tick = TickSize::Simple(dec!(0));
        assert_eq!(zero_tick.round_up(dec!(100.123)), None);
    }

    #[test]
    fn test_round_down_simple() {
        let tick = TickSize::Simple(dec!(0.01));

        // Already on tick
        assert_eq!(tick.round_down(dec!(100.00)), Some(dec!(100.00)));

        // Between ticks - should round down
        assert_eq!(tick.round_down(dec!(100.001)), Some(dec!(100.00)));
        assert_eq!(tick.round_down(dec!(100.005)), Some(dec!(100.00)));
        assert_eq!(tick.round_down(dec!(100.009)), Some(dec!(100.00)));

        // Negative prices
        assert_eq!(tick.round_down(dec!(-99.995)), Some(dec!(-100.00)));
        assert_eq!(tick.round_down(dec!(-100.00)), Some(dec!(-100.00)));

        // Zero tick size
        let zero_tick = TickSize::Simple(dec!(0));
        assert_eq!(zero_tick.round_down(dec!(100.123)), None);
    }

    #[test]
    fn test_round_up_varying() {
        let tick = TickSize::Varying {
            thresholds: vec![
                (dec!(0), dec!(0.01)),
                (dec!(100), dec!(0.05)),
                (dec!(500), dec!(0.10)),
            ],
        };

        // In first threshold range
        assert_eq!(tick.round_up(dec!(50.00)), Some(dec!(50.00)));
        assert_eq!(tick.round_up(dec!(50.001)), Some(dec!(50.01)));
        assert_eq!(tick.round_up(dec!(99.996)), Some(dec!(100.00)));

        // In second threshold range
        assert_eq!(tick.round_up(dec!(100.00)), Some(dec!(100.00)));
        assert_eq!(tick.round_up(dec!(100.01)), Some(dec!(100.05)));
        assert_eq!(tick.round_up(dec!(100.03)), Some(dec!(100.05)));
        assert_eq!(tick.round_up(dec!(100.05)), Some(dec!(100.05)));

        // In third threshold range
        assert_eq!(tick.round_up(dec!(500.00)), Some(dec!(500.00)));
        assert_eq!(tick.round_up(dec!(500.01)), Some(dec!(500.10)));
        assert_eq!(tick.round_up(dec!(500.05)), Some(dec!(500.10)));
        assert_eq!(tick.round_up(dec!(500.09)), Some(dec!(500.10)));
        assert_eq!(tick.round_up(dec!(500.10)), Some(dec!(500.10)));
    }

    #[test]
    fn test_round_down_varying() {
        let tick = TickSize::Varying {
            thresholds: vec![
                (dec!(0), dec!(0.01)),
                (dec!(100), dec!(0.05)),
                (dec!(500), dec!(0.10)),
            ],
        };

        // In first threshold range
        assert_eq!(tick.round_down(dec!(50.00)), Some(dec!(50.00)));
        assert_eq!(tick.round_down(dec!(50.001)), Some(dec!(50.00)));
        assert_eq!(tick.round_down(dec!(99.999)), Some(dec!(99.99)));

        // In second threshold range
        assert_eq!(tick.round_down(dec!(100.00)), Some(dec!(100.00)));
        assert_eq!(tick.round_down(dec!(100.01)), Some(dec!(100.00)));
        assert_eq!(tick.round_down(dec!(100.04)), Some(dec!(100.00)));
        assert_eq!(tick.round_down(dec!(100.05)), Some(dec!(100.05)));
        assert_eq!(tick.round_down(dec!(100.09)), Some(dec!(100.05)));

        // In third threshold range
        assert_eq!(tick.round_down(dec!(500.00)), Some(dec!(500.00)));
        assert_eq!(tick.round_down(dec!(500.01)), Some(dec!(500.00)));
        assert_eq!(tick.round_down(dec!(500.09)), Some(dec!(500.00)));
        assert_eq!(tick.round_down(dec!(500.10)), Some(dec!(500.10)));
        assert_eq!(tick.round_down(dec!(500.19)), Some(dec!(500.10)));
    }

    #[test]
    fn test_round_aggressive() {
        let tick = TickSize::Simple(dec!(0.01));

        // Buy direction - rounds up (more aggressive = higher price)
        assert_eq!(tick.round_aggressive(dec!(100.001), Dir::Buy), Some(dec!(100.01)));
        assert_eq!(tick.round_aggressive(dec!(100.00), Dir::Buy), Some(dec!(100.00)));

        // Sell direction - rounds down (more aggressive = lower price)
        assert_eq!(tick.round_aggressive(dec!(100.009), Dir::Sell), Some(dec!(100.00)));
        assert_eq!(tick.round_aggressive(dec!(100.00), Dir::Sell), Some(dec!(100.00)));
    }

    #[test]
    fn test_round_passive() {
        let tick = TickSize::Simple(dec!(0.01));

        // Buy direction - rounds down (more passive = lower price)
        assert_eq!(tick.round_passive(dec!(100.009), Dir::Buy), Some(dec!(100.00)));
        assert_eq!(tick.round_passive(dec!(100.00), Dir::Buy), Some(dec!(100.00)));

        // Sell direction - rounds up (more passive = higher price)
        assert_eq!(tick.round_passive(dec!(100.001), Dir::Sell), Some(dec!(100.01)));
        assert_eq!(tick.round_passive(dec!(100.00), Dir::Sell), Some(dec!(100.00)));
    }

    #[test]
    fn test_rounding_with_large_tick_sizes() {
        // Test with tick size of 0.25
        let tick = TickSize::Simple(dec!(0.25));

        assert_eq!(tick.round_up(dec!(10.00)), Some(dec!(10.00)));
        assert_eq!(tick.round_up(dec!(10.10)), Some(dec!(10.25)));
        assert_eq!(tick.round_up(dec!(10.25)), Some(dec!(10.25)));
        assert_eq!(tick.round_up(dec!(10.30)), Some(dec!(10.50)));

        assert_eq!(tick.round_down(dec!(10.00)), Some(dec!(10.00)));
        assert_eq!(tick.round_down(dec!(10.10)), Some(dec!(10.00)));
        assert_eq!(tick.round_down(dec!(10.25)), Some(dec!(10.25)));
        assert_eq!(tick.round_down(dec!(10.30)), Some(dec!(10.25)));
        assert_eq!(tick.round_down(dec!(10.50)), Some(dec!(10.50)));
        assert_eq!(tick.round_down(dec!(10.60)), Some(dec!(10.50)));
    }
}