fix: sector_ctx日期回溯+按键合并——snapshot日只有部分行业时回退取板块指数adx,修复生产行业牛杠杆不触发
This commit is contained in:
+19
-3
@@ -546,7 +546,7 @@ def is_hk_code(code):
|
|||||||
|
|
||||||
def prepare_sector_context(start_date, end_date):
|
def prepare_sector_context(start_date, end_date):
|
||||||
"""行业上下文: sector_index_daily(全历史) 提供板块趋势; sector_snapshots(近期) 补充净流入"""
|
"""行业上下文: sector_index_daily(全历史) 提供板块趋势; sector_snapshots(近期) 补充净流入"""
|
||||||
global _SECTOR_CTX, _STOCK_SECTOR
|
global _SECTOR_CTX, _STOCK_SECTOR, _SECTOR_DATES
|
||||||
_SECTOR_CTX, _STOCK_SECTOR = {}, {}
|
_SECTOR_CTX, _STOCK_SECTOR = {}, {}
|
||||||
conn = sqlite3.connect(DB_PATH)
|
conn = sqlite3.connect(DB_PATH)
|
||||||
try:
|
try:
|
||||||
@@ -615,6 +615,7 @@ def prepare_sector_context(start_date, end_date):
|
|||||||
"SELECT code, sector_name, source FROM stock_sectors").fetchall():
|
"SELECT code, sector_name, source FROM stock_sectors").fetchall():
|
||||||
if code not in _STOCK_SECTOR and src in ('ths', 'hk_em', 'hk_manual'):
|
if code not in _STOCK_SECTOR and src in ('ths', 'hk_em', 'hk_manual'):
|
||||||
_STOCK_SECTOR[code] = sec
|
_STOCK_SECTOR[code] = sec
|
||||||
|
_SECTOR_DATES = sorted(_SECTOR_CTX.keys())
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
@@ -624,11 +625,26 @@ def mkt_ctx(date, code=None):
|
|||||||
return _MKT_CTX_HK.get(date, {})
|
return _MKT_CTX_HK.get(date, {})
|
||||||
return _MKT_CTX.get(date, {})
|
return _MKT_CTX.get(date, {})
|
||||||
|
|
||||||
|
_SECTOR_DATES = [] # sorted list of dates present in _SECTOR_CTX
|
||||||
|
|
||||||
def sector_ctx(code, date):
|
def sector_ctx(code, date):
|
||||||
sec = _STOCK_SECTOR.get(code)
|
sec = _STOCK_SECTOR.get(code)
|
||||||
if not sec:
|
if not sec or not _SECTOR_DATES:
|
||||||
return {}
|
return {}
|
||||||
return _SECTOR_CTX.get(date, {}).get(sec, {})
|
import bisect
|
||||||
|
i = bisect.bisect_right(_SECTOR_DATES, date) - 1
|
||||||
|
# 从 <= date 向前回溯,按键合并:snapshot 日期可能只有部分行业/字段,
|
||||||
|
# 取每个字段最近一次出现的值(inflow/change 来自 snapshot,adx/slope 来自板块指数)
|
||||||
|
result = {}
|
||||||
|
while i >= 0:
|
||||||
|
v = _SECTOR_CTX[_SECTOR_DATES[i]].get(sec)
|
||||||
|
if v:
|
||||||
|
for k, val in v.items():
|
||||||
|
result.setdefault(k, val)
|
||||||
|
if 'adx' in result and ('change' in result or 'inflow' in result or 'slope' in result):
|
||||||
|
break
|
||||||
|
i -= 1
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
# ══════════════════════════════════════════════════════
|
# ══════════════════════════════════════════════════════
|
||||||
|
|||||||
Reference in New Issue
Block a user