def run_all_assets(self): for idx, asset in enumerate(self.sim_assets): output = self.load_curr_results(idx) if len(output.keys()) == len(self.scenarios): continue self.set_config(idx) self.load_data(idx) for ix, s in enumerate(self.scenarios): if str(ix) in output: continue res = {'asset': '_'.join(asset), 'scen_id': ix,\ 'sim_name': self.sim_name, 'sim_class': self.sim_class.__name__, 'sim_func': self.sim_func, 'end_date': str(self.config['end_date'])} for i in range(5): res['par_name' + str(i)] = '' res['par_value' + str(i)] = 0 for i, (key, seq) in enumerate(zip(self.scen_keys, s)): self.config[key] = self.scen_param[key][seq] res['par_name' + str(i)] = key res['par_value' + str(i)] = str(self.scen_param[key][seq]) self.prepare_data(idx, cont_idx=0) sim_strat = self.sim_class(self.config) sim_dfs, closed_trades = getattr(sim_strat, self.sim_func)() (res_pnl, ts) = get_pnl_stats(sim_dfs, self.config['marginrate'], self.sim_freq, self.pnl_tenors, cost_ratio=self.cost_ratio) res_trade = get_trade_stats(closed_trades) res.update(dict(res_pnl.items() + res_trade.items())) file_prefix = self.file_prefix + '_' + '_'.join( [self.sim_mode] + asset) res['trade_file'] = file_prefix + '_' + str(ix) + '_trades.csv' res['pnl_file'] = file_prefix + '_' + str( ix) + '_dailydata.csv' output[str(ix)] = res all_trades = {} for i, tradepos in enumerate(closed_trades): all_trades[i] = trade_position.tradepos2dict(tradepos) trades = pd.DataFrame.from_dict(all_trades).T trades.to_csv(res['trade_file']) ts.to_csv(res['pnl_file']) fname = file_prefix + '_stats.json' with open(fname, 'w') as ofile: json.dump(output, ofile) cnx = dbaccess.connect(**self.dbconfig) #cnx.set_converter_class(mysql_helper.NumpyMySQLConverter) dbaccess.insert_row_by_dict(cnx, self.dbtable, res, is_replace=True) cnx.close() print 'The results for asset = %s, scen = %s are saved' % ( asset, str(ix))
def run_all_assets(self): self.restart() for idx, asset in enumerate(self.sim_assets): output = self.load_curr_results(idx) if len(output.keys()) == len(self.scenarios): continue self.set_config(idx) self.load_data(idx) for ix, s in enumerate(self.scenarios): if str(ix) in output: continue file_prefix = self.file_prefix + '_' + '_'.join([self.sim_mode] + asset) fname1 = file_prefix + '_'+ str(ix) + '_trades.csv' fname2 = file_prefix + '_'+ str(ix) + '_dailydata.csv' for key, seq in zip(self.scen_keys, s): self.config[key] = self.scen_param[key][seq] self.prepare_data(idx, cont_idx = 0) sim_strat = self.sim_class(self.config) sim_df, closed_trades = getattr(sim_strat, self.sim_func)() (res_pnl, ts) = get_pnl_stats( [sim_df], self.config['marginrate'], 'm') res_trade = get_trade_stats(closed_trades) res = dict( res_pnl.items() + res_trade.items()) res.update(dict(zip(self.scen_keys, s))) res['asset'] = '_'.join(asset) output[ix] = res print 'saving results for asset = %s, scen = %s' % (asset, str(ix)) all_trades = {} for i, tradepos in enumerate(closed_trades): all_trades[i] = trade_position.tradepos2dict(tradepos) trades = pd.DataFrame.from_dict(all_trades).T trades.to_csv(fname1) ts.to_csv(fname2) fname = file_prefix + '_stats.json' with open(fname, 'w') as ofile: json.dump(output, ofile) res = pd.DataFrame.from_dict(output, orient = 'index') res.index.name = 'scenario' res = res.sort_values(by = ['sharp_ratio'], ascending=False) res = res.reset_index() res.set_index(['asset', 'scenario']) out_res = res[self.output_columns()] if len(self.summary_df)==0: self.summary_df = out_res[:30].copy(deep = True) else: self.summary_df = self.summary_df.append(out_res[:30]) fname = self.file_prefix + 'summary.csv' self.summary_df.to_csv(fname)
def run_all_assets(self): for idx, asset in enumerate(self.sim_assets): output = self.load_curr_results(idx) if len(output.keys()) == len(self.scenarios): continue self.set_config(idx) self.load_data(idx) for ix, s in enumerate(self.scenarios): if str(ix) in output: continue res = {'asset': '_'.join(asset), 'scen_id': ix,\ 'sim_name': self.sim_name, 'sim_class': self.sim_class.__name__, 'sim_func': self.sim_func, 'end_date': str(self.config['end_date'])} for i in range(5): res['par_name' + str(i)] = '' res['par_value' + str(i)] = 0 for i, (key, seq) in enumerate(zip(self.scen_keys, s)): self.config[key] = self.scen_param[key][seq] res['par_name' + str(i)] = key res['par_value' + str(i)] = str(self.scen_param[key][seq]) self.prepare_data(idx, cont_idx = 0) sim_strat = self.sim_class(self.config) sim_dfs, closed_trades = getattr(sim_strat, self.sim_func)() (res_pnl, ts) = get_pnl_stats( sim_dfs, self.config['marginrate'], self.sim_freq, self.pnl_tenors, cost_ratio = self.cost_ratio) res_trade = get_trade_stats(closed_trades) res.update(dict( res_pnl.items() + res_trade.items())) file_prefix = self.file_prefix + '_' + '_'.join([self.sim_mode] + asset) res['trade_file'] = file_prefix + '_'+ str(ix) + '_trades.csv' res['pnl_file'] = file_prefix + '_'+ str(ix) + '_dailydata.csv' output[str(ix)] = res all_trades = {} for i, tradepos in enumerate(closed_trades): all_trades[i] = trade_position.tradepos2dict(tradepos) trades = pd.DataFrame.from_dict(all_trades).T trades.to_csv(res['trade_file']) ts.to_csv(res['pnl_file']) fname = file_prefix + '_stats.json' with open(fname, 'w') as ofile: json.dump(output, ofile) cnx = dbaccess.connect(**self.dbconfig) #cnx.set_converter_class(mysql_helper.NumpyMySQLConverter) dbaccess.insert_row_by_dict(cnx, self.dbtable, res, is_replace=True) cnx.close() print 'The results for asset = %s, scen = %s are saved' % (asset, str(ix))
def run_all_assets(self): for idx, asset in enumerate(self.sim_assets): cont_map = self.cont_maplist[idx] output = self.load_curr_results(idx) if len(output) == 0: output = {'total': {}, 'cont': {}} elif len(output.keys()) == len(self.scenarios): continue self.set_config(idx) self.load_data(idx) for ix, s in enumerate(self.scenarios): file_prefix = self.file_prefix + '_' + '_'.join(self.sim_mode + asset) fname1 = file_prefix + str(ix) + '_trades.csv' fname2 = file_prefix + str(ix) + '_dailydata.csv' if os.path.isfile(fname1) and os.path.isfile(fname2): continue for key, seq in zip(self.scen_keys, s): self.config[key] = self.scen_param[key][seq] df_list = [] trade_list = [] for idy in range(abs(min(cont_map)), len(self.contlist[asset[0]]) - max(cont_map)): cont = self.contlist[asset[0]][idy] self.prepare_data(idx, cont_idx = idy) sim_strat = self.sim_class(self.config) sim_df, closed_trades = getattr(sim_strat, self.sim_func)() df_list.append(sim_df) trade_list = trade_list + closed_trades (res_pnl, ts) = get_pnl_stats( [sim_df], self.config['marginrate'], 'm') res_trade = get_trade_stats(closed_trades) res = dict( res_pnl.items() + res_trade.items()) res.update(dict(zip(self.scen_keys, s))) res['asset'] = cont if cont not in output['cont']: output['cont'][cont] = {} output['cont'][cont][ix] = res (res_pnl, ts) = get_pnl_stats(df_list, self.config['marginrate'], 'm') output[ix] = res res_trade = get_trade_stats(trade_list) res = dict(res_pnl.items() + res_trade.items()) res.update(dict(zip(self.scen_keys, s))) res['asset'] = '_'.join(asset) output['total'][ix] = res print 'saving results for asset = %s, scen = %s' % (asset, str(ix)) all_trades = {} for i, tradepos in enumerate(trade_list): all_trades[i] = trade_position.tradepos2dict(tradepos) trades = pd.DataFrame.from_dict(all_trades).T trades.to_csv(fname1) ts.to_csv(fname2) fname = file_prefix + '_stats.json' with open(fname, 'w') as ofile: json.dump(output, ofile) cont_df = pd.DataFrame() for idy in range(abs(min(cont_map)), len(self.contlist[asset[0]]) - max(cont_map)): cont = self.contlist[asset[0]][idy] if cont not in output['cont']: continue res = scen_dict_to_df(output['cont'][cont]) out_res = res[self.output_columns()] if len(cont_df) == 0: cont_df = out_res[:30].copy(deep=True) else: cont_df = cont_df.append(out_res[:30]) fname = file_prefix + 'cont_stat.csv' cont_df.to_csv(fname) res = scen_dict_to_df(output['total']) out_res = res[self.output_columns()] if len(summary_df)==0: summary_df = out_res[:30].copy(deep = True) else: summary_df = summary_df.append(out_res[:30]) fname = self.file_prefix + 'summary.csv' summary_df.to_csv(fname)
def run_all_assets(self): for idx, asset in enumerate(self.sim_assets): cont_map = self.cont_maplist[idx] output = self.load_curr_results(idx) if len(output) == 0: output = {'total': {}, 'cont': {}} elif len(output.keys()) == len(self.scenarios): continue self.set_config(idx) self.load_data(idx) for ix, s in enumerate(self.scenarios): file_prefix = self.file_prefix + '_' + '_'.join(self.sim_mode + asset) fname1 = file_prefix + str(ix) + '_trades.csv' fname2 = file_prefix + str(ix) + '_dailydata.csv' if os.path.isfile(fname1) and os.path.isfile(fname2): continue for key, seq in zip(self.scen_keys, s): self.config[key] = self.scen_param[key][seq] df_list = [] trade_list = [] for idy in range(abs(min(cont_map)), len(self.contlist[asset[0]]) - max(cont_map)): cont = self.contlist[asset[0]][idy] self.prepare_data(idx, cont_idx = idy) sim_strat = self.sim_class(self.config) sim_df, closed_trades = getattr(sim_strat, self.sim_func)() df_list.append(sim_df) trade_list = trade_list + closed_trades (res_pnl, ts) = get_pnl_stats( sim_df, self.config['marginrate'], 'm', cost_ratio = self.cost_ratio) res_trade = get_trade_stats(closed_trades) res = dict( res_pnl.items() + res_trade.items()) res.update(dict(zip(self.scen_keys, s))) res['asset'] = cont if cont not in output['cont']: output['cont'][cont] = {} output['cont'][cont][ix] = res (res_pnl, ts) = get_pnl_stats(df_list, self.config['marginrate'], 'm', cost_ratio = self.cost_ratio) output[ix] = res res_trade = get_trade_stats(trade_list) res = dict(res_pnl.items() + res_trade.items()) res.update(dict(zip(self.scen_keys, s))) res['asset'] = '_'.join(asset) output['total'][ix] = res print 'saving results for asset = %s, scen = %s' % (asset, str(ix)) all_trades = {} for i, tradepos in enumerate(trade_list): all_trades[i] = trade_position.tradepos2dict(tradepos) trades = pd.DataFrame.from_dict(all_trades).T trades.to_csv(fname1) ts.to_csv(fname2) fname = file_prefix + '_stats.json' with open(fname, 'w') as ofile: json.dump(output, ofile) cont_df = pd.DataFrame() for idy in range(abs(min(cont_map)), len(self.contlist[asset[0]]) - max(cont_map)): cont = self.contlist[asset[0]][idy] if cont not in output['cont']: continue res = scen_dict_to_df(output['cont'][cont]) out_res = res[self.output_columns()] if len(cont_df) == 0: cont_df = out_res[:30].copy(deep=True) else: cont_df = cont_df.append(out_res[:30]) fname = file_prefix + 'cont_stat.csv' cont_df.to_csv(fname) res = scen_dict_to_df(output['total']) out_res = res[self.output_columns()] if len(summary_df)==0: summary_df = out_res[:30].copy(deep = True) else: summary_df = summary_df.append(out_res[:30]) fname = self.file_prefix + 'summary.csv' summary_df.to_csv(fname)