tradingSystem/app/core/price_limits.py

154 lines
6.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""限制价主源的纯校验。只接受目标日沪深A股事实,不计算或补造价格。"""
from collections.abc import Mapping
from datetime import date, datetime
from decimal import Decimal, InvalidOperation, localcontext
import hashlib
import json
import re
MAX_ROWS = 6000
REQUIRED_FIELDS = ('trade_date', 'ts_code', 'pre_close', 'up_limit', 'down_limit')
_CENT = Decimal('0.01')
_SUPPORTED = re.compile(r'(?:60[0-9]{4}\.SH|68[0-9]{4}\.SH|00[0-9]{4}\.SZ|30[0-9]{4}\.SZ)')
_IDENTIFIABLE = re.compile(r'[0-9]{6}\.(?:SH|SZ|BJ)')
def supported(code):
"""仅规范化大小写与首尾空白,不猜测交易所,不包含北交所。"""
return isinstance(code, str) and _SUPPORTED.fullmatch(code.strip().upper()) is not None
def _result(error=None):
return {'records': {}, 'invalid': {}, 'errors': [error] if error else []}
def _day(value):
if isinstance(value, datetime):
return value.date().isoformat()
if isinstance(value, date):
return value.isoformat()
if isinstance(value, bool) or not isinstance(value, (str, int)):
raise ValueError('INVALID_DATE')
value = str(value).strip()
if re.fullmatch(r'[0-9]{8}', value):
value = value[:4]+'-'+value[4:6]+'-'+value[6:]
elif not re.fullmatch(r'[0-9]{4}-[0-9]{2}-[0-9]{2}', value):
raise ValueError('INVALID_DATE')
try:
return date.fromisoformat(value).isoformat()
except ValueError:
raise ValueError('INVALID_DATE') from None
def _price(value):
if isinstance(value, bool) or not isinstance(value, (str, int, float, Decimal)):
raise ValueError('INVALID_PRICE')
text = str(value).strip()
if not text or len(text) > 128:
raise ValueError('INVALID_PRICE')
try:
number = Decimal(text)
if not number.is_finite() or number < 0:
raise ValueError('INVALID_PRICE')
with localcontext() as context:
context.prec = 28
context.traps[InvalidOperation] = True
cents = number.quantize(_CENT)
if cents != number:
raise ValueError('INVALID_PRECISION')
return cents
except InvalidOperation:
raise ValueError('INVALID_PRICE') from None
def normalize_rows(rows, day, requested_codes=None):
"""返回可JSON序列化记录;坏票隔离,结构错误拒绝整批。
日期输出ISO格式,价格输出精确两位字符串。同票任何坏行都会隔离该票,
不能用另一条正确日期或正确价格的行掩盖。错误仅返回固定原因码。
"""
try:
target = _day(day)
except ValueError:
return _result('INVALID_TARGET_DATE')
if not isinstance(rows, (list, tuple)):
return _result('INVALID_ROWS')
if len(rows) > MAX_ROWS:
return _result('TOO_MANY_ROWS')
if any(not isinstance(row, Mapping) for row in rows):
return _result('INVALID_ROW_STRUCTURE')
allowed = None
if requested_codes is not None:
if not isinstance(requested_codes, (list, tuple, set, frozenset)):
return _result('INVALID_REQUESTED_CODES')
if any(not supported(code) for code in requested_codes):
return _result('INVALID_REQUESTED_CODES')
allowed = {code.strip().upper() for code in requested_codes} or None
result = _result()
records, invalid = result['records'], result['invalid']
for index, row in enumerate(rows):
raw_code = row.get('ts_code')
code = raw_code.strip().upper() if isinstance(raw_code, str) else ''
# 不回显任意上游文本;无法识别为证券代码时只记录行号。
key = code if _IDENTIFIABLE.fullmatch(code) else f'row:{index+1}'
reason = None
if not supported(code):
reason = 'OUT_OF_SCOPE' if _IDENTIFIABLE.fullmatch(code) else 'INVALID_CODE'
elif allowed is not None and code not in allowed:
reason = 'NOT_REQUESTED'
elif any(field not in row for field in REQUIRED_FIELDS):
reason = 'MISSING_FIELD'
else:
try:
if _day(row['trade_date']) != target:
raise ValueError('DATE_MISMATCH')
close, high, low = (_price(row[field]) for field in ('pre_close', 'up_limit', 'down_limit'))
if high == 0 or low == 0:
raise ValueError('UNSUPPORTED')
if close <= 0:
raise ValueError('INVALID_PRE_CLOSE')
if low >= high:
raise ValueError('INVALID_RANGE')
if not low <= close <= high:
raise ValueError('PRE_CLOSE_OUTSIDE_LIMITS')
record = {'trade_date': target, 'ts_code': code, 'pre_close': format(close, '.2f'),
'up_limit': format(high, '.2f'), 'down_limit': format(low, '.2f')}
digest = hashlib.sha256(json.dumps(record, sort_keys=True, ensure_ascii=False,
separators=(',', ':')).encode('utf-8')).hexdigest()
record['version'] = digest
if code in records and records[code] != record:
raise ValueError('DUPLICATE_CONFLICT')
if code not in invalid:
records[code] = record
except ValueError as exc:
reason = str(exc)
if reason:
records.pop(code, None)
invalid.setdefault(key, reason)
return result
def provider_decode(body, day, requested_codes=None):
"""校验Tushare JSON封套。HTTP状态由调用层检查,不输出msg或响应正文。"""
if not isinstance(body, Mapping) or type(body.get('code')) is not int:
return _result('INVALID_PROVIDER_RESPONSE')
if body['code'] != 0:
return _result('PROVIDER_ERROR')
data = body.get('data')
if not isinstance(data, Mapping):
return _result('INVALID_PROVIDER_DATA')
fields, items = data.get('fields'), data.get('items')
if (not isinstance(fields, list) or not fields
or any(not isinstance(field, str) or not field for field in fields)
or len(set(fields)) != len(fields)):
return _result('INVALID_PROVIDER_FIELDS')
if any(field not in fields for field in REQUIRED_FIELDS):
return _result('MISSING_PROVIDER_FIELDS')
if not isinstance(items, list):
return _result('INVALID_PROVIDER_ITEMS')
if len(items) > MAX_ROWS:
return _result('TOO_MANY_ROWS')
if any(not isinstance(item, (list, tuple)) or len(item) != len(fields) for item in items):
return _result('INVALID_PROVIDER_ITEM_STRUCTURE')
return normalize_rows([dict(zip(fields, item)) for item in items], day, requested_codes)