-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoc_zsmk_seg.py
234 lines (227 loc) · 14.6 KB
/
poc_zsmk_seg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 14 15:42:12 2017
@author: wangm
"""
import os as os
import numpy as np
import robolib as rl
from pylab import *
import mpt as mpt
import pandas as pd
import iolib as il
import funds_selection as fs
import time as time
import zsmk_util as zsmk
import datetime as datetime
def get_ZScom_for_users(poctype, company_file, user_detail_df, datelist_out, days_before, funds_profit_df, funds_net_df,
riskfree,
minpercent, change_return, symbolstr, changeratio):
'''
定期计算某一个用户在某一段时间内的最优组合,并根据计算情况输出产生组合配置的文件
'''
zs_combination_df = pd.DataFrame(
columns=["userid", "date", "ticker", "name", "percent", "type", "risk_type", "risk_score"])
moneyfund_ticker_for_net = zsmk.get_best_moneyfundticker(endday_str="2017-08-01", days_before=30,
funds_profit_df=funds_profit_df,
method="maxmeanreturn")
time_cost = 0
usercount = 0
funds_type_df, fund_type_list = il.get_funds_type()
funds_net_df_fill = funds_net_df.copy()
funds_net_df_fill = funds_net_df_fill.fillna(method="pad")
funds_net_df_fill = funds_net_df_fill.fillna(method="bfill")
# type_return_avg_df = fs.type_return_avg(funds_net_df_fill, fund_type_list, funds_type_df)
type_return_avg_df = il.get_combine_index_net_matrix("2017-01-01", "2017-12-31", True, "2017")
fund_type_list = type_return_avg_df.columns.tolist()
type_num = len(set(user_detail_df["risk_type"].values.tolist()))
for index, row in user_detail_df.iterrows():
usercount += 1
userid = row["userid"]
usermoneyamount = row["moneyamount"]
userriskscore = row["risk_score"]
userrisktype = row["risk_type"]
start = time.clock()
print("计算第" + str(usercount) + "/" + str(len(user_detail_df)) + "个用户.")
if userrisktype == "0保守型":
change_dic = {}
change_dic["userid"] = userid
change_dic["date"] = datelist_out[0]
change_dic["ticker"] = moneyfund_ticker_for_net
change_dic["name"] = \
funds_type_df[funds_type_df["ticker"] == moneyfund_ticker_for_net]["name"].values.tolist()[0]
change_dic["percent"] = 1.0
change_dic["type"] = \
funds_type_df[funds_type_df["ticker"] == moneyfund_ticker_for_net]["fund_type"].values.tolist()[0]
change_dic["risk_type"] = userrisktype
change_dic["risk_score"] = userriskscore
zs_combination_df = zs_combination_df.append(change_dic, ignore_index=True)
else:
count = 0
current_return = 0.0
combination_df_inside = pd.DataFrame(
columns=["userid", "date", "ticker", "name", "percent", "type", "risk_type", "risk_score"])
old_funds_weight_dic = {}
for endday_str in datelist_out:
count += 1
# 回测的时候每天都检测太慢了,每20天检测一次
if count == 1 or count % 30 == 0:
if not bool(old_funds_weight_dic):
pass
else:
current_return = zsmk.get_return_by_combination(old_funds_weight_dic, datelist_out[0],
endday_str,
funds_net_df_fill)
datelist_inside = rl.dateRange_daysbefore(endday_str, days_before)
startday_str = datelist_inside[0]
print(endday_str)
type_return_avg_pass_df = type_return_avg_df.ix[
startday_str.replace("-", ""):endday_str.replace("-", "")]
funds_net_df_pass = funds_net_df.ix[startday_str.replace("-", ""):endday_str.replace("-", "")]
log_return_df = np.log(type_return_avg_pass_df / type_return_avg_pass_df.shift(1))
# start:根据不同的波动值来确定用户的组合
if "var" in company_file:
type_weight_list, target_ret, target_var = zsmk.get_ZScom_by_var(type_return_avg_pass_df,
riskfree,
type_num, minpercent)
# type_fundticker_dic, selected_fund_list = fs.funds_select_for_type(funds_net_df_pass, fund_type_list,
# funds_type_df,
# type_return_avg_pass_df,
# funds_each_type=2,
# selectby="corr")
type_fundticker_dic, selected_fund_list = fs.funds_select_for_index(funds_net_df_pass,
type_return_avg_pass_df,
funds_each_type=1)
funds_weight_dic, total_net_percent, type_weight_dic = zsmk.get_user_fund_weight_by_risk(
type_weight_list,
fund_type_list,
type_fundticker_dic,
userriskscore)
# end:根据不同的波动值来确定用户的组合
else:
# start:根据不同的上下限来确定用户的组合
bnds = zsmk.get_user_bnds(type_return_avg_pass_df, row, minpercent)
# type_fundticker_dic, selected_fund_list = fs.funds_select_for_type(funds_net_df_pass, fund_type_list,
# funds_type_df,
# type_return_avg_pass_df,
# funds_each_type=2,
# selectby="corr")
type_fundticker_dic, selected_fund_list = fs.funds_select_for_index(funds_net_df_pass,
type_return_avg_pass_df,
funds_each_type=1)
funds_weight_dic, total_net_percent, type_weight_dic = zsmk.get_user_fund_weight_by_bunds(bnds,
type_return_avg_pass_df,
riskfree,
type_fundticker_dic,
fund_type_list)
# end:根据不同的上下限来确定用户的组合
new_return = zsmk.get_return_by_combination(funds_weight_dic, startday_str, endday_str,
funds_net_df_fill)
if combination_df_inside.empty:
for fund, percent in funds_weight_dic.items():
change_dic = {}
change_dic["userid"] = userid
change_dic["date"] = datelist_out[0]
change_dic["ticker"] = fund
change_dic["name"] = funds_type_df[funds_type_df["ticker"] == fund]["name"].values.tolist()[
0]
change_dic["percent"] = float(percent) * total_net_percent
# change_dic["type"] = \
# funds_type_df[funds_type_df["ticker"] == fund]["fund_type"].values.tolist()[0]
change_dic["type"] = {value[0]: key for key, value in type_fundticker_dic.items()}[fund]
change_dic["risk_type"] = userrisktype
change_dic["risk_score"] = userriskscore
combination_df_inside = combination_df_inside.append(change_dic, ignore_index=True)
change_dic = {}
change_dic["userid"] = userid
change_dic["date"] = datelist_out[0]
change_dic["ticker"] = moneyfund_ticker_for_net
change_dic["name"] = \
funds_type_df[funds_type_df["ticker"] == moneyfund_ticker_for_net]["name"].values.tolist()[
0]
change_dic["percent"] = 1 - total_net_percent
# change_dic["type"] = \
# funds_type_df[funds_type_df["ticker"] == moneyfund_ticker_for_net][
# "fund_type"].values.tolist()[0]
change_dic["type"] = funds_type_df[funds_type_df["ticker"] == moneyfund_ticker_for_net][
"fund_type"].values.tolist()[0]
change_dic["risk_type"] = userrisktype
change_dic["risk_score"] = userriskscore
combination_df_inside = combination_df_inside.append(change_dic, ignore_index=True)
current_return = new_return
old_type_weight_dic = type_weight_dic
elif (new_return - current_return) > change_return:
# elif zsmk.test_change_by_ratio(old_type_weight_dic,type_weight_dic,changeratio):
for fund, percent in funds_weight_dic.items():
change_dic = {}
change_dic["userid"] = userid
change_dic["date"] = endday_str
change_dic["ticker"] = fund
change_dic["name"] = funds_type_df[funds_type_df["ticker"] == fund]["name"].values.tolist()[
0]
# change_dic["type"] = \
# funds_type_df[funds_type_df["ticker"] == fund]["fund_type"].values.tolist()[0]
change_dic["type"] = {value[0]: key for key, value in type_fundticker_dic.items()}[fund]
change_dic["percent"] = float(percent) * total_net_percent
change_dic["risk_type"] = userrisktype
change_dic["risk_score"] = userriskscore
combination_df_inside = combination_df_inside.append(change_dic, ignore_index=True)
current_return = new_return
change_dic = {}
change_dic["userid"] = userid
change_dic["date"] = endday_str
change_dic["ticker"] = moneyfund_ticker_for_net
change_dic["name"] = \
funds_type_df[funds_type_df["ticker"] == moneyfund_ticker_for_net]["name"].values.tolist()[
0]
# change_dic["type"] = \
# funds_type_df[funds_type_df["ticker"] == moneyfund_ticker_for_net][
# "fund_type"].values.tolist()[
# 0]
change_dic["type"] = funds_type_df[funds_type_df["ticker"] == moneyfund_ticker_for_net][
"fund_type"].values.tolist()[0]
change_dic["percent"] = 1 - total_net_percent
change_dic["risk_type"] = userrisktype
change_dic["risk_score"] = userriskscore
combination_df_inside = combination_df_inside.append(change_dic, ignore_index=True)
old_type_weight_dic = type_weight_dic
zs_combination_df = zs_combination_df.append(combination_df_inside, ignore_index=True)
elapsed = (time.clock() - start)
time_cost += elapsed
print("Time used:", elapsed)
print("Time Left Estimated:", (time_cost / (int(usercount))) * len(user_detail_df) - time_cost)
zs_combination_df["userid"] = zs_combination_df["userid"].astype("int64")
zs_combination_df = zs_combination_df.sort_values(by=["userid", "date"])
output_filename = il.cwd + r"\result\\" + poctype + "_" + company_file + ".xls"
output_filename = output_filename.replace("_.", ".")
zs_combination_df.to_excel(output_filename)
print("File saved:", output_filename)
if __name__ == '__main__':
cwd = os.getcwd()
format = "%Y-%m-%d"
days_before = 90
userid = 1
riskfree = 0.03
combination_startdate = "2017-11-23"
combination_enddate = "2017-12-31"
datelist_out = rl.dateRange(combination_startdate, combination_enddate)
funds_net_df_out = il.getZS_funds_net(fill=False)
funds_profit_df = il.getZS_funds_Profit()
# user_detail_df = il.getZS_users_complete(os.getcwd() + r"\history_data\zs_user_test.csv")
user_detail_df = il.getZS_users_complete(cwd + r"\history_data\zs_user_change.csv")
minpercent = 0.05
change_return_differ_out = 0.05
poctype = "zs"
changeratio = 0.8
company_file = ["varindex-90-minpercnet" + str(minpercent) + "-change_return" + str(
change_return_differ_out) + "-indexcombine2-total-150",
"bdnindex-90-minpercnet" + str(minpercent) + "-change_return" + str(
change_return_differ_out) + "-indexcombine2-total-150"]
# company_file = ["varindex-90-minpercent0.1-changetest0.05"]
time_cost = 0
usercount = 0
date_count = 0
for symbolstr in company_file:
get_ZScom_for_users(poctype, symbolstr, user_detail_df[100:], datelist_out, days_before, funds_profit_df,
funds_net_df_out, riskfree,
minpercent, change_return_differ_out, "", changeratio)