tradingSystem/app/core/signal_health.py

36 lines
1.8 KiB
Python

"""把资金监测的数据状态译成交易员提示;不生成交易信号。"""
from datetime import datetime
from zoneinfo import ZoneInfo
def funds_warning(doc, codes, now=None, *, trade_day=True):
now = now or datetime.now(ZoneInfo('Asia/Shanghai'))
hm = now.strftime('%H:%M')
active = trade_day and ('09:45' <= hm <= '11:31' or '13:01' <= hm <= '15:01')
unavailable = '资金监测状态暂不可用,不能据此判断持仓和关注股票没有资金异动。'
try:
at = datetime.fromisoformat(doc['at'])
as_of = datetime.fromisoformat(doc['as_of'])
if at.tzinfo is None or as_of.tzinfo is None:
raise ValueError('missing timezone')
if not 0 <= (now-at).total_seconds() <= 180 or as_of.date() != now.date():
return unavailable if active else ''
if active and not 0 <= (now-as_of).total_seconds() <= 180:
return unavailable
rows = doc['priority']
if not isinstance(rows, dict) or doc.get('source') != 'redis_stock_flow_realtime':
raise ValueError('invalid health report')
wanted = set(codes) | set(rows)
if not wanted:
return ''
bad = sum(rows.get(c, {}).get('state') != 'available' for c in wanted)
incomplete = sum(rows.get(c, {}).get('money_flow_baseline_ready') is False for c in wanted)
messages = []
if bad:
messages.append(f'持仓及关注中有{bad}只股票资金数据暂不可用,暂无提示不代表没有资金异动。')
if incomplete:
messages.append('部分股票的资金流强度仍缺历史数据,该类提示暂不可用;其他信号继续监测。')
return ''.join(messages)
except (KeyError, TypeError, ValueError):
return unavailable if active else ''