Skip to content

Commit dae640a

Browse files
committed
fix: wire all factor runners into master.py and remove dead code
- master.py cmd_factors() now calls all 6 factor runner modules - master.py INGEST_CHAIN includes ingest_events.py - Remove dead compute_mom_season (O(N^2) Python loop, never registered) - Fix unused variable in compute_mom12m_off_season
1 parent 9d9c5c1 commit dae640a

3 files changed

Lines changed: 10 additions & 58 deletions

File tree

factors/predictors/price_extended_factors.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,16 @@ def compute_momrev(df: pl.DataFrame) -> pl.DataFrame:
126126
def compute_mom12m_off_season(df: pl.DataFrame) -> pl.DataFrame:
127127
"""12-month momentum excluding the same calendar month.
128128
129-
Standard Mom12m minus the return in the same month last year.
130-
Approximated as: mom12m_cumret / (1 + ret_same_month_lag)
131-
where ret_same_month_lag ≈ the return 252 trading days ago.
129+
Approximated as: mom12m / (1 + ret_same_month_lag) - 1
132130
"""
133131
panel = _ordered_panel(df, ["ts_code", "trade_date", "ret"])
134132
mom12 = ((1 + pl.col("ret")).log().rolling_sum(window_size=252).exp() - 1).shift(21)
135-
# Approximate same-month return by single day 252 periods ago
136133
ret_season = pl.col("ret").shift(252)
137-
expr = pl.when(ret_season.abs() < 0.99).then(
138-
(1 + mom12) / (1 + ret_season) - 1
139-
)
140-
return _rolling_factor(panel.with_columns(
134+
panel = panel.with_columns(
141135
mom12.over("ts_code").alias("_m12"),
142136
ret_season.over("ts_code").alias("_rs"),
143-
), "Mom12mOffSeason",
137+
)
138+
return _rolling_factor(panel, "Mom12mOffSeason",
144139
pl.when(pl.col("_rs").abs() < 0.99)
145140
.then((1 + pl.col("_m12")) / (1 + pl.col("_rs")) - 1)
146141
)

factors/predictors/price_volume_factors.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -152,55 +152,6 @@ def compute_mrreversal(df: pl.DataFrame) -> pl.DataFrame:
152152
return _rolling_factor(panel, "MRreversal", expr)
153153

154154

155-
# ---------------------------------------------------------------------------
156-
# Seasonal momentum (Heston-Sadka 2008 JFE): same-month avg over years 2-5
157-
# ---------------------------------------------------------------------------
158-
159-
160-
def compute_mom_season(df: pl.DataFrame) -> pl.DataFrame:
161-
"""Average return in the same calendar month over the preceding 2-5 years.
162-
163-
This requires monthly data. We compute it from the daily panel by first
164-
collapsing to monthly returns, then averaging same-month returns from
165-
years t-2 through t-5, then joining back to month-end dates.
166-
"""
167-
panel = _ordered_panel(df, ["ts_code", "trade_date", "ret"])
168-
panel = panel.with_columns(
169-
(pl.col("trade_date").dt.year() * 100 + pl.col("trade_date").dt.month()).alias("yyyymm"),
170-
pl.col("trade_date").dt.month().alias("_month"),
171-
pl.col("trade_date").dt.year().alias("_year"),
172-
)
173-
monthly = (
174-
panel.group_by(["ts_code", "yyyymm", "_month", "_year"])
175-
.agg(
176-
((1 + pl.col("ret")).product() - 1).alias("month_ret"),
177-
pl.col("trade_date").max().alias("trade_date"),
178-
)
179-
.sort(["ts_code", "yyyymm"])
180-
)
181-
rows = []
182-
for (ts_code, month, year), sub in monthly.group_by(["ts_code", "_month", "_year"]):
183-
same_month = monthly.filter(
184-
(pl.col("ts_code") == ts_code)
185-
& (pl.col("_month") == month)
186-
& (pl.col("_year") >= year - 5)
187-
& (pl.col("_year") <= year - 2)
188-
)
189-
if same_month.height == 0:
190-
continue
191-
avg = same_month["month_ret"].mean()
192-
rows.append({"ts_code": ts_code, "yyyymm": year * 100 + month, "MomSeason": avg})
193-
194-
if not rows:
195-
return pl.DataFrame(schema={"ts_code": pl.String, "trade_date": pl.Date, "MomSeason": pl.Float64})
196-
197-
result = pl.DataFrame(rows)
198-
month_end = monthly.select(["ts_code", "yyyymm", "trade_date"])
199-
return result.join(month_end, on=["ts_code", "yyyymm"], how="left").select(
200-
["ts_code", "trade_date", "MomSeason"]
201-
)
202-
203-
204155
# ---------------------------------------------------------------------------
205156
# Log price (Blume-Husic 1973 JF)
206157
# ---------------------------------------------------------------------------

master.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def _call(mod: str, fn: str = "run") -> None:
1616
"data.ingest.tushare.ingest_industry",
1717
"data.ingest.tushare.ingest_index",
1818
"data.ingest.tushare.ingest_status",
19+
"data.ingest.tushare.ingest_events",
1920
]
2021

2122
NORMALIZE_CHAIN = [
@@ -53,6 +54,11 @@ def cmd_intermediate() -> None:
5354

5455
def cmd_factors() -> None:
5556
_call("factors.predictors.run_seed_factors")
57+
_call("factors.predictors.price_volume_factors")
58+
_call("factors.predictors.accounting_factors")
59+
_call("factors.predictors.extended_accounting_factors")
60+
_call("factors.predictors.additional_accounting_factors")
61+
_call("factors.predictors.price_extended_factors")
5662

5763

5864
def cmd_all() -> None:

0 commit comments

Comments
 (0)