rebuild(screener): add controlled formulas and rolling backtests

This commit is contained in:
leefer
2026-07-30 10:45:40 +08:00
parent 08f69b0641
commit b79b4ba280
21 changed files with 616 additions and 37 deletions
@@ -88,6 +88,28 @@ class ScreenerRepository:
(snapshot_id,),
).fetchone()
def factor_snapshots_through(
self, connection: sqlite3.Connection, through: str, limit: int
) -> tuple[sqlite3.Row, ...]:
return tuple(
connection.execute(
"""
SELECT * FROM (
SELECT snapshots.*,
ROW_NUMBER() OVER (
PARTITION BY trade_date ORDER BY id DESC
) AS revision_rank
FROM screener_factor_snapshots AS snapshots
WHERE trade_date <= ?
)
WHERE revision_rank = 1
ORDER BY trade_date DESC, id DESC
LIMIT ?
""",
(through, limit),
).fetchall()
)
def factor_rows(self, connection: sqlite3.Connection, snapshot_id: int) -> list[dict[str, Any]]:
return [
json.loads(str(row["payload_json"]))
@@ -180,6 +202,30 @@ class ScreenerRepository:
),
)
def save_backtest(
self,
connection: sqlite3.Connection,
run_id: int,
payload: dict[str, Any],
) -> None:
connection.execute(
"""
INSERT OR IGNORE INTO screener_run_backtests (
run_id, payload_json, created_at
) VALUES (?, ?, ?)
""",
(run_id, _json(payload), _now()),
)
def backtest(
self, connection: sqlite3.Connection, run_id: int
) -> dict[str, Any] | None:
row = connection.execute(
"SELECT payload_json FROM screener_run_backtests WHERE run_id = ?",
(run_id,),
).fetchone()
return json.loads(str(row["payload_json"])) if row else None
def latest_runs(
self,
connection: sqlite3.Connection,