63 lines
2.7 KiB
Python
63 lines
2.7 KiB
Python
"""执行限制价的本地只读入口,不调用外部行情接口。"""
|
|
from app.core import price_limits as core
|
|
from app.core.execution_policy import timestamp, local_now
|
|
from app.db import session as db
|
|
from app.repo import rotation_repo as store
|
|
from app.services import param_store
|
|
|
|
SOURCES = ('gp_stk_limit', 'tushare.stk_limit')
|
|
|
|
|
|
def day_key(day):
|
|
return 'price-limits:' + core._day(day)
|
|
|
|
|
|
def read_cache(day):
|
|
"""坏缓存按无值处理;执行读取不更新缓存、不延长有效日期。"""
|
|
target = core._day(day)
|
|
value, _ = store.get(day_key(target))
|
|
if not isinstance(value, dict) or value.get('trade_date') != target:
|
|
return {}
|
|
entries = value.get('entries')
|
|
if not isinstance(entries, dict):
|
|
return {}
|
|
valid = {}
|
|
for code, entry in entries.items():
|
|
if not core.supported(code) or not isinstance(entry, dict):
|
|
continue
|
|
record = entry.get('record')
|
|
if entry.get('status') != 'VALID' or entry.get('source') not in SOURCES or not entry.get('fetched_at'):
|
|
continue
|
|
fetched = timestamp(entry['fetched_at'])
|
|
if not fetched or fetched.date().isoformat() != target or fetched > local_now():
|
|
continue
|
|
result = core.normalize_rows([record], target, [code])
|
|
normalized = result['records'].get(code)
|
|
if normalized and normalized['version'] == (record or {}).get('version'):
|
|
valid[code] = dict(normalized, source=entry['source'], fetched_at=entry['fetched_at'])
|
|
return valid
|
|
|
|
|
|
def get_limits(day, codes=None):
|
|
"""开关开启后只读本实例当日缓存;失败不混用其他日期或推算来源。"""
|
|
day = core._day(day)
|
|
wanted = None if codes is None else {str(c).strip().upper() for c in codes if core.supported(c)}
|
|
if wanted == set():
|
|
return {}
|
|
if param_store.get_bool('PMS_LIMIT_CACHE_ENABLED', False):
|
|
try:
|
|
result = read_cache(day)
|
|
except Exception:
|
|
return {}
|
|
return {c: r for c, r in result.items() if wanted is None or c in wanted}
|
|
# 关闭新功能时保留原有数据路径,但在读取入口限定沪深证券。
|
|
params = {'day': day}
|
|
where = ''
|
|
if wanted is not None:
|
|
params.update({f'c{i}': c for i, c in enumerate(sorted(wanted))})
|
|
where = ' AND ts_code IN (' + ','.join(':'+k for k in params if k != 'day') + ')'
|
|
else:
|
|
where = " AND (ts_code LIKE '60____.SH' OR ts_code LIKE '68____.SH' OR ts_code LIKE '00____.SZ' OR ts_code LIKE '30____.SZ')"
|
|
rows = db.fetch_all('SELECT ts_code,up_limit,down_limit FROM gp_stk_limit WHERE trade_date=:day'+where, params)
|
|
return {r['ts_code']: r for r in rows if core.supported(r.get('ts_code'))}
|