|
10 | 10 | - Academic formula correctness |
11 | 11 | """ |
12 | 12 |
|
| 13 | +from decimal import Decimal |
13 | 14 | from io import StringIO |
14 | 15 |
|
15 | 16 | import numpy as np |
@@ -569,6 +570,44 @@ def test_integer_prices_keep_tick_rule_compatibility(lazy): |
569 | 570 | assert result.to_series().to_list() == [0, -1, -1, 1, 1] |
570 | 571 |
|
571 | 572 |
|
| 573 | +@pytest.mark.parametrize( |
| 574 | + "prices,expected", |
| 575 | + [ |
| 576 | + pytest.param( |
| 577 | + ["100.00", "99.00", "99.00", "99.01", None, "99.01", "99.01"], |
| 578 | + [0, -1, -1, 1, 0, 0, 1], |
| 579 | + id="decimal-with-missing-price", |
| 580 | + ), |
| 581 | + pytest.param( |
| 582 | + [ |
| 583 | + "1000000000000000.00000000000001", |
| 584 | + "1000000000000000.00000000000002", |
| 585 | + "1000000000000000.00000000000002", |
| 586 | + "1000000000000000.00000000000001", |
| 587 | + ], |
| 588 | + [0, 1, 1, -1], |
| 589 | + id="decimal-moves-below-float-price-resolution", |
| 590 | + ), |
| 591 | + ], |
| 592 | +) |
| 593 | +@pytest.mark.parametrize("lazy", [False, True]) |
| 594 | +def test_decimal_prices_keep_exact_tick_direction_and_order_flow(prices, expected, lazy): |
| 595 | + frame = pl.DataFrame( |
| 596 | + { |
| 597 | + "close": [Decimal(value) if value is not None else None for value in prices], |
| 598 | + "volume": [1.0] * len(prices), |
| 599 | + } |
| 600 | + ) |
| 601 | + source = frame.lazy() if lazy else frame |
| 602 | + selected = source.select( |
| 603 | + effective_tick_rule("close").alias("tick"), |
| 604 | + order_flow_imbalance("volume", "close").alias("ofi"), |
| 605 | + ) |
| 606 | + result = selected.collect() if lazy else selected |
| 607 | + assert result["tick"].to_list() == expected |
| 608 | + assert result["ofi"].to_list() == expected |
| 609 | + |
| 610 | + |
572 | 611 | def test_tick_rule_logic(small_microstructure_data): |
573 | 612 | """Test tick rule classification logic.""" |
574 | 613 | result = small_microstructure_data.with_columns(effective_tick_rule("close").alias("tick_rule")) |
|
0 commit comments