#!/usr/bin/env python3 """키움 전종목 침묵 구간의 한투 틱이 키움 구독 종목인지.""" from __future__ import annotations import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) from database import TradeDB db = TradeDB() windows = [ ("2026-08-19 09:05:07", "2026-08-19 09:05:33"), ("2026-08-19 09:08:50", "2026-08-19 09:09:01"), ("2026-08-19 09:11:09", "2026-08-19 09:14:55"), ("2026-08-19 09:14:57", "2026-08-19 09:19:11"), ("2026-08-19 09:27:24", "2026-08-19 09:31:32"), ] kw_codes = { r["code"] for r in db.conn.execute( "SELECT DISTINCT code FROM ws_ticks WHERE source=%s AND recv_ts >= %s AND recv_ts < %s", ("kiwoom", "2026-08-19 09:00:00", "2026-08-19 09:35:00"), ).fetchall() } print("kiwoom codes today morning", len(kw_codes)) for a, b in windows: rows = db.conn.execute( "SELECT code, COUNT(*) n FROM ws_ticks WHERE source=%s AND recv_ts >= %s AND recv_ts <= %s " "GROUP BY code ORDER BY n DESC LIMIT 8", ("kis", a, b), ).fetchall() tot = db.conn.execute( "SELECT COUNT(*) n, COUNT(DISTINCT code) codes FROM ws_ticks " "WHERE source=%s AND recv_ts >= %s AND recv_ts <= %s", ("kis", a, b), ).fetchone() overlap_n = 0 overlap_codes = 0 for r in db.conn.execute( "SELECT code, COUNT(*) n FROM ws_ticks WHERE source=%s AND recv_ts >= %s AND recv_ts <= %s GROUP BY code", ("kis", a, b), ).fetchall(): if r["code"] in kw_codes: overlap_n += int(r["n"]) overlap_codes += 1 print(f"\n{a} ~ {b}") print(" kis", dict(tot), "overlap_kw_univ ticks", overlap_n, "codes", overlap_codes) print(" top", [dict(x) for x in rows[:5]])