def get_first_round_to_reach_new_fundamental(trade_prices, trade_rounds): trade_prices = np.array(trade_prices) fas = get_fundamental_after_shock() try: first_round = np.min(trade_rounds[np.where(trade_prices == fas)]) except ValueError: first_round = 10**6 return first_round
def make_tradeprice_plot(rounds, prices, fit, par, filename): fig = plt.figure(figsize=(10, 8), dpi=100) ax = fig.add_axes([0.1, 0.3, 0.8, 0.6]) ax.set_xlabel("Round") ax.set_ylabel("Traded price") caption = "\n".join(textwrap.wrap(repr([(k,par[k]) for k in settings.parameters_in_genes]), 100)) caption += '\nFitness: %s'%str(settings.fitness_weights.keys()) caption += '\nFitness: %s'%str(fit) fig.text(0.1,0.1, caption) ax.plot(rounds, prices, lw=2) fas = get_fundamental_after_shock() ax.hlines([fas - settings.stability_margin, fas + settings.stability_margin], 0, settings.n_simulation_rounds) print "Saving plot to %s"%filename fig.savefig(filename) plt.close() gc.collect()
def __evaluate_simulation_results(parameters, logdata_folder, graph_folder, generation_number, plot_name = None): assert logdata_folder, "Please specify where the logdata is located" data = empty_data_matrix(1) #ob_round_based = np.genfromtxt(logdata_folder + 'columnLog_roundBased_orderbook(0,0).csv', names=True, dtype=int, delimiter=',', usecols=(1,2)) #stock_round_based = np.genfromtxt(logdata_folder + 'columnLog_roundBased_stock0.csv', names=True, dtype=int, delimiter=',', usecols=(0,1)) trades = IO.load_trade_log_data(logdata_folder) fas = get_fundamental_after_shock() within_margin = get_number_of_rounds_within_stability_margin(trades['price'], trades['round'], fas) #data['n_simulation_rounds_within_stability_margin'] = within_margin['total_number_of_rounds'] #data['n_seperate_intervals_within_stability_margin'] = within_margin['n_intervals'] if 'longest_interval_within_margin' in data_for_failed_simulation.keys(): data['longest_interval_within_margin'] = within_margin['longest_interval'] if 'stdev' in data_for_failed_simulation.keys(): data['stdev'] = get_tp_std_after_entering_margin(trades['price'], trades['round']) if 'overshoot' in data_for_failed_simulation.keys(): data['overshoot'] = calculate_overshoot(fas, trades['price'], trades['round']) if 'time_to_reach_new_fundamental' in data_for_failed_simulation.keys(): data['time_to_reach_new_fundamental'] = get_first_round_to_reach_new_fundamental(trades['price'], trades['round']) if 'round_stable' in data_for_failed_simulation.keys(): data['round_stable'] = calculate_round_stable(trades['price'], trades['round'], fas) if np.random.random() <= PLOT_SAVE_PROB: data_id = 'gen%s_%s_%s'%(generation_number, get_epoch_time(), str(abs(hash(np.random.random())))) print graph_folder + data_id + '.npz' IO.save_tradeprice_data(trades['round'], trades['price'], data, parameters, graph_folder + data_id + '.npz') plotting.make_pretty_tradeprice_plot(trades['round'], trades['price'], graph_folder + data_id + '.png') #plotting.make_tradeprice_plot(trades['round'], trades['price'], data, parameters, graph_folder + data_id + '.png') else: data_id = None if not KEEP_SIMULATION_DATA: IO.delete_simulation_data(logdata_folder) return data, data_id
def make_pretty_tradeprice_plot(rounds, prices, filename, format = 'png', **figargs): from settings import default_parameters as dp cmap = brewer2mpl.get_map('Set1', 'qualitative', 9) p = Ppl(cmap, alpha=1) fig = plt.figure(num=None, **figargs) ax = fig.add_subplot(1,1,1) #fig, ax = plt.subplots(1) """ if kwargs.has_key('dpi'): fig.set_dpi(kwargs['dpi']) if kwargs.has_key('figwidth'): fig.set_figwidth(kwargs['figwidth']) if kwargs.has_key('figheight'): fig.set_figheight(kwargs['figheight']) """ p.plot(ax, rounds, prices, zorder=1) #ax.yaxis.get_major_formatter().set_powerlimits((0, 1)) ### Plotting statbility margins fas = get_fundamental_after_shock() ax.hlines(y = [fas - settings.stability_margin, fas + settings.stability_margin], xmin=0, xmax=settings.n_simulation_rounds, linestyles = 'dashed') ### Plotting fundamental step function y = [dp['fundamental_initial_value'], dp['fundamental_initial_value'] + dp['fundamental_shock_size'], ] xmin = [0, dp['fundamental_shock_round']] xmax = [dp['fundamental_shock_round'], settings.n_simulation_rounds] ax.hlines(y, xmin, xmax, linestyles = 'solid', colors='black', linewidth=2, zorder=2) ax.vlines(x = dp['fundamental_shock_round'], ymin=dp['fundamental_initial_value'] + dp['fundamental_shock_size'], ymax = dp['fundamental_initial_value'], colors='black', linewidth=2, zorder=3) ax.set_ylabel('Traded price (ticks)') ax.set_xlabel('Time (rounds)') ax.set_xlim([0, max(rounds)]) ax.set_ylim([min(fas - settings.stability_margin-2, min(prices)), max(fas + settings.stability_margin, max(prices))]) fig.savefig(filename) plt.close() gc.collect()