feat: expand market discovery and auction workflow
This commit is contained in:
+100
-1
@@ -217,6 +217,22 @@ class ReviewDatabase:
|
||||
PRIMARY KEY (trade_date, ts_code)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS auction_factors (
|
||||
trade_date TEXT NOT NULL,
|
||||
ts_code TEXT NOT NULL,
|
||||
price REAL NOT NULL DEFAULT 0,
|
||||
pre_close REAL NOT NULL DEFAULT 0,
|
||||
change REAL NOT NULL DEFAULT 0,
|
||||
vol REAL NOT NULL DEFAULT 0,
|
||||
amount REAL NOT NULL DEFAULT 0,
|
||||
turnover_rate REAL NOT NULL DEFAULT 0,
|
||||
volume_ratio REAL NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (trade_date, ts_code)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_auction_factors_code_date
|
||||
ON auction_factors(ts_code, trade_date DESC);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS screener_strategies (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER,
|
||||
@@ -1093,6 +1109,13 @@ class ReviewDatabase:
|
||||
).fetchall()
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
def list_stock_master(self) -> list[dict[str, Any]]:
|
||||
with self.connect() as connection:
|
||||
rows = connection.execute(
|
||||
"SELECT ts_code, code, name, industry, market, list_date FROM stock_master"
|
||||
).fetchall()
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
def upsert_daily_bars(self, rows: list[dict[str, Any]]) -> int:
|
||||
values = [
|
||||
(
|
||||
@@ -1171,6 +1194,72 @@ class ReviewDatabase:
|
||||
)
|
||||
return len(values)
|
||||
|
||||
def upsert_auction_factors(self, rows: list[dict[str, Any]]) -> int:
|
||||
values = []
|
||||
for row in rows:
|
||||
trade_date = str(row.get("trade_date") or "")
|
||||
ts_code = str(row.get("ts_code") or "")
|
||||
price = float(row.get("price") or 0)
|
||||
pre_close = float(row.get("pre_close") or 0)
|
||||
if not trade_date or not ts_code or price <= 0 or pre_close <= 0:
|
||||
continue
|
||||
values.append(
|
||||
(
|
||||
trade_date,
|
||||
ts_code,
|
||||
price,
|
||||
pre_close,
|
||||
(price / pre_close - 1) * 100,
|
||||
float(row.get("vol") or 0),
|
||||
float(row.get("amount") or 0),
|
||||
float(row.get("turnover_rate") or 0),
|
||||
float(row.get("volume_ratio") or 0),
|
||||
)
|
||||
)
|
||||
with self.connect() as connection:
|
||||
connection.executemany(
|
||||
"""
|
||||
INSERT INTO auction_factors
|
||||
(trade_date, ts_code, price, pre_close, change, vol, amount,
|
||||
turnover_rate, volume_ratio)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(trade_date, ts_code) DO UPDATE SET
|
||||
price=excluded.price, pre_close=excluded.pre_close,
|
||||
change=excluded.change, vol=excluded.vol, amount=excluded.amount,
|
||||
turnover_rate=excluded.turnover_rate,
|
||||
volume_ratio=excluded.volume_ratio
|
||||
""",
|
||||
values,
|
||||
)
|
||||
return len(values)
|
||||
|
||||
def auction_factor_dates(self, end_date: str = "", limit: int = 80) -> list[str]:
|
||||
where = "WHERE trade_date <= ?" if end_date else ""
|
||||
parameters: tuple[Any, ...] = (end_date, limit) if end_date else (limit,)
|
||||
with self.connect() as connection:
|
||||
rows = connection.execute(
|
||||
f"SELECT DISTINCT trade_date FROM auction_factors {where} "
|
||||
"ORDER BY trade_date DESC LIMIT ?",
|
||||
parameters,
|
||||
).fetchall()
|
||||
return [row["trade_date"] for row in reversed(rows)]
|
||||
|
||||
def auction_factors_for_date(self, trade_date: str) -> list[dict[str, Any]]:
|
||||
with self.connect() as connection:
|
||||
rows = connection.execute(
|
||||
"SELECT * FROM auction_factors WHERE trade_date = ? ORDER BY ts_code",
|
||||
(trade_date,),
|
||||
).fetchall()
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
def daily_bars_for_date(self, trade_date: str) -> list[dict[str, Any]]:
|
||||
with self.connect() as connection:
|
||||
rows = connection.execute(
|
||||
"SELECT * FROM daily_bars WHERE trade_date = ? ORDER BY ts_code",
|
||||
(trade_date,),
|
||||
).fetchall()
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
def factor_dates(self, end_date: str = "", limit: int = 80) -> list[str]:
|
||||
where = "WHERE trade_date <= ?" if end_date else ""
|
||||
parameters: tuple[Any, ...] = (end_date, limit) if end_date else (limit,)
|
||||
@@ -1184,7 +1273,7 @@ class ReviewDatabase:
|
||||
def load_factor_data(self, end_date: str, limit_dates: int = 80) -> dict[str, Any]:
|
||||
dates = self.factor_dates(end_date, limit_dates)
|
||||
if not dates:
|
||||
return {"dates": [], "bars": [], "master": [], "indicators": [], "moneyflow": []}
|
||||
return {"dates": [], "bars": [], "master": [], "indicators": [], "moneyflow": [], "auction": []}
|
||||
placeholders = ",".join("?" for _ in dates)
|
||||
with self.connect() as connection:
|
||||
bars = connection.execute(
|
||||
@@ -1210,12 +1299,22 @@ class ReviewDatabase:
|
||||
""",
|
||||
(end_date,),
|
||||
).fetchall()
|
||||
auction = connection.execute(
|
||||
"""
|
||||
SELECT * FROM auction_factors
|
||||
WHERE trade_date = (
|
||||
SELECT MAX(trade_date) FROM auction_factors WHERE trade_date <= ?
|
||||
)
|
||||
""",
|
||||
(end_date,),
|
||||
).fetchall()
|
||||
return {
|
||||
"dates": dates,
|
||||
"bars": [dict(row) for row in bars],
|
||||
"master": [dict(row) for row in master],
|
||||
"indicators": [dict(row) for row in indicators],
|
||||
"moneyflow": [dict(row) for row in moneyflow],
|
||||
"auction": [dict(row) for row in auction],
|
||||
}
|
||||
|
||||
def snapshot_summaries(self, end_date: str, limit: int = 10) -> list[dict[str, Any]]:
|
||||
|
||||
Reference in New Issue
Block a user