Commit 72357017 authored by 赵杰's avatar 赵杰

新版本

parent b587313d
This diff is collapsed.
......@@ -172,7 +172,7 @@ def single_evaluation(fund_id, invest_type=2, index_id='000905.SH'):
return evaluation_dict
def fund_index_compare(fund_id, invest_type=2, index_id='000905.SH'):
def fund_index_compare(fund_id, invest_type=2, index_id='4000233'):
start_date = datetime.datetime(2000, 1, 1)
index_daily = get_index_daily(index_id, start_date)
fund_nav = get_tamp_nav(fund_id, start_date, invest_type=invest_type)
......
......@@ -203,14 +203,12 @@ def get_index_daily(index_id, start_date):
Returns:与组合净值形式相同的表
"""
with TAMP_SQL(tamp_fund_engine) as tamp_product:
tamp_product_session = tamp_product.session
sql = "SELECT ts_code, trade_date, close FROM index_daily " \
"WHERE ts_code='{}' AND trade_date>'{}'".format(index_id, start_date)
# df = pd.read_sql(sql, con).dropna(how='any')
cur = tamp_product_session.execute(sql)
with TAMP_SQL(tamp_fund_engine) as tamp_fund:
tamp_fund_session = tamp_fund.session
sql = "select DISTINCT `index_id`, `price_date`, `close` from tx_fund_market_indexes where index_id='{}' order by price_date ASC".format(
index_id)
cur = tamp_fund_session.execute(sql)
data = cur.fetchall()
df = pd.DataFrame(list(data), columns=['ts_code', 'trade_date', 'close'])
df.rename({'ts_code': 'fund_id', 'trade_date': 'end_date', 'close': 'adj_nav'}, axis=1, inplace=True)
df['end_date'] = pd.to_datetime(df['end_date'])
......@@ -490,7 +488,7 @@ tamp_fund = get_tamp_fund()
class PortfolioDiagnose(object):
def __init__(self, client_type, portfolio, invest_amount, expect_return=0.1,
expect_drawdown=0.15, index_id='000905.SH', invest_type='private', start_date=None, end_date=None):
expect_drawdown=0.15, index_id='4000233', invest_type='private', start_date=None, end_date=None):
"""基金诊断
Args:
......@@ -519,7 +517,8 @@ class PortfolioDiagnose(object):
if self.end_date is None:
self.end_date = datetime.datetime(datetime.date.today().year,
datetime.date.today().month, 1) - datetime.timedelta(1)
datetime.date.today().month,
datetime.date.today().day)
if self.start_date is None:
self.start_date = cal_date(self.end_date, 'Y', 1)
else:
......@@ -536,6 +535,7 @@ class PortfolioDiagnose(object):
self.origin_portfolio = None
self.abandoned_portfolio = None
self.propose_portfolio = None
self.ori_prod = None
def get_portfolio(self, ):
"""获取组合净值表
......@@ -734,8 +734,7 @@ class PortfolioDiagnose(object):
prod[proposal] = proposal_nav[proposal]
self.proposal_fund.append(proposal)
prod.ffill(inplace=True)
prod = prod[prod.index >= self.start_date]
prod = prod[(prod.index >= self.start_date) & (prod.index <= self.end_date)]
prod = resample(prod, get_trade_cal(), min(self.freq_list))
if 'cal_date' in prod.columns:
prod.drop(labels='cal_date', inplace=True, axis=1)
......@@ -744,6 +743,8 @@ class PortfolioDiagnose(object):
self.new_correlation = cal_correlation(prod)
prod.dropna(how='all', inplace=True)
prod.ffill(inplace=True)
self.ori_prod = prod.copy()
prod.fillna(method="bfill", inplace=True)
self.new_correlation = self.new_correlation.fillna(1).round(2)
self.new_correlation.columns = self.new_correlation.columns.map(lambda x: get_fund_name(x, suggest_fund_type[x]).values[0][0])
......@@ -863,12 +864,20 @@ class PortfolioDiagnose(object):
print("模型计算一次时间:", end4 - start)
def return_compare(self):
weight_prod = self.ori_prod.copy()
for col in weight_prod.columns:
weight_prod[col] = weight_prod[col].apply(lambda x: 0 if math.isnan(x) else 1)
weight_prod = weight_prod * self.new_weights
weight_prod = weight_prod.apply(lambda x: x/x.sum(), axis=1)
index_data = get_index_daily(self.index_id, self.start_date)
index_data = pd.merge(index_data, self.propose_portfolio, how='inner', left_index=True, right_index=True)
index_return = index_data.iloc[:, :] / index_data.iloc[0, :] - 1
# origin_fund_return = origin_portfolio.iloc[:, :] / origin_portfolio.iloc[0, :] - 1
propose_fund_return = self.propose_portfolio.iloc[:, :] / self.propose_portfolio.iloc[0, :] - 1
propose_fund_return['return'] = propose_fund_return.T.iloc[:, :].apply(lambda x: np.dot(self.new_weights, x))
pct_change = self.propose_portfolio.pct_change().fillna(0) * weight_prod
propose_fund_return['return'] = (pct_change.sum(axis=1)+1).cumprod() - 1
# propose_fund_return['return'] = propose_fund_return.T.iloc[:, :].apply(lambda x: np.dot(self.new_weights, x))
return index_return, propose_fund_return
def old_evaluation(self, group_name, group_result, data_adaptor):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment