Files
stock/backend/app/data/symbols.py
2026-08-07 16:08:34 +08:00

26 lines
795 B
Python
Raw 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 股代码归一化。支持 6 位纯数字或带交易所后缀000001 / 000001.SZ"""
from __future__ import annotations
def plain_code(code: str) -> str:
"""000001.SZ -> 000001"""
return code.strip().upper().split(".")[0]
def to_ts_code(code: str) -> str:
"""转 Tushare ts_code带交易所后缀"""
c = code.strip().upper()
if "." in c:
return c
c = plain_code(c)
# 沪市60xxxx 主板、68xxxx 科创、9xxxxx B 股
if c.startswith(("60", "68", "9")):
return c + ".SH"
# 深市00xxxx 主板/中小、30xxxx 创业、20xxxx B 股
if c.startswith(("00", "30", "20")):
return c + ".SZ"
# 北交所8xxxxx / 4xxxxx
if c.startswith(("8", "4")):
return c + ".BJ"
return c + ".SZ"