Skip to content

Commit

Permalink
增强 common 模块中的配置以支持自定义获取更多属性
Browse files Browse the repository at this point in the history
  • Loading branch information
Micro-sheep committed Jun 19, 2022
1 parent e3ce73c commit a8bdaa2
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 28 deletions.
9 changes: 5 additions & 4 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Changelog

## [Unrelease]
## v0.4.6(2022-06-19)

### Changed

-`stock` 模块中抽离出获取证券最新行情的函数
- 增强 `common` 模块中的配置以支持自定义获取更多属性

## v0.4.5(2022-06-05)

Expand Down Expand Up @@ -38,7 +39,7 @@
### Changed

- 扩展 `get_quote_history` 函数,支持通过设定 `kwargs` 来指定返回类型

### Added

-`stock` 模块添加函数 `get_quote_snapshot` 以支持获取沪深市场 3 秒行情快照
Expand All @@ -50,10 +51,10 @@
### Fiexd

- 修复 fund 模块的命名错误以及其他 bug

### Added

- 添加对获取北证A股市场行情的支持
- 添加对获取北证 A 股市场行情的支持

---

Expand Down
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.4.5'
__version__ = '0.4.6'
__author__ = 'micro sheep'
__url__ = 'https://github.com/Micro-sheep/efinance'
__author_email__ = '[email protected]'
Expand Down
4 changes: 2 additions & 2 deletions efinance/bond/getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
获取沪深市场全部债券实时行情信息
Expand All @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions efinance/common/config.py
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -108,7 +114,9 @@
'f18': '昨日收盘',
'f20': '总市值',
'f21': '流通市值',
'f13': '市场编号'
'f13': '市场编号',
'f124': '更新时间戳',
'f297': '最新交易日',
}

# 股票、债券历史大单数据表头
Expand Down Expand Up @@ -149,4 +157,4 @@
'f105': '净利润',
'f186': '毛利率'

}
}
48 changes: 33 additions & 15 deletions efinance/common/getter.py
Original file line number Diff line number Diff line change
@@ -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:
"""
获取沪深市场最新行情总体情况
Expand All @@ -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'),
Expand All @@ -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


Expand All @@ -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)
Expand Down Expand Up @@ -273,7 +282,6 @@ def get_today_bill(code: str) -> pd.DataFrame:
DataFrame
单只股票、债券最新交易日的日内分钟级单子流入流出数据
"""
quote_id = get_quote_id(code)
params = (
Expand Down Expand Up @@ -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:
"""
获取股票、期货、债券的最新行情
Expand All @@ -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'),
Expand All @@ -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
10 changes: 6 additions & 4 deletions efinance/stock/getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
获取单个或者多个市场行情的最新状况
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
"""
获取沪深市场多只股票的实时涨幅情况
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit a8bdaa2

Please sign in to comment.