From a8bdaa270333f87d67dffdddcf5cc0b8ce48a946 Mon Sep 17 00:00:00 2001 From: sheep Date: Sun, 19 Jun 2022 10:38:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=20common=20=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E4=B8=AD=E7=9A=84=E9=85=8D=E7=BD=AE=E4=BB=A5=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89=E8=8E=B7=E5=8F=96=E6=9B=B4?= =?UTF-8?q?=E5=A4=9A=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changelog.md | 9 ++++---- efinance/__version__.py | 2 +- efinance/bond/getter.py | 4 ++-- efinance/common/config.py | 12 ++++++++-- efinance/common/getter.py | 48 +++++++++++++++++++++++++++------------ efinance/stock/getter.py | 10 ++++---- 6 files changed, 57 insertions(+), 28 deletions(-) diff --git a/changelog.md b/changelog.md index b0214cc..4048ac1 100644 --- a/changelog.md +++ b/changelog.md @@ -1,10 +1,11 @@ # Changelog -## [Unrelease] +## v0.4.6(2022-06-19) ### Changed - 从 `stock` 模块中抽离出获取证券最新行情的函数 +- 增强 `common` 模块中的配置以支持自定义获取更多属性 ## v0.4.5(2022-06-05) @@ -38,7 +39,7 @@ ### Changed - 扩展 `get_quote_history` 函数,支持通过设定 `kwargs` 来指定返回类型 - + ### Added - 为 `stock` 模块添加函数 `get_quote_snapshot` 以支持获取沪深市场 3 秒行情快照 @@ -50,10 +51,10 @@ ### Fiexd - 修复 fund 模块的命名错误以及其他 bug - + ### Added -- 添加对获取北证A股市场行情的支持 +- 添加对获取北证 A 股市场行情的支持 --- diff --git a/efinance/__version__.py b/efinance/__version__.py index 9b93b22..45810fe 100644 --- a/efinance/__version__.py +++ b/efinance/__version__.py @@ -1,5 +1,5 @@ __title__ = 'efinance' -__version__ = '0.4.5' +__version__ = '0.4.6' __author__ = 'micro sheep' __url__ = 'https://github.com/Micro-sheep/efinance' __author_email__ = 'micro-sheep@outlook.com' diff --git a/efinance/bond/getter.py b/efinance/bond/getter.py index 328cbb5..5686a06 100644 --- a/efinance/bond/getter.py +++ b/efinance/bond/getter.py @@ -189,7 +189,7 @@ def get_all_base_info() -> pd.DataFrame: @process_dataframe_and_series(remove_columns_and_indexes=['市场编号']) @to_numeric -def get_realtime_quotes() -> pd.DataFrame: +def get_realtime_quotes(**kwargs) -> pd.DataFrame: """ 获取沪深市场全部债券实时行情信息 @@ -216,7 +216,7 @@ def get_realtime_quotes() -> pd.DataFrame: 394 123066 赛意转债 -6.0 193.08 203.999 193.08 203.0 -12.32 323.13 0.22 - 133317 261546032.0 205.4 79660753 79660753 0.123066 深A """ - df = get_realtime_quotes_by_fs(FS_DICT['bond']) + df = get_realtime_quotes_by_fs(FS_DICT['bond'], **kwargs) df.rename(columns={'代码': '债券代码', '名称': '债券名称'}, inplace=True) diff --git a/efinance/common/config.py b/efinance/common/config.py index 97c9ae0..77360e7 100644 --- a/efinance/common/config.py +++ b/efinance/common/config.py @@ -1,6 +1,12 @@ from pathlib import Path HERE = Path(__file__).parent + +class MagicConfig: + EXTRA_FIELDS = 'extra_fields' + QUOTE_ID_MODE = 'quote_id_mode' + + # 各个市场编号 MARKET_NUMBER_DICT = { '0': '深A', @@ -108,7 +114,9 @@ 'f18': '昨日收盘', 'f20': '总市值', 'f21': '流通市值', - 'f13': '市场编号' + 'f13': '市场编号', + 'f124': '更新时间戳', + 'f297': '最新交易日', } # 股票、债券历史大单数据表头 @@ -149,4 +157,4 @@ 'f105': '净利润', 'f186': '毛利率' -} \ No newline at end of file +} diff --git a/efinance/common/getter.py b/efinance/common/getter.py index d6529c7..13f83d4 100644 --- a/efinance/common/getter.py +++ b/efinance/common/getter.py @@ -1,23 +1,23 @@ +from datetime import datetime from typing import Dict, List, Union import multitasking -import numpy as np import pandas as pd from jsonpath import jsonpath from retry import retry from tqdm import tqdm -from efinance.stock.config import EASTMONEY_STOCK_BASE_INFO_FIELDS - from ..common.config import MARKET_NUMBER_DICT from ..shared import BASE_INFO_CACHE, session from ..utils import get_quote_id, to_numeric -from .config import (EASTMONEY_BASE_INFO_FIELDS, EASTMONEY_HISTORY_BILL_FIELDS, EASTMONEY_KLINE_FIELDS, - EASTMONEY_QUOTE_FIELDS, EASTMONEY_REQUEST_HEADERS) +from .config import (EASTMONEY_BASE_INFO_FIELDS, EASTMONEY_HISTORY_BILL_FIELDS, + EASTMONEY_KLINE_FIELDS, EASTMONEY_QUOTE_FIELDS, + EASTMONEY_REQUEST_HEADERS, MagicConfig) @to_numeric -def get_realtime_quotes_by_fs(fs: str) -> pd.DataFrame: +def get_realtime_quotes_by_fs(fs: str, + **kwargs) -> pd.DataFrame: """ 获取沪深市场最新行情总体情况 @@ -28,7 +28,11 @@ def get_realtime_quotes_by_fs(fs: str) -> pd.DataFrame: """ - fields = ",".join(EASTMONEY_QUOTE_FIELDS.keys()) + columns = { + **EASTMONEY_QUOTE_FIELDS, + **kwargs.get(MagicConfig.EXTRA_FIELDS, {}) + } + fields = ",".join(columns.keys()) params = ( ('pn', '1'), ('pz', '1000000'), @@ -45,12 +49,17 @@ def get_realtime_quotes_by_fs(fs: str) -> pd.DataFrame: headers=EASTMONEY_REQUEST_HEADERS, params=params).json() df = pd.DataFrame(json_response['data']['diff']) - df = df.rename(columns=EASTMONEY_QUOTE_FIELDS) - df = df[EASTMONEY_QUOTE_FIELDS.values()] - df['行情ID'] = df['市场编号'].astype(str)+'.'+df['代码'].astype('str') + 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['更新时间戳'].apply(lambda x: str(datetime.fromtimestamp(x))) + df['最新交易日'] = pd.to_datetime(df['最新交易日'], format='%Y%m%d').astype(str) + tmp = df['最新交易日'] + del df['最新交易日'] + df['最新交易日'] = tmp + del df['更新时间戳'] return df @@ -69,7 +78,7 @@ def get_quote_history_single(code: str, fields = list(EASTMONEY_KLINE_FIELDS.keys()) columns = list(EASTMONEY_KLINE_FIELDS.values()) fields2 = ",".join(fields) - if kwargs.get('quote_id_mode'): + if kwargs.get(MagicConfig.QUOTE_ID_MODE): quote_id = code else: quote_id = get_quote_id(code) @@ -273,7 +282,6 @@ def get_today_bill(code: str) -> pd.DataFrame: DataFrame 单只股票、债券最新交易日的日内分钟级单子流入流出数据 - """ quote_id = get_quote_id(code) params = ( @@ -378,7 +386,8 @@ def get_deal_detail(quote_id: str, @to_numeric -def get_latest_quote(quote_id_list: Union[str, List[str]]) -> pd.DataFrame: +def get_latest_quote(quote_id_list: Union[str, List[str]], + **kwargs) -> pd.DataFrame: """ 获取股票、期货、债券的最新行情 @@ -404,7 +413,10 @@ def get_latest_quote(quote_id_list: Union[str, List[str]]) -> pd.DataFrame: quote_id_list = [quote_id_list] secids: List[str] = quote_id_list - columns = EASTMONEY_QUOTE_FIELDS + columns = { + **EASTMONEY_QUOTE_FIELDS, + **kwargs.get(MagicConfig.EXTRA_FIELDS, {}) + } fields = ",".join(columns.keys()) params = ( ('OSVersion', '14.3'), @@ -430,4 +442,10 @@ def get_latest_quote(quote_id_list: Union[str, List[str]]) -> pd.DataFrame: df['市场类型'] = df['市场编号'].apply(lambda x: MARKET_NUMBER_DICT.get(str(x))) df['行情ID'] = df['市场编号'].astype(str) + '.' + df['代码'].astype(str) del df['市场编号'] + df['更新时间'] = df['更新时间戳'].apply(lambda x: str(datetime.fromtimestamp(x))) + df['最新交易日'] = pd.to_datetime(df['最新交易日'], format='%Y%m%d').astype(str) + tmp = df['最新交易日'] + del df['最新交易日'] + df['最新交易日'] = tmp + del df['更新时间戳'] return df diff --git a/efinance/stock/getter.py b/efinance/stock/getter.py index 36aa367..b6dff36 100644 --- a/efinance/stock/getter.py +++ b/efinance/stock/getter.py @@ -257,7 +257,8 @@ def get_quote_history(stock_codes: Union[str, List[str]], @process_dataframe_and_series(remove_columns_and_indexes=['市场编号']) @to_numeric -def get_realtime_quotes(fs: Union[str, List[str]] = None) -> pd.DataFrame: +def get_realtime_quotes(fs: Union[str, List[str]] = None, + **kwargs) -> pd.DataFrame: """ 获取单个或者多个市场行情的最新状况 @@ -368,7 +369,7 @@ def get_realtime_quotes(fs: Union[str, List[str]] = None) -> pd.DataFrame: if not fs_list: fs_list.append(FS_DICT['stock']) fs_str = ','.join(fs_list) - df = get_realtime_quotes_by_fs(fs_str) + df = get_realtime_quotes_by_fs(fs_str, **kwargs) df.rename(columns={'代码': '股票代码', '名称': '股票名称' }, inplace=True) @@ -458,7 +459,8 @@ def get_today_bill(stock_code: str) -> pd.DataFrame: return df -def get_latest_quote(stock_codes: Union[str, List[str]]) -> pd.DataFrame: +def get_latest_quote(stock_codes: Union[str, List[str]], + **kwargs) -> pd.DataFrame: """ 获取沪深市场多只股票的实时涨幅情况 @@ -493,7 +495,7 @@ def get_latest_quote(stock_codes: Union[str, List[str]]) -> pd.DataFrame: stock_codes = [stock_codes] secids: List[str] = [get_quote_id(stock_code) for stock_code in stock_codes] - df = get_latest_quote_for_stock(secids) + df = get_latest_quote_for_stock(secids,**kwargs) return df