feat: add local correlation filters and full recheck action
Deploy production / deploy (push) Successful in 54s

This commit is contained in:
yuxuanhui
2026-09-13 00:03:17 +08:00
parent bbf0e37130
commit 2080f812b4
13 changed files with 309 additions and 15 deletions
+16
View File
@@ -195,6 +195,22 @@ def list_statement(filters):
query = query.where(Alpha.submission_blocked == filters.submission_blocked)
if filters.submission_blocked:
query = query.where(submission_condition("UNSUBMITTED"))
if (filters.local_correlation_status is not None or filters.local_correlation_min is not None
or filters.local_correlation_max is not None):
# One cache row per Alpha keeps totals/export stable; stale overrides the displayed status.
query = query.outerjoin(SelfCorrelation, SelfCorrelation.alpha_id == Alpha.id)
status = filters.local_correlation_status
if status == "not_cached":
query = query.where(SelfCorrelation.alpha_id.is_(None))
elif status == "stale":
query = query.where(SelfCorrelation.stale.is_(True))
elif status is not None:
query = query.where(SelfCorrelation.stale.is_(False), SelfCorrelation.result["status"].as_string() == status)
coefficient = SelfCorrelation.result["max_correlation"].as_float()
if filters.local_correlation_min is not None:
query = query.where(coefficient >= filters.local_correlation_min)
if filters.local_correlation_max is not None:
query = query.where(coefficient <= filters.local_correlation_max)
source_filters = {k: getattr(filters, k) for k in ("source", "source_reference", "research_id", "backtest_run_id")}
if any(source_filters.values()):
query = query.where(Alpha.id.in_(source_alpha_ids(**source_filters)))
+11 -2
View File
@@ -193,7 +193,7 @@ class Business:
async def create_sync_job(self, body: JobInput):
account = await self.db.scalar(select(Account).where(Account.id == 1).with_for_update())
if body.kind != "self_correlation" and (
if body.kind not in ("self_correlation", "self_correlation_recheck") and (
not account.password_encrypted or account.connection_status in ("disconnected", "error")
):
raise HTTPException(409, "请先连接 WorldQuant")
@@ -205,9 +205,18 @@ class Business:
for job in (
await self.db.scalars(select(Job).where(Job.kind == body.kind, Job.status.in_(ACTIVE)))
).all():
if body.kind == "pnl_backfill" or job.payload == payload:
if body.kind in ("pnl_backfill", "self_correlation_recheck") or job.payload == payload:
return JobOutput.model_validate(job).model_dump(mode="json")
job = Job(id=str(uuid4()), kind=body.kind, payload=payload)
if body.kind == "self_correlation_recheck":
# Freeze every qualifying target at click time, without the manual-ID batch limit.
ids = list((await self.db.scalars(
select(Alpha.id).where(Alpha.check_type.in_(("PRE_CHECK", "PASS"))).order_by(Alpha.id)
)).all())
job.payload = {"alpha_ids": ids}
job.total = len(ids)
if not ids:
job.status = "completed"
if body.kind == "pnl_backfill":
# Fix the full missing set on the server, independently of UI paging.
# The account lock above also serializes duplicate button clicks.
+1 -1
View File
@@ -242,7 +242,7 @@ class Runner:
async with self.sessions() as db:
job = await db.get(Job, job_id)
kind, payload = job.kind, job.payload
if kind == "self_correlation":
if kind in ("self_correlation", "self_correlation_recheck"):
# Cached local comparisons also work while the platform is disconnected.
await self.check_correlations(job_id, payload["alpha_ids"])
elif kind == "verify":
+9 -3
View File
@@ -69,6 +69,9 @@ class PreferencesInput(Contract):
class AlphaFilters(Contract):
local_correlation_status: Literal["not_cached", "stale", "low", "high", "partial", "insufficient_data"] | None = None
local_correlation_min: float | None = Field(default=None, ge=-1, le=1)
local_correlation_max: float | None = Field(default=None, ge=-1, le=1)
submission_blocked: bool | None = None
submission: Submission | None = None
source: str | None = Field(default=None, max_length=100)
@@ -125,7 +128,7 @@ class AlphaFilters(Contract):
@model_validator(mode="after")
def range_order(self):
for key in (
"sharpe", "fitness", "returns", "turnover", "margin", "drawdown",
"local_correlation", "sharpe", "fitness", "returns", "turnover", "margin", "drawdown",
"sub_universe_sharpe", "robust_universe_sharpe", "two_year_sharpe", "prod_correlation", "pnl",
):
lo, hi = getattr(self, f"{key}_min"), getattr(self, f"{key}_max")
@@ -188,7 +191,7 @@ class BulkUpdate(BulkInput):
class JobInput(Contract):
kind: Literal["full_sync", "daily_sync", "alpha_refresh", "pnl_refresh", "pnl_backfill", "self_correlation"]
kind: Literal["full_sync", "daily_sync", "alpha_refresh", "pnl_refresh", "pnl_backfill", "self_correlation", "self_correlation_recheck"]
alpha_ids: list[str] = Field(default_factory=list)
submission: Submission | None = None
date_from: date | None = None
@@ -196,7 +199,10 @@ class JobInput(Contract):
@model_validator(mode="after")
def validate_ids(self):
if self.kind == "pnl_backfill":
if self.kind == "self_correlation_recheck":
if self.alpha_ids or self.submission is not None or self.date_from is not None or self.date_to is not None:
raise ValueError("重检自相关覆盖全部预检通过和检查通过的 Alpha,不接受 ID、分组或日期范围")
elif self.kind == "pnl_backfill":
if self.alpha_ids or self.submission is not None or self.date_from is not None or self.date_to is not None:
raise ValueError("检查 PnL 自动覆盖全部本地已提交 Alpha,不接受 ID、分组或日期范围")
elif self.kind in ("full_sync", "daily_sync"):
+50
View File
@@ -0,0 +1,50 @@
"""Full rechecks freeze all passing targets and overwrite existing correlation results."""
import pytest
from app.alphas import upsert_alpha
from app.models import Alpha, SelfCorrelation
from tests.conftest import alpha
from tests.test_jobs import result
async def test_recheck_all_passed_targets_deduplicates_and_repeats(app, logged_in):
runner = app.state.runner
async with runner.sessions.begin() as db:
for i in range(105):
row = await upsert_alpha(db, alpha(f"pass{i:03}"))
row.check_type = "PRE_CHECK" if i % 2 else "PASS"
for name, status in [("failed", "FAIL_1"), ("failed2", "FAIL_2"), ("pending", "PENDING")]:
row = await upsert_alpha(db, alpha(name))
row.check_type = status
db.add(SelfCorrelation(alpha_id="pass000", region="USA", stale=False,
result={"status": "high", "max_correlation": 0.9}))
response = await logged_in.post("/api/v1/sync-jobs", json={"kind": "self_correlation_recheck"})
assert response.status_code == 202
job = response.json()
assert job["total"] == 105
assert job["payload"]["alpha_ids"] == [f"pass{i:03}" for i in range(105)]
duplicate = (await logged_in.post("/api/v1/sync-jobs", json={"kind": "self_correlation_recheck"})).json()
assert duplicate["id"] == job["id"]
await runner.execute(job["id"])
completed = await result(runner, job["id"])
assert completed.status == "completed" and completed.processed == 105
async with runner.sessions() as db:
for i in range(105):
cached = await db.get(SelfCorrelation, f"pass{i:03}")
assert cached.stale is False and cached.result["max_correlation"] == 0
assert await db.get(SelfCorrelation, "failed") is None
assert (await db.get(Alpha, "pass000")).check_type == "PASS"
again = (await logged_in.post("/api/v1/sync-jobs", json={"kind": "self_correlation_recheck"})).json()
assert again["id"] != job["id"] and again["total"] == 105
async def test_empty_recheck_completes_without_platform_connection(logged_in):
response = await logged_in.post("/api/v1/sync-jobs", json={"kind": "self_correlation_recheck"})
assert response.status_code == 202
assert response.json()["total"] == 0 and response.json()["status"] == "completed"
@pytest.mark.parametrize("extra", [{"alpha_ids": ["target"]}, {"submission": "UNSUBMITTED"}, {"date_from": "2026-01-01"}])
async def test_recheck_rejects_partial_scope(logged_in, extra):
assert (await logged_in.post("/api/v1/sync-jobs", json={"kind": "self_correlation_recheck", **extra})).status_code == 422
@@ -0,0 +1,45 @@
"""Cached correlation filters compose before pagination and match visible states."""
import csv
import io
import pytest
from app.alphas import upsert_alpha
from app.models import SelfCorrelation
from tests.conftest import alpha
async def test_correlation_filters(app, logged_in):
patterns = [("negative", "low", -0.4, False), ("zero", "low", 0, False),
("yellow", "high", 0.7, False), ("boundary", "high", 0.75, False),
("red", "high", 0.8, False), ("old", "low", 0.2, True),
("partial", "partial", 0.5, False), ("empty", "insufficient_data", None, False)]
async with app.state.sessions.begin() as db:
for name, status, value, stale in patterns:
await upsert_alpha(db, alpha(name))
db.add(SelfCorrelation(alpha_id=name, region="USA", stale=stale,
result={"status": status, "max_correlation": value}))
await upsert_alpha(db, alpha("missing"))
for status, ids in [("not_cached", ["missing"]), ("stale", ["old"]), ("low", ["negative", "zero"]),
("high", ["boundary", "red", "yellow"]), ("partial", ["partial"]), ("insufficient_data", ["empty"])]:
response = await logged_in.get("/api/v1/alphas", params={"local_correlation_status": status, "sort": "id", "direction": "asc"})
assert response.status_code == 200
assert [row["id"] for row in response.json()["items"]] == ids
query = "local_correlation_min=0.7&local_correlation_max=0.75&region=USA&submission=UNSUBMITTED&sort=id&direction=asc&limit=1&offset=1"
page = (await logged_in.get(f"/api/v1/alphas?{query}")).json()
assert page["total"] == 2 and [row["id"] for row in page["items"]] == ["yellow"]
export = await logged_in.get(f"/api/v1/alphas/export?{query}")
assert [row["id"] for row in csv.DictReader(io.StringIO(export.text.lstrip("\ufeff")))] == ["boundary", "yellow"]
for query, expected in [("local_correlation_max=0", ["negative", "zero"]),
("local_correlation_min=0.1&local_correlation_max=0.3", ["old"]),
("local_correlation_status=low&local_correlation_min=0.1&local_correlation_max=0.3", []),
("local_correlation_status=not_cached&local_correlation_min=-1", [])]:
page = (await logged_in.get(f"/api/v1/alphas?{query}&sort=id&direction=asc")).json()
assert [row["id"] for row in page["items"]] == expected
@pytest.mark.parametrize("query", ["local_correlation_min=0.8&local_correlation_max=0.7", "local_correlation_min=-1.1",
"local_correlation_max=1.1", "local_correlation_min=nan", "local_correlation_status=invalid"])
async def test_correlation_filter_validation(logged_in, query):
assert (await logged_in.get(f"/api/v1/alphas?{query}")).status_code == 422