Skip to content

Commit

Permalink
Merge pull request #87 from 1nchaos/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
1nchaos authored Jul 23, 2024
2 parents 70bc3a0 + 6cf023f commit 6d33da9
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 9 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ master

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

2.5.0 (2024-07-23)
------------------
1. 新增:股票:单只股票的申万一二级行业信息。
2. 新增:股票:行情接口增加5min,15min,60min,复权类型参数。
3. 修复:股票:名称有空格的问题。

2.4.0 (2024-07-15)
------------------
1. 新增:股票:单只股票龙虎榜信息接口。
Expand Down
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, 4, 1)
VERSION = (2, 5, 0)
PRERELEASE = None # alpha, beta or rc
REVISION = None

Expand Down
3 changes: 2 additions & 1 deletion adata/common/headers/baidu_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
@time: 2023/3/29
@log: change log
"""
cookie = 'BAIDUID=FB17C036F6F79E647798CCDBF10D0A3A:FG=1; BIDUPSID=72A958B07427F9F9CB3F63FD8B6C6565; PSTM=1721131010; BA_HECTOR=21252ka18l04ah0g0k8k84a4brl9d31j9co021u; ZFY=RDRljN0vytzynS1BEIuC:A79HiL:AOmQZYkiXI4VbDvTo:C; ab_sr=1.0.1_ZDg2NTdmYTk5MjE3MDhiMzNhMDRiZDQyNzJhOWQwZTcwMDkzMjU2YzFiOTNiNjZlODU1ODcwYTMzMjRlYjVkODlhY2UwZWFkYmM1MmM1MGUxMDVmYWM4YWMyYTdmOWFjN2JiZWQyMjUzMDVkMmNhNzg1ZjIzNjk1ZDlmZjQyZTdmNjkwNjFmMDk0YjEzY2Y1NTkxNzI2ZWQ2NGY3MzEzNw=='
# cookie = 'BAIDUID=61D17458C811DF0B06467F8750EE21C2:FG=1; ab_sr=1.0.1_NDc4ZjNhNWJlNDk2N2M2MjI2ZGM2MzEwNzZiMGZiYTMyMGMyMzgxNWUxYTY1MjBhZDU5ODJhMWRjYmMxOTY5NWQ5MGI0M2MzMGMwYzQyMmUwZjcxNGQyNGZjZTZjNGJjN2NiODM3N2NlMjQ3YWNkNGJjNWVlNzI4YmI2OGI3NTMyMGE5NmQ0ZDZlZjZiODM1ZTIxNDAzMDYzMjIyZTE3ZQ=='
cookie = 'BAIDUID=883657749CC8B8D669D673902D41EBC5:FG=1;ab_sr=1.0.1_ZDA1MjU4MjQ2MWYwNDQ1M2M5OWNiYWI5YTI1YzhlMzNlYjk5YTlmNTk1YzY2N2NmOTBkZjkwNjE1Yjg5YzUwNmJhZDQ4NmE4ZTMzZDFhZTBjODhjM2Q4MmYzMjg1Y2MzMzgzOWQ2NDI0NDk1YWFlZWEzZmNmZTk1ZTgzMTMzZTNmYjRhYWZjZWJkZDJjOTBmYzFlMGMxNDJkNzZjYWQ4Yg==; Path=/; Domain=baidu.com; Max-Age=7200; HttpOnly; Secure; SameSite=None'

json_headers = {
'Host': 'finance.pae.baidu.com',
Expand Down
30 changes: 30 additions & 0 deletions adata/common/utils/date_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
"""
@desc: readme
@author: 1nchaos
@time: 2024/7/23
@log: change log
"""
from datetime import datetime, timedelta


def get_n_days_date(days=0, fmt="%Y-%m-%d"):
"""
获取 N 天后的日期,
:param days: 天数;N可以是负数,表示N天前的日期
:param fmt: 日期格式;默认:%Y-%m-%d
:return: 对应的日期
"""
# 获取当前日期
current_date = datetime.now().date()
# 计算前N天的日期
target_date = current_date + timedelta(days=days)
# 将日期格式化为指定格式
return target_date.strftime(fmt)


def get_cur_time(fmt="%Y-%m-%d %H:%M:%S"):
"""
获取当前时间,
"""
return datetime.now().strftime(fmt)
1 change: 1 addition & 0 deletions adata/stock/info/stock_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def all_code(self):
df['list_date'] = df['list_date'].fillna(df['list_date2'])
df['list_date'] = pd.to_datetime(df['list_date'], errors='coerce').dt.date
df['list_date'] = df['list_date'].where(df['list_date'].notnull(), np.nan)
df['short_name'] = df['short_name'].str.replace(' ', '')
return df.sort_values('stock_code').reset_index(drop=True)[self.__CODE_COLUMNS]

def __market_rank_baidu(self):
Expand Down
2 changes: 2 additions & 0 deletions adata/stock/market/index_market/market_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ 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)
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
16 changes: 12 additions & 4 deletions adata/stock/market/stock_market/stock_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pandas as pd

from adata.stock.market.stock_market.stock_market_baidu import StockMarketBaiDu
from adata.stock.market.stock_market.stock_market_east import StockMarketEast
from adata.stock.market.stock_market.stock_market_qq import StockMarketQQ
from adata.stock.market.stock_market.stock_market_sina import StockMarketSina

Expand All @@ -24,17 +25,24 @@ def __init__(self) -> None:
self.sina_market = StockMarketSina()
self.qq_market = StockMarketQQ()
self.baidu_market = StockMarketBaiDu()
self.east_market = StockMarketEast()

def get_market(self, stock_code: str = '000001', start_date='1990-01-01', k_type=1, adjust_type: int = 1):
def get_market(self, stock_code: str = '000001', start_date='1990-01-01', end_date=None, k_type=1,
adjust_type: int = 1):
"""
获取单个股票的行情
:param stock_code: 股票代码
:param start_date: 开始时间
:param k_type: k线类型:1.日;2.周;3.月 默认:1 日k
:param end_date: 结束日期
:param k_type: k线类型:1.日;2.周;3.月,4季度,5.5min,15.15min,30.30min,60.60min 默认:1 日k
:param adjust_type: k线复权类型:0.不复权;1.前复权;2.后复权 默认:1 前复权 (目前:只有前复权,作为股票交易已经可用)
:return: k线行情数据
"""
return self.baidu_market.get_market(stock_code=stock_code, start_date=start_date, k_type=k_type)
df = self.east_market.get_market(stock_code=stock_code, start_date=start_date, end_date=end_date,
k_type=k_type, adjust_type=adjust_type)
# if df.empty:
# df = self.baidu_market.get_market(stock_code=stock_code, start_date=start_date, k_type=k_type)
return df

def get_market_min(self, stock_code: str = '000001'):
"""
Expand Down Expand Up @@ -88,7 +96,7 @@ def get_market_bar(self, stock_code: str = '000001'):


if __name__ == '__main__':
print(StockMarket().get_market(stock_code='300416', start_date='2021-01-01', k_type=1))
print(StockMarket().get_market(stock_code='002230', start_date='2024-07-22', k_type=1))
print(StockMarket().get_market_min(stock_code='000001'))
print(StockMarket().list_market_current(code_list=['000001', '600001', '000795', '872925']))
print(StockMarket().get_market_five(stock_code='000001'))
Expand Down
6 changes: 4 additions & 2 deletions adata/stock/market/stock_market/stock_market_baidu.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class StockMarketBaiDu(StockMarketTemplate):
def __init__(self) -> None:
super().__init__()

def get_market(self, stock_code: str = '000001', start_date='1990-01-01', k_type=1, adjust_type: int = 1):
def get_market(self, stock_code: str = '000001', start_date='1990-01-01', end_date=None, k_type=1,
adjust_type: int = 1):
"""
获取百度的股票行情数据
web: https://gushitong.baidu.com/stock/ab-002926
Expand All @@ -40,10 +41,11 @@ def get_market(self, stock_code: str = '000001', start_date='1990-01-01', k_type
all=1&code=601318&isIndex=false&isBk=false&isBlock=false&isFutures=false&isStock=true&newFormat=1&
group=quotation_minute_ab&finClientType=pc
"ma5均价","ma5成交量","ma10均价","ma10成交量","ma20均价","ma20成交量"
:param end_date: 结束日期
:param stock_code: 6位股票代码
:param start_date: 开始时间
:param k_type: k线类型:1.日;2.周;3.月
# :param adjust_type: k线复权类型:0.不复权;1.前复权;2.后复权 默认:1 前复权 TODO
:param adjust_type: k线复权类型:0.不复权;1.前复权;2.后复权 默认:1 前复权 TODO
:return: k线行情数据:"时间戳", "时间","开盘","收盘","成交量","最高","最低","成交额","涨跌额","涨跌幅","换手率","昨收"
"""
# 1. 请求接口 url
Expand Down
89 changes: 89 additions & 0 deletions adata/stock/market/stock_market/stock_market_east.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# -*- coding: utf-8 -*-
"""
@desc: 东方财富
https://quote.eastmoney.com/center/
@author: 1nchaos
@time: 2023/06/19
@log: change log
"""

import pandas as pd

from adata.common.utils import requests
from adata.common.utils.date_utils import get_cur_time
from adata.stock.market.stock_market.stock_market_template import StockMarketTemplate


class StockMarketEast(StockMarketTemplate):
"""
百度股票行情
"""

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

def get_market(self, stock_code: str = '000001', start_date='1990-01-01', end_date=None, k_type=1,
adjust_type: int = 1):
"""
:param stock_code: 6位股票代码
:param start_date: 开始时间
:param end_date: 结束日期
:param k_type: k线类型:1.日;2.周;3.月
:param adjust_type: k线复权类型:0.不复权;1.前复权;2.后复权 默认:1 前复权
:return: k线行情数据:"时间戳", "时间","开盘","收盘","成交量","最高","最低","成交额","涨跌额","涨跌幅","换手率","昨收"
"""
# 1. 参数处理
se_cid = 1 if stock_code.startswith('6') else 0
start_date = start_date.replace('-', '') if start_date else '19900101'
end_date = end_date.replace('-', '') if end_date else get_cur_time("%Y%m%d")
k_type = f"10{k_type}" if k_type < 5 else k_type
params = {"fields1": "f1,f2,f3,f4,f5,f6",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61,f116",
"ut": "7eea3edcaed734bea9cbfc24409ed989",
"klt": k_type, "fqt": adjust_type,
"secid": f"{se_cid}.{stock_code}",
"beg": start_date, "end": end_date,
"_": "1623766962675",
}
# 2. 请求url
url = "http://push2his.eastmoney.com/api/qt/stock/kline/get"
r = requests.request(method='get', url=url, params=params)
data_json = r.json()

# 3. 结果处理
if not data_json["data"]:
return pd.DataFrame()
lines = data_json["data"]["klines"]
if not lines:
return pd.DataFrame()
data = [item.split(",") for item in lines]
df = pd.DataFrame(data=data, columns=["trade_date", "open", "close", "high", "low", "volume", "amount",
'', "change_pct", "change", "turnover_ratio"])
# 4.清洗数据
df['pre_close'] = df['close'].astype(float) - df['change'].astype(float)
df['trade_time'] = pd.to_datetime(df['trade_date']).dt.strftime('%Y-%m-%d %H:%M:%S')
df['trade_date'] = pd.to_datetime(df['trade_date']).dt.strftime('%Y-%m-%d')
df['stock_code'] = stock_code
numeric_columns = ['open', 'close', 'volume', 'high', 'low', 'amount', 'change', 'change_pct',
'turnover_ratio', 'pre_close']
df[numeric_columns] = df[numeric_columns].apply(pd.to_numeric)
df.reset_index(inplace=True, drop=True)
return df

def get_market_min(self, stock_code: str = '000001'):
"""
获取百度的股票行情数据
web: https://gushitong.baidu.com/stock/ab-002926
url: https://finance.pae.baidu.com/selfselect/getstockquotation?
all=1&code=601318&isIndex=false&isBk=false&isBlock=false&isFutures=false&isStock=true&newFormat=1
&group=quotation_minute_ab&finClientType=pc
time, price, ratio, increase, volume, avgPrice, amount, timeKey, datetime, oriAmount
:param stock_code: 6位股票代码
:return: k线行情数据:"时间","价格","涨跌率","涨幅","均价","成交量", "成交额"
"""
pass


if __name__ == '__main__':
print(StockMarketEast().get_market(stock_code='600020', k_type=1, adjust_type=1))
4 changes: 3 additions & 1 deletion adata/stock/market/stock_market/stock_market_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ class StockMarketTemplate(object):
'b1', 'bv1', 'b2', 'bv2', 'b3', 'bv3', 'b4', 'bv4', 'b5', 'bv5']
_MARKET_BAR_COLUMNS = ['stock_code', 'trade_time', 'price', 'volume', 'bs_type']

def get_market(self, stock_code: str = '000001', start_date='1990-01-01', k_type=1, adjust_type: int = 1):
def get_market(self, stock_code: str = '000001', start_date='1990-01-01', end_date=None, k_type=1,
adjust_type: int = 1):
"""
获取单个股票的行情
:param stock_code: 股票代码
:param start_date: 开始时间
:param end_date: 结束日期
:param k_type: k线类型:1.日;2.周;3.月 默认:1 日k
:param adjust_type: k线复权类型:0.不复权;1.前复权;2.后复权 默认:1 前复权 (目前:只有前复权,作为股票交易已经可用)
:return: k线行情数据
Expand Down
30 changes: 30 additions & 0 deletions tests/other/cookie_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
"""
@desc: readme
@author: yinchao
@time: 2024/7/23
@log: change log
"""
import requests

# cookie = 'BAIDUID=61D17458C811DF0B06467F8750EE21C2:FG=1; ab_sr=1.0.1_NDc4ZjNhNWJlNDk2N2M2MjI2ZGM2MzEwNzZiMGZiYTMyMGMyMzgxNWUxYTY1MjBhZDU5ODJhMWRjYmMxOTY5NWQ5MGI0M2MzMGMwYzQyMmUwZjcxNGQyNGZjZTZjNGJjN2NiODM3N2NlMjQ3YWNkNGJjNWVlNzI4YmI2OGI3NTMyMGE5NmQ0ZDZlZjZiODM1ZTIxNDAzMDYzMjIyZTE3ZQ=='
cookie = 'BAIDUID=61D17458C811DF0B06467F8750EE21C2:FG=1; ab_jid=4612ca84faa72ab407fd722d950fc31fe1d1; ab_bid=faa72ab407fd722d950fc31fe1d217e49c55; ab_sr=1.0.1_YjY5ZGYxMzM0NTc3ZmYwNjE4OTE2ZTNiMDEzMzEwN2JmMWRjMjBhOWY0Njc4YjlhNmM5M2ViMjZiZmNmYWI1MTU0NzQ3YmU0MjJmYTM2YWNiZjEyYjE1ODdjMjc0MDQxNWU4ZmI0MmNjZWFhODRlZDAyMjIxNmE0YzdkNmUyYjI5M2Q3NjE3OTc2ZDliNDIxYWUyMWRhYmVhODZjZjlhZA==; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598'

json_headers = {
'Host': 'finance.pae.baidu.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0',
'Accept': 'application/vnd.finance-web.v1+json',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'application/josn',
'Origin': 'https://gushitong.baidu.com',
'Connection': 'keep-alive',
'Referer': 'https://gushitong.baidu.com/',
'Cookie': cookie
}
params = {
'_o': 'https://gushitong.baidu.com'
}
response = requests.post(url='https://miao.baidu.com/abdr', params=params)
# response = requests.get('https://gushitong.baidu.com/stock/ab-300059')
print(response.cookies)

0 comments on commit 6d33da9

Please sign in to comment.