feat: complete strategy and market data improvements
This commit is contained in:
+199
-2
@@ -276,6 +276,49 @@ class ReviewDatabase:
|
||||
CREATE INDEX IF NOT EXISTS idx_auction_factors_code_date
|
||||
ON auction_factors(ts_code, trade_date DESC);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS earnings_events (
|
||||
end_date TEXT NOT NULL,
|
||||
ann_date TEXT NOT NULL,
|
||||
ts_code TEXT NOT NULL,
|
||||
forecast_profit REAL,
|
||||
actual_profit REAL,
|
||||
surprise_pct REAL,
|
||||
revenue_yoy REAL,
|
||||
netprofit_yoy REAL,
|
||||
source TEXT NOT NULL DEFAULT '',
|
||||
PRIMARY KEY (end_date, ann_date, ts_code)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_earnings_events_code_announcement
|
||||
ON earnings_events(ts_code, ann_date DESC, end_date DESC);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS popularity_factors (
|
||||
trade_date TEXT NOT NULL,
|
||||
ts_code TEXT NOT NULL,
|
||||
ths_rank INTEGER,
|
||||
dc_rank INTEGER,
|
||||
combined_score REAL NOT NULL DEFAULT 0,
|
||||
rank_change INTEGER,
|
||||
dual_source INTEGER NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (trade_date, ts_code)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_popularity_factors_code_date
|
||||
ON popularity_factors(ts_code, trade_date DESC);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS lhb_institution_daily (
|
||||
trade_date TEXT NOT NULL,
|
||||
ts_code TEXT NOT NULL,
|
||||
net_buy_amount REAL NOT NULL DEFAULT 0,
|
||||
buy_amount REAL NOT NULL DEFAULT 0,
|
||||
sell_amount REAL NOT NULL DEFAULT 0,
|
||||
seat_count INTEGER NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (trade_date, ts_code)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_lhb_institution_code_date
|
||||
ON lhb_institution_daily(ts_code, trade_date DESC);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS screener_strategies (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER,
|
||||
@@ -1446,6 +1489,109 @@ class ReviewDatabase:
|
||||
)
|
||||
return len(values)
|
||||
|
||||
def upsert_earnings_events(self, rows: list[dict[str, Any]]) -> int:
|
||||
values = [
|
||||
(
|
||||
str(row.get("end_date") or ""),
|
||||
str(row.get("ann_date") or ""),
|
||||
str(row.get("ts_code") or ""),
|
||||
_optional_float(row.get("forecast_profit")),
|
||||
_optional_float(row.get("actual_profit")),
|
||||
_optional_float(row.get("surprise_pct")),
|
||||
_optional_float(row.get("revenue_yoy")),
|
||||
_optional_float(row.get("netprofit_yoy")),
|
||||
str(row.get("source") or ""),
|
||||
)
|
||||
for row in rows
|
||||
if row.get("end_date") and row.get("ann_date") and row.get("ts_code")
|
||||
]
|
||||
with self.connect() as connection:
|
||||
connection.executemany(
|
||||
"""
|
||||
INSERT INTO earnings_events
|
||||
(end_date, ann_date, ts_code, forecast_profit, actual_profit,
|
||||
surprise_pct, revenue_yoy, netprofit_yoy, source)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(end_date, ann_date, ts_code) DO UPDATE SET
|
||||
forecast_profit=excluded.forecast_profit,
|
||||
actual_profit=excluded.actual_profit,
|
||||
surprise_pct=excluded.surprise_pct,
|
||||
revenue_yoy=excluded.revenue_yoy,
|
||||
netprofit_yoy=excluded.netprofit_yoy,
|
||||
source=excluded.source
|
||||
""",
|
||||
values,
|
||||
)
|
||||
return len(values)
|
||||
|
||||
def upsert_popularity_factors(self, rows: list[dict[str, Any]]) -> int:
|
||||
values = [
|
||||
(
|
||||
str(row.get("trade_date") or ""),
|
||||
str(row.get("ts_code") or ""),
|
||||
int(row["ths_rank"]) if row.get("ths_rank") not in (None, "") else None,
|
||||
int(row["dc_rank"]) if row.get("dc_rank") not in (None, "") else None,
|
||||
float(row.get("combined_score") or 0),
|
||||
int(row["rank_change"]) if row.get("rank_change") not in (None, "") else None,
|
||||
int(bool(row.get("dual_source"))),
|
||||
)
|
||||
for row in rows
|
||||
if row.get("trade_date") and row.get("ts_code")
|
||||
]
|
||||
with self.connect() as connection:
|
||||
connection.executemany(
|
||||
"""
|
||||
INSERT INTO popularity_factors
|
||||
(trade_date, ts_code, ths_rank, dc_rank, combined_score,
|
||||
rank_change, dual_source)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(trade_date, ts_code) DO UPDATE SET
|
||||
ths_rank=excluded.ths_rank,
|
||||
dc_rank=excluded.dc_rank,
|
||||
combined_score=excluded.combined_score,
|
||||
rank_change=excluded.rank_change,
|
||||
dual_source=excluded.dual_source
|
||||
""",
|
||||
values,
|
||||
)
|
||||
return len(values)
|
||||
|
||||
def upsert_lhb_institutions(self, rows: list[dict[str, Any]]) -> int:
|
||||
grouped: dict[tuple[str, str], dict[str, float | int]] = {}
|
||||
for row in rows:
|
||||
trade_date = str(row.get("trade_date") or "")
|
||||
ts_code = str(row.get("ts_code") or "")
|
||||
seat_name = str(row.get("exalter") or row.get("seat_name") or "")
|
||||
if not trade_date or not ts_code or "机构专用" not in seat_name:
|
||||
continue
|
||||
group = grouped.setdefault(
|
||||
(trade_date, ts_code),
|
||||
{"net": 0.0, "buy": 0.0, "sell": 0.0, "seats": 0},
|
||||
)
|
||||
group["net"] = float(group["net"]) + float(row.get("net_buy") or row.get("net_amount") or 0)
|
||||
group["buy"] = float(group["buy"]) + float(row.get("buy") or row.get("buy_amount") or 0)
|
||||
group["sell"] = float(group["sell"]) + float(row.get("sell") or row.get("sell_amount") or 0)
|
||||
group["seats"] = int(group["seats"]) + 1
|
||||
values = [
|
||||
(trade_date, ts_code, item["net"], item["buy"], item["sell"], item["seats"])
|
||||
for (trade_date, ts_code), item in grouped.items()
|
||||
]
|
||||
with self.connect() as connection:
|
||||
connection.executemany(
|
||||
"""
|
||||
INSERT INTO lhb_institution_daily
|
||||
(trade_date, ts_code, net_buy_amount, buy_amount, sell_amount, seat_count)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(trade_date, ts_code) DO UPDATE SET
|
||||
net_buy_amount=excluded.net_buy_amount,
|
||||
buy_amount=excluded.buy_amount,
|
||||
sell_amount=excluded.sell_amount,
|
||||
seat_count=excluded.seat_count
|
||||
""",
|
||||
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,)
|
||||
@@ -1566,6 +1712,21 @@ class ReviewDatabase:
|
||||
""",
|
||||
(end_date,),
|
||||
).fetchone()[0]
|
||||
earnings_rows = connection.execute(
|
||||
"""
|
||||
SELECT COUNT(*) FROM earnings_events
|
||||
WHERE ann_date <= ? AND ann_date >= replace(date(?, '-45 day'), '-', '')
|
||||
""",
|
||||
(end_date, f"{end_date[:4]}-{end_date[4:6]}-{end_date[6:8]}"),
|
||||
).fetchone()[0]
|
||||
popularity_rows = connection.execute(
|
||||
"SELECT COUNT(*) FROM popularity_factors WHERE trade_date = ?",
|
||||
(end_date,),
|
||||
).fetchone()[0]
|
||||
institution_rows = connection.execute(
|
||||
"SELECT COUNT(*) FROM lhb_institution_daily WHERE trade_date = ?",
|
||||
(end_date,),
|
||||
).fetchone()[0]
|
||||
return {
|
||||
"market": bool(market),
|
||||
"auction": bool(auction),
|
||||
@@ -1579,6 +1740,12 @@ class ReviewDatabase:
|
||||
"dividend_years": int(dividend_years or 0),
|
||||
"moneyflow_history": int(moneyflow_dates or 0) >= 5,
|
||||
"moneyflow_dates": int(moneyflow_dates or 0),
|
||||
"earnings_events": int(earnings_rows or 0) > 0,
|
||||
"earnings_event_rows": int(earnings_rows or 0),
|
||||
"popularity": int(popularity_rows or 0) > 0,
|
||||
"popularity_rows": int(popularity_rows or 0),
|
||||
"institutions": int(institution_rows or 0) > 0,
|
||||
"institution_rows": int(institution_rows or 0),
|
||||
}
|
||||
|
||||
def load_factor_data(self, end_date: str, limit_dates: int = 80) -> dict[str, Any]:
|
||||
@@ -1588,7 +1755,8 @@ class ReviewDatabase:
|
||||
"dates": [], "bars": [], "master": [], "indicators": [],
|
||||
"indicator_history": [], "indicator_series": [], "fundamentals": [],
|
||||
"moneyflow": [], "moneyflow_history": [], "auction": [],
|
||||
"benchmarks": [],
|
||||
"benchmarks": [], "fundamental_history": [],
|
||||
"earnings_events": [], "popularity": [], "institutions": [],
|
||||
}
|
||||
placeholders = ",".join("?" for _ in dates)
|
||||
with self.connect() as connection:
|
||||
@@ -1623,7 +1791,8 @@ class ReviewDatabase:
|
||||
).fetchall()
|
||||
indicator_series = connection.execute(
|
||||
f"""
|
||||
SELECT trade_date, ts_code, turnover_rate, volume_ratio
|
||||
SELECT trade_date, ts_code, turnover_rate, volume_ratio,
|
||||
total_mv, circ_mv, pe_ttm, pb, ps_ttm, dv_ttm
|
||||
FROM daily_indicators
|
||||
WHERE trade_date IN ({placeholders})
|
||||
ORDER BY trade_date, ts_code
|
||||
@@ -1644,6 +1813,14 @@ class ReviewDatabase:
|
||||
""",
|
||||
(end_date,),
|
||||
).fetchall()
|
||||
fundamental_history = connection.execute(
|
||||
"""
|
||||
SELECT * FROM fundamental_indicators
|
||||
WHERE ann_date = '' OR ann_date <= ?
|
||||
ORDER BY ann_date, end_date, ts_code
|
||||
""",
|
||||
(end_date,),
|
||||
).fetchall()
|
||||
moneyflow = connection.execute(
|
||||
"""
|
||||
SELECT * FROM moneyflow_daily
|
||||
@@ -1680,6 +1857,22 @@ class ReviewDatabase:
|
||||
""",
|
||||
dates,
|
||||
).fetchall()
|
||||
earnings_events = connection.execute(
|
||||
"""
|
||||
SELECT * FROM earnings_events
|
||||
WHERE ann_date <= ?
|
||||
ORDER BY ann_date, end_date, ts_code
|
||||
""",
|
||||
(end_date,),
|
||||
).fetchall()
|
||||
popularity = connection.execute(
|
||||
"SELECT * FROM popularity_factors WHERE trade_date = ? ORDER BY ts_code",
|
||||
(end_date,),
|
||||
).fetchall()
|
||||
institutions = connection.execute(
|
||||
"SELECT * FROM lhb_institution_daily WHERE trade_date = ? ORDER BY ts_code",
|
||||
(end_date,),
|
||||
).fetchall()
|
||||
return {
|
||||
"dates": dates,
|
||||
"bars": [dict(row) for row in bars],
|
||||
@@ -1688,10 +1881,14 @@ class ReviewDatabase:
|
||||
"indicator_history": [dict(row) for row in indicator_history],
|
||||
"indicator_series": [dict(row) for row in indicator_series],
|
||||
"fundamentals": [dict(row) for row in fundamentals],
|
||||
"fundamental_history": [dict(row) for row in fundamental_history],
|
||||
"moneyflow": [dict(row) for row in moneyflow],
|
||||
"moneyflow_history": [dict(row) for row in moneyflow_history],
|
||||
"auction": [dict(row) for row in auction],
|
||||
"benchmarks": [dict(row) for row in benchmarks],
|
||||
"earnings_events": [dict(row) for row in earnings_events],
|
||||
"popularity": [dict(row) for row in popularity],
|
||||
"institutions": [dict(row) for row in institutions],
|
||||
}
|
||||
|
||||
def snapshot_summaries(self, end_date: str, limit: int = 10) -> list[dict[str, Any]]:
|
||||
|
||||
Reference in New Issue
Block a user