migration: preserve screener and tracking slice
This commit is contained in:
@@ -6,6 +6,74 @@ from typing import Any
|
||||
|
||||
|
||||
class MarketRepositoryMixin:
|
||||
def upsert_stock_master(self, rows: list[dict[str, Any]]) -> int:
|
||||
now = datetime.now().astimezone().isoformat(timespec="seconds")
|
||||
values = [
|
||||
(
|
||||
row.get("ts_code", ""),
|
||||
str(row.get("ts_code", "")).split(".")[0],
|
||||
row.get("name") or "--",
|
||||
row.get("industry") or "",
|
||||
row.get("market") or "",
|
||||
str(row.get("list_date") or ""),
|
||||
now,
|
||||
)
|
||||
for row in rows if row.get("ts_code")
|
||||
]
|
||||
with self.connect() as connection:
|
||||
connection.executemany(
|
||||
"""
|
||||
INSERT INTO stock_master
|
||||
(ts_code, code, name, industry, market, list_date, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(ts_code) DO UPDATE SET
|
||||
code=excluded.code, name=excluded.name, industry=excluded.industry,
|
||||
market=excluded.market, list_date=excluded.list_date, updated_at=excluded.updated_at
|
||||
""",
|
||||
values,
|
||||
)
|
||||
return len(values)
|
||||
|
||||
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 = [
|
||||
(
|
||||
str(row.get("trade_date") or ""), row.get("ts_code", ""),
|
||||
float(row.get("open") or 0), float(row.get("high") or 0),
|
||||
float(row.get("low") or 0), float(row.get("close") or 0),
|
||||
float(row.get("pct_chg") or 0), float(row.get("vol") or 0),
|
||||
float(row.get("amount") or 0),
|
||||
)
|
||||
for row in rows if row.get("trade_date") and row.get("ts_code")
|
||||
]
|
||||
with self.connect() as connection:
|
||||
connection.executemany(
|
||||
"""
|
||||
INSERT INTO daily_bars
|
||||
(trade_date, ts_code, open, high, low, close, pct_chg, vol, amount)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(trade_date, ts_code) DO UPDATE SET
|
||||
open=excluded.open, high=excluded.high, low=excluded.low,
|
||||
close=excluded.close, pct_chg=excluded.pct_chg,
|
||||
vol=excluded.vol, amount=excluded.amount
|
||||
""",
|
||||
values,
|
||||
)
|
||||
return len(values)
|
||||
|
||||
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 get_snapshot(self, trade_date: str) -> dict[str, Any] | None:
|
||||
with self.connect() as connection:
|
||||
row = connection.execute(
|
||||
|
||||
Reference in New Issue
Block a user