Skip to content

Commit 5bdad31

Browse files
committed
fix(features): keep missing prices from corrupting tick direction
1 parent 8fab64b commit 5bdad31

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def effective_tick_rule(close: pl.Expr | str) -> pl.Expr:
1616
1717
Classifies trades as buyer or seller initiated based on price movements.
1818
An uptick is +1 and a downtick is -1. A zero price change keeps the previous
19-
nonzero sign. The value is 0 when no earlier price change exists or the price
20-
itself is missing.
19+
nonzero sign. The value is 0 when no earlier price change exists or an
20+
adjacent price is missing.
2121
2222
Parameters
2323
----------
@@ -31,7 +31,7 @@ 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()
34+
price_change = close.diff().fill_nan(None)
3535
# A zero change is null here so forward-fill can recover the previous sign.
3636
# A missing price has a null change too, and stays unclassified (0).
3737
direction = pl.when(price_change > 0).then(1).when(price_change < 0).then(-1).otherwise(None)

‎tests/features/test_microstructure.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,71 @@ def test_effective_tick_rule_carries_previous_sign():
504504
assert imbalance[6] == 0.0
505505

506506

507+
@pytest.mark.parametrize("missing", ["", "NaN"])
508+
@pytest.mark.parametrize("move", [-1.0, 1.0])
509+
@pytest.mark.parametrize("lazy", [False, True])
510+
@pytest.mark.parametrize("expression_input", [False, True])
511+
def test_missing_csv_prices_do_not_overwrite_tick_direction(missing, move, lazy, expression_input):
512+
moved = 100.0 + move
513+
payload = f"row,close\n0,100.0\n1,{moved}\n2,{moved}\n3,{missing}\n4,{moved}\n5,{moved}\n"
514+
frame = pl.read_csv(StringIO(payload), schema_overrides={"close": pl.Float64}).with_columns(
515+
volume=pl.lit(5.0)
516+
)
517+
source = frame.lazy() if lazy else frame
518+
close = pl.col("close") if expression_input else "close"
519+
selected = source.select(
520+
effective_tick_rule(close).alias("tick"),
521+
order_flow_imbalance("volume", close).alias("ofi"),
522+
)
523+
result = selected.collect() if lazy else selected
524+
expected = [0, int(move), int(move), 0, 0, int(move)]
525+
assert result["tick"].to_list() == expected
526+
assert result["ofi"].to_list() == expected
527+
528+
529+
@pytest.mark.parametrize("lazy", [False, True])
530+
def test_groups_keep_tick_state_separate_across_missing_prices(lazy):
531+
frame = pl.DataFrame(
532+
{
533+
"symbol": ["A", "B"] * 5,
534+
"close": [100.0, 100.0, 99.0, 101.0, float("nan"), 101.0, 99.0, 101.0, 99.0, 101.0],
535+
}
536+
)
537+
source = frame.lazy() if lazy else frame
538+
selected = source.select(effective_tick_rule("close").over("symbol"))
539+
result = selected.collect() if lazy else selected
540+
assert result.to_series().to_list() == [0, 0, -1, 1, 0, 1, 0, 1, -1, 1]
541+
542+
543+
@pytest.mark.parametrize(
544+
"prices,expected",
545+
[
546+
([], []),
547+
([100], [0]),
548+
([100, 100, 100], [0, 0, 0]),
549+
([100, 100, 99, 99, 100, 100], [0, 0, -1, -1, 1, 1]),
550+
([None, None], [0, 0]),
551+
([100, None, None, 101, 101], [0, 0, 0, 0, 0]),
552+
],
553+
)
554+
@pytest.mark.parametrize("lazy", [False, True])
555+
def test_tick_direction_stays_unknown_until_a_real_price_change(prices, expected, lazy):
556+
frame = pl.DataFrame({"close": pl.Series(prices, dtype=pl.Float64)})
557+
source = frame.lazy() if lazy else frame
558+
selected = source.select(effective_tick_rule("close"))
559+
result = selected.collect() if lazy else selected
560+
assert result.to_series().to_list() == expected
561+
562+
563+
@pytest.mark.parametrize("lazy", [False, True])
564+
def test_integer_prices_keep_tick_rule_compatibility(lazy):
565+
frame = pl.DataFrame({"close": pl.Series([100, 99, 99, 100, 100], dtype=pl.Int64)})
566+
source = frame.lazy() if lazy else frame
567+
selected = source.select(effective_tick_rule("close"))
568+
result = selected.collect() if lazy else selected
569+
assert result.to_series().to_list() == [0, -1, -1, 1, 1]
570+
571+
507572
def test_tick_rule_logic(small_microstructure_data):
508573
"""Test tick rule classification logic."""
509574
result = small_microstructure_data.with_columns(effective_tick_rule("close").alias("tick_rule"))

0 commit comments

Comments
 (0)