Skip to content

Commit 8b44031

Browse files
committed
fix(features): preserve Decimal prices while normalizing missing tick changes
1 parent 5bdad31 commit 8b44031

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎src/ml4t/engineer/features/microstructure/effective_tick_rule.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def effective_tick_rule(close: pl.Expr | str) -> pl.Expr:
3131
"""
3232
close = pl.col(close) if isinstance(close, str) else close
3333

34-
price_change = close.diff().fill_nan(None)
34+
# Compute differences before casting to preserve small Decimal price changes.
35+
price_change = close.diff().cast(pl.Float64).fill_nan(None)
3536
# A zero change is null here so forward-fill can recover the previous sign.
3637
# A missing price has a null change too, and stays unclassified (0).
3738
direction = pl.when(price_change > 0).then(1).when(price_change < 0).then(-1).otherwise(None)

‎tests/features/test_microstructure.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Academic formula correctness
1111
"""
1212

13+
from decimal import Decimal
1314
from io import StringIO
1415

1516
import numpy as np
@@ -569,6 +570,44 @@ def test_integer_prices_keep_tick_rule_compatibility(lazy):
569570
assert result.to_series().to_list() == [0, -1, -1, 1, 1]
570571

571572

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+
572611
def test_tick_rule_logic(small_microstructure_data):
573612
"""Test tick rule classification logic."""
574613
result = small_microstructure_data.with_columns(effective_tick_rule("close").alias("tick_rule"))

0 commit comments

Comments
 (0)