Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #31

Merged
merged 4 commits into from
Aug 17, 2023
Merged

Dev #31

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ print(res_df)
- proxy_url: 获取代理Ip的链接;ip和proxy_url方式选择其一;
- 每次请求获取一次,为节省ip资源建议使用自建的代理池。

## 二、数据列表
## 二、[数据列表](https://adata.1nchaos.com/dic/dataList.html)

整理了最新版本的数据列表和相关使用Api,详细内容和相关使用参数,请参考数据字典文档。

Expand Down Expand Up @@ -172,10 +172,10 @@ print(res_df)

### (2)基金-ETF

| 数据 | API | 说明 | 备注 |
| -------------- | ---------------------------------------- | ------------------------------ | --------------- |
| ETF(场内) | fund.info.all_etf_exchange_traded_info() | 获取所有A股市场的ETF信息 | 来源:1. 同花顺 |
| 其它数据排期中 | TODO | 若您有相关资源可以一起参与贡献 | |
| 数据 | API | 说明 | 备注 |
| -------------- | ---------------------------------------- | ------------------------------ | ------------------------------------------------------------ |
| ETF(场内) | fund.info.all_etf_exchange_traded_info() | 获取所有A股市场的ETF信息 | 来源:1. [东方财富](http://quote.eastmoney.com/center/gridlist.html#fund_etf) |
| 其它数据排期中 | TODO | 若您有相关资源可以一起参与贡献 | |

### (3)债券-Bond

Expand All @@ -196,7 +196,7 @@ print(res_df)
| | sentiment.north.north_flow() | 获取北向资金历史流入行情 | |
| 其它数据排期中 | TODO | 若您有相关资源可以一起参与贡献 | |

## 三、数据源
## 三、[数据源](https://adata.1nchaos.com/dataSource.html)

| 数据源 | 板块 | 描述 |
| ---------- | ------------------------------------------------------------ | ---------------- |
Expand Down
7 changes: 7 additions & 0 deletions adata/bond/cache/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
"""
@desc: readme
@author: 1nchaos
@time: 2023/6/2
@log: change log
"""
13 changes: 13 additions & 0 deletions adata/common/base/base_req.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
"""
@desc: readme
@author: 1nchaos
@time: 2023/8/14
@log: change log
"""


class BaseReq(object):

def __init__(self) -> None:
super().__init__()
3 changes: 2 additions & 1 deletion adata/common/base/base_ths.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def _get_text(self, api_url, code):

def _get_years_by_start_date(self, start_date):
"""
根据开始时间获取年份
根据开始时间获取大于开始时间的所有年份的列表
例:start_date=2020-10-01 -> years=[2020,2021,2022,2023]
:param start_date: 开始时间
:return: 年份
"""
Expand Down
19 changes: 19 additions & 0 deletions adata/common/exception/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
"""
@desc: readme
@author: 1nchaos
@time: 2023/8/14
@log: change log
"""

import pandas as pd


def handler_null(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception:
return pd.DataFrame(data=[], columns=[])

return wrapper
2 changes: 1 addition & 1 deletion adata/common/headers/east_headers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""
@desc: readme
@author: yinchao
@author: 1nchaos
@time: 2023/7/24
@log: change log
"""
Expand Down
7 changes: 7 additions & 0 deletions adata/sentiment/cache/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
"""
@desc: readme
@author: 1nchaos
@time: 2023/6/2
@log: change log
"""
1 change: 0 additions & 1 deletion adata/sentiment/securities_margin.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def __securities_margin_east(self, start_date=None):
:param start_date: 开始时间
:return:
"""

# 1. url拼接页码等参数
data = []
total_pages = 1
Expand Down
39 changes: 36 additions & 3 deletions adata/stock/info/stock_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,30 @@
import pandas as pd
import requests

from adata.common.exception.handler import handler_null
from adata.common.headers import baidu_headers


class StockCode(object):
"""
股票代码
"""
__CODE_COLUMNS = ['stock_code', 'short_name', 'exchange']

def __init__(self) -> None:
super().__init__()

def all_code(self):
"""
获取所有股票的代码 ,后续补充多数据源
获取所有股票的代码
:return: 所有股票的代码信息: ['stock_code','short_name','exchange']
"""
return self.__market_rank_baidu()
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)
res_df = res_df.drop_duplicates(subset=['stock_code'], keep='first')
return res_df.sort_values('stock_code').reset_index(drop=True)

def __market_rank_baidu(self):
"""
Expand Down Expand Up @@ -63,7 +70,33 @@ def __market_rank_baidu(self):
continue
# 4. 封装数据
rename = {'name': 'short_name', 'code': 'stock_code'}
return pd.DataFrame(data=data)[['code', 'name', 'exchange']].rename(columns=rename)
return pd.DataFrame(data=data)[['code', 'name', 'exchange']].rename(columns=rename)[self.__CODE_COLUMNS]

@handler_null
def __new_sub_east(self):
"""
东方财富新股申购列表
https://data.eastmoney.com/xg/xg/default.html
https://datacenter-web.eastmoney.com/api/data/v1/get?sortColumns=APPLY_DATE,SECURITY_CODE&sortTypes=-1,-1&pageSize=50&pageNumber=1&reportName=RPTA_APP_IPOAPPLY&columns=SECURITY_CODE,SECURITY_NAME&quoteType=0&filter=(APPLY_DATE>'2010-01-01')&source=WEB&client=WEB
"""
url = f"https://datacenter-web.eastmoney.com/api/data/v1/get?" \
f"sortColumns=APPLY_DATE,SECURITY_CODE&sortTypes=-1,-1&pageSize=50&pageNumber=1&" \
f"reportName=RPTA_APP_IPOAPPLY&columns=SECURITY_CODE,SECURITY_NAME,TRADE_MARKET&quoteType=0&" \
f"filter=(APPLY_DATE>'2010-01-01')&source=WEB&client=WEB"
res_json = requests.request('get', url, headers={}, proxies={}).json()
res_data = res_json['result']['data']
data = []
for _ in res_data:
exchange = str(_['TRADE_MARKET'])
if exchange.startswith('上海'):
exchange = 'SH'
elif exchange.startswith('深圳'):
exchange = 'SZ'
elif exchange.startswith('北京'):
exchange = 'BJ'
data.append({'stock_code': _['SECURITY_CODE'], 'short_name': _['SECURITY_NAME'], 'exchange': exchange})
result_df = pd.DataFrame(data=data, columns=self.__CODE_COLUMNS)
return result_df


if __name__ == '__main__':
Expand Down
4 changes: 4 additions & 0 deletions adata/stock/market/index_market/market_index_east.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import pandas as pd

from adata.common import requests
from adata.common.exception.handler import handler_null
from adata.stock.market.index_market.market_index_template import StockMarketIndexTemplate


class StockMarketIndexEast(StockMarketIndexTemplate):
def __init__(self) -> None:
super().__init__()

@handler_null
def get_market_index(self, index_code: str = '000001', start_date='2020-01-01', k_type: int = 1):
"""
获取指数行情
Expand Down Expand Up @@ -51,6 +53,7 @@ def get_market_index(self, index_code: str = '000001', start_date='2020-01-01',
result_df = result_df[result_df['trade_date'] >= start_date]
return result_df[self._MARKET_INDEX_COLUMNS]

@handler_null
def get_market_index_min(self, index_code='000001'):
"""
获取指数当日的分时行情
Expand Down Expand Up @@ -87,6 +90,7 @@ def get_market_index_min(self, index_code='000001'):
result_df = result_df.round(2)
return result_df

@handler_null
def get_market_index_current(self, index_code: str = '000001'):
"""
获取当前的指数行情
Expand Down
2 changes: 1 addition & 1 deletion tests/adata_test/stock/dividend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_get_dividend(self):
print("开始测试:test_get_dividend")
df = adata.stock.market.get_dividend(stock_code='000001')
print(df)
self.assertEqual(True, len(df) > 5)
self.assertEqual(True, len(df) > 20)


if __name__ == '__main__':
Expand Down
18 changes: 15 additions & 3 deletions tests/adata_test/stock/info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,21 @@ def test_all_concept_code_ths(self):

def test_concept_constituent_ths(self):
print("开始测试:test_concept_constituent_ths")
df = adata.stock.info.concept_constituent_ths(index_code="885556", wait_time=1000)
df = adata.stock.info.concept_constituent_ths(index_code="885556")
print(df)
self.assertEqual(True, len(df) > 10)
self.assertEqual(True, len(df) > 200)

def test_all_concept_code_east(self):
print("开始测试:test_all_concept_code_ths")
df = adata.stock.info.all_concept_code_east()
print(df)
self.assertEqual(True, len(df) > 300)

def test_concept_constituent_east(self):
print("开始测试:test_concept_constituent_ths")
df = adata.stock.info.concept_constituent_east(concept_code="BK1104")
print(df)
self.assertEqual(True, len(df) > 200)

def test_all_index_code(self):
print("开始测试:test_all_index_code")
Expand All @@ -52,7 +64,7 @@ def test_all_index_code(self):

def test_index_constituent(self):
print("开始测试:test_index_constituent")
df = adata.stock.info.index_constituent(index_code="000033", wait_time=1000)
df = adata.stock.info.index_constituent(index_code="000033")
print(df)
self.assertEqual(True, len(df) > 10)

Expand Down
4 changes: 0 additions & 4 deletions tests/adata_test/stock/market_concept_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
@time: 2023/7/2
@log: change log
"""
import os
import time
import unittest

import requests

import adata


Expand Down
23 changes: 0 additions & 23 deletions tests/other/stock_test.py

This file was deleted.