Files
fxhnt/tests/unit/test_equity_backtest_domain.py
2026-06-17 12:25:41 +02:00

43 lines
1.5 KiB
Python

from fxhnt.domain.equity_backtest import (
universe_asof,
month_end_rebalance_dates,
_epoch_day,
)
MEMBERS = [
("LIVE", "NASDAQ", "2000-01-01", "2026-06-01"),
("DEAD", "NYSE", "1998-01-01", "2005-03-15"),
("IPO", "NASDAQ", "2010-06-01", "2026-06-01"),
]
def test_universe_asof_excludes_not_yet_listed_and_delisted():
dollar_vol = {"LIVE": 1e9, "DEAD": 1e9, "IPO": 1e9}
assert universe_asof(MEMBERS, dollar_vol, "2008-01-01", n=10) == ["LIVE"]
def test_universe_asof_includes_delisted_while_alive():
dollar_vol = {"LIVE": 2e9, "DEAD": 1e9}
assert universe_asof(MEMBERS, dollar_vol, "2003-01-01", n=10) == ["LIVE", "DEAD"]
def test_universe_asof_ranks_by_dollar_volume_desc_and_takes_top_n():
members = [(s, "NYSE", "2000-01-01", "2026-06-01") for s in ("A", "B", "C")]
dollar_vol = {"A": 10.0, "B": 30.0, "C": 20.0}
assert universe_asof(members, dollar_vol, "2020-01-01", n=2) == ["B", "C"]
def test_universe_asof_drops_zero_or_missing_dollar_volume():
members = [(s, "NYSE", "2000-01-01", "2026-06-01") for s in ("A", "B")]
assert universe_asof(members, {"A": 5.0, "B": 0.0}, "2020-01-01", n=10) == ["A"]
def test_month_end_rebalance_dates_one_per_month():
dates = ["2020-01-02", "2020-01-31", "2020-02-03", "2020-02-27", "2020-03-02"]
assert month_end_rebalance_dates(dates) == ["2020-01-31", "2020-02-27", "2020-03-02"]
def test_epoch_day_roundtrip():
assert _epoch_day("1970-01-01") == 0
assert _epoch_day("1970-01-02") == 1