fix: 完善市场代码判断逻辑,增加北交所支持

市场代码规则:
- 6开头 → SH(上海)
- 0、3开头 → SZ(深圳)
- 4、8开头 → BJ(北京)
This commit is contained in:
2026-04-08 19:14:23 +08:00
parent 2857d8febb
commit a2c9b347ca

View File

@@ -67,14 +67,28 @@ def load_stock_list():
def get_stock_codes_with_suffix(df):
"""将股票代码转换为tushare格式添加后缀"""
"""将股票代码转换为tushare格式添加后缀
市场代码规则:
- 6开头 → SH上海
- 0、3开头 → SZ深圳
- 4、8开头 → BJ北京
"""
codes = []
for code in df['code']:
code = str(code).zfill(6) # 补零到6位
if code.startswith('6'):
first_digit = code[0]
if first_digit == '6':
ts_code = f"{code}.SH"
else:
elif first_digit in ('0', '3'):
ts_code = f"{code}.SZ"
elif first_digit in ('4', '8'):
ts_code = f"{code}.BJ"
else:
# 未知市场,默认深圳
ts_code = f"{code}.SZ"
codes.append(ts_code)
return codes