def plot_visibility(simulation_names, labels, i_wlen, wlen, star_name, title="Visibilities for different wavelenghts"): vis_dir = u.make_dir(visibility_dir, star_name) fig_dir = u.make_dir(figures_dir, star_name) for k in range(len(simulation_names)): simulation_name = simulation_names[k] label = labels[k] bands = [[]] * (len(i_wlen)) x = np.empty(0) filename = vis_dir + 'visibilities_' + simulation_name + star_name + '.csv'; with open(filename, 'rt') as csvfile: reader = csv.reader(csvfile, delimiter=",", lineterminator='\n') next(reader) for row in reader: x = np.append(x, [float(row[0])]) for i in range(len(i_wlen)): bands[i] = np.append(bands[i], [float(row[i_wlen[i]+1])]) for j in range(len(i_wlen)): plt.plot(x, bands[j], label=label) plt.grid(b=True, which='major', color='b', linestyle='--', alpha=0.2) plt.xticks(np.arange(0, 200, step=20)) plt.xlabel('Baseline ($m$)') plt.xlim([0, 200]) plt.ylabel('Visibility') plt.ylim([0, 1]) plt.legend() plt.savefig(fig_dir + u.clean_filename(title) + ".png") plt.title(title) plt.savefig(u.make_dir(fig_dir, 'title') + u.clean_filename(title) + ".png")
def plot_brightness(simulation_names, labels, i_wlen, title="Monochromatic flux"): fig = plt.figure() xi = np.linspace(-5, 5, 100) date_dir = u.make_dir(figures_dir, 'brightness') for i in range(len(simulation_names)): simulation_name = simulation_names[i] mono_file = fits.open(results_dir + simulation_name + '_mono_continuum_map_mono.fits.gz') raytrace_file = fits.open(results_dir + simulation_name + '_mono_continuum_map_raytrace.fits.gz') data_mono = mono_file[0].data[0][i_wlen] data_raytrace = raytrace_file[0].data[0][i_wlen] mono_file.close() raytrace_file.close() data = data_mono + data_raytrace del data_raytrace del data_mono data = data * u.get_factor(simulation_name) plt.semilogy(xi, data[200][150:250], '-o', label=labels[i]) plt.axhline(1e-2 * np.max(data), color="black") plt.text(-5, 1e-2 * np.max(data), "1:100 " + labels[i], color="black", weight="bold", ha="left", va="bottom", fontsize=12) del data plt.ylim(bottom=1) plt.xlim([-5, 5]) plt.xlabel('$\\rm{x [AU]}$') plt.ylabel('$\\rm{S [Jy/arcsec^{2}]}$') plt.xticks(np.linspace(-5, 5, 11)) plt.legend() num = uuid.uuid4() plt.savefig(date_dir + u.clean_filename(title) + num + ".png") plt.title(title) plt.savefig(u.make_dir(date_dir, 'title') + u.clean_filename(title) + num + ".png")
def plot_temperatures(simulation_names, labels, title="Temperature profiles"): dir = u.make_dir(figures_dir, 'temperatures') plt.figure() xi = np.linspace(0, 200, 100) for i in range(len(simulation_names)): simulation_name = simulation_names[i] plt.semilogy(xi, get_t_profile(simulation_name), label=labels[i]) plt.xlabel('$x [\\rm{AU}]$') plt.ylabel('$T [\\rm{K}]$') plt.legend() num = str(uuid.uuid4()) plt.savefig(dir + u.clean_filename(title) + num + ".png") plt.suptitle(title) plt.savefig(u.make_dir(dir, 'title') + u.clean_filename(title) + num + ".png")
def calculate_visibility(simulation_name, i_wlen, wlen, star_name, map_rad): # Visibility calculation mono_file = fits.open(results_dir + simulation_name + '_mono_continuum_map_mono.fits.gz') raytrace_file = fits.open(results_dir + simulation_name + '_mono_continuum_map_raytrace.fits.gz') data_mono = mono_file[0].data[0][i_wlen] data_raytrace = raytrace_file[0].data[0][i_wlen] mono_file.close() raytrace_file.close() data = data_mono + data_raytrace if (not os.path.isfile(results_dir + simulation_name + '_mono_continuum_map_total.fits.gz')): hdu = fits.PrimaryHDU(data) hdul = fits.HDUList([hdu]) hdul.writeto(results_dir + simulation_name + '_mono_continuum_map_total.fits.gz') del data_raytrace del data_mono dir = u.make_dir(visibility_dir, star_name) filename = dir + 'visibilities_' + simulation_name + star_name + '.csv'; with open(filename, 'w') as fd: writer = csv.writer(fd, delimiter=",", lineterminator='\n') writer.writerow(np.concatenate([['baseline'], wlen])) writer.writerow(np.concatenate([[0], [1] * len(wlen)])) for i in range(5, 205, 5): visibility, phases, maps = it.get_visibility_2D(np.array([0]), np.array([i]), wlen, data, map_rad) writer.writerow(np.concatenate([[i], visibility[:, 0]])) del data
def plot_brightness_2D( simulation_names, labels, i_wlen, title="Monochromatic flux for two different star luminosities at $\\rm{\lambda =%s \mu m}$", bins=100 ): n = len(simulation_names)//2 fig, axs = plt.subplots(ncols=2, nrows=n, sharex=True, sharey=True, subplot_kw={'aspect':'equal', 'autoscale_on' : True}) xi = np.linspace(-5, 5, 100) dir = u.make_dir(figures_dir, 'brightness_2D') for i in range(len(simulation_names)): simulation_name = simulation_names[i] mono_file = fits.open(results_dir + simulation_name + '_mono_continuum_map_mono.fits.gz') raytrace_file = fits.open(results_dir + simulation_name + '_mono_continuum_map_raytrace.fits.gz') data_mono = mono_file[0].data[0][i_wlen] data_raytrace = raytrace_file[0].data[0][i_wlen] mono_file.close() raytrace_file.close() data = data_mono + data_raytrace del data_raytrace del data_mono data = data * u.get_factor(simulation_names[i]) im = axs[i//2, i % 2].contourf(xi, xi, data[150:250, 150:250], locator=ticker.MaxNLocator(bins)) axs[i//2, i % 2].set_title(labels[i]) axs[i // 2, i % 2].set_ylabel('$y [\\rm{AU}]$') axs[i // 2, i % 2].set_ylabel('$x [\\rm{AU}]$') del data # COLORBAR fig.subplots_adjust(right=0.8) cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7]) fig.colorbar(im, cax=cbar_ax) clb_ax.set_title('$\\rm{S [Jy/arcsec^{2}]}$') num = str(uuid.uuid4()) fig.savefig(dir + u.clean_filename(title) + num + ".png") fig.suptitle(title) fig.savefig(u.make_dir(dir, 'title') + u.clean_filename(title) + num + ".png")
def copy_files(strategy): original_name = concat_strategy_name(strategy, start) original_strategy_path = results_path + original_name single_results_original = results_path + original_name + '/single_results.csv' new_name = concat_strategy_name(strategy, new_start) new_strategy_path = results_path + new_name util.make_dir(new_strategy_path) single_results_new = new_strategy_path + '/single_results.csv' shutil.copyfile(single_results_original, single_results_new) single_results_df = pd.read_csv(single_results_new) for strategy_code in single_results_df.strategy_code.unique(): original_strategy_code_path = original_strategy_path + \ '/daily_pnls/' + strategy_code + '/' new_strategy_dailies_path = new_strategy_path + \ '/daily_pnls/' util.make_dir(new_strategy_dailies_path) new_strategy_code_path = new_strategy_dailies_path + strategy_code + '/' util.make_dir(new_strategy_code_path) for filename in os.listdir(original_strategy_code_path): if filename.endswith('.csv'): daily_pnls = pd.read_csv(original_strategy_code_path + filename, parse_dates=['date'], index_col=['date']) if (daily_pnls.index[0].date() >= new_start): shutil.copyfile(original_strategy_code_path + filename, new_strategy_code_path + filename)
def backtest(strategy, underlying, strategy_name, risk_capital, quantity, start, end, parameters, frequency_string="m"): if util.printalot: print('strategy_name: ' + str(strategy_name)) if util.printalot: print('risk_capital: ' + str(risk_capital)) if util.printalot: print('underlying: ' + str(underlying)) if util.printalot: print('start: ' + str(start)) if util.printalot: print('end: ' + str(end)) if util.printalot: print() # prepare if util.printalot: print('number of combinations: ' + str(len(list(dict_product(parameters))))) for permutation in dict_product(parameters): print(util.derive_strategy_code(permutation, parameters)) permutations = dict_product(parameters) # create directories path = os.getcwd() util.make_dir(path + '/results') strategy_path = path + '/results/' + strategy_name util.make_dir(strategy_path) try: shutil.rmtree(strategy_path + '/daily_pnls') except Exception as e: print(e) util.make_dir(strategy_path + '/daily_pnls') run_strategies(permutations, strategy_name, parameters, strategy_path, frequency_string, underlying, start, end, strategy, risk_capital, quantity)
def plot_visibility_for_wavelength(simulation_names, labels, star_name, wlen): vis_dir = u.make_dir(visibility_dir, star_name) for k in range(len(simulation_names)): simulation_name = simulation_names[k] label = labels[k] filename = vis_dir + 'visibilities_' + simulation_name + star_name + '.csv'; if os.path.isfile(filename): data = np.genfromtxt(filename, delimiter=",") index = np.where(data[0, :]==wlen) x = data[1:, 0] y = data[1:, index[0][0]] print(x, y) plt.plot(x, y, label = label) else: print("Warning! File not found: " + filename)
def plot_visibility_for_baseline(simulation_names, labels, star_name, baseline, wlen=[]): vis_dir = u.make_dir(visibility_dir, star_name) for k in range(len(simulation_names)): simulation_name = simulation_names[k] label = labels[k] ln = np.empty(0) filename = vis_dir + 'visibilities_' + simulation_name + star_name + '.csv'; if os.path.isfile(filename): data = np.genfromtxt(filename, delimiter=",") index = np.where(data[:, 0]==baseline) x = data[0, 1:] for l in wlen: index2 = np.where(data[0, :]==l) ln = np.append(ln, data[index, index2]) y = data[index, 1:][0][0] plt.plot(x, y, label = label) plt.plot(wlen, ln, '--') else: print("Warning! File not found: " + filename)
def add_underlying(start, end, underlying, risk_capital, strategy_name): # create results file and trade log entry for underlying underlying_daily_pnls_dict = {} underlying_date = start underlying_multiplier = None underlying_at_entry = None entry_vix = None previouspnl = 0 while (underlying_date <= end): while ((xnys.is_session(pd.Timestamp(underlying_date, tz=pytz.UTC)) is False) or (util.connector.query_midprice_underlying(underlying, underlying_date) is None)): underlying_date = underlying_date + timedelta(days=1) if (underlying_date >= end) or (underlying_date >= datetime.now().date()): break underlying_midprice = util.connector.query_midprice_underlying( underlying, underlying_date) if underlying_midprice !=None and underlying_midprice > 0: if underlying_multiplier is None: underlying_start_date = underlying_date underlying_at_entry = underlying_midprice underlying_multiplier = (risk_capital / underlying_midprice) entry_vix = util.connector.query_midprice_underlying( "^VIX", underlying_date) current_pnl = (underlying_multiplier * underlying_midprice) - risk_capital if current_pnl != None: underlying_daily_pnls_dict[underlying_date] = format( float(current_pnl - previouspnl), '.2f') previouspnl = current_pnl underlying_date = underlying_date + timedelta(days=1) # save results underlying_daily_pnls = pd.DataFrame.from_dict( underlying_daily_pnls_dict, orient='index') underlying_daily_pnls = underlying_daily_pnls.reindex( underlying_daily_pnls.index.rename('date')) underlying_daily_pnls.index = pd.to_datetime(underlying_daily_pnls.index) underlying_daily_pnls.sort_index(inplace=True) underlying_daily_pnls.columns = ['pnl'] underlying_strategy_code_path = cwd + '/results/' + strategy_name + \ '/daily_pnls/' + str(underlying.replace("^", "")) + "/" util.make_dir(underlying_strategy_code_path) underlying_file_name = underlying_strategy_code_path + 'underlying.csv' underlying_daily_pnls.to_csv(underlying_file_name) # add to single_results single_results_file = cwd + '/results/' + strategy_name + '/single_results.csv' single_results_df = pd.read_csv( single_results_file, parse_dates=['entry_date'], index_col=0) # if row exists delete it if single_results_df.tail(1)['strategy_code'].values[0] == "RUT": single_results_df.drop(single_results_df.tail(1).index, inplace=True) # save underlying to trade_log trade_log = dict({'trade nr.': 0, 'strategy_code': str(underlying.replace("^", "")), 'entry_date': start, 'expiration': None, 'exit_date': underlying_date, 'entry_underlying': str(format(float(underlying_at_entry), '.2f')), 'entry_vix': entry_vix, 'strikes': None, 'iv_legs': None, 'entry_price': str(format( float(risk_capital), '.2f')), 'dte': 0, 'dit': (end - start).days, 'pnl': str(format(float(current_pnl), '.2f')), 'dailypnls': None, 'max_risk': str(format(float(risk_capital), '.2f')), 'position_size': underlying_multiplier, 'percentage': str(format(float(round((float(current_pnl) / risk_capital) * 100, 2)), '.2f')) + '%', 'exit': None}) single_results_df = single_results_df.append(trade_log, ignore_index=True) single_results_df.to_csv(single_results_file)
def run_strategies(permutations, strategy_name, parameters, strategy_path, frequency_string, underlying, start, end, strategy, risk_capital, quantity): trade_log = {} i = 0 for permutation in permutations: running = True for k, v in permutation.items(): if util.printalot: print(k, v) if (strategy_name == 'bf70' or strategy_name == 'bf70_plus') and (permutation['cheap_entry'] == None) and ( permutation['down_day_entry'] == False) and (permutation['patient_entry'] == True): if util.printalot: print('continue') continue strategy_code = util.derive_strategy_code(permutation, parameters) print(strategy_code) # make dir for permutation strategy_code_path = strategy_path + '/daily_pnls/' + strategy_code + "/" util.make_dir(strategy_code_path) single_entries = None # get entries, measure time starttime = time.time() if frequency_string == 'c': single_entries = {} if frequency_string == 'b': single_entries = entries.getDailyEntries(underlying, start, end, permutation['dte_entry']) if frequency_string == 'sms': single_entries = entries.getSMSEntries(underlying, start, end, permutation['dte_entry']) if frequency_string == 'm': single_entries = entries.getEntries(underlying, start, end, permutation['dte_entry']) if single_entries is None: print("frequency string not known: " + frequency_string) return False print('time needed to get entries: ' + format(float(time.time() - starttime), '.2f')) # loop through entries trade_nr = 0 index_nr = 0 next_date = None while (running): if next_date is None: next_date = start entry = get_next_entry(index_nr, frequency_string, single_entries, next_date, end, underlying, permutation['dte_entry']) if entry is not None: entrydate = entry[0] expiration = entry[1] if entrydate >= (datetime.now().date() - timedelta(days=7)): break if strategy_name.startswith('the_bull'): permutation['dte_exit'] = 37 try: next_entry = get_next_entry(index_nr + 1, frequency_string, single_entries, next_date, end, underlying, permutation['dte_entry']) if next_entry is not None: permutation['dte_exit'] = 66 - (next_entry[0] - entrydate).days except Exception as e: print(e) # run with parameters strategy.setParameters(permutation) if ((strategy_name.startswith('bf70') and (frequency_string == 'c'))): strategy.patient_days_before = 0 result = run_strategy.fly(strategy, underlying, risk_capital, quantity, entrydate, expiration) if (not result is None): trade_nr += 1 i += 1 # save dailypnls file_name = strategy_code_path + str(i) + '.csv' result['dailypnls'].to_csv(file_name) del result['dailypnls'] trade_log[i] = dict( { 'trade nr.': trade_nr, 'strategy_code': strategy_code }, **result) print(trade_log[i]) next_date = (result["exit_date"]) + timedelta(days=1) else: next_date = next_date + timedelta(days=1) index_nr += 1 else: running = False # finished looping, save trade_log df_log = pd.DataFrame.from_dict(trade_log, orient='index') df_log.to_csv(strategy_path + '/single_results.csv')
type=int, help='epoch nums (default: %(default)s)') return parser if __name__ == '__main__': parser = get_parser() args = parser.parse_args() kwargs = (args.data_type, args.net_name, args.batch_norm, args.bound, args.reg_cf, args.lr, args.batch_size) args.sub_path = '{}_{}_{}_bound_{:4.2e}_reg_cf_{:4.2e}_lr_{:4.2e}_batch_{}'.format( *kwargs) print('setting as follows') for arg in vars(args): print('{} : {}'.format(arg, getattr(args, arg))) try: run_func(args) except Exception as e: from config.config import DEFAULT_PATH from util.util import make_dir error_log_dir = DEFAULT_PATH + '/' + args.sub_path error_log_fn = error_log_dir + '/error.txt' make_dir(error_log_dir) with open(error_log_fn, 'w') as f: f.write(str(e)) raise e