Skip to content

Commit

Permalink
找到优化股票和期货基础文件配置的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Micro-sheep committed Jun 15, 2021
1 parent 732d0ca commit cd4cc13
Show file tree
Hide file tree
Showing 16 changed files with 355 additions and 134 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Folders
*/__pycache__/*
efinance.egg-info
*.egg-info*
build
dist

Expand All @@ -9,4 +9,5 @@ dist
*.csv
*.xlsx
*.zip
*.ipynb


3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "D:\\Scoop\\shims\\python.exe"
}
1 change: 0 additions & 1 deletion efinance/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
import efinance.fund as fund
import efinance.futures as futures


2 changes: 1 addition & 1 deletion efinance/fund/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
get_period_change,
get_public_dates,
get_industry_distributing,
get_qoute_history)
get_quote_history)
123 changes: 60 additions & 63 deletions efinance/fund/getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .config import EastmoneyFundHeaders


def get_qoute_history(fund_code: str, pz: int = 40000) -> pd.DataFrame:
def get_quote_history(fund_code: str, pz: int = 40000) -> pd.DataFrame:
'''
根据基金代码和要获取的页码抓取基金净值信息
Expand Down Expand Up @@ -64,9 +64,6 @@ def get_qoute_history(fund_code: str, pz: int = 40000) -> pd.DataFrame:
return df





def get_realtime_increase_rate(fund_codes: Union[List[str], str]) -> pd.DataFrame:
'''
获取基金实时预期涨跌幅度
Expand Down Expand Up @@ -176,46 +173,46 @@ def get_inverst_postion(fund_code: str, dates: Union[str, List[str]] = None) ->
DataFrame : 包含指定基金特定日期的公开持仓信息
'''
columns = {
'GPDM': '股票代码',
'GPJC': '股票简称',
'JZBL': '持仓占比',
'PCTNVCHG': '较上期变化',
}
'GPDM': '股票代码',
'GPJC': '股票简称',
'JZBL': '持仓占比',
'PCTNVCHG': '较上期变化',
}
df = pd.DataFrame(columns=columns.values())
if not isinstance(dates,List):
if not isinstance(dates, List):
dates = [dates]

for date in dates:
params = [
('FCODE', fund_code),
('MobileKey', '3EA024C2-7F22-408B-95E4-383D38160FB3'),
('OSVersion', '14.3'),
('appType', 'ttjj'),
('appVersion', '6.2.8'),
('deviceid', '3EA024C2-7F22-408B-95E4-383D38160FB3'),
('passportid', '3061335960830820'),
('plat', 'Iphone'),
('product', 'EFund'),
('serverVersion', '6.2.8'),
('uToken', '6cfr1qdanf8nfhd6uc6u-hdj86f1f8kfe9f8k108.6'),
('userId', 'f8d95b2330d84d9e804e7f28a802d809'),
('version', '6.2.8'),
]
('FCODE', fund_code),
('MobileKey', '3EA024C2-7F22-408B-95E4-383D38160FB3'),
('OSVersion', '14.3'),
('appType', 'ttjj'),
('appVersion', '6.2.8'),
('deviceid', '3EA024C2-7F22-408B-95E4-383D38160FB3'),
('passportid', '3061335960830820'),
('plat', 'Iphone'),
('product', 'EFund'),
('serverVersion', '6.2.8'),
('uToken', '6cfr1qdanf8nfhd6uc6u-hdj86f1f8kfe9f8k108.6'),
('userId', 'f8d95b2330d84d9e804e7f28a802d809'),
('version', '6.2.8'),
]
if date is not None:
params.append(('DATE',date))
params.append(('DATE', date))
params = tuple(params)
response = requests.get('https://fundmobapi.eastmoney.com/FundMNewApi/FundMNInverstPosition',
headers=EastmoneyFundHeaders, params=params)
stocks = response.json()['Datas']['fundStocks']

if stocks is None or len(stocks) == 0:
continue

_df = pd.DataFrame(stocks)
_df = _df[list(columns.keys())].rename(columns=columns)
_df['公开日期'] = [date for _ in range(len(_df))]
df = pd.concat([df,_df],axis=0)
df.insert(0,'基金代码',[fund_code for _ in range(len(df))])
_df['公开日期'] = [date for _ in range(len(_df))]
df = pd.concat([df, _df], axis=0)
df.insert(0, '基金代码', [fund_code for _ in range(len(df))])
return df


Expand Down Expand Up @@ -271,7 +268,7 @@ def get_period_change(fund_code: str) -> pd.DataFrame:

df = df[list(columns.keys())].rename(columns=columns)
df['时间段'] = titles.values()
df.insert(0,'基金代码',[fund_code for _ in range(len(df))])
df.insert(0, '基金代码', [fund_code for _ in range(len(df))])
return df


Expand Down Expand Up @@ -311,7 +308,7 @@ def get_public_dates(fund_code: str) -> List[str]:
return json_response['Datas']


def get_types_persentage(fund_code: str, dates:Union[List[str],str,None]=None) -> pd.DataFrame:
def get_types_persentage(fund_code: str, dates: Union[List[str], str, None] = None) -> pd.DataFrame:
'''
获取指定基金不同类型占比信息
Expand All @@ -329,43 +326,43 @@ def get_types_persentage(fund_code: str, dates:Union[List[str],str,None]=None) -
DataFrame : 指定基金的在不同日期的不同类型持仓占比信息
'''
columns = {
'GP': '股票比重',
'ZQ': '债券比重',
'HB': '现金比重',
'JZC': '总规模(亿元)',
'QT': '其他比重'
}
df = pd.DataFrame(columns = columns.values())
if not isinstance(dates,List):
'GP': '股票比重',
'ZQ': '债券比重',
'HB': '现金比重',
'JZC': '总规模(亿元)',
'QT': '其他比重'
}
df = pd.DataFrame(columns=columns.values())
if not isinstance(dates, List):
dates = [dates]
for date in dates:
params = [
('FCODE', fund_code),
('MobileKey', '3EA024C2-7F22-408B-95E4-383D38160FB3'),
('OSVersion', '14.3'),
('appVersion', '6.3.8'),
('cToken', 'a6hdhrfejje88ruaeduau1rdufna1e--.6'),
('deviceid', '3EA024C2-7F22-408B-95E4-383D38160FB3'),
('passportid', '3061335960830820'),
('plat', 'Iphone'),
('product', 'EFund'),
('serverVersion', '6.3.6'),
('uToken', 'a166hhqnrajucnfcjkfkeducanekj1dd1cc2a-e9.6'),
('userId', 'f8d95b2330d84d9e804e7f28a802d809'),
('version', '6.3.8'),
]
('FCODE', fund_code),
('MobileKey', '3EA024C2-7F22-408B-95E4-383D38160FB3'),
('OSVersion', '14.3'),
('appVersion', '6.3.8'),
('cToken', 'a6hdhrfejje88ruaeduau1rdufna1e--.6'),
('deviceid', '3EA024C2-7F22-408B-95E4-383D38160FB3'),
('passportid', '3061335960830820'),
('plat', 'Iphone'),
('product', 'EFund'),
('serverVersion', '6.3.6'),
('uToken', 'a166hhqnrajucnfcjkfkeducanekj1dd1cc2a-e9.6'),
('userId', 'f8d95b2330d84d9e804e7f28a802d809'),
('version', '6.3.8'),
]
if date is not None:
params.append(('DATE', date))
params = tuple(params)
json_response = requests.get(
'https://fundmobapi.eastmoney.com/FundMNewApi/FundMNAssetAllocationNew', params=params).json()
'https://fundmobapi.eastmoney.com/FundMNewApi/FundMNAssetAllocationNew', params=params).json()

if len(json_response['Datas']) == 0:
continue
_df = pd.DataFrame(json_response['Datas'])[columns.keys()]
_df = _df.rename(columns=columns)
df = pd.concat([df,_df],axis=0)
df.insert(0,'基金代码',[fund_code for _ in range(len(df))])
df = pd.concat([df, _df], axis=0)
df.insert(0, '基金代码', [fund_code for _ in range(len(df))])
return df


Expand Down Expand Up @@ -427,7 +424,7 @@ def get_industry_distributing(fund_code: str, dates: Union[str, List[str]] = Non
Return
------
DataFrame : 包含指定基金行业持仓信息的表格
'''
columns = {
'HYMC': '行业名称',
Expand Down Expand Up @@ -458,13 +455,13 @@ def get_industry_distributing(fund_code: str, dates: Union[str, List[str]] = Non
]
if date is not None:
params.append(('DATE', date))

response = requests.get('https://fundmobapi.eastmoney.com/FundMNewApi/FundMNSectorAllocation',
headers=EastmoneyFundHeaders, params=params)
datas = response.json()['Datas']
_df = pd.DataFrame(datas)
_df = _df.rename(columns=columns)
df = pd.concat([df, _df],axis=0)
df.insert(0,'基金代码',[fund_code for _ in range(len(df))])
df = pd.concat([df, _df], axis=0)
df.insert(0, '基金代码', [fund_code for _ in range(len(df))])
df = df.drop_duplicates()
return df
6 changes: 3 additions & 3 deletions efinance/futures/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .getter import (get_futures_base_info,
get_qoute_history)

from .getter import (
get_futures_base_info,
get_quote_history)
5 changes: 5 additions & 0 deletions efinance/futures/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

from pathlib import Path
HERE = Path(__file__).parent
FUTURES_BASE_INFO_SAVE_PATH = HERE/'futures_info.csv'

EastmoneyKlines = {
'f51': '日期',
'f52': '开盘',
Expand Down
105 changes: 100 additions & 5 deletions efinance/futures/getter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from .utils import update_local_futures_info
from typing import Dict, List, Union
import pandas as pd
import requests
from urllib.parse import urlencode

import multitasking
from tqdm import tqdm
from .config import EastmoneyHeaders, EastmoneyKlines
from retry import retry


def get_futures_base_info() -> pd.DataFrame:
Expand Down Expand Up @@ -42,7 +46,11 @@ def get_futures_base_info() -> pd.DataFrame:
return df


def get_qoute_history(secid: str, beg: str = '19000101', end: str = '20500101', klt: int = 101, fqt: int = 1) -> pd.DataFrame:
def get_quote_history_single(secid: str,
beg: str = '19000101',
end: str = '20500101',
klt: int = 101,
fqt: int = 1) -> pd.DataFrame:
'''
获取k线数据
Expand All @@ -67,11 +75,10 @@ def get_qoute_history(secid: str, beg: str = '19000101', end: str = '20500101',
DateFrame : 包含股票k线数据
'''

fields = list(EastmoneyKlines.keys())
columns = list(EastmoneyKlines.values())
fields2 = ",".join(fields)


params = (
('fields1', 'f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13'),
Expand All @@ -93,7 +100,6 @@ def get_qoute_history(secid: str, beg: str = '19000101', end: str = '20500101',
print(secid, '无数据')
return None
# code = data['code']
# name
# name = data['name']
klines = data['klines']

Expand All @@ -106,3 +112,92 @@ def get_qoute_history(secid: str, beg: str = '19000101', end: str = '20500101',
df = pd.DataFrame(rows, columns=columns)

return df


def get_quote_history_multi(secids: List[str],
beg: str = '19000101',
end: str = '20500101',
klt: int = 101,
fqt: int = 1,
tries: int = 3) -> Dict[str, pd.DataFrame]:
'''
获取多只股票历史行情信息
Parameters
----------
secids : 多个 期货 secid 列表
beg : 开始日期 例如 20200101
end : 结束日期 例如 20200201
klt : k线间距 默认为 101 即日k
klt : 1 1 分钟
klt : 5 5 分钟
klt : 101 日
klt : 102 周
fqt: 复权方式
不复权 : 0
前复权 : 1
后复权 : 2
Return
------
DateFrame : 包含股票k线数据
'''
dfs: Dict[str, pd.DataFrame] = {}
total = len(secids)
if total != 0:
update_local_futures_info()

@retry(tries=tries)
@multitasking.task
def start(stock_code: str):
_df = get_quote_history_single(
stock_code, beg=beg, end=end, klt=klt, fqt=fqt)
dfs[stock_code] = _df
pbar.update(1)
pbar.set_description_str(f'Processing: {stock_code}')

pbar = tqdm(total=total)
for stock_code in secids:
start(stock_code)
multitasking.wait_for_tasks()
pbar.close()
return dfs


def get_quote_history(secids: Union[str, List[str]],
beg: str = '19000101',
end: str = '20500101',
klt: int = 101,
fqt: int = 1) -> pd.DataFrame:
'''
获取k线数据
Parameters
----------
secids : 6 位股票代码 或者 6 位股票代码构成的列表
beg : 开始日期 例如 20200101
end : 结束日期 例如 20200201
klt : k线间距 默认为 101 即日k
klt : 1 1 分钟
klt : 5 5 分钟
klt : 101 日
klt : 102 周
fqt: 复权方式
不复权 : 0
前复权 : 1
后复权 : 2
Return
------
DateFrame : 包含股票k线数据
'''
if isinstance(secids, str):
return get_quote_history_single(secids, beg=beg, end=end, klt=klt, fqt=fqt)
elif hasattr(secids, '__iter__'):
secids = list(secids)
return get_quote_history_multi(secids, beg=beg, end=end, klt=klt, fqt=fqt)
else:
raise TypeError(
'期货 secid 类型输入不正确!'
)
Loading

0 comments on commit cd4cc13

Please sign in to comment.