Skip to content

Commit

Permalink
增强获取行情的函数
Browse files Browse the repository at this point in the history
  • Loading branch information
allen-li1231 authored and Micro-sheep committed Mar 4, 2024
1 parent abf4d83 commit 12e07a8
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 62 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.5.1(2024-02-22)

### Added

- 增加用于检索股票市场的参数

## v0.5.0(2023-01-08)

### Added
Expand Down
6 changes: 4 additions & 2 deletions efinance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
bond,
fund,
futures,
stock,
stock
)

__all__ = ['stock', 'fund', 'bond', 'futures']
from efinance import utils

__all__ = ['stock', 'fund', 'bond', 'futures', 'utils']
2 changes: 1 addition & 1 deletion efinance/__version__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__title__ = 'efinance'
__version__ = '0.5.0'
__version__ = '0.5.1'
__author__ = 'micro sheep'
__url__ = 'https://github.com/Micro-sheep/efinance'
__author_email__ = '[email protected]'
Expand Down
22 changes: 22 additions & 0 deletions efinance/common/config.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import enum
from pathlib import Path

HERE = Path(__file__).parent


class MarketType(enum.Enum):
A_stock = "AStock" # A股
A_stock_index = "Index" # A股指数
B_stock = "BStock" # B股
index = "Index" # 沪深京指数
STAR_market = "23" # 科创板
CSI_free_float = "24" # 中证系列指数
NEEQ = "NEEQ" # 京A,新三板(全国中小企业股份转让系统)
BK = "BK" # 板块
Hongkong = "HK" # 港股
US_stock = "UsStock" # 美股
London_stock = "LSE" # 英股
London_stock_IOB = "LSEIOB" # 伦敦交易所国际挂盘册
universal_index = "UniversalIndex" # 国外指数
SIX_Swiss = "SIX" # SIX瑞士股市

@classmethod
def has_value(cls, value):
return value in cls._value2member_map_

class MagicConfig:
EXTRA_FIELDS = 'extra_fields'
QUOTE_ID_MODE = 'quote_id_mode'
QUOTE_SYMBOL_MODE = 'quote_symbol_mode'
RETURN_DF = 'return_df'


Expand Down
125 changes: 86 additions & 39 deletions efinance/common/getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,21 @@
import pandas as pd
from jsonpath import jsonpath
from retry import retry
from tqdm import tqdm
from tqdm.auto import tqdm
import time

from ..common.config import MARKET_NUMBER_DICT
from ..common.config import MARKET_NUMBER_DICT, MarketType
from ..shared import BASE_INFO_CACHE, session, MAX_CONNECTIONS
from ..utils import get_quote_id, to_numeric
from .config import (
EASTMONEY_BASE_INFO_FIELDS,
EASTMONEY_HISTORY_BILL_FIELDS,
EASTMONEY_KLINE_FIELDS,
EASTMONEY_KLINE_NDAYS_FIELDS,
EASTMONEY_QUOTE_FIELDS,
EASTMONEY_REQUEST_HEADERS,
MagicConfig,
)

import warnings
warnings.filterwarnings("once")
from .config import (EASTMONEY_BASE_INFO_FIELDS, EASTMONEY_HISTORY_BILL_FIELDS,
EASTMONEY_KLINE_FIELDS, EASTMONEY_KLINE_NDAYS_FIELDS,
EASTMONEY_QUOTE_FIELDS, EASTMONEY_REQUEST_HEADERS,
MagicConfig)


@to_numeric
def get_realtime_quotes_by_fs(fs: str, **kwargs) -> pd.DataFrame:
def get_realtime_quotes_by_fs(fs: str,
**kwargs) -> pd.DataFrame:
"""
获取沪深市场最新行情总体情况
Expand All @@ -36,7 +30,10 @@ 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'),
Expand All @@ -47,12 +44,12 @@ def get_realtime_quotes_by_fs(fs: str, **kwargs) -> pd.DataFrame:
('invt', '2'),
('fid', 'f3'),
('fs', fs),
('fields', fields),
('fields', fields)
)
url = 'http://push2.eastmoney.com/api/qt/clist/get'
json_response = session.get(
url, headers=EASTMONEY_REQUEST_HEADERS, params=params
).json()
json_response = session.get(url,
headers=EASTMONEY_REQUEST_HEADERS,
params=params).json()
df = pd.DataFrame(json_response['data']['diff'])
df = df.rename(columns=columns)
df: pd.DataFrame = df[columns.values()]
Expand All @@ -74,7 +71,10 @@ def get_quote_history_single(
end: str = '20500101',
klt: int = 101,
fqt: int = 1,
**kwargs,
market_type: Union[MarketType, None] = None,
suppress_error: bool = False,
use_id_cache: bool = True,
**kwargs
) -> pd.DataFrame:
"""
获取单只股票、债券 K 线数据
Expand All @@ -87,7 +87,13 @@ def get_quote_history_single(
if kwargs.get(MagicConfig.QUOTE_ID_MODE):
quote_id = code
else:
quote_id = get_quote_id(code)
quote_id = get_quote_id(
stock_code=code,
market_type=market_type,
use_local=use_id_cache,
suppress_error=suppress_error,
**kwargs
)
params = (
('fields1', 'f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13'),
('fields2', fields2),
Expand Down Expand Up @@ -127,7 +133,10 @@ def get_quote_history_multi(
klt: int = 101,
fqt: int = 1,
tries: int = 3,
**kwargs,
market_type: Union[MarketType, None] = None,
suppress_error: bool = False,
use_id_cache: bool = True,
**kwargs
) -> Dict[str, pd.DataFrame]:
"""
获取多只股票、债券历史行情信息
Expand All @@ -141,8 +150,15 @@ def get_quote_history_multi(
@retry(tries=tries, delay=1)
def start(code: str):
_df = get_quote_history_single(
code, beg=beg, end=end, klt=klt, fqt=fqt, **kwargs
)
code,
beg=beg,
end=end,
klt=klt,
fqt=fqt,
market_type=market_type,
suppress_error=suppress_error,
use_id_cache=use_id_cache,
**kwargs)
dfs[code] = _df
pbar.update(1)
pbar.set_description_str(f'Processing => {code}')
Expand All @@ -152,20 +168,24 @@ def start(code: str):
if len(multitasking.get_active_tasks()) > MAX_CONNECTIONS:
time.sleep(3)
start(code)

multitasking.wait_for_tasks()
pbar.close()

if kwargs.get(MagicConfig.RETURN_DF):
return pd.concat(dfs, axis=0, ignore_index=True)
return dfs


def get_quote_history(
codes: Union[str, List[str]],
beg: str = '19000101',
end: str = '20500101',
klt: int = 101,
fqt: int = 1,
**kwargs,
def get_quote_history(codes: Union[str, List[str]],
beg: str = '19000101',
end: str = '20500101',
klt: int = 101,
fqt: int = 1,
market_type: Union[MarketType, None] = None,
suppress_error: bool = False,
use_id_cache: bool = True,
**kwargs
) -> Union[pd.DataFrame, Dict[str, pd.DataFrame]]:
"""
获取股票、ETF、债券的 K 线数据
Expand Down Expand Up @@ -197,6 +217,19 @@ def get_quote_history(
- ``1`` : 前复权
- ``2`` : 后复权
market_type : MarketType, optional
市场类型,目前可筛选A股,港股,美股和英股。默认不筛选,可选示例如下
- ``A_stock`` : A股
- ``Hongkong`` : 香港
- ``London_stock_exchange`` : 英股
- ``US_stock`` : 美股
suppress_error : bool, optional
遇到错误的股票代码,是否不报错,返回空的DataFrame
use_id_cache : bool, optional
是否使用本地缓存的东方财富股票行情ID
Returns
-------
Union[DataFrame, Dict[str, DataFrame]]
Expand All @@ -208,16 +241,30 @@ def get_quote_history(
"""

if isinstance(codes, str):
return get_quote_history_single(
codes, beg=beg, end=end, klt=klt, fqt=fqt, **kwargs
)
return get_quote_history_single(codes,
beg=beg,
end=end,
klt=klt,
fqt=fqt,
market_type=market_type,
suppress_error=suppress_error,
use_id_cache=use_id_cache,
**kwargs)

elif hasattr(codes, '__iter__'):
codes = list(codes)
return get_quote_history_multi(
codes, beg=beg, end=end, klt=klt, fqt=fqt, **kwargs
)
raise TypeError('代码数据类型输入不正确!')
return get_quote_history_multi(codes,
beg=beg,
end=end,
klt=klt,
fqt=fqt,
market_type=market_type,
suppress_error=suppress_error,
use_id_cache=use_id_cache,
**kwargs)
raise TypeError(
'代码数据类型输入不正确!'
)


@to_numeric
Expand Down
2 changes: 1 addition & 1 deletion efinance/fund/getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import rich
from jsonpath import jsonpath
from retry import retry
from tqdm import tqdm
from tqdm.auto import tqdm
import time

from ..utils import to_numeric
Expand Down
59 changes: 55 additions & 4 deletions efinance/stock/getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
import rich
from jsonpath import jsonpath
from retry import retry
from tqdm import tqdm
from tqdm.auto import tqdm

from ..common import get_base_info as get_base_info_for_stock
from ..common import get_deal_detail as get_deal_detail_for_stock
from ..common import get_history_bill as get_history_bill_for_stock
from ..common import get_quote_history as get_quote_history_for_stock
from ..common import get_realtime_quotes_by_fs
from ..common import get_today_bill as get_today_bill_for_stock
from ..common.config import EASTMONEY_REQUEST_HEADERS, FS_DICT, MagicConfig
from ..common.config import EASTMONEY_REQUEST_HEADERS, FS_DICT, MagicConfig, MarketType
from ..common.getter import get_latest_quote as get_latest_quote_for_stock
from ..shared import session
from ..utils import (
Expand All @@ -44,6 +44,7 @@
if python_version >= (3, 10):
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = 'ALL:@SECLEVEL=1'

requests.packages.urllib3.disable_warnings()

@to_numeric
def get_base_info_single(stock_code: str) -> pd.Series:
Expand Down Expand Up @@ -163,6 +164,9 @@ def get_quote_history(
end: str = '20500101',
klt: int = 101,
fqt: int = 1,
market_type: Union[MarketType, None] = None,
suppress_error: bool = False,
use_id_cache: bool = True,
**kwargs,
) -> Union[pd.DataFrame, Dict[str, pd.DataFrame]]:
"""
Expand Down Expand Up @@ -195,6 +199,19 @@ def get_quote_history(
- ``1`` : 前复权
- ``2`` : 后复权
market_type : MarketType, optional
市场类型,目前可筛选A股,港股,美股和英股。默认不筛选,可选示例如下
- ``A_stock`` : A股
- ``Hongkong`` : 香港
- ``London_stock_exchange`` : 英股
- ``US_stock`` : 美股
suppress_error : bool, optional
遇到未查到的股票代码,是否不报错,返回空的DataFrame
use_id_cache : bool, optional
是否使用本地缓存的东方财富股票行情ID
Returns
-------
Union[DataFrame, Dict[str, DataFrame]]
Expand Down Expand Up @@ -241,12 +258,46 @@ def get_quote_history(
4759 贵州茅台 600519 2021-07-28 1703.00 1768.90 1788.20 1682.12 85369 1.479247e+10 6.19 3.27 56.01 0.68
4760 贵州茅台 600519 2021-07-29 1810.01 1749.79 1823.00 1734.34 63864 1.129957e+10 5.01 -1.08 -19.11 0.51
>>> # 关键字查询'PG',默认优先返回拼音缩写匹配项
>>> ef.stock.get_quote_history('PG')
股票名称 股票代码 日期 开盘 收盘 最高 最低 成交量 成交额 振幅 涨跌幅 涨跌额 换手率
0 苹果 AAPL 1984-09-07 -7.91 -7.91 -7.90 -7.91 2981600 0.000000e+00 0.00 0.00 0.00 0.02
1 苹果 AAPL 1984-09-10 -7.91 -7.91 -7.90 -7.91 2346400 0.000000e+00 -0.13 0.00 0.00 0.02
2 苹果 AAPL 1984-09-11 -7.90 -7.90 -7.90 -7.90 5444000 0.000000e+00 0.00 0.13 0.01 0.04
3 苹果 AAPL 1984-09-12 -7.90 -7.91 -7.90 -7.91 4773600 0.000000e+00 -0.13 -0.13 -0.01 0.03
4 苹果 AAPL 1984-09-13 -7.90 -7.90 -7.90 -7.90 7429600 0.000000e+00 0.00 0.13 0.01 0.05
... ... ... ... ... ... ... ... ... ... ... ... ... ...
>>> # 关键字查询'PG',限定股市为A股
>>> from efinance.utils import MarketType
>>> ef.stock.get_quote_history('PG', market_type=MarketType.A_stock)
股票名称 股票代码 日期 开盘 收盘 最高 最低 成交量 成交额 振幅 涨跌幅 涨跌额 换手率
0 平高电气 600312 2001-02-21 1.20 1.30 1.32 1.06 391577 738479000.0 -650.00 3350.00 1.34 65.26
1 平高电气 600312 2001-02-22 1.24 1.27 1.35 1.23 104905 202688000.0 9.23 -2.31 -0.03 17.48
2 平高电气 600312 2001-02-23 1.28 1.25 1.30 1.16 56734 108086000.0 11.02 -1.57 -0.02 9.46
3 平高电气 600312 2001-02-26 1.26 1.33 1.40 1.23 69886 136875000.0 13.60 6.40 0.08 11.65
4 平高电气 600312 2001-02-27 1.32 1.32 1.35 1.27 27719 53969000.0 6.02 -0.75 -0.01 4.62
... ... ... ... ... ... ... ... ... ... ... ... ... ...
>>> # 精确匹配股票代码模式:`quote_symbol_mode`
>>> # 同时设置`use_id_cache=False`以防止直接使用过去相同关键字查询到的结果缓存
>>> ef.stock.get_quote_history('PG', quote_symbol_mode=True, use_id_cache=False)
股票名称 股票代码 日期 开盘 收盘 最高 最低 成交量 成交额 振幅 涨跌幅 涨跌额 换手率
0 宝洁 PG 1986-01-02 -51.15 -51.18 -51.15 -51.22 3772800 0.000000e+00 0.00 0.00 0.00 0.16
1 宝洁 PG 1986-01-03 -51.19 -51.15 -51.13 -51.19 3652800 0.000000e+00 -0.12 0.06 0.03 0.16
2 宝洁 PG 1986-01-06 -51.17 -51.18 -51.15 -51.19 1708800 0.000000e+00 -0.08 -0.06 -0.03 0.07
3 宝洁 PG 1986-01-07 -51.20 -51.20 -51.13 -51.20 7260800 0.000000e+00 -0.14 -0.04 -0.02 0.31
4 宝洁 PG 1986-01-08 -51.18 -51.35 -51.18 -51.39 5556800 0.000000e+00 -0.41 -0.29 -0.15 0.24
... ... ... ... ... ... ... ... ... ... ... ... ... ...
"""

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,
market_type=market_type, suppress_error=suppress_error, use_id_cache=use_id_cache,
**kwargs
)
if isinstance(df, pd.DataFrame):

df.rename(columns={'代码': '股票代码', '名称': '股票名称'}, inplace=True)
elif isinstance(df, dict):
for stock_code in df.keys():
Expand Down
Loading

0 comments on commit 12e07a8

Please sign in to comment.