From 49536864a794b28677d0b526b628a0c655eed0d1 Mon Sep 17 00:00:00 2001 From: rootphantomer Date: Tue, 4 Jul 2023 17:57:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix=20stock.get=5Fquote=5Fhistory=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E8=8E=B7=E5=8F=96=E6=95=B0=E6=8D=AE=E6=88=96=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + efinance/common/getter.py | 47 +++++++++++++++++++++++++++------------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 8bac3e7..bffbc6e 100644 --- a/.gitignore +++ b/.gitignore @@ -158,6 +158,7 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. .idea/ +.vscode/ docs/build/ efinance/data diff --git a/efinance/common/getter.py b/efinance/common/getter.py index f16f5fd..32f0279 100644 --- a/efinance/common/getter.py +++ b/efinance/common/getter.py @@ -24,6 +24,7 @@ import warnings warnings.filterwarnings("once") + @to_numeric def get_realtime_quotes_by_fs(fs: str, **kwargs) -> pd.DataFrame: """ @@ -36,7 +37,8 @@ def get_realtime_quotes_by_fs(fs: str, **kwargs) -> pd.DataFrame: """ - columns = {**EASTMONEY_QUOTE_FIELDS, **kwargs.get(MagicConfig.EXTRA_FIELDS, {})} + columns = {**EASTMONEY_QUOTE_FIELDS, ** + kwargs.get(MagicConfig.EXTRA_FIELDS, {})} fields = ",".join(columns.keys()) params = ( ('pn', '1'), @@ -57,7 +59,8 @@ def get_realtime_quotes_by_fs(fs: str, **kwargs) -> pd.DataFrame: df = df.rename(columns=columns) df: pd.DataFrame = df[columns.values()] df['行情ID'] = df['市场编号'].astype(str) + '.' + df['代码'].astype(str) - df['市场类型'] = df['市场编号'].astype(str).apply(lambda x: MARKET_NUMBER_DICT.get(x)) + df['市场类型'] = df['市场编号'].astype(str).apply( + lambda x: MARKET_NUMBER_DICT.get(x)) df['更新时间'] = df['更新时间戳'].apply(lambda x: str(datetime.fromtimestamp(x))) df['最新交易日'] = pd.to_datetime(df['最新交易日'], format='%Y%m%d').astype(str) tmp = df['最新交易日'] @@ -74,6 +77,7 @@ def get_quote_history_single( end: str = '20500101', klt: int = 101, fqt: int = 1, + lmt: int = 100, **kwargs, ) -> pd.DataFrame: """ @@ -88,16 +92,29 @@ def get_quote_history_single( quote_id = code else: quote_id = get_quote_id(code) - params = ( - ('fields1', 'f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13'), - ('fields2', fields2), - ('beg', beg), - ('end', end), - ('rtntype', '6'), - ('secid', quote_id), - ('klt', f'{klt}'), - ('fqt', f'{fqt}'), - ) + if klt < 101: + params = ( + ("fields1", "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13"), + ("fields2", fields2), + # ("beg", beg), + ("end", "20500101"), + ("rtntype", "6"), + ("secid", quote_id), + ("klt", f"{klt}"), + ("fqt", f"{fqt}"), + ("lmt", f"{lmt}"), + ) + else: + params = ( + ('fields1', 'f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13'), + ('fields2', fields2), + ('beg', beg), + ('end', end), + ('rtntype', '6'), + ('secid', quote_id), + ('klt', f'{klt}'), + ('fqt', f'{fqt}'), + ) url = 'https://push2his.eastmoney.com/api/qt/stock/kline/get' @@ -330,7 +347,8 @@ def get_base_info(quote_id: str) -> pd.Series: if not items: return pd.Series(index=EASTMONEY_BASE_INFO_FIELDS.values(), dtype='object') - s = pd.Series(items, dtype='object').rename(index=EASTMONEY_BASE_INFO_FIELDS) + s = pd.Series(items, dtype='object').rename( + index=EASTMONEY_BASE_INFO_FIELDS) return s @@ -412,7 +430,8 @@ def get_latest_quote(quote_id_list: Union[str, List[str]], **kwargs) -> pd.DataF quote_id_list = [quote_id_list] secids: List[str] = quote_id_list - columns = {**EASTMONEY_QUOTE_FIELDS, **kwargs.get(MagicConfig.EXTRA_FIELDS, {})} + columns = {**EASTMONEY_QUOTE_FIELDS, ** + kwargs.get(MagicConfig.EXTRA_FIELDS, {})} fields = ",".join(columns.keys()) params = ( ('OSVersion', '14.3'), From 342fe245fb1e78c18a40f1c07d2a630ca35e708b Mon Sep 17 00:00:00 2001 From: rootphantomer Date: Tue, 4 Jul 2023 18:22:47 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=94=AF=E6=8C=81ef.stock.get=5Fquote=5Fhi?= =?UTF-8?q?story=E8=B0=83=E7=94=A8=E7=9A=84=E6=97=B6=E5=80=99=20=E4=BC=A0?= =?UTF-8?q?=E5=85=A5=20lmt=20=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- efinance/common/getter.py | 3 ++- efinance/stock/getter.py | 26 +++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/efinance/common/getter.py b/efinance/common/getter.py index 32f0279..8ce6527 100644 --- a/efinance/common/getter.py +++ b/efinance/common/getter.py @@ -182,6 +182,7 @@ def get_quote_history( end: str = '20500101', klt: int = 101, fqt: int = 1, + lmt: int = 100, **kwargs, ) -> Union[pd.DataFrame, Dict[str, pd.DataFrame]]: """ @@ -226,7 +227,7 @@ def get_quote_history( if isinstance(codes, str): return get_quote_history_single( - codes, beg=beg, end=end, klt=klt, fqt=fqt, **kwargs + codes, beg=beg, end=end, klt=klt, fqt=fqt, lmt=lmt, **kwargs ) elif hasattr(codes, '__iter__'): diff --git a/efinance/stock/getter.py b/efinance/stock/getter.py index 9f7365f..57e985e 100644 --- a/efinance/stock/getter.py +++ b/efinance/stock/getter.py @@ -163,6 +163,7 @@ def get_quote_history( end: str = '20500101', klt: int = 101, fqt: int = 1, + lmt: int = 100, **kwargs, ) -> Union[pd.DataFrame, Dict[str, pd.DataFrame]]: """ @@ -243,14 +244,15 @@ def get_quote_history( """ df = get_quote_history_for_stock( - stock_codes, beg=beg, end=end, klt=klt, fqt=fqt, **kwargs + stock_codes, beg=beg, end=end, klt=klt, fqt=fqt, lmt=lmt, **kwargs ) if isinstance(df, pd.DataFrame): df.rename(columns={'代码': '股票代码', '名称': '股票名称'}, inplace=True) elif isinstance(df, dict): for stock_code in df.keys(): - df[stock_code].rename(columns={'代码': '股票代码', '名称': '股票名称'}, inplace=True) + df[stock_code].rename( + columns={'代码': '股票代码', '名称': '股票名称'}, inplace=True) return df @@ -592,7 +594,8 @@ def get_public_dates(stock_code: str) -> List[str]: url = 'https://emh5.eastmoney.com/api/GuBenGuDong/GetShiDaLiuTongGuDong' response = requests.post(url, json=data) response.encoding = 'utf-8' - items: List[dict] = jsonpath(response.json(), f'$..ShiDaLiuTongGuDongList[:]') + items: List[dict] = jsonpath( + response.json(), f'$..ShiDaLiuTongGuDongList[:]') if not items: continue df = pd.DataFrame(items) @@ -669,7 +672,8 @@ def get_all_report_dates() -> pd.DataFrame: ('ps', '2000'), ) url = 'https://datacenter.eastmoney.com/securities/api/data/get' - response = requests.get(url, headers=EASTMONEY_REQUEST_HEADERS, params=params) + response = requests.get( + url, headers=EASTMONEY_REQUEST_HEADERS, params=params) items = jsonpath(response.json(), '$..data[:]') if not items: pd.DataFrame(columns=fields.values()) @@ -777,10 +781,12 @@ def get_all_company_performance(date: str = None) -> pd.DataFrame: ('sty', 'ALL'), ('token', '894050c76af8597a853f5b408b759f5d'), # ! 只选沪深A股 - ('filter', f'(SECURITY_TYPE_CODE in ("058001001","058001008")){date}'), + ('filter', + f'(SECURITY_TYPE_CODE in ("058001001","058001008")){date}'), ) url = 'http://datacenter-web.eastmoney.com/api/data/get' - response = session.get(url, headers=EASTMONEY_REQUEST_HEADERS, params=params) + response = session.get( + url, headers=EASTMONEY_REQUEST_HEADERS, params=params) items = jsonpath(response.json(), '$..data[:]') if not items: break @@ -901,7 +907,8 @@ def get_latest_holder_number(date: str = None) -> pd.DataFrame: params = tuple(params) url = 'http://datacenter-web.eastmoney.com/api/data/v1/get' - response = session.get(url, headers=EASTMONEY_REQUEST_HEADERS, params=params) + response = session.get( + url, headers=EASTMONEY_REQUEST_HEADERS, params=params) items = jsonpath(response.json(), '$..data[:]') if not items: break @@ -1012,7 +1019,8 @@ def get_daily_billboard(start_date: str = None, end_date: str = None) -> pd.Data ('columns', 'ALL'), ('source', 'WEB'), ('client', 'WEB'), - ('filter', f"(TRADE_DATE<='{end_date}')(TRADE_DATE>='{start_date}')"), + ('filter', + f"(TRADE_DATE<='{end_date}')(TRADE_DATE>='{start_date}')"), ) url = 'http://datacenter-web.eastmoney.com/api/data/v1/get' @@ -1321,7 +1329,7 @@ def get_quote_snapshot(stock_code: str) -> pd.Series: } s = pd.Series(index=columns.values(), dtype='object') try: - qd: dict = json.loads(response.text[start_index : end_index + 1]) + qd: dict = json.loads(response.text[start_index: end_index + 1]) except: return s if not qd.get('fivequote'):