Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve get_stock_type detection and support BSE #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions easyquotation/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os
import re

import requests

STOCK_CODE_PATH = os.path.join(os.path.dirname(__file__), "stock_codes.conf")
Expand All @@ -27,15 +28,18 @@ def get_stock_codes(realtime=False):
def get_stock_type(stock_code):
"""判断股票ID对应的证券市场
匹配规则
['50', '51', '60', '90', '110'] 为 sh
['00', '13', '18', '15', '16', '18', '20', '30', '39', '115'] 为 sz
['5', '6', '9'] 开头的为 sh, 其余为 sz
:param stock_code:股票ID, 若以 'sz', 'sh' 开头直接返回对应类型,否则使用内置规则判断
:return 'sh' or 'sz'"""
assert type(stock_code) is str, "stock code need str type"
sh_head = ("50", "51", "60", "90", "110", "113", "118",
"132", "204", "5", "6", "9", "7")
if stock_code.startswith(("sh", "sz", "zz")):
['4', '8'] 为 bj
['5', '6', '7', '9', '110', '113', '118', '132', '204'] 为 sh
其余为 sz
:param stock_code:股票ID, 若以 'sz', 'sh', 'bj' 开头直接返回对应类型,否则使用内置规则判断
:return 'bj', 'sh' or 'sz'"""
assert isinstance(stock_code, str), "stock code need str type"
bj_head = ("4", "8")
sh_head = ("5", "6", "7", "9", "110", "113", "118", "132", "204")
if stock_code.startswith(("sh", "sz", "zz", "bj")):
return stock_code[:2]
else:
return "sh" if stock_code.startswith(sh_head) else "sz"
elif stock_code.startswith(bj_head):
return "bj"
elif stock_code.startswith(sh_head):
return "sh"
return "sz"