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')
Exemplo n.º 2
0
# 4. calc new value with new trades
# shares, cash and value at t = 0 at delta_s[0]'s date
# move date by date, calc value for each date
# if reach delta_s.date, read the delta_s and wait placement
# if placement, write transaction slip and update shares, cash and value
# move date by date
start_time = process_time()
mapping_date, mapping_shares, mapping_weights, \
    mapping_closes, mapping_cash, mapping_value = \
    mapping(tickers, delta_s, tickers_pl, init_cash, \
    trans_dates, trans_prices, shares, residual_cashs, 'T')
#print(mapping_cash)
portfolio = get_portfolio(tickers, mapping_date, \
    mapping_closes, mapping_cash, mapping_value)
weights = get_pd(tickers, mapping_date, mapping_weights)
print('4. Mapping Running Time: ', process_time()-start_time)
#
start_year = portfolio.index[0].year
end_year = portfolio.index[-1].year
csv_file = get_filename(start_year, end_year, 'csv', 'Reporting\\results')
annual_r, annual_std, total_r, sharpe_ratio, max_loss = get_performance(portfolio)
csv_performance(csv_file, 'w', portfolio, \
                total_r, annual_r, annual_std, sharpe_ratio, max_loss, 0)
# Performance for each year
for n in range(start_year, end_year+1):
    p_n = portfolio[portfolio.index.year==n]
    annual_r, annual_std, total_r, sharpe_ratio, max_loss = get_performance(p_n)
    csv_performance(csv_file, 'a', p_n, \
                    total_r, annual_r, annual_std, sharpe_ratio, max_loss)
fig_file = get_filename(start_year, end_year, 'jpg', 'Reporting\\results')
plot_performance(portfolio, weights, None, None, fig_file)
Exemplo n.º 3
0
# 3.2 scenario two: input transaction prices

# 4. calc new value with new trades
# shares, cash and value at t = 0 at delta_s[0]'s date
# move date by date, calc value for each date
# if reach delta_s.date, read the delta_s and wait placement
# if placement, write transaction slip and update shares, cash and value
# move date by date
start_time = process_time()
mapping_date, mapping_shares, mapping_weights, \
    mapping_closes, mapping_cash, mapping_value = \
    mapping(tickers, delta_s, tickers_pl, init_cash, \
    trans_dates, trans_prices, shares, residual_cashs, 'T')
portfolio = get_portfolio(tickers, mapping_date, \
    mapping_closes, mapping_cash, mapping_value)
weights = get_pd(tickers, mapping_date, mapping_weights)
print('4. Mapping Running Time: ', process_time() - start_time)
#
annual_r, annual_std, total_r, sharpe_ratio, max_loss \
    = get_performance(portfolio)
csv_file = get_filename(portfolio.index[0].year, \
                       portfolio.index[-1].year, \
                       'csv', \
                       'Reporting\\results')
csv_performance(csv_file, 'w', portfolio, total_r, annual_r, annual_std, \
                sharpe_ratio, max_loss, 0)
fig_file = get_filename(portfolio.index[0].year, \
                       portfolio.index[-1].year, \
                       'jpg', \
                       'Reporting\\results')
plot_performance(portfolio, weights, None, None, fig_file)