Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in
Toggle navigation
F
fund_report
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
彭熊
fund_report
Commits
73060184
Commit
73060184
authored
Jan 21, 2021
by
赵杰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新版本推荐基金,支持ifa导入基金
parent
0fecd195
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
245 additions
and
192 deletions
+245
-192
data_service_v2_1.py
app/service/data_service_v2_1.py
+23
-1
portfolio_diagnose.py
app/service/portfolio_diagnose.py
+190
-180
result_service_v2.py
app/service/result_service_v2.py
+27
-8
jinjia2html_v2.py
app/utils/jinjia2html_v2.py
+5
-3
No files found.
app/service/data_service_v2_1.py
View file @
73060184
...
@@ -29,6 +29,8 @@ class UserCustomerDataAdaptor:
...
@@ -29,6 +29,8 @@ class UserCustomerDataAdaptor:
end_date
=
""
end_date
=
""
group_data
=
{}
group_data
=
{}
trade_cal_date
=
None
trade_cal_date
=
None
all_fund_id_list
=
None
all_fund_type_dict
=
None
all_fund_distribution
=
{}
all_fund_distribution
=
{}
all_fund_performance
=
{}
all_fund_performance
=
{}
...
@@ -38,7 +40,7 @@ class UserCustomerDataAdaptor:
...
@@ -38,7 +40,7 @@ class UserCustomerDataAdaptor:
self
.
compare_index_id
=
index_id
self
.
compare_index_id
=
index_id
self
.
valueSex
=
''
self
.
valueSex
=
''
p_end_date
=
pd
.
to_datetime
(
end_date
)
.
date
()
p_end_date
=
pd
.
to_datetime
(
end_date
)
.
date
()
p_end_date
=
datetime
.
date
(
year
=
p_end_date
.
year
,
month
=
p_end_date
.
month
,
day
=
1
)
-
datetime
.
timedelta
(
days
=
1
)
#
p_end_date = datetime.date(year=p_end_date.year, month=p_end_date.month, day=1) - datetime.timedelta(days=1)
self
.
end_date
=
pd
.
to_datetime
(
str
(
p_end_date
))
self
.
end_date
=
pd
.
to_datetime
(
str
(
p_end_date
))
# self.end_date = pd.to_datetime("2020-12-25")
# self.end_date = pd.to_datetime("2020-12-25")
p_start_date
=
datetime
.
date
(
year
=
p_end_date
.
year
,
month
=
p_end_date
.
month
,
day
=
1
)
p_start_date
=
datetime
.
date
(
year
=
p_end_date
.
year
,
month
=
p_end_date
.
month
,
day
=
1
)
...
@@ -104,6 +106,20 @@ class UserCustomerDataAdaptor:
...
@@ -104,6 +106,20 @@ class UserCustomerDataAdaptor:
if
len
(
fund_df
)
>
0
:
if
len
(
fund_df
)
>
0
:
product_df
=
product_df
.
append
(
fund_df
)
product_df
=
product_df
.
append
(
fund_df
)
product_df
=
product_df
.
drop_duplicates
(
"fund_id"
)
product_df
=
product_df
.
drop_duplicates
(
"fund_id"
)
fund_type3
=
order_df
[
order_df
[
"type"
]
==
3
]
if
len
(
fund_type3
)
>
0
:
type3_fund_id
=
list
(
fund_type3
[
"fund_id"
]
.
unique
())
fund_list_str_3
=
str
(
type3_fund_id
)
.
replace
(
"["
,
"("
)
.
replace
(
"]"
,
")"
)
sql_fund_3
=
"select distinct `id`, `fund_name`, `substrategy` from `ifa_imported_fund_info` where `id` in {}"
.
format
(
fund_list_str_3
)
cur
=
tamp_fund_session
.
execute
(
sql_fund_3
)
data
=
cur
.
fetchall
()
fund_df_3
=
pd
.
DataFrame
(
list
(
data
),
columns
=
[
'fund_id'
,
'fund_name'
,
'substrategy'
])
fund_df_3
[
"freq"
]
=
1
if
len
(
fund_df_3
)
>
0
:
product_df
=
product_df
.
append
(
fund_df_3
)
product_df
=
product_df
.
drop_duplicates
(
"fund_id"
)
user_customer_order_df
=
order_df
.
set_index
(
'fund_id'
)
.
join
(
product_df
.
set_index
(
'fund_id'
))
.
reset_index
()
user_customer_order_df
=
order_df
.
set_index
(
'fund_id'
)
.
join
(
product_df
.
set_index
(
'fund_id'
))
.
reset_index
()
user_customer_order_df
[
"confirm_share_date"
]
=
user_customer_order_df
[
"confirm_share_date"
]
.
apply
(
lambda
x
:
pd
.
to_datetime
(
x
.
date
()))
user_customer_order_df
[
"confirm_share_date"
]
=
user_customer_order_df
[
"confirm_share_date"
]
.
apply
(
lambda
x
:
pd
.
to_datetime
(
x
.
date
()))
...
@@ -168,6 +184,10 @@ class UserCustomerDataAdaptor:
...
@@ -168,6 +184,10 @@ class UserCustomerDataAdaptor:
sql
=
"""select distinct `price_date`, `nav`,`cumulative_nav` from `fund_nav` where `fund_id`='{}' order by `price_date` ASC"""
.
format
(
sql
=
"""select distinct `price_date`, `nav`,`cumulative_nav` from `fund_nav` where `fund_id`='{}' order by `price_date` ASC"""
.
format
(
cur_fund_id
)
cur_fund_id
)
cur
=
tamp_product_session
.
execute
(
sql
)
cur
=
tamp_product_session
.
execute
(
sql
)
elif
fund_type
==
3
:
sql
=
"""select distinct `price_date`, `nav`,`cumulative_nav` from `ifa_imported_fund_nav` where `fund_id`='{}' order by `price_date` ASC"""
.
format
(
cur_fund_id
)
cur
=
tamp_fund_session
.
execute
(
sql
)
data
=
cur
.
fetchall
()
data
=
cur
.
fetchall
()
cur_fund_nav_df
=
pd
.
DataFrame
(
list
(
data
),
columns
=
[
'price_date'
,
'nav'
,
'cnav'
])
cur_fund_nav_df
=
pd
.
DataFrame
(
list
(
data
),
columns
=
[
'price_date'
,
'nav'
,
'cnav'
])
...
@@ -342,6 +362,8 @@ class UserCustomerDataAdaptor:
...
@@ -342,6 +362,8 @@ class UserCustomerDataAdaptor:
cnav_df
=
p_cnav_df
[
p_cnav_df
.
index
>=
start_date
]
.
copy
()
cnav_df
=
p_cnav_df
[
p_cnav_df
.
index
>=
start_date
]
.
copy
()
p_fund_id_list
=
list
(
p_order_df
[
"fund_id"
]
.
unique
())
p_fund_id_list
=
list
(
p_order_df
[
"fund_id"
]
.
unique
())
self
.
all_fund_id_list
=
p_fund_id_list
self
.
all_fund_type_dict
=
{
values
[
"fund_id"
]:
values
[
"type"
]
for
values
in
p_order_df
[[
"fund_id"
,
"type"
]]
.
drop_duplicates
()
.
to_dict
(
orient
=
'records'
)}
for
p_fund_id
in
p_fund_id_list
:
for
p_fund_id
in
p_fund_id_list
:
order_min_date
=
p_order_df
[
p_order_df
[
"fund_id"
]
==
p_fund_id
][
"confirm_share_date"
]
.
min
()
order_min_date
=
p_order_df
[
p_order_df
[
"fund_id"
]
==
p_fund_id
][
"confirm_share_date"
]
.
min
()
if
pd
.
to_datetime
(
order_min_date
)
>
start_date
:
if
pd
.
to_datetime
(
order_min_date
)
>
start_date
:
...
...
app/service/portfolio_diagnose.py
View file @
73060184
This diff is collapsed.
Click to expand it.
app/service/result_service_v2.py
View file @
73060184
...
@@ -19,8 +19,8 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
...
@@ -19,8 +19,8 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
total_result_data
=
{}
total_result_data
=
{}
group_result_data
=
{}
group_result_data
=
{}
def
__init__
(
self
,
user_id
,
customer_id
,
end_date
=
str
(
datetime
.
date
.
today
())
):
def
__init__
(
self
,
user_id
,
customer_id
):
super
()
.
__init__
(
user_id
,
customer_id
,
end_date
)
super
()
.
__init__
(
user_id
,
customer_id
)
# 组合结果数据
# 组合结果数据
def
calculate_group_result_data
(
self
):
def
calculate_group_result_data
(
self
):
...
@@ -57,6 +57,11 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
...
@@ -57,6 +57,11 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
return_ratio_df
,
month_return_ratio_df
,
contribution_decomposition
=
self
.
combination_yield
(
resample_cur_folio_result_cnav_data
,
return_ratio_df
,
month_return_ratio_df
,
contribution_decomposition
=
self
.
combination_yield
(
resample_cur_folio_result_cnav_data
,
fund_id_list
)
fund_id_list
)
resample_df
=
resample
(
return_ratio_df
,
self
.
trade_cal_date
,
freq_max
)
resample_df
=
resample
(
return_ratio_df
,
self
.
trade_cal_date
,
freq_max
)
if
resample_df
.
index
.
values
[
-
1
]
>
self
.
end_date
:
last
=
resample_df
.
index
.
values
[
-
1
]
resample_df
[
"index_date"
]
=
resample_df
.
index
resample_df
.
loc
[
last
,
"index_date"
]
=
self
.
end_date
resample_df
.
set_index
(
"index_date"
,
inplace
=
True
)
resample_df
=
resample_df
[
resample_df
.
index
<=
self
.
end_date
]
resample_df
=
resample_df
[
resample_df
.
index
<=
self
.
end_date
]
...
@@ -87,7 +92,7 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
...
@@ -87,7 +92,7 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
# 波动率
# 波动率
volatility_
=
volatility
(
resample_df
[
"cum_return_ratio"
],
n_freq
)
volatility_
=
volatility
(
resample_df
[
"cum_return_ratio"
],
n_freq
)
folio_report_data
[
"volatility"
]
=
float
(
volatility_
)
folio_report_data
[
"volatility"
]
=
float
(
volatility_
)
if
not
math
.
isnan
(
volatility_
)
else
0.0
# 最大回撤
# 最大回撤
drawdown
=
max_drawdown
(
resample_df
[
"cum_return_ratio"
])
drawdown
=
max_drawdown
(
resample_df
[
"cum_return_ratio"
])
...
@@ -100,7 +105,7 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
...
@@ -100,7 +105,7 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
sharpe
=
sharpe_ratio
(
exc
,
sim
,
n_freq
)
sharpe
=
sharpe_ratio
(
exc
,
sim
,
n_freq
)
except
ZeroDivisionError
:
except
ZeroDivisionError
:
sharpe
=
0.0
sharpe
=
0.0
folio_report_data
[
"sharpe"
]
=
float
(
sharpe
)
folio_report_data
[
"sharpe"
]
=
float
(
sharpe
)
if
not
math
.
isnan
(
sharpe
)
else
0.0
# 期末资产
# 期末资产
ending_assets
=
cumulative_profit
+
total_cost
ending_assets
=
cumulative_profit
+
total_cost
...
@@ -135,7 +140,10 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
...
@@ -135,7 +140,10 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
if
len
(
last_profit_ratio
)
<=
0
:
if
len
(
last_profit_ratio
)
<=
0
:
cur_year_profit_ratio
=
cur_profit_ratio
[
-
1
]
-
1
cur_year_profit_ratio
=
cur_profit_ratio
[
-
1
]
-
1
else
:
else
:
cur_year_profit_ratio
=
(
cur_profit_ratio
[
-
1
]
-
last_profit_ratio
[
-
1
])
/
last_profit_ratio
[
-
1
]
if
len
(
cur_profit_ratio
)
<
1
:
cur_year_profit_ratio
=
0
else
:
cur_year_profit_ratio
=
(
cur_profit_ratio
[
-
1
]
-
last_profit_ratio
[
-
1
])
/
last_profit_ratio
[
-
1
]
folio_report_data
[
"cur_year_profit_ratio"
]
=
float
(
cur_year_profit_ratio
)
folio_report_data
[
"cur_year_profit_ratio"
]
=
float
(
cur_year_profit_ratio
)
# 累积收益率
# 累积收益率
...
@@ -226,6 +234,11 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
...
@@ -226,6 +234,11 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
resample_cur_folio_result_cnav_data
=
resample_cur_folio_result_cnav_data
[
resample_cur_folio_result_cnav_data
.
index
<=
self
.
end_date
]
resample_cur_folio_result_cnav_data
=
resample_cur_folio_result_cnav_data
[
resample_cur_folio_result_cnav_data
.
index
<=
self
.
end_date
]
return_ratio_df
,
month_return_ratio_df
,
contribution_decomposition
=
self
.
combination_yield
(
resample_cur_folio_result_cnav_data
,
fund_id_list
)
return_ratio_df
,
month_return_ratio_df
,
contribution_decomposition
=
self
.
combination_yield
(
resample_cur_folio_result_cnav_data
,
fund_id_list
)
resample_df
=
resample
(
return_ratio_df
,
self
.
trade_cal_date
,
freq_max
)
resample_df
=
resample
(
return_ratio_df
,
self
.
trade_cal_date
,
freq_max
)
if
resample_df
.
index
.
values
[
-
1
]
>
self
.
end_date
:
last
=
resample_df
.
index
.
values
[
-
1
]
resample_df
[
"index_date"
]
=
resample_df
.
index
resample_df
.
loc
[
last
,
"index_date"
]
=
self
.
end_date
resample_df
.
set_index
(
"index_date"
,
inplace
=
True
)
resample_df
=
resample_df
[
resample_df
.
index
<=
self
.
end_date
]
resample_df
=
resample_df
[
resample_df
.
index
<=
self
.
end_date
]
# 总成本
# 总成本
...
@@ -289,7 +302,13 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
...
@@ -289,7 +302,13 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
if
len
(
last_profit_ratio
)
<=
0
:
if
len
(
last_profit_ratio
)
<=
0
:
cur_year_profit_ratio
=
cur_profit_ratio
[
-
1
]
-
1
cur_year_profit_ratio
=
cur_profit_ratio
[
-
1
]
-
1
else
:
else
:
cur_year_profit_ratio
=
(
cur_profit_ratio
[
-
1
]
-
last_profit_ratio
[
-
1
])
/
last_profit_ratio
[
-
1
]
if
len
(
cur_profit_ratio
)
<
1
:
cur_year_profit_ratio
=
0.0
else
:
if
len
(
cur_profit_ratio
)
<
1
:
cur_year_profit_ratio
=
0
else
:
cur_year_profit_ratio
=
(
cur_profit_ratio
[
-
1
]
-
last_profit_ratio
[
-
1
])
/
last_profit_ratio
[
-
1
]
report_data
[
"cur_year_profit_ratio"
]
=
float
(
cur_year_profit_ratio
)
report_data
[
"cur_year_profit_ratio"
]
=
float
(
cur_year_profit_ratio
)
# 月度回报
# 月度回报
...
@@ -422,7 +441,7 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
...
@@ -422,7 +441,7 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
# 基金持仓数据
# 基金持仓数据
total_market_values
=
p_sum_profit
+
p_total_amount
# 月末总市值
total_market_values
=
p_sum_profit
+
p_total_amount
# 月末总市值
fund_strategy_name
=
dict_substrategy
[
int
(
row
[
"substrategy"
])]
fund_strategy_name
=
dict_substrategy
[
int
(
row
[
"substrategy"
])]
if
"长富"
in
row
[
"fund_name"
]
or
"盈沛"
in
row
[
"fund_name"
]
:
if
"长富"
in
row
[
"fund_name"
]
or
"盈沛"
in
row
[
"fund_name"
]:
fund_strategy_name
=
"FOF"
fund_strategy_name
=
"FOF"
fund_hoding_info
=
{
"fund_strategy_name"
:
fund_strategy_name
,
"fund_name"
:
row
[
"fund_name"
]}
fund_hoding_info
=
{
"fund_strategy_name"
:
fund_strategy_name
,
"fund_name"
:
row
[
"fund_name"
]}
fund_hoding_info
[
"confirm_date"
]
=
row
[
"confirm_share_date"
]
.
strftime
(
"
%
Y-
%
m-
%
d"
)
fund_hoding_info
[
"confirm_date"
]
=
row
[
"confirm_share_date"
]
.
strftime
(
"
%
Y-
%
m-
%
d"
)
...
@@ -571,7 +590,7 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
...
@@ -571,7 +590,7 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
# 波动率
# 波动率
volatility_
=
volatility
(
fund_nav_df
[
"cum_return_ratio"
],
n_freq
)
volatility_
=
volatility
(
fund_nav_df
[
"cum_return_ratio"
],
n_freq
)
result
[
"volatility"
]
=
float
(
volatility_
)
result
[
"volatility"
]
=
float
(
volatility_
)
if
not
math
.
isnan
(
volatility_
)
else
0.0
# 最大回撤
# 最大回撤
drawdown
=
max_drawdown
(
fund_nav_df
[
"cum_return_ratio"
])
drawdown
=
max_drawdown
(
fund_nav_df
[
"cum_return_ratio"
])
...
...
app/utils/jinjia2html_v2.py
View file @
73060184
...
@@ -45,7 +45,6 @@ class DataIntegrate:
...
@@ -45,7 +45,6 @@ class DataIntegrate:
# 月度回报表格
# 月度回报表格
self
.
get_month_table_return
()
self
.
get_month_table_return
()
# 分组和计算个基点评以及新增基金等结果
# 分组和计算个基点评以及新增基金等结果
def
get_group_result
(
self
):
def
get_group_result
(
self
):
for
group_name
,
group_result
in
self
.
d
.
items
():
for
group_name
,
group_result
in
self
.
d
.
items
():
...
@@ -83,7 +82,10 @@ class DataIntegrate:
...
@@ -83,7 +82,10 @@ class DataIntegrate:
self
.
all_folio_result
[
group_name
]
=
cur_group_portfolio_result
self
.
all_folio_result
[
group_name
]
=
cur_group_portfolio_result
def
get_portfolio_diagnose
(
self
,
portfolio
,
client_type
=
1
,
invest_amount
=
10000000
):
def
get_portfolio_diagnose
(
self
,
portfolio
,
client_type
=
1
,
invest_amount
=
10000000
):
portfolio_diagnose
=
PortfolioDiagnose
(
client_type
=
client_type
,
portfolio
=
portfolio
,
invest_amount
=
float
(
invest_amount
),
if
invest_amount
<
10000000
:
invest_amount
=
10000000
portfolio_diagnose
=
PortfolioDiagnose
(
client_type
=
client_type
,
portfolio
=
self
.
user_customer
.
all_fund_type_dict
,
invest_amount
=
float
(
invest_amount
),
start_date
=
self
.
user_customer
.
start_date
)
start_date
=
self
.
user_customer
.
start_date
)
portfolio_diagnose
.
optimize
()
portfolio_diagnose
.
optimize
()
return
portfolio_diagnose
return
portfolio_diagnose
...
@@ -299,7 +301,7 @@ class DataIntegrate:
...
@@ -299,7 +301,7 @@ class DataIntegrate:
template
=
env
.
get_template
(
'/v2/monthReportV2.1.html'
)
# 获取一个模板文件
template
=
env
.
get_template
(
'/v2/monthReportV2.1.html'
)
# 获取一个模板文件
monthReport_html
=
template
.
render
(
self
.
data
)
.
replace
(
'None'
,
'none'
)
# 渲染
monthReport_html
=
template
.
render
(
self
.
data
)
.
replace
(
'None'
,
'none'
)
# 渲染
# 保存 monthReport_html
# 保存 monthReport_html
# save_file = "app/
html
/monthReport.html"
# save_file = "app/
pdf
/monthReport.html"
# with open(save_file, 'w', encoding="utf-8") as f:
# with open(save_file, 'w', encoding="utf-8") as f:
# f.write(monthReport_html)
# f.write(monthReport_html)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment