Skip to content

Commit

Permalink
Merge pull request #64 from 1nchaos/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
1nchaos authored Apr 24, 2024
2 parents cc2c7cd + d92a66e commit cf40200
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 11 deletions.
4 changes: 2 additions & 2 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ master

专注股票量化数据,为Ai(爱)发电,向阳而生。

2.0.0b0 (2024-04-17)
2.0.0 (2024-04-24)
------------------
1. 新增:基金ETF行情接口。

2. 新增:股票股东信息接口。

1.2.4 (2024-02-02)
------------------
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ print(res_df)
| 数据 | API | 说明 | 备注 |
| ---------------- | ------------------------------------- | -------------------------------------- | ------------------------------------------------------------ |
| A股代码 | stock.info.all_code() | 所有A股代码信息 | |
| 股本信息 | stock.info.get_stock_shares() | 获取单只股票的股本信息 | 来源:东方财富 |
| **概念** | | | |
| 来源:同花顺 | | | |
| 概念代码 | stock.info.all_concept_code_ths() | 所有A股概念代码信息(同花顺) | 来源:同花顺公开数据 |
Expand Down
2 changes: 1 addition & 1 deletion adata/__version__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

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


Expand Down
4 changes: 3 additions & 1 deletion adata/bond/market/bond_market.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
"""
@desc:
@desc:
http://www.iwencai.com/unifiedwap/result?typed=1&preParams=&ts=1&f=1&qs=1&selfsectsn=&querytype=&searchfilter=&tid=stockpick&w=127093
@author: 1nchaos
@time:2023/4/5
@log:
Expand Down
6 changes: 6 additions & 0 deletions adata/fund/market/etf_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ def get_market_etf_current(self, fund_code: str = '512880', k_type: int = 1):
成交额:元 16959251000.000 169.6亿
"""
return self.ths.get_market_etf_current_ths(fund_code, k_type)


if __name__ == '__main__':
print(ETFMarket().get_market_etf(fund_code='512880', start_date='2024-01-01'))
print(ETFMarket().get_market_etf_min(fund_code='512880'))
print(ETFMarket().get_market_etf_current(fund_code='512880'))
6 changes: 3 additions & 3 deletions adata/fund/market/etf_market_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class ETFMarketTemplate(object):
"""
etf 行情
"""
_MARKET_COLUMNS = ['index_code', 'trade_time', 'trade_date', 'open', 'high', 'low', 'close', 'volume', 'amount',
_MARKET_COLUMNS = ['fund_code', 'trade_time', 'trade_date', 'open', 'high', 'low', 'close', 'volume', 'amount',
'change', 'change_pct']
_MARKET_ETF_MIN_COLUMNS = ['index_code', 'trade_time', 'trade_date', 'price', 'avg_price', 'volume', 'amount',
_MARKET_ETF_MIN_COLUMNS = ['fund_code', 'trade_time', 'trade_date', 'price', 'avg_price', 'volume', 'amount',
'change', 'change_pct']
_MARKET_ETF_CURRENT_COLUMNS = ['index_code', 'trade_time', 'trade_date', 'open', 'high', 'low', 'price', 'volume',
_MARKET_ETF_CURRENT_COLUMNS = ['fund_code', 'trade_time', 'trade_date', 'open', 'high', 'low', 'price', 'volume',
'amount', 'change', 'change_pct']
6 changes: 3 additions & 3 deletions adata/fund/market/etf_market_ths.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_market_etf_ths(self, fund_code: str = '512880', k_type: int = 1, start_d
for d in data_list:
data.append(str(d).split(',')[0:7])
result_df = pd.DataFrame(data=data, columns=['trade_date', 'open', 'high', 'low', 'close', 'volume', 'amount'])
result_df['index_code'] = fund_code
result_df['fund_code'] = fund_code
result_df['trade_time'] = pd.to_datetime(result_df['trade_date']).dt.strftime('%Y-%m-%d %H:%M:%S')
result_df['trade_date'] = pd.to_datetime(result_df['trade_date'], format='%Y%m%d').dt.strftime('%Y-%m-%d')
result_df['close'] = result_df['close'].astype(float)
Expand Down Expand Up @@ -92,7 +92,7 @@ def get_market_etf_min_ths(self, fund_code='512880'):
data.append(str(d).split(','))
# 3. 封装数据
result_df = pd.DataFrame(data=data, columns=['trade_time', 'price', 'amount', 'avg_price', 'volume'])
result_df['index_code'] = fund_code
result_df['fund_code'] = fund_code
result_df['trade_time'] = trade_date + result_df['trade_time']
result_df['trade_date'] = pd.to_datetime(trade_date, format='%Y%m%d').strftime('%Y-%m-%d')
result_df['trade_time'] = pd.to_datetime(result_df['trade_time'], format='%Y%m%d%H%M').dt.strftime(
Expand Down Expand Up @@ -139,7 +139,7 @@ def get_market_etf_current_ths(self, fund_code: str = '512880', k_type: int = 1)
'%Y-%m-%d %H:%M:%S')
columns = ['trade_time', 'trade_date', 'open', 'high', 'low', 'price', 'volume', 'amount']
result_df = result_df[columns]
result_df['index_code'] = fund_code
result_df['fund_code'] = fund_code
result_df['trade_date'] = pd.to_datetime(result_df['trade_date'], format='%Y%m%d').dt.strftime('%Y-%m-%d')
result_df['change'] = None
result_df['change_pct'] = None
Expand Down
3 changes: 2 additions & 1 deletion adata/stock/info/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
from adata.stock.info.stock_code import StockCode
from adata.stock.info.concept.stock_concept import StockConcept
from adata.stock.info.stock_index import StockIndex
from adata.stock.info.stock_info import StockInfo
from adata.stock.info.trade_calendar import TradeCalendar


class Info(StockCode, StockConcept, TradeCalendar, StockIndex):
class Info(StockCode, StockConcept, TradeCalendar, StockIndex, StockInfo):

def __init__(self) -> None:
super().__init__()
Expand Down
49 changes: 49 additions & 0 deletions adata/stock/info/stock_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
"""
@desc:
@author: 1nchaos
@time:2024/4/23
@log:
"""
import pandas as pd

from adata.common import requests
from adata.common.utils.code_utils import compile_exchange_by_stock_code


class StockInfo(object):
__STOCK_SHARES_COLUMNS = ['stock_code', 'change_date', 'total_shares', 'limit_shares', 'list_a_shares',
'change_reason']

def get_stock_shares(self, stock_code: str = '000033', is_history=True):
"""
https://datacenter.eastmoney.com/securities/api/data/v1/get?reportName=RPT_F10_EH_EQUITY&columns=SECUCODE,SECURITY_CODE,END_DATE,TOTAL_SHARES,LIMITED_SHARES,LIMITED_OTHARS,LIMITED_DOMESTIC_NATURAL,LIMITED_STATE_LEGAL,LIMITED_OVERSEAS_NOSTATE,LIMITED_OVERSEAS_NATURAL,UNLIMITED_SHARES,LISTED_A_SHARES,B_FREE_SHARE,H_FREE_SHARE,FREE_SHARES,LIMITED_A_SHARES,NON_FREE_SHARES,LIMITED_B_SHARES,OTHER_FREE_SHARES,LIMITED_STATE_SHARES,LIMITED_DOMESTIC_NOSTATE,LOCK_SHARES,LIMITED_FOREIGN_SHARES,LIMITED_H_SHARES,SPONSOR_SHARES,STATE_SPONSOR_SHARES,SPONSOR_SOCIAL_SHARES,RAISE_SHARES,RAISE_STATE_SHARES,RAISE_DOMESTIC_SHARES,RAISE_OVERSEAS_SHARES,CHANGE_REASON&quoteColumns=&filter=(SECUCODE="688192.SH")&pageNumber=1&pageSize=20&sortTypes=-1&sortColumns=END_DATE&source=HSF10&client=PC&v=0656612716529632
:param is_history: 是否获取历史 默认是
:param stock_code: 股票代码
:return:
"""
stock_code = compile_exchange_by_stock_code(stock_code)
url = f"""https://datacenter.eastmoney.com/securities/api/data/v1/get?reportName=RPT_F10_EH_EQUITY&columns=SECUCODE,SECURITY_CODE,END_DATE,TOTAL_SHARES,LIMITED_SHARES,LIMITED_OTHARS,LIMITED_DOMESTIC_NATURAL,LIMITED_STATE_LEGAL,LIMITED_OVERSEAS_NOSTATE,LIMITED_OVERSEAS_NATURAL,UNLIMITED_SHARES,LISTED_A_SHARES,B_FREE_SHARE,H_FREE_SHARE,FREE_SHARES,LIMITED_A_SHARES,NON_FREE_SHARES,LIMITED_B_SHARES,OTHER_FREE_SHARES,LIMITED_STATE_SHARES,LIMITED_DOMESTIC_NOSTATE,LOCK_SHARES,LIMITED_FOREIGN_SHARES,LIMITED_H_SHARES,SPONSOR_SHARES,STATE_SPONSOR_SHARES,SPONSOR_SOCIAL_SHARES,RAISE_SHARES,RAISE_STATE_SHARES,RAISE_DOMESTIC_SHARES,RAISE_OVERSEAS_SHARES,CHANGE_REASON&quoteColumns=&filter=(SECUCODE="{stock_code}")&pageNumber=1&pageSize=20&sortTypes=-1&sortColumns=END_DATE&source=HSF10&client=PC&v=0656612716529632"""
res_json = requests.request('get', url, headers={}, proxies={}).json()
# 1. 返回结果判断
if not res_json['success']:
return pd.DataFrame(data=[], columns=self.__STOCK_SHARES_COLUMNS)

# 2. 正常返回数据结果封装
res_data = res_json['result']['data']
data = []
for _ in res_data:
data.append({'stock_code': _['SECURITY_CODE'], 'change_date': _['END_DATE'],
'total_shares': _['TOTAL_SHARES'],
'limit_shares': _['LIMITED_SHARES'],
'list_a_shares': int(_['LISTED_A_SHARES']) if _['LISTED_A_SHARES'] else _['LISTED_A_SHARES'],
'change_reason': _['CHANGE_REASON']})
result_df = pd.DataFrame(data=data, columns=self.__STOCK_SHARES_COLUMNS)
if not is_history:
result_df = result_df.iloc[0:1]
result_df['change_date'] = pd.to_datetime(result_df['change_date']).dt.strftime('%Y-%m-%d')
return result_df


if __name__ == '__main__':
print(StockInfo().get_stock_shares(stock_code='600001', is_history=True))
6 changes: 6 additions & 0 deletions tests/adata_test/stock/info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ def test_index_constituent(self):
print(df)
self.assertEqual(True, len(df) > 10)

def test_get_stock_shares_history(self):
print("开始测试:get_stock_shares_history")
df = adata.stock.info.get_stock_shares(stock_code="000033")
print(df)
self.assertEqual(True, len(df) > 2)


if __name__ == '__main__':
unittest.main()

0 comments on commit cf40200

Please sign in to comment.