介绍
本项目主要包含以下功能:
- 基本信息获取
- 实时估值获取
- 基金持有人结构历史信息(洞察机构动向)
- 基金回撤信息(科学评估基金的风险控制能力、恢复能力、以及下跌与节假日的关系)
- 自定义时间范围内的基金排名
引用的库
import requests, json, copy, re, time, datetime
import numpy as np
import datetime
import chinese_calendar
from chinese_calendar import is_workday
import httpx
import asyncio
from colorama import Fore,Back,Style
基金初始化
#我们创建了一个Fund:类
#这是其中一个功能函数
async def async_init(self):
code = self.code
try:
async with httpx.AsyncClient() as client:
resp = await client.get(f"http://***************/{self.code}.js")
self.text = resp.text
self.init_with_text()
# print(f"[ok]:{code}")
return True
except:
print(f"[error]:{code}")
return False
获得实时估值
def get_real_guzhi(self, code=None):
code = self.code if code is None else code
try:
realtime_url = f"http://************************/js/{code}.js"
headers = ""
data = {
"rt": "**********************",
}
real_json = requests.get(url=realtime_url, params=data, headers=headers)
real_json = re.match(".*?({.*}).*", real_json.text, re.S).groups(1)[0]
real_json = json.loads(real_json)
return {
"name": real_json["name"],
"worth": real_json["dwjz"],
"estimate_worth": real_json["gsz"],
"estimate_rate": f"{real_json['gszzl']}%",
"worth_date": real_json["jzrq"],
"estimate_datetime": real_json["gztime"],
}
except:
return None
打印基金信息
def print_info(self):
self.init_type()
try:
geren_m = self.get_fundScale(when=self.fundStruct[-1]["date"]) * float(self.fundStruct[-1]["个人持有比例"]) / 100
geren_m_p = self.get_fundScale(when=self.fundStruct[-2]["date"]) * float(self.fundStruct[-2]["个人持有比例"]) / 100
jigou_m = self.get_fundScale(when=self.fundStruct[-1]["date"]) * float(self.fundStruct[-1]["机构持有比例"]) / 100
jigou_m_p = self.get_fundScale(when=self.fundStruct[-2]["date"]) * float(self.fundStruct[-2]["机构持有比例"]) / 100
neibu_m = self.get_fundScale(when=self.fundStruct[-1]["date"]) * float(self.fundStruct[-1]["内部持有比例"]) / 100
neibu_m_p = self.get_fundScale(when=self.fundStruct[-2]["date"]) * float(self.fundStruct[-2]["内部持有比例"]) / 100
except:
geren_m = geren_m_p =0
jigou_m = jigou_m_p = 0
neibu_m = neibu_m_p = 0
pass
diefu=6
days = 1500
days = len(self.net_worth_data) if days is None else days
days = days if len(self.net_worth_data) >= days else len(self.net_worth_data)
fall_lst = self.get_fall_conlusion(days)
fall_rank = np.argsort([item["max_fall_rate"] for item in fall_lst])
guzhi = "None" if self.get_real_guzhi() is None else self.get_real_guzhi()["estimate_rate"]
print("--------------------------------------------------------------------")
print(" [基金名称]{} [代号]{} [今日估值]{}".format(self.name, self.code, guzhi))
print(" [类型]{} [规模]{} [经理]{}".format(self.type, self.fundScale[-1]["scale"], ",".join([m["name"]+'(共管理'+m["fundSize"]+")" for m in self.manager])))
print("--------------------------------------------------------------------")
print(f" [增持/减持信息] (更新至{self.fundScale[-1]['date']})\n")
try:
print(f" [个人持有比例] :{self.fundStruct[-1]['个人持有比例']:>6}% {'%.3f'%geren_m:>7}亿 较上期增长 :{'%.2f'%(geren_m-geren_m_p)}亿")
print(f" [机构持有比例] :{self.fundStruct[-1]['机构持有比例']:>6}% {'%.3f'%jigou_m:>7}亿 较上期增长 :{'%.2f'%(jigou_m - jigou_m_p)}亿")
print(f" [内部持有比例] :{self.fundStruct[-1]['内部持有比例']:>6}% {'%.3f'%neibu_m:>7}亿 较上期增长 :{'%.2f'%(neibu_m - neibu_m_p)}亿")
except:
pass
print("--------------------------------------------------------------------")
print(" [历史下跌] {}至{}(共{}交易日) (仅显示跌幅大于 {}% 的回撤数据)\n".format(self.net_worth_data[-days][0], self.net_worth_data[-1][0], days, diefu))
for idx in range(len(fall_rank)):
if fall_lst[fall_rank[idx]]["max_fall_rate"]*100 > -6:
break
loginfo = "[ top {} ] 下跌:{:>6.2f}%, 下跌天数:{:>4}, 底部反弹天数:{:>4}, {}({})起跌 至 {}({:>3}) ,包含节日:{}".format(
idx+1,fall_lst[fall_rank[idx]]["max_fall_rate"]*100,fall_lst[fall_rank[idx]]["fall_day"],fall_lst[fall_rank[idx]]["up_day"],
fall_lst[fall_rank[idx]]["date"][0],get_week_day(datetime.date(*map(int,fall_lst[fall_rank[idx]]["date"][0].split("-")))),
fall_lst[fall_rank[idx]]["date"][-1],
"已回本" if fall_lst[fall_rank[idx]]["back"] else "未回本",
fall_lst[fall_rank[idx]]["content_holiday"]
)
if not fall_lst[fall_rank[idx]]["back"]:
loginfo = '▶' + loginfo
loginfo = Fore.GREEN + loginfo
else:
loginfo = ' ' + loginfo
loginfo = Fore.RESET + loginfo
print(loginfo)
if not fall_lst[-1]["back"]:
print(Fore.RESET+"\n* 当前净值({:.2f}%),距离最高点回本需增长 : {:.2f}%".format(
(fall_lst[-1]["worth"][-1]-min(fall_lst[-1]["worth"]))*100/min(fall_lst[-1]["worth"]),
(fall_lst[-1]["worth"][0]-min(fall_lst[-1]["worth"]))*100/fall_lst[-1]["worth"][-1]))
print("--------------------------------------------------------------------")
提供技术支持,请联系我。