|
| 1 | +from decimal import Decimal |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from ordersim import ( |
| 6 | + CompiledEventColumns, |
| 7 | + CppMatchingEngine, |
| 8 | + MatchingEngine, |
| 9 | + MBOEvent, |
| 10 | + advance_until_fill_boundary, |
| 11 | + cpp_execution_engine_available, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def boundary_events() -> tuple[MBOEvent, ...]: |
| 16 | + return ( |
| 17 | + MBOEvent( |
| 18 | + ts_ns=1, |
| 19 | + action="add", |
| 20 | + side="bid", |
| 21 | + price=Decimal("100.0"), |
| 22 | + size=1, |
| 23 | + order_id=1, |
| 24 | + ), |
| 25 | + MBOEvent( |
| 26 | + ts_ns=2, |
| 27 | + action="trade", |
| 28 | + side="bid", |
| 29 | + price=Decimal("100.0"), |
| 30 | + size=2, |
| 31 | + order_id=2, |
| 32 | + ), |
| 33 | + MBOEvent( |
| 34 | + ts_ns=3, |
| 35 | + action="add", |
| 36 | + side="ask", |
| 37 | + price=Decimal("101.0"), |
| 38 | + size=1, |
| 39 | + order_id=3, |
| 40 | + ), |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def test_boundary_advance_scalar_path_stops_on_first_passive_fill() -> None: |
| 45 | + engine = MatchingEngine() |
| 46 | + resting = engine.place_limit(side="buy", price=Decimal("100.0"), size=1) |
| 47 | + |
| 48 | + advance = advance_until_fill_boundary(engine, boundary_events()) |
| 49 | + |
| 50 | + assert resting.order_id is not None |
| 51 | + assert advance.events_consumed == 2 |
| 52 | + assert advance.stopped_on_fill is True |
| 53 | + assert [(fill.order_id, fill.ts_ns, fill.size) for fill in advance.fills] == [ |
| 54 | + (resting.order_id, 2, 1), |
| 55 | + ] |
| 56 | + assert engine.book_top() == (None, None) |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.skipif( |
| 60 | + not cpp_execution_engine_available(), |
| 61 | + reason="optional C++ execution engine is not built", |
| 62 | +) |
| 63 | +def test_boundary_advance_compiled_path_matches_scalar_boundary() -> None: |
| 64 | + events = boundary_events() |
| 65 | + columns = CompiledEventColumns.from_events(events, tick_size=Decimal("0.10")) |
| 66 | + scalar_engine = MatchingEngine() |
| 67 | + compiled_engine = CppMatchingEngine(tick_size=Decimal("0.10")) |
| 68 | + |
| 69 | + scalar_order = scalar_engine.place_limit( |
| 70 | + side="buy", |
| 71 | + price=Decimal("100.0"), |
| 72 | + size=1, |
| 73 | + ) |
| 74 | + compiled_order = compiled_engine.place_limit( |
| 75 | + side="buy", |
| 76 | + price=Decimal("100.0"), |
| 77 | + size=1, |
| 78 | + ) |
| 79 | + |
| 80 | + scalar_advance = advance_until_fill_boundary(scalar_engine, events) |
| 81 | + compiled_advance = advance_until_fill_boundary( |
| 82 | + compiled_engine, |
| 83 | + events, |
| 84 | + compiled_events=columns, |
| 85 | + ) |
| 86 | + |
| 87 | + assert scalar_order.order_id is not None |
| 88 | + assert compiled_order.order_id is not None |
| 89 | + assert scalar_advance.events_consumed == compiled_advance.events_consumed |
| 90 | + assert scalar_advance.stopped_on_fill is True |
| 91 | + assert compiled_advance.stopped_on_fill is True |
| 92 | + assert [ |
| 93 | + (fill.side, fill.price, fill.size, fill.ts_ns) |
| 94 | + for fill in scalar_advance.fills |
| 95 | + ] == [ |
| 96 | + (fill.side, fill.price, fill.size, fill.ts_ns) |
| 97 | + for fill in compiled_advance.fills |
| 98 | + ] |
| 99 | + assert scalar_engine.book_top() == compiled_engine.book_top() |
| 100 | + |
| 101 | + |
| 102 | +def test_boundary_advance_reports_slice_end_without_fill() -> None: |
| 103 | + events = ( |
| 104 | + MBOEvent( |
| 105 | + ts_ns=1, |
| 106 | + action="add", |
| 107 | + side="bid", |
| 108 | + price=Decimal("100.0"), |
| 109 | + size=1, |
| 110 | + order_id=1, |
| 111 | + ), |
| 112 | + MBOEvent( |
| 113 | + ts_ns=2, |
| 114 | + action="add", |
| 115 | + side="ask", |
| 116 | + price=Decimal("101.0"), |
| 117 | + size=1, |
| 118 | + order_id=2, |
| 119 | + ), |
| 120 | + ) |
| 121 | + engine = MatchingEngine() |
| 122 | + |
| 123 | + advance = advance_until_fill_boundary(engine, events, start=0, stop=2) |
| 124 | + |
| 125 | + assert advance.events_consumed == 2 |
| 126 | + assert advance.stopped_on_fill is False |
| 127 | + assert advance.fills == () |
| 128 | + assert engine.book_top() == (Decimal("100.0"), Decimal("101.0")) |
| 129 | + |
| 130 | + |
| 131 | +def test_boundary_advance_accepts_empty_slice() -> None: |
| 132 | + events = boundary_events() |
| 133 | + engine = MatchingEngine() |
| 134 | + |
| 135 | + advance = advance_until_fill_boundary(engine, events, start=1, stop=1) |
| 136 | + |
| 137 | + assert advance.events_consumed == 0 |
| 138 | + assert advance.stopped_on_fill is False |
| 139 | + assert advance.fills == () |
| 140 | + |
| 141 | + |
| 142 | +def test_boundary_advance_validates_slice_bounds() -> None: |
| 143 | + events = boundary_events() |
| 144 | + engine = MatchingEngine() |
| 145 | + |
| 146 | + with pytest.raises(ValueError, match="start"): |
| 147 | + advance_until_fill_boundary(engine, events, start=-1) |
| 148 | + |
| 149 | + with pytest.raises(ValueError, match="greater than or equal"): |
| 150 | + advance_until_fill_boundary(engine, events, start=2, stop=1) |
| 151 | + |
| 152 | + with pytest.raises(ValueError, match="number of events"): |
| 153 | + advance_until_fill_boundary(engine, events, stop=len(events) + 1) |
0 commit comments