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
03fa8113
Commit
03fa8113
authored
4 years ago
by
赵杰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
内外部订单隔离计算分红订单收益与成本
parent
f59eef35
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
46 deletions
+43
-46
result_service_v2.py
app/service/result_service_v2.py
+43
-46
No files found.
app/service/result_service_v2.py
View file @
03fa8113
...
...
@@ -14,6 +14,7 @@ from app.service.data_service_v2_1 import UserCustomerDataAdaptor
from
app.utils.fund_rank
import
get_frequency
from
app.service.portfolio_diagnose
import
cal_correlation
,
get_fund_name
,
get_frequency
from
app.utils.week_evaluation
import
*
from
app.service.substrategy_dict
import
get_substrategy_name
class
UserCustomerResultAdaptor
(
UserCustomerDataAdaptor
):
...
...
@@ -65,7 +66,6 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
resample_df
.
set_index
(
"index_date"
,
inplace
=
True
)
resample_df
=
resample_df
[
resample_df
.
index
<=
self
.
end_date
]
# 收益分解df
contribution_decomposition_df
=
contribution_decomposition
.
fillna
(
0
)
*
100
p_plot_data
=
[]
...
...
@@ -79,7 +79,7 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
"cumulative"
:
cumulative_data
}
# 总成本
total_cost
=
round
(
float
(
(
cur_folio_order_data
[
cur_folio_order_data
[
"order_type"
]
==
1
][
"confirm_share"
]
*
cur_folio_order_data
[
cur_folio_order_data
[
"order_type"
]
==
1
][
"nav"
])
.
sum
(
)),
0
)
total_cost
=
round
(
float
(
round
(
float
(
cur_folio_result_cnav_data
[[
i
+
"_amount"
for
i
in
fund_id_list
]]
.
tail
(
1
)
.
sum
()
.
sum
()),
0
)),
0
)
folio_report_data
[
"total_cost"
]
=
total_cost
# 累积盈利
...
...
@@ -109,8 +109,8 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
folio_report_data
[
"sharpe"
]
=
float
(
sharpe
)
if
not
math
.
isnan
(
sharpe
)
else
0.0
# 期末资产
ending_assets
=
cumulative_profit
+
total_cost
folio_report_data
[
"ending_assets"
]
=
float
(
ending_assets
)
ending_assets
=
round
(
float
(
cur_folio_result_cnav_data
[[
i
+
"_net_amount"
for
i
in
fund_id_list
]]
.
tail
(
1
)
.
sum
()
.
sum
()),
0
)
folio_report_data
[
"ending_assets"
]
=
ending_assets
# 本月收益
cur_month_profit_df
=
profit_df
.
loc
[
self
.
month_start_date
:
self
.
end_date
+
datetime
.
timedelta
(
days
=
1
),
fund_id_list_earn
]
...
...
@@ -250,7 +250,7 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
resample_df
=
resample_df
[
resample_df
.
index
<=
self
.
end_date
]
# 总成本
total_cost
=
round
(
float
(
(
cur_folio_order_data
[
cur_folio_order_data
[
"order_type"
]
==
1
][
"confirm_share"
]
*
cur_folio_order_data
[
cur_folio_order_data
[
"order_type"
]
==
1
][
"nav"
]
)
.
sum
()),
0
)
total_cost
=
round
(
float
(
cur_folio_result_cnav_data
[[
i
+
"_amount"
for
i
in
fund_id_list
]]
.
tail
(
1
)
.
sum
(
)
.
sum
()),
0
)
report_data
[
"total_cost"
]
=
total_cost
# 累积盈利
...
...
@@ -277,8 +277,8 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
# report_data["sharpe"] = float(sharpe)
# 期末资产
ending_assets
=
cumulative_profit
+
total_cost
report_data
[
"ending_assets"
]
=
float
(
ending_assets
)
ending_assets
=
round
(
float
(
cur_folio_result_cnav_data
[[
i
+
"_net_amount"
for
i
in
fund_id_list
]]
.
tail
(
1
)
.
sum
()
.
sum
()),
0
)
report_data
[
"ending_assets"
]
=
ending_assets
# 本月收益
cur_month_profit_df
=
profit_df
.
loc
[
self
.
month_start_date
:
self
.
end_date
+
datetime
.
timedelta
(
days
=
1
),
...
...
@@ -377,8 +377,6 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
index_result
=
self
.
signal_fund_profit_result
(
index_df
[
index_df
.
index
>=
pd
.
to_datetime
(
first_trade_date
)],
"index"
)
report_data
[
"index_result"
]
=
index_result
# self.__month_return(cur_folio_result_cnav_data, fund_id_list)
self
.
total_result_data
=
report_data
return
report_data
...
...
@@ -389,12 +387,14 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
freq_max
=
p_order_df
[
"freq"
]
.
max
()
n_freq
=
freq_days
(
int
(
freq_max
))
resample_df
=
resample
(
p_result_cnav_data
,
self
.
trade_cal_date
,
freq_max
)
for
index
,
row
in
p_order_df
.
iterrows
():
if
row
[
'order_type'
]
==
2
or
row
[
"confirm_share"
]
<=
0
:
continue
# for index, row in p_order_df.iterrows():
for
hold_fund_id
in
p_order_df
[
"fund_id"
]
.
unique
():
order
=
p_order_df
[(
p_order_df
[
"fund_id"
]
==
hold_fund_id
)]
row
=
order
[
order
[
"order_type"
]
==
1
]
.
iloc
[
0
]
cur_fund_id
=
str
(
row
[
"fund_id"
])
cur_fund_performance
=
self
.
all_fund_performance
[
cur_fund_id
]
if
len
(
cur_fund_performance
)
<=
0
:
if
len
(
cur_fund_performance
)
<=
0
:
fund_basic_info
=
{
"fund_name"
:
row
[
"fund_name"
],
"confirm_nav"
:
round
(
row
[
"nav"
],
4
)}
fund_basic_info
[
"cur_nav"
]
=
round
(
float
(
self
.
fund_nav_total
[
cur_fund_id
]
.
dropna
()
.
values
[
-
1
]),
4
)
fund_basic_info
[
"cur_cnav"
]
=
round
(
float
(
self
.
fund_cnav_total
[
cur_fund_id
]
.
dropna
()
.
values
[
-
1
]),
4
)
...
...
@@ -408,8 +408,8 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
# 申购以来
confirm_date
=
pd
.
to_datetime
(
row
[
"confirm_share_date"
])
confirm_cnav
=
float
(
p_result_cnav_data
.
loc
[
confirm_date
,
cur_fund_id
])
fund_basic_info
[
"ret_after_confirm"
]
=
round
(
(
fund_basic_info
[
"cur_cnav"
]
-
confirm_cnav
)
/
confirm_cnav
*
100
,
2
)
fund_basic_info
[
"ret_after_confirm"
]
=
str
(
round
(
(
fund_basic_info
[
"cur_cnav"
]
-
confirm_cnav
)
/
confirm_cnav
*
100
,
2
)
)
+
"
%
"
# 分红
distribution_df
=
self
.
all_fund_distribution
[
cur_fund_id
]
if
distribution_df
.
empty
:
...
...
@@ -448,23 +448,19 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
# 基金持仓数据
total_market_values
=
p_sum_profit
+
p_total_amount
# 月末总市值
fund_strategy_name
=
dict_substrategy
[
int
(
row
[
"substrategy"
])]
fund_strategy_name
=
get_substrategy_name
(
row
[
"substrategy"
])
if
"长富"
in
row
[
"fund_name"
]
or
"盈沛"
in
row
[
"fund_name"
]:
fund_strategy_name
=
"FOF"
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
[
"hold_year"
]
=
"
%.2
f"
%
round
((
self
.
end_date
-
pd
.
to_datetime
(
row
[
"confirm_share_date"
]))
.
days
/
365.0
,
2
)
# 存续年数
fund_hoding_info
[
"market_values"
]
=
round
((
float
(
row
[
"confirm_share"
])
*
(
fund_basic_info
[
"cur_cnav"
]
-
confirm_cnav
)
+
float
(
row
[
"confirm_amount"
]))
/
10000
,
2
)
# temp_market_values = float(row["confirm_share"]) * (fund_basic_info["cur_cnav"] - confirm_cnav) + float(row["confirm_amount"])
# fund_hoding_info["weight"] = "%.2f" % round(float(fund_hoding_info["market_values"]) / total_market_values * 10000.0 * 100, 2) # 月末占比
# # fund_hoding_info["cost"] = "%.2f" % round(float(row["confirm_amount"])/10000, 2) # 投资本金
# fund_hoding_info["cost"] = "%.2f" % round(float(row["confirm_share"]*row["nav"])/10000, 2)
temp_market_values
=
float
(
row
[
"confirm_share"
])
*
(
fund_basic_info
[
"cur_cnav"
]
-
confirm_cnav
+
float
(
row
[
"nav"
]))
# fund_hoding_info["market_values"] = round((float(row["confirm_share"]) * (fund_basic_info["cur_cnav"] - confirm_cnav) + float(row["confirm_amount"]))/10000, 2)
temp_market_values
=
float
(
p_result_cnav_data
[
cur_fund_id
+
"_net_amount"
]
.
values
[
-
1
])
fund_hoding_info
[
"market_values"
]
=
round
(
temp_market_values
/
10000.0
,
2
)
fund_hoding_info
[
"weight"
]
=
"
%.2
f"
%
round
(
float
(
fund_hoding_info
[
"market_values"
])
/
total_market_values
*
10000.0
*
100
,
2
)
# 月末占比
fund_hoding_info
[
"cost"
]
=
"
%.2
f"
%
round
(
float
(
row
[
"confirm_share"
]
*
row
[
"nav"
])
/
10000
,
2
)
# 投资本金
temp_cost
=
float
(
p_result_cnav_data
[
cur_fund_id
+
"_amount"
]
.
values
[
-
1
])
fund_hoding_info
[
"cost"
]
=
"
%.2
f"
%
round
(
temp_cost
/
10000
,
2
)
# 投资本金
# 当月收益
if
row
[
'confirm_share_date'
]
>
self
.
month_start_date
:
...
...
@@ -472,28 +468,29 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
last_month_cnav_serise
=
p_result_cnav_data
[
p_result_cnav_data
.
index
==
pd
.
to_datetime
(
cal_month_start_date
)][
row
[
"fund_id"
]]
.
dropna
()
else
:
# cal_month_start_date = self.month_start_date - datetime.timedelta(days=1)
cal_month_start_date
=
self
.
month_start_date
last_month_cnav_serise
=
p_result_cnav_data
[
p_result_cnav_data
.
index
<
pd
.
to_datetime
(
cal_month_start_date
)][
row
[
"fund_id"
]]
.
dropna
()
cal_month_cnav_result
=
p_result_cnav_data
[
p_result_cnav_data
.
index
>=
pd
.
to_datetime
(
cal_month_start_date
)]
if
len
(
last_month_cnav_serise
)
==
0
:
fund_hoding_info
[
"profit"
]
=
round
(
float
(
row
[
"confirm_share"
])
*
(
fund_basic_info
[
"cur_cnav"
]
-
confirm_cnav
)
/
10000
,
2
)
temp_profit
=
float
(
row
[
"confirm_share"
])
*
(
fund_basic_info
[
"cur_cnav"
]
-
confirm_cnav
)
temp_profit_ratio
=
(
fund_basic_info
[
"cur_cnav"
]
-
confirm_cnav
)
/
confirm_cnav
temp_profit
=
0
fund_hoding_info
[
"profit"
]
=
temp_profit
temp_profit_ratio
=
0
else
:
last_month_cnav
=
float
(
last_month_cnav_serise
.
values
[
-
1
])
temp_profit
=
float
(
cal_month_cnav_result
[
cur_fund_id
+
"_earn"
]
.
sum
())
fund_hoding_info
[
"profit"
]
=
round
(
float
(
row
[
"confirm_share"
])
*
(
fund_basic_info
[
"cur_cnav"
]
-
last_month_cnav
)
/
10000
,
2
)
temp_profit
=
float
(
row
[
"confirm_share"
])
*
(
fund_basic_info
[
"cur_cnav"
]
-
last_month_cnav
)
temp_profit_ratio
=
(
fund_basic_info
[
"cur_cnav"
]
-
last_month_cnav
)
/
last_month_cnav
# 当月收益率
#
fund_hoding_info["month_return_ratio"] = "%.2f" % round(temp_profit / temp_market_values*100, 2)
fund_hoding_info
[
"month_return_ratio"
]
=
"
%.2
f"
%
round
(
temp_profit_ratio
*
100
,
2
)
fund_hoding_info
[
"month_return_ratio"
]
=
"
%.2
f"
%
round
(
temp_profit
/
temp_market_values
*
100
,
2
)
#
fund_hoding_info["month_return_ratio"] = "%.2f" % round(temp_profit_ratio * 100, 2)
# 累积收益
fund_hoding_info
[
"cum_profit"
]
=
"
%.2
f"
%
round
(
float
(
row
[
"confirm_share"
])
*
(
fund_basic_info
[
"cur_cnav"
]
-
confirm_cnav
)
/
10000
,
2
)
cum_profit
=
float
(
p_result_cnav_data
[
cur_fund_id
+
"_earn"
]
.
sum
())
fund_hoding_info
[
"cum_profit"
]
=
"
%.2
f"
%
round
(
cum_profit
/
10000
,
2
)
# 累积收益率
fund_hoding_info
[
"cum_profit_ratio"
]
=
"
%.2
f"
%
round
(
(
fund_basic_info
[
"cur_cnav"
]
-
confirm_cnav
)
/
confirm_cnav
*
100
,
2
)
cum_profit_ratio_temp
=
(
fund_basic_info
[
"cur_cnav"
]
-
confirm_cnav
)
/
confirm_cnav
fund_hoding_info
[
"cum_profit_ratio"
]
=
"
%.2
f"
%
round
(
cum_profit
/
temp_cost
*
100
,
2
)
cum_profit_ratio_temp
=
cum_profit
/
temp_cost
# 累积年化收益率
cur_resample_df
=
resample_df
[[
row
[
"fund_id"
]]]
.
dropna
()
cur_resample_df
=
resample_df
[[
row
[
"fund_id"
]]]
return_ratio_year
=
annual_return
(
float
(
cum_profit_ratio_temp
),
cur_resample_df
,
n_freq
)
fund_hoding_info
[
"return_ratio_year"
]
=
"
%.2
f"
%
round
(
float
(
return_ratio_year
)
*
100
,
2
)
group_fund_hoding_info
.
append
(
fund_hoding_info
)
...
...
@@ -504,28 +501,28 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
fund_id_list_amount
=
[
i
+
"_net_amount"
for
i
in
fund_id_list
]
fund_id_list_profit_ratio
=
[
i
+
"_profit_ratio"
for
i
in
fund_id_list
]
nav_net_amount_df
=
p_combina_df
[
fund_id_list
+
fund_id_list_amount
+
fund_id_list_profit_ratio
]
.
copy
()
# nav_net_amount_df = resample(return_ratio_df, self.trade_cal_date, freq_max)
nav_net_amount_df
[
"sum_net_amount"
]
=
nav_net_amount_df
[
fund_id_list_amount
]
.
sum
(
axis
=
1
)
.
apply
(
lambda
x
:
Decimal
.
from_float
(
x
))
for
amount_name
in
fund_id_list
:
price
=
nav_net_amount_df
[
amount_name
]
.
dropna
()
profit
=
price
.
diff
()
.
fillna
(
Decimal
(
0
))
profit_ratio_new
=
profit
/
price
.
shift
(
1
)
profit_ratio_old
=
nav_net_amount_df
[
amount_name
+
"_profit_ratio"
]
nan_index
=
profit_ratio_new
[
pd
.
isna
(
profit_ratio_new
)]
.
index
profit_ratio_new
[
nan_index
]
=
profit_ratio_old
[
nan_index
]
nav_net_amount_df
[
amount_name
+
"_profit_ratio"
]
=
profit_ratio_new
# price = nav_net_amount_df[amount_name].dropna()
# profit = price.diff().fillna(Decimal(0))
# profit_ratio_new = profit / price.shift(1)
# profit_ratio_old = nav_net_amount_df[amount_name+"_profit_ratio"]
# nan_index = profit_ratio_new[pd.isna(profit_ratio_new)].index
# profit_ratio_new[nan_index] = profit_ratio_old[nan_index]
#
# nav_net_amount_df[amount_name + "_profit_ratio"] = profit_ratio_new
nav_net_amount_df
[
amount_name
+
"_amount_ratio"
]
=
nav_net_amount_df
[
amount_name
+
"_net_amount"
]
/
(
nav_net_amount_df
[
"sum_net_amount"
])
fund_profit_ratio
=
nav_net_amount_df
[
amount_name
+
"_profit_ratio"
]
.
dropna
()
+
1
amount_ratio_shift
=
nav_net_amount_df
[
amount_name
+
"_amount_ratio"
]
.
shift
(
1
)
num_va
=
len
(
amount_ratio_shift
[
amount_ratio_shift
.
values
==
0
])
num_va
=
len
(
amount_ratio_shift
[
amount_ratio_shift
.
values
==
0
])
if
num_va
+
1
>=
len
(
amount_ratio_shift
):
amount_ratio_shift
.
iloc
[
num_va
]
=
0
else
:
amount_ratio_shift
.
iloc
[
num_va
]
=
amount_ratio_shift
.
values
[
num_va
+
1
]
nav_net_amount_df
[
amount_name
+
"_profit_ratio_weight"
]
=
amount_ratio_shift
*
nav_net_amount_df
[
amount_name
+
"_profit_ratio"
]
nav_net_amount_df
[
amount_name
+
"_profit_ratio_weight"
]
=
amount_ratio_shift
*
nav_net_amount_df
[
amount_name
+
"_profit_ratio"
]
nav_net_amount_df
[
amount_name
+
"_profit_cum_ratio_weight"
]
=
(
fund_profit_ratio
.
cumprod
()
-
1
)
*
amount_ratio_shift
# enter_date = nav_net_amount_df[amount_name+"_profit_ratio"].dropna()
fund_id_list_profit_ratio_weight
=
[
i
+
"_profit_ratio_weight"
for
i
in
fund_id_list
]
...
...
@@ -544,7 +541,7 @@ class UserCustomerResultAdaptor(UserCustomerDataAdaptor):
cum_return_ratio_df
.
index
=
return_ratio
.
index
# 单个基金累计收益分解df
weight_name_list
=
[
i
+
"_profit_cum_ratio_weight"
for
i
in
fund_id_list
]
weight_name_list
=
[
i
+
"_profit_cum_ratio_weight"
for
i
in
fund_id_list
]
signal_fund_cum_weight
=
nav_net_amount_df
[
weight_name_list
]
re_name
=
{
x
:
x
.
replace
(
"_profit_cum_ratio_weight"
,
""
)
for
x
in
weight_name_list
}
signal_fund_cum_weight
.
rename
(
columns
=
re_name
,
inplace
=
True
)
...
...
This diff is collapsed.
Click to expand it.
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