basic_tickers = ['SHY','SPY','XLB','XLE','XLF','XLI','XLK','XLP','XLU','XLV','XLY'] etf_tickers = basic_tickers mc_budget = [0.8, 0.2] if len(etf_tickers)>2: for e in range(2,len(etf_tickers)): mc_budget.append(0.0) pl=read_csv(etf_tickers) #周线 wp = pl.resample('W', loffset=pd.offsets.timedelta(days=0)).last().dropna() # 选择时间 end_year = 2019 start_year = 2004 hist_p = wp[wp.index.year<=end_year] hist_p = hist_p[hist_p.index.year>=start_year] evaluated_data = hist_p.copy() hist_p = wp[wp.index.year<start_year] supporting_data = hist_p.copy() # back testing # Performance for Strategy start_time = process_time() pfm, s, w, accumulated_return, total_return, \ annual_return, annual_std, sharpe_ratio, \ max_loss, indicators = get_performance(\ supporting_data, evaluated_data, mc_budget, get_shares) print('Running Time: ', process_time()-start_time) s_d = s.diff() s_d.iloc[0] = s.iloc[0] s_d = s_d.drop(['Cash'], axis=1) #s_d = s_d.drop(s_d.index[-1]) share_file = get_filename(start_year, end_year, 'xlsx', 'Reporting\\results') s_d.to_excel(share_file, encoding='utf-8')
0.00] # pl = read_csv(etf_tickers, end_date) #周线 wp = pl.resample('W', loffset=pd.offsets.timedelta(days=-6)).mean().dropna() # 选择时间 end_year = 2018 start_year = 2004 hist_p = wp[wp.index.year <= end_year] hist_p = hist_p[hist_p.index.year >= start_year] evaluated_data = hist_p.copy() hist_p = wp[wp.index.year < start_year] supporting_data = hist_p.copy() # back testing # Performance for Strategy start_time = process_time() pfm, s, w, accumulated_return, total_return, annual_return, annual_std, sharpe_ratio, max_loss, indicators = get_performance( supporting_data, evaluated_data, mc_budget, get_shares) print('Running Time: ', process_time() - start_time) result = display_performance(w, accumulated_return, total_return, annual_return, annual_std, sharpe_ratio, max_loss, annual=0) figname = get_figname(start_year, end_year) show_indicators = indicators[indicators.columns[:2]] plot_performance(w, accumulated_return, show_indicators, figname) csv_file = get_figname(start_year, end_year, 'csv') csv_performance(csv_file, 'w',
portfolio = 'Portfolio' #周线 wp = pl.resample('W', loffset=pd.offsets.timedelta(days=-6)).mean().dropna() # 准备测试数据 end_year = 2018 start_year = 2013 hist_p = wp[wp.index.year<=end_year] hist_p = hist_p[hist_p.index.year>=start_year] evaluated_data = hist_p.copy() hist_p = wp[wp.index.year<start_year] supporting_data = hist_p.copy() # Performance for Strategy start_time = process_time() pfm, s, w, accumulated_return, total_return, annual_return, annual_std, sharp_ratio, max_loss, risk_bias = get_performance(supporting_data, evaluated_data, get_shares) print('Running Time: ', process_time()-start_time, 's') print('Total Risk Bias: ', risk_bias) title = display_performance(pfm, s, w, accumulated_return, total_return, annual_return, annual_std, sharp_ratio, max_loss, annual=0) figname = get_figname(start_year, end_year) plot_performance(w, accumulated_return, figname) #pfm.to_csv('checking1.csv') #s.to_csv('checking2.csv') #w.to_csv('checking3.csv') #accumulated_return.to_csv('checking4.csv') ''' sy = start_year ey = end_year for i in range(sy,ey+1): end_year = i start_year = i