Skip to content

Commit

Permalink
Merge pull request #89 from 1nchaos/dev
Browse files Browse the repository at this point in the history
fixed
  • Loading branch information
1nchaos authored Jul 24, 2024
2 parents 6d33da9 + d62a780 commit 64d4fb4
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 12 deletions.
2 changes: 1 addition & 1 deletion adata/__version__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

VERSION = (2, 5, 0)
VERSION = (2, 5, 1)
PRERELEASE = None # alpha, beta or rc
REVISION = None

Expand Down
13 changes: 13 additions & 0 deletions adata/common/utils/code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@ def compile_exchange_by_stock_code(stock_code):
if prefix in exchange_suffix:
return stock_code + exchange_suffix[prefix]
return stock_code


def get_exchange_by_stock_code(stock_code):
"""根据股票代码补全市场后缀"""
exchange_suffix = {
'0': 'SZ',
'3': 'SZ',
'6': 'SH',
'9': 'SH',
'4': 'BJ',
'8': 'BJ'
}
return exchange_suffix[stock_code[0]]
35 changes: 34 additions & 1 deletion adata/stock/info/stock_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from adata.common.exception.handler import handler_null
from adata.common.headers import baidu_headers
from adata.common.utils.code_utils import get_exchange_by_stock_code
from adata.stock.cache import get_code_csv_path


Expand All @@ -42,7 +43,10 @@ def all_code(self):
"""
# 拼接股票上市日期
code = pd.read_csv(get_code_csv_path())[['stock_code', 'list_date2']]
res_df = self.__market_rank_baidu()
# 请求数据:优先东方财富,其次百度
res_df = self.__market_rank_east()
if res_df.empty:
res_df = self.__market_rank_baidu()
east = self.__new_sub_east()
if not east.empty:
res_df = pd.concat([east, res_df], axis=0, ignore_index=True)
Expand Down Expand Up @@ -128,6 +132,35 @@ def __new_sub_east(self):
result_df['list_date'] = pd.to_datetime(result_df['list_date']).dt.date
return result_df

@handler_null
def __market_rank_east(self):
"""
东方财富新股申购列表
https://quote.eastmoney.com/center/gridlist.html
"""
url = "https://82.push2.eastmoney.com/api/qt/clist/get"
params = {
"pn": "1", "pz": "50000",
"po": "1", "np": "1",
"ut": "bd1d9ddb04089700cf9c27f6f7426281",
"fltt": "2", "invt": "2", "fid": "f3",
"fs": "m:0 t:6,m:0 t:80,m:1 t:2,m:1 t:23,m:0 t:81 s:2048",
"fields": "f12,f14",
"_": "1623833739532",
}
# 请求数据
r = requests.get(url, timeout=15, params=params)
data_json = r.json()
if not data_json["data"]["diff"]:
return pd.DataFrame()
df = pd.DataFrame(data=data_json["data"]["diff"])
df.columns = ['stock_code', 'short_name']
# 数据etl
df['exchange'] = df['stock_code'].apply(lambda x: get_exchange_by_stock_code(x))
df['list_date'] = np.nan
df.reset_index(inplace=True)
return df[self.__CODE_COLUMNS]


if __name__ == '__main__':
print(StockCode().all_code())
6 changes: 3 additions & 3 deletions adata/stock/market/index_market/market_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def get_market_index(self, index_code: str = '000001', start_date='2020-01-01',
"""
获取指数行情
"""
res_df = self.baidu_index.get_market_index(index_code=index_code, start_date=start_date, k_type=k_type)
# res_df = self.baidu_index.get_market_index(index_code=index_code, start_date=start_date, k_type=k_type)
# if res_df.empty:
res_df = self.east_index.get_market_index(index_code=index_code, start_date=start_date, k_type=k_type)
if res_df.empty:
res_df = self.ths_index.get_market_index(index_code=index_code, start_date=start_date, k_type=k_type)
if res_df.empty:
res_df = self.east_index.get_market_index(index_code=index_code, start_date=start_date, k_type=k_type)
return res_df

def get_market_index_min(self, index_code='000001'):
Expand Down
11 changes: 6 additions & 5 deletions adata/stock/market/index_market/market_index_east.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ def get_market_index(self, index_code: str = '000001', start_date='2020-01-01',
:param k_type: k线类型:1.日;2.周;3.月 默认:1 日k
:return: k线行情数据 [日期,开,高,低,收,成交量,成交额]
"""
sec_id = {'1': '1', '3': '0', '4': '0', '9': '0'}[index_code[0]]
url = f"https://push2his.eastmoney.com/api/qt/stock/kline/get?" \
f"secid=0.{index_code}&fields1=f1,f2,f3,f4,f5,f6&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&" \
f"secid={sec_id}.{index_code}&fields1=f1,f2,f3,f4,f5,f6&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&" \
f"klt=10{k_type}&fqt=1&end=20500101&lmt=1000000"
res_json = requests.request('get', url, headers={}, proxies={}).json()
# 解析数据
code = res_json['data']['code']
if code != index_code:
return
return pd.DataFrame(data=[], columns=[])
res_data = res_json['data']['klines']
data = []
for _ in res_data:
Expand Down Expand Up @@ -128,6 +129,6 @@ def get_market_index_current(self, index_code: str = '000001'):


if __name__ == '__main__':
print(StockMarketIndexEast().get_market_index(index_code='000001', start_date='2022-12-01'))
print(StockMarketIndexEast().get_market_index_min(index_code='000001'))
print(StockMarketIndexEast().get_market_index_current(index_code='000001'))
print(StockMarketIndexEast().get_market_index(index_code='980072', start_date='2022-12-01'))
# print(StockMarketIndexEast().get_market_index_min(index_code='000001'))
# print(StockMarketIndexEast().get_market_index_current(index_code='000001'))
3 changes: 2 additions & 1 deletion adata/stock/market/index_market/market_index_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class StockMarketIndexTemplate(object):
"""
_MARKET_INDEX_BASE_COLUMNS = ['trade_time', 'open', 'high', 'low', 'close', 'volume', 'amount', 'change',
'change_pct']
_MARKET_INDEX_COLUMNS = ['index_code', 'trade_date'].extend(_MARKET_INDEX_BASE_COLUMNS)
_MARKET_INDEX_COLUMNS = ['index_code', 'trade_date', 'trade_time', 'open', 'high', 'low', 'close', 'volume',
'amount', 'change', 'change_pct']
_MARKET_INDEX_MIN_COLUMNS = ['index_code', 'trade_time', 'trade_date', 'price', 'avg_price', 'volume', 'amount',
'change', 'change_pct']
_MARKET_INDEX_CURRENT_COLUMNS = ['index_code', 'trade_time', 'trade_date', 'open', 'high', 'low', 'price',
Expand Down
4 changes: 3 additions & 1 deletion adata/stock/market/stock_market/stock_market_east.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def get_market(self, stock_code: str = '000001', start_date='1990-01-01', end_da
'turnover_ratio', 'pre_close']
df[numeric_columns] = df[numeric_columns].apply(pd.to_numeric)
df.reset_index(inplace=True, drop=True)
return df
return df[
['trade_time', "trade_date", "open", "close", "high", "low", "volume", "amount", "change_pct", "change",
"turnover_ratio", "pre_close"]]

def get_market_min(self, stock_code: str = '000001'):
"""
Expand Down

0 comments on commit 64d4fb4

Please sign in to comment.