88import re
99from typing import Optional
1010
11+ from data_provider .base import is_bse_code
12+
1113
1214# Known exchange prefixes (case-insensitive) and the digit lengths they accept.
1315# e.g. SH600519 -> 600519, HK00700 -> 00700
2830}
2931
3032
33+ def _valid_exchange_code (exchange : str , base : str , digit_lens : tuple [int , ...]) -> bool :
34+ if not (base .isdigit () and len (base ) in digit_lens ):
35+ return False
36+ if exchange == "BJ" :
37+ return is_bse_code (base )
38+ return True
39+
40+
3141def _strip_exchange_prefix (text : str ) -> Optional [str ]:
3242 """Strip leading exchange prefix (SH/SZ/HK etc.) and return the bare digits, or None."""
3343 for prefix , digit_lens in _PREFIX_DIGIT_LENS .items ():
3444 if text .startswith (prefix ):
3545 base = text [len (prefix ):]
36- if base . isdigit () and len ( base ) in digit_lens :
46+ if _valid_exchange_code ( prefix , base , digit_lens ) :
3747 return base .zfill (5 ) if prefix == "HK" else base
3848 return None
3949
@@ -43,7 +53,8 @@ def _strip_exchange_suffix(text: str) -> Optional[str]:
4353 for suffix , digit_lens in _SUFFIX_DIGIT_LENS .items ():
4454 if text .endswith (suffix ):
4555 base = text [: - len (suffix )].strip ()
46- if base .isdigit () and len (base ) in digit_lens :
56+ exchange = suffix .lstrip ("." )
57+ if _valid_exchange_code (exchange , base , digit_lens ):
4758 return base .zfill (5 ) if suffix == ".HK" else base
4859 return None
4960
@@ -70,7 +81,7 @@ def normalize_code(raw: str) -> Optional[str]:
7081
7182 Supports:
7283 - Plain digit codes: 600519, 00700
73- - Suffix format: 600519.SH, 600519.SZ, 00700.HK
84+ - Suffix format: 600519.SH, 600519.SZ, 920493.BJ, 00700.HK
7485 - Prefix format: SH600519, SZ000001, BJ920493, HK00700 (case-insensitive)
7586 - US ticker symbols: AAPL, TSLA
7687 """
0 commit comments