record_with_id['aggregate_returns'] = backtest_statistics['aggregate_returns'].tolist() record_with_id['returns'] = backtest_statistics['transaction_adjusted_returns'].tolist() record_with_id['statistics'] = backtest_statistics['statistics'] return record # Generate backtests. for er_idx in range(len(ers)): print('er_idx = {0}'.format(er_idx)) er = ers[er_idx] for mv_idx in range(len(mv_weights)): print('mv_idx = {0}'.format(mv_idx)) w = mv_weights[mv_idx] mvu, mvw = rkw_rolling_backtest(gen_mvportfolio(w), er) mvstats = rkw_statistics(mvu, mvw) record = dict() record['id'] = '20160910_er_{0}_mv_{1}'.format(er_idx, mv_idx) record['er_idx'] = er_idx record['mv_idx'] = mv_idx record['portfolio_name'] = 'mean-variance' record = write_backtest_results_record( record, mvu, mvw, mvstats) db.insert(record) for eta_idx in range(len(etas)): # print 'eta_idx = {0}'.format(eta_idx) w = msv_parameters[er_idx][mv_idx][eta_idx]['weight'] eta = msv_parameters[er_idx][mv_idx][eta_idx]['eta'] error = msv_parameters[er_idx][mv_idx][eta_idx]['error'] msvu, msvw = rkw_rolling_backtest(gen_msvportfolio(w, 1.0, eta), er)
results = bt[1] experiment = db.load_experiment(experiment_id) backtest_params = db.load_backtests([backtest_id]) returns = db.load_returns(experiment) excess_returns = returns.values train, test = backtest.split_returns( excess_returns, backtest_params['update_periods'], backtest_params['trailing_periods']) portfolios = pg.decimallistseries2nparray(results.portfolio_weights) portfolio_returns = pg.decimallistseries2nparray(results.test_returns) train_mean = ps.mean(portfolios[0,:], train[0]) train_variance = ps.variance(portfolios[0,:], train[0]) eta = results.eta.iloc[0] backtest_statistics = ps.rkw_statistics( portfolios, portfolio_returns, eta=eta) test_statistics = backtest_statistics['statistics'] agg_returns = backtest_statistics['aggregate_returns'] output = test_statistics output['aggregate_returns'] = pg.array2pgarray(agg_returns) output['train_mean_return'] = train_mean output['train_variance'] = train_variance output['parameters_id'] = parameters_id output['experiment_id'] = experiment_id output['backtest_id'] = backtest_id outputs.append(output) output_table = pd.DataFrame(outputs) output_table.to_csv('dcc_backtest_statistics.csv', index=False)