Skip to content

Commit

Permalink
fix: fix stock_board_industry_cons_ths
Browse files Browse the repository at this point in the history
  • Loading branch information
albertandking committed Jun 14, 2024
1 parent d32f130 commit 9de289d
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 27 deletions.
12 changes: 9 additions & 3 deletions akshare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2814,9 +2814,10 @@
1.14.1 fix: fix energy_carbon_hb interface
1.14.2 fix: fix fund_portfolio_hold_em interface
1.14.3 fix: fix stock_industry_clf_hist_sw interface
1.14.4 add: add stock_margin_account_info interface
"""

__version__ = "1.14.3"
__version__ = "1.14.4"
__author__ = "AKFamily"

import sys
Expand All @@ -2838,6 +2839,11 @@

del sys

"""
东方财富网-数据中心-融资融券-融资融券账户统计-两融账户信息
"""
from akshare.stock_feature.stock_margin_em import stock_margin_account_info

"""
现货走势
"""
Expand Down Expand Up @@ -3719,7 +3725,7 @@
"""
融资融券-深圳
"""
from akshare.stock_feature.stock_szse_margin import (
from akshare.stock_feature.stock_margin_szse import (
stock_margin_underlying_info_szse,
stock_margin_detail_szse,
stock_margin_szse,
Expand Down Expand Up @@ -4054,7 +4060,7 @@
"""
融资融券数据
"""
from akshare.stock_feature.stock_sse_margin import (
from akshare.stock_feature.stock_margin_sse import (
stock_margin_detail_sse,
stock_margin_sse,
stock_margin_ratio_pa,
Expand Down
7 changes: 5 additions & 2 deletions akshare/bond/bond_china_money.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/6/4 18:30
Date: 2024/6/14 18:20
Desc: 收盘收益率曲线历史数据
https://www.chinamoney.com.cn/chinese/bkcurvclosedyhis/?bondType=CYCC000&reference=1
"""
Expand Down Expand Up @@ -30,7 +30,10 @@ def __bond_register_service() -> requests.Session:
)
cookies_dict = session.cookies.get_dict()
cookies_str = "; ".join(f"{k}={v}" for k, v in cookies_dict.items())
data = {"key": "WjlTdFhsNU5UNVo="}
# 此处需要通过未访问的游览器,首次打开
# https://www.chinamoney.com.cn/chinese/bkcurvclosedyhis/?bondType=CYCC000&reference=1
# 页面进行人工获取
data = {"key": "WjlTdFhsNU5UOVo="}
headers = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Encoding": "gzip, deflate, br",
Expand Down
99 changes: 99 additions & 0 deletions akshare/stock_feature/stock_margin_em.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/6/14 17:00
Desc: 东方财富网-数据中心-融资融券-融资融券账户统计-两融账户信息
https://www.szse.cn/disclosure/margin/object/index.html
"""

import pandas as pd
import requests

from akshare.utils.tqdm import get_tqdm


def stock_margin_account_info() -> pd.DataFrame:
"""
东方财富网-数据中心-融资融券-融资融券账户统计-两融账户信息
https://data.eastmoney.com/rzrq/zhtjday.html
:return: 融资融券账户统计
:rtype: pandas.DataFrame
"""
url = "https://datacenter-web.eastmoney.com/api/data/v1/get"
params = {
"reportName": "RPTA_WEB_MARGIN_DAILYTRADE",
"columns": "ALL",
"pageNumber": "1",
"pageSize": "500",
"sortColumns": "STATISTICS_DATE",
"sortTypes": "-1",
"p": "1",
"pageNo": "1",
"pageNum": "1",
"_": "1718357149317",
}
r = requests.get(url=url, params=params)
data_json = r.json()
total_page = data_json["result"]["pages"]
tqdm = get_tqdm()
big_df = pd.DataFrame()
for page in tqdm(range(1, total_page + 1), leave=False):
params.update(
{
"pageNumber": page,
"p": page,
"pageNo": page,
"pageNum": page,
}
)
r = requests.get(url=url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["result"]["data"])
big_df = pd.concat(objs=[big_df, temp_df], ignore_index=True)

big_df.rename(
columns={
"STATISTICS_DATE": "日期",
"FIN_BALANCE": "融资余额",
"LOAN_BALANCE": "融券余额",
"FIN_BUY_AMT": "融资买入额",
"LOAN_SELL_AMT": "融券卖出额",
"SECURITY_ORG_NUM": "证券公司数量",
"OPERATEDEPT_NUM": "营业部数量",
"PERSONAL_INVESTOR_NUM": "个人投资者数量",
"ORG_INVESTOR_NUM": "机构投资者数量",
"INVESTOR_NUM": "参与交易的投资者数量",
"MARGINLIAB_INVESTOR_NUM": "有融资融券负债的投资者数量",
"TOTAL_GUARANTEE": "担保物总价值",
"AVG_GUARANTEE_RATIO": "平均维持担保比例",
},
inplace=True,
)
big_df = big_df[
[
"日期",
"融资余额",
"融券余额",
"融资买入额",
"融券卖出额",
"证券公司数量",
"营业部数量",
"个人投资者数量",
"机构投资者数量",
"参与交易的投资者数量",
"有融资融券负债的投资者数量",
"担保物总价值",
"平均维持担保比例",
]
]

big_df["日期"] = pd.to_datetime(big_df["日期"], errors="coerce").dt.date
for item in big_df.columns[1:]:
big_df[item] = pd.to_numeric(big_df[item], errors="coerce")
big_df.sort_values(["日期"], ignore_index=True, inplace=True)
return big_df


if __name__ == "__main__":
stock_margin_account_info_df = stock_margin_account_info()
print(stock_margin_account_info_df)
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2023/9/24 15:30
Date: 2024/6/14 17:20
Desc: 上海证券交易所-融资融券数据
http://www.sse.com.cn/market/othersdata/margin/sum/
https://www.sse.com.cn/market/othersdata/margin/sum/
"""

import pandas as pd
import requests

Expand Down Expand Up @@ -41,16 +42,19 @@ def stock_margin_ratio_pa(date: str = "20231013") -> pd.DataFrame:
"secuCode": "证券代码",
"secuName": "证券简称",
"slMarginRatio": "融券比例",
}, inplace=True
},
inplace=True,
)
temp_df = temp_df[[
temp_df = temp_df[
[
"证券代码",
"证券简称",
"融资比例",
"融券比例",
]]
temp_df['融资比例'] = pd.to_numeric(temp_df['融资比例'], errors="coerce")
temp_df['融券比例'] = pd.to_numeric(temp_df['融券比例'], errors="coerce")
]
]
temp_df["融资比例"] = pd.to_numeric(temp_df["融资比例"], errors="coerce")
temp_df["融券比例"] = pd.to_numeric(temp_df["融券比例"], errors="coerce")
return temp_df


Expand All @@ -59,15 +63,15 @@ def stock_margin_sse(
) -> pd.DataFrame:
"""
上海证券交易所-融资融券数据-融资融券汇总
http://www.sse.com.cn/market/othersdata/margin/sum/
https://www.sse.com.cn/market/othersdata/margin/sum/
:param start_date: 交易开始日期
:type start_date: str
:param end_date: 交易结束日期
:type end_date: str
:return: 融资融券汇总
:rtype: pandas.DataFrame
"""
url = "http://query.sse.com.cn/marketdata/tradedata/queryMargin.do"
url = "https://query.sse.com.cn/marketdata/tradedata/queryMargin.do"
params = {
"isPagination": "true",
"beginDate": start_date,
Expand All @@ -82,8 +86,9 @@ def stock_margin_sse(
"_": "1612773448860",
}
headers = {
"Referer": "http://www.sse.com.cn/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36",
"Referer": "https://www.sse.com.cn/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/88.0.4324.150 Safari/537.36",
}
r = requests.get(url, params=params, headers=headers)
data_json = r.json()
Expand Down Expand Up @@ -126,13 +131,13 @@ def stock_margin_sse(
def stock_margin_detail_sse(date: str = "20230922") -> pd.DataFrame:
"""
上海证券交易所-融资融券数据-融资融券明细
http://www.sse.com.cn/market/othersdata/margin/detail/
https://www.sse.com.cn/market/othersdata/margin/detail/
:param date: 交易日期
:type date: str
:return: 融资融券明细
:rtype: pandas.DataFrame
"""
url = "http://query.sse.com.cn/marketdata/tradedata/queryMargin.do"
url = "https://query.sse.com.cn/marketdata/tradedata/queryMargin.do"
params = {
"isPagination": "true",
"tabType": "mxtype",
Expand All @@ -149,8 +154,9 @@ def stock_margin_detail_sse(date: str = "20230922") -> pd.DataFrame:
"_": "1612773448860",
}
headers = {
"Referer": "http://www.sse.com.cn/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36",
"Referer": "https://www.sse.com.cn/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/88.0.4324.150 Safari/537.36",
}
r = requests.get(url, params=params, headers=headers)
data_json = r.json()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/4/13 18:00
Date: 2024/6/14 17:00
Desc: 深圳证券交易所-融资融券数据
https://www.szse.cn/disclosure/margin/object/index.html
"""
Expand Down Expand Up @@ -76,17 +76,17 @@ def stock_margin_szse(date: str = "20240411") -> pd.DataFrame:
"融资融券余额",
]
temp_df["融资买入额"] = temp_df["融资买入额"].str.replace(",", "")
temp_df["融资买入额"] = pd.to_numeric(temp_df["融资买入额"])
temp_df["融资买入额"] = pd.to_numeric(temp_df["融资买入额"], errors="coerce")
temp_df["融资余额"] = temp_df["融资余额"].str.replace(",", "")
temp_df["融资余额"] = pd.to_numeric(temp_df["融资余额"])
temp_df["融资余额"] = pd.to_numeric(temp_df["融资余额"], errors="coerce")
temp_df["融券卖出量"] = temp_df["融券卖出量"].str.replace(",", "")
temp_df["融券卖出量"] = pd.to_numeric(temp_df["融券卖出量"])
temp_df["融券卖出量"] = pd.to_numeric(temp_df["融券卖出量"], errors="coerce")
temp_df["融券余量"] = temp_df["融券余量"].str.replace(",", "")
temp_df["融券余量"] = pd.to_numeric(temp_df["融券余量"])
temp_df["融券余量"] = pd.to_numeric(temp_df["融券余量"], errors="coerce")
temp_df["融券余额"] = temp_df["融券余额"].str.replace(",", "")
temp_df["融券余额"] = pd.to_numeric(temp_df["融券余额"])
temp_df["融券余额"] = pd.to_numeric(temp_df["融券余额"], errors="coerce")
temp_df["融资融券余额"] = temp_df["融资融券余额"].str.replace(",", "")
temp_df["融资融券余额"] = pd.to_numeric(temp_df["融资融券余额"])
temp_df["融资融券余额"] = pd.to_numeric(temp_df["融资融券余额"], errors="coerce")
return temp_df


Expand Down
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@

## 更新说明详情

1.14.4 add: add stock_margin_account_info interface

1. 新增 stock_margin_account_info 接口
2. 修复 bond_china_close_return 接口

1.14.3 fix: fix stock_industry_clf_hist_sw interface

1. 修复 stock_industry_clf_hist_sw 接口
Expand Down Expand Up @@ -4219,6 +4224,8 @@

## 版本更新说明

1.14.4 add: add stock_margin_account_info interface

1.14.3 fix: fix stock_industry_clf_hist_sw interface

1.14.2 fix: fix fund_portfolio_hold_em interface
Expand Down
61 changes: 61 additions & 0 deletions docs/data/stock/stock.md
Original file line number Diff line number Diff line change
Expand Up @@ -18310,6 +18310,67 @@ print(stock_margin_ratio_pa_df)
[1807 rows x 4 columns]
```

#### 标的证券名单及保证金比例查询

接口: stock_margin_account_info

目标地址: https://data.eastmoney.com/rzrq/zhtjday.html

描述: 东方财富网-数据中心-融资融券-融资融券账户统计-两融账户信息

限量: 单次返回所有历史数据

输入参数

| 名称 | 类型 | 描述 |
|----|----|----|
| - | - | - |

输出参数

| 名称 | 类型 | 描述 |
|---------------|---------|----------|
| 日期 | object | - |
| 融资余额 | float64 | 注意单位: 亿 |
| 融券余额 | float64 | 注意单位: 亿 |
| 融资买入额 | float64 | 注意单位: 亿 |
| 融券卖出额 | float64 | 注意单位: 亿 |
| 证券公司数量 | float64 | 注意单位: 家 |
| 营业部数量 | float64 | 注意单位: 家 |
| 个人投资者数量 | float64 | 注意单位: 万名 |
| 机构投资者数量 | float64 | 注意单位: 家 |
| 参与交易的投资者数量 | float64 | 注意单位: 名 |
| 有融资融券负债的投资者数量 | float64 | 注意单位: 名 |
| 担保物总价值 | float64 | 注意单位: 亿 |
| 平均维持担保比例 | float64 | 注意单位: % |

接口示例

```python
import akshare as ak

stock_margin_account_info_df = ak.stock_margin_account_info()
print(stock_margin_account_info_df)
```

数据示例

```
日期 融资余额 融券余额 ... 有融资融券负债的投资者数量 担保物总价值 平均维持担保比例
0 2012-09-27 704.53 15.31 ... NaN NaN NaN
1 2012-09-28 685.47 15.20 ... NaN NaN NaN
2 2012-10-08 696.69 15.64 ... NaN NaN NaN
3 2012-10-09 699.63 17.59 ... NaN NaN NaN
4 2012-10-10 708.57 17.63 ... NaN NaN NaN
... ... ... ... ... ... ... ...
2836 2024-06-06 14761.62 367.59 ... 1545084.0 44142.04 247.5
2837 2024-06-07 14697.46 341.56 ... 1538584.0 44096.42 248.3
2838 2024-06-11 14744.14 336.41 ... 1545026.0 44228.19 249.0
2839 2024-06-12 14761.55 338.57 ... 1547653.0 44506.55 250.1
2840 2024-06-13 14769.50 338.84 ... 1548858.0 44328.60 249.3
[2841 rows x 13 columns]
```

#### 上海证券交易所

##### 融资融券汇总
Expand Down
2 changes: 2 additions & 0 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,8 @@
"macro_info_ws" # 华尔街见闻-日历-宏观
# 现货走势
"spot_price_qh" # 现货走势
# 东方财富网-数据中心-融资融券-融资融券账户统计-两融账户信息
"stock_margin_account_info" # 东方财富网-数据中心-融资融券-融资融券账户统计-两融账户信息
```

## 案例演示
Expand Down

0 comments on commit 9de289d

Please sign in to comment.