Skip to content

Commit

Permalink
Date (#4479)
Browse files Browse the repository at this point in the history
* add version 1.12.47

* fix get_receipt

* fix stock_zh_ah_daily

* update date

* fix get_receipt

* add version 1.12.47
  • Loading branch information
albertandking authored Jan 30, 2024
1 parent 5c9d509 commit ae3c937
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 75 deletions.
3 changes: 2 additions & 1 deletion akshare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2659,9 +2659,10 @@
1.12.44 fix: fix stock_zh_ah_daily interface
1.12.45 fix: fix stock_individual_spot_xq interface
1.12.46 fix: fix futures_contract_detail interface
1.12.47 fix: fix stock_zh_ah_daily interface
"""

__version__ = "1.12.46"
__version__ = "1.12.47"
__author__ = "AKFamily"

import sys
Expand Down
48 changes: 26 additions & 22 deletions akshare/futures/receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,11 @@ def get_gfex_receipt(date: str = None, vars_list: List = cons.contract_symbols)
temp_df = pd.DataFrame(data_json['data'])
temp_df = temp_df[temp_df['variety'].str.contains("小计")]
result_df = temp_df[['wbillQty', 'diff']].copy()
if result_df.empty:
return pd.DataFrame()

result_df.loc[:, 'date'] = date.isoformat().replace("-", "")
result_df.loc[:, 'var'] = [item.upper() for item in temp_df['varietyOrder'].tolist()]

result_df.reset_index(drop=True, inplace=True)
result_df.rename(columns={
"wbillQty": "receipt",
Expand All @@ -397,76 +399,78 @@ def get_gfex_receipt(date: str = None, vars_list: List = cons.contract_symbols)
'var', 'receipt', 'receipt_chg', 'date'
]]
result_df.set_index(['var'], inplace=True)
if 'LC' not in result_df.index:
vars_list.remove('LC')
result_df = result_df.loc[vars_list, :]
result_df.reset_index(inplace=True)
return result_df


def get_receipt(start_day: str = None, end_day: str = None, vars_list: List = cons.contract_symbols):
def get_receipt(start_date: str = None, end_date: str = None, vars_list: List = cons.contract_symbols):
"""
大宗商品-注册仓单数据
:param start_day: 开始日期 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date 对象 为空时为当天
:type start_day: str
:param end_day: 结束数据 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date 对象 为空时为当天
:type end_day: str
:param start_date: 开始日期 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date 对象 为空时为当天
:type start_date: str
:param end_date: 结束数据 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date 对象 为空时为当天
:type end_date: str
:param vars_list: 合约品种如 RB、AL 等列表为空时为所有商品
:type vars_list: str
:return: 展期收益率数据
:rtype: pandas.DataFrame
"""
if not isinstance(vars_list, list):
return warnings.warn(f"vars_list: 必须是列表")
start_day = cons.convert_date(start_day) if start_day is not None else datetime.date.today()
end_day = cons.convert_date(end_day) if end_day is not None else cons.convert_date(
start_date = cons.convert_date(start_date) if start_date is not None else datetime.date.today()
end_date = cons.convert_date(end_date) if end_date is not None else cons.convert_date(
cons.get_latest_data_date(datetime.datetime.now()))
records = pd.DataFrame()
while start_day <= end_day:
if start_day.strftime('%Y%m%d') not in calendar:
warnings.warn(f"{start_day.strftime('%Y%m%d')} 非交易日")
while start_date <= end_date:
if start_date.strftime('%Y%m%d') not in calendar:
warnings.warn(f"{start_date.strftime('%Y%m%d')} 非交易日")
else:
print(start_day)
print(start_date)
for market, market_vars in cons.market_exchange_symbols.items():
if market == 'dce':
if start_day >= datetime.date(2009, 4, 7):
if start_date >= datetime.date(2009, 4, 7):
f = get_dce_receipt
else:
print('20090407 起,大连商品交易所每个交易日更新仓单数据')
f = None
elif market == 'shfe':
if datetime.date(2008, 10, 6) <= start_day <= datetime.date(2014, 5, 16):
if datetime.date(2008, 10, 6) <= start_date <= datetime.date(2014, 5, 16):
f = get_shfe_receipt_1
elif start_day > datetime.date(2014, 5, 16):
elif start_date > datetime.date(2014, 5, 16):
f = get_shfe_receipt_2
else:
f = None
print('20081006 起,上海期货交易所每个交易日更新仓单数据')
elif market == "gfex":
if start_day > datetime.date(2022, 12, 22):
if start_date > datetime.date(2022, 12, 22):
f = get_gfex_receipt
else:
f = None
print('20081006 起,上海期货交易所每个交易日更新仓单数据')
elif market == 'czce':
if datetime.date(2008, 3, 3) <= start_day <= datetime.date(2010, 8, 24):
if datetime.date(2008, 3, 3) <= start_date <= datetime.date(2010, 8, 24):
f = get_czce_receipt_1
elif datetime.date(2010, 8, 24) < start_day <= datetime.date(2015, 11, 11):
elif datetime.date(2010, 8, 24) < start_date <= datetime.date(2015, 11, 11):
f = get_czce_receipt_2
elif start_day > datetime.date(2015, 11, 11):
elif start_date > datetime.date(2015, 11, 11):
f = get_czce_receipt_3
else:
f = None
print('20080303 起,郑州商品交易所每个交易日更新仓单数据')
get_vars = [var for var in vars_list if var in market_vars]
if market != 'cffex' and get_vars != []:
if f is not None:
records = pd.concat([records, f(start_day, get_vars)])
start_day += datetime.timedelta(days=1)
records = pd.concat([records, f(start_date, get_vars)])
start_date += datetime.timedelta(days=1)
records.reset_index(drop=True, inplace=True)
if records.empty:
return records
return records


if __name__ == '__main__':
get_receipt_df = get_receipt(start_day='20240122', end_day='20240122', vars_list=['LC'])
get_receipt_df = get_receipt(start_date='20230601', end_date='20230615')
print(get_receipt_df)
37 changes: 19 additions & 18 deletions akshare/stock/stock_zh_ah_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
"""
import random

import requests
import pandas as pd
from akshare.utils import demjson
from akshare.utils.tqdm import get_tqdm
import requests

from akshare.stock.cons import (
hk_url,
Expand All @@ -19,6 +17,8 @@
hk_stock_headers,
hk_stock_payload,
)
from akshare.utils import demjson
from akshare.utils.tqdm import get_tqdm


def _get_zh_stock_ah_page_count() -> int:
Expand Down Expand Up @@ -52,10 +52,10 @@ def stock_zh_ah_spot() -> pd.DataFrame:
hk_payload.update({"reqPage": i})
r = requests.get(hk_url, params=hk_payload, headers=hk_headers)
data_json = demjson.decode(
r.text[r.text.find("{") : r.text.rfind("}") + 1]
r.text[r.text.find("{"): r.text.rfind("}") + 1]
)
big_df = pd.concat(
[
objs=[
big_df,
pd.DataFrame(data_json["data"]["page_data"])
.iloc[:, 0]
Expand Down Expand Up @@ -110,11 +110,12 @@ def stock_zh_ah_spot() -> pd.DataFrame:
return big_df


def stock_zh_ah_name() -> dict:
def stock_zh_ah_name() -> pd.DataFrame:
"""
腾讯财经-港股-AH-股票名称
https://stockapp.finance.qq.com/mstats/#mod=list&id=hk_ah&module=HK&type=AH
:return: 股票代码和股票名称的字典
:rtype: dict
:rtype: pandas.DataFrame
"""
big_df = pd.DataFrame()
page_count = _get_zh_stock_ah_page_count() + 1
Expand All @@ -123,10 +124,10 @@ def stock_zh_ah_name() -> dict:
hk_payload.update({"reqPage": i})
r = requests.get(hk_url, params=hk_payload, headers=hk_headers)
data_json = demjson.decode(
r.text[r.text.find("{") : r.text.rfind("}") + 1]
r.text[r.text.find("{"): r.text.rfind("}") + 1]
)
big_df = pd.concat(
[
objs=[
big_df,
pd.DataFrame(data_json["data"]["page_data"])
.iloc[:, 0]
Expand Down Expand Up @@ -159,10 +160,10 @@ def stock_zh_ah_name() -> dict:


def stock_zh_ah_daily(
symbol: str = "02318",
start_year: str = "2000",
end_year: str = "2019",
adjust: str = "",
symbol: str = "02318",
start_year: str = "2000",
end_year: str = "2019",
adjust: str = "",
) -> pd.DataFrame:
"""
腾讯财经-港股-AH-股票历史行情
Expand Down Expand Up @@ -210,18 +211,18 @@ def stock_zh_ah_daily(
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36",
}
r = requests.get(
"http://web.ifzq.gtimg.cn/appstock/app/kline/kline",
url="http://web.ifzq.gtimg.cn/appstock/app/kline/kline",
params=hk_stock_payload_copy,
headers=headers,
)
else:
r = requests.get(
"https://web.ifzq.gtimg.cn/appstock/app/hkfqkline/get",
url="https://web.ifzq.gtimg.cn/appstock/app/hkfqkline/get",
params=hk_stock_payload_copy,
headers=hk_stock_headers,
)
data_json = demjson.decode(
r.text[r.text.find("{") : r.text.rfind("}") + 1]
r.text[r.text.find("{"): r.text.rfind("}") + 1]
)
try:
if adjust == "":
Expand Down Expand Up @@ -251,7 +252,7 @@ def stock_zh_ah_daily(
except:
temp_df.columns = ["日期", "开盘", "收盘", "最高", "最低", "成交量"]
temp_df = temp_df[["日期", "开盘", "收盘", "最高", "最低", "成交量"]]
big_df = pd.concat([big_df, temp_df], ignore_index=True)
big_df = pd.concat(objs=[big_df, temp_df], ignore_index=True)
big_df["日期"] = pd.to_datetime(big_df["日期"], errors="coerce").dt.date
big_df["开盘"] = pd.to_numeric(big_df["开盘"], errors="coerce")
big_df["收盘"] = pd.to_numeric(big_df["收盘"], errors="coerce")
Expand All @@ -269,6 +270,6 @@ def stock_zh_ah_daily(
print(stock_zh_ah_name_df)

stock_zh_ah_daily_df = stock_zh_ah_daily(
symbol="00241", start_year="2022", end_year="2024", adjust="qfq"
symbol="02318", start_year="2022", end_year="2024", adjust=""
)
print(stock_zh_ah_daily_df)
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@

## 更新说明详情

1.12.47 fix: fix stock_zh_ah_daily interface

1. 修复 stock_zh_ah_daily 接口
2. 修复 get_receipt 接口

1.12.46 fix: fix futures_contract_detail interface

1. 修复 futures_contract_detail 接口
Expand Down Expand Up @@ -3481,6 +3486,8 @@

## 版本更新说明

1.12.47 fix: fix stock_zh_ah_daily interface

1.12.46 fix: fix futures_contract_detail interface

1.12.45 fix: fix stock_individual_spot_xq interface
Expand Down
2 changes: 1 addition & 1 deletion docs/data/futures/futures.md
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ ak.get_roll_yield(date="20180718", var="IF", symbol1="IF1812", symbol2="IF1811")
```python
import akshare as ak

ak.get_receipt(start_day="20180712", end_day="20180719", vars_list=["CU", "NI"])
ak.get_receipt(start_date="20180712", end_date="20180719", vars_list=["CU", "NI"])
```

注意:
Expand Down
67 changes: 35 additions & 32 deletions docs/data/stock/stock.md
Original file line number Diff line number Diff line change
Expand Up @@ -3001,11 +3001,11 @@ print(stock_zh_ah_spot_df)

接口: stock_zh_ah_daily

目标地址: http://gu.qq.com/hk02359/gp(示例)
目标地址: https://gu.qq.com/hk02359/gp

描述: A+H 股数据是从腾讯财经获取的数据, 历史数据按日频率更新
描述: 腾讯财经-A+H 股数据

限量: 单次返回所有 A+H 上市公司的历史行情数据
限量: 单次返回指定参数的 A+H 上市公司的历史行情数据

输入参数

Expand All @@ -3032,32 +3032,33 @@ print(stock_zh_ah_spot_df)
```python
import akshare as ak

stock_zh_ah_daily_df = ak.stock_zh_ah_daily(symbol="02318", start_year="2000", end_year="2019", adjust="")
stock_zh_ah_daily_df = ak.stock_zh_ah_daily(symbol="02318", start_year="2022", end_year="2024", adjust="")
print(stock_zh_ah_daily_df)
```

数据示例

```
日期 开盘 收盘 最高 最低 成交量
0 2018-12-13 67.000 68.000 68.700 64.950 20213983.000
1 2018-12-14 68.000 68.000 68.100 67.000 2579900.000
2 2018-12-17 68.000 68.000 68.500 67.600 1131467.000
3 2018-12-18 68.000 67.900 68.000 67.250 961400.000
4 2018-12-19 67.850 67.500 68.000 66.400 2965300.000
.. ... ... ... ... ... ...
225 2019-10-30 121.690 119.100 122.460 118.260 621131.000
226 2019-10-31 119.660 133.240 136.880 119.660 4659080.000
227 2019-11-01 132.470 131.560 134.640 129.180 1424212.000
228 2019-11-04 135.690 136.460 137.860 132.820 1298760.000
229 2019-11-05 136.530 134.010 138.210 132.470 868180.000
日期 开盘 收盘 最高 最低 成交量
0 2022-01-03 56.80 55.75 56.95 55.60 7231921.0
1 2022-01-04 55.95 56.50 56.95 55.75 17975862.0
2 2022-01-05 57.50 56.80 58.00 56.60 28768656.0
3 2022-01-06 56.90 58.00 58.00 56.90 15464523.0
4 2022-01-07 58.00 61.10 61.10 57.55 46904651.0
.. ... ... ... ... ... ...
753 2024-01-23 30.00 31.30 31.85 29.60 69397766.0
754 2024-01-24 31.75 33.00 33.35 31.10 79928025.0
755 2024-01-25 33.50 34.60 34.75 32.85 86827346.0
756 2024-01-26 34.15 33.75 34.70 33.35 56539336.0
757 2024-01-29 34.05 33.85 34.90 33.75 41424070.0
[758 rows x 6 columns]
```

#### A+H股票字典

接口: stock_zh_ah_name

目标地址: http://stockapp.finance.qq.com/mstats/#mod=list&id=hk_ah&module=HK&type=AH
目标地址: https://stockapp.finance.qq.com/mstats/#mod=list&id=hk_ah&module=HK&type=AH

描述: A+H 股数据是从腾讯财经获取的数据, 历史数据按日频率更新

Expand All @@ -3081,24 +3082,26 @@ print(stock_zh_ah_daily_df)
```python
import akshare as ak

stock_zh_ah_name_dict = ak.stock_zh_ah_name()
print(stock_zh_ah_name_dict)
stock_zh_ah_name_df = ak.stock_zh_ah_name()
print(stock_zh_ah_name_df)
```

数据示例

```
0 01211 比亚迪股份
1 01880 中国中免
2 06821 凯莱英
3 06160 百济神州
4 09696 天齐锂业
.. ... ...
145 01053 重庆钢铁股份
146 02880 辽港股份
147 01033 中石化油服
148 00042 东北电气
149 06116 拉夏贝尔
代码 名称
0 01211 比亚迪股份
1 06160 百济神州
2 01880 中国中免
3 00941 中国移动
4 06821 凯莱英
.. ... ...
144 01053 重庆钢铁股份
145 00588 北京北辰实业股份
146 02009 金隅集团
147 02880 辽港股份
148 01033 中石化油服
[149 rows x 2 columns]
```

### 美股
Expand All @@ -3107,7 +3110,7 @@ print(stock_zh_ah_name_dict)

接口: stock_us_spot_em

目标地址: http://quote.eastmoney.com/center/gridlist.html#us_stocks
目标地址: https://quote.eastmoney.com/center/gridlist.html#us_stocks

描述: 美股实时行情数据, 从东方财富获取; 注意数据有 15 分钟延迟

Expand Down Expand Up @@ -3170,7 +3173,7 @@ print(stock_us_spot_em_df)

接口: stock_us_spot

目标地址: http://finance.sina.com.cn/stock/usstock/sector.shtml
目标地址: https://finance.sina.com.cn/stock/usstock/sector.shtml

描述: 新浪财经-美股; 获取的数据有 15 分钟延迟; 建议使用 ak.stock_us_spot_em() 来获取数据

Expand Down
Loading

0 comments on commit ae3c937

Please sign in to comment.