Esempio n. 1
0
def make_plots(plot_data, plot_params_dict):
    """ Draw and save plot from preprocessed data.

    :param plot_data: Data structure processed by __name__.prep_data
    :param plot_params_dict: dict holding some plotting paramaters, see e.g. plot_configs/single_day/pov_single_day_config.example.json["PLOT_PARAMS_DICT"]
    :return:
    """
    fig, axes = plt.subplots(nrows=2, ncols=1)
    fig.set_size_inches(h=14, w=9)

    colors = list(get_plot_colors(plot_data + ['dummy']*2))
    color = colors.pop(0)
    linestyles = get_plot_linestyles(len(plot_data) + 2)
    linestyle = linestyles.pop(0)

    orderbook_df = plot_data[0]['no_execution_df']
    orderbook_df["MID_PRICE"].plot(ax=axes[0], label=plot_params_dict['baseline_label'], color=color, linestyle=linestyle)

    for plot_data_dict in plot_data:
        color = colors.pop(0)
        linestyle = linestyles.pop(0)
        pov = f'{100 * plot_data_dict["pov"]} %'
        orderbook_df = plot_data_dict['no_execution_df']
        orderbook_with_execution_df = plot_data_dict['yes_execution_df']

        # mid_price
        orderbook_with_execution_df["MID_PRICE"].plot(ax=axes[0], label=
            f'{plot_params_dict["execution_label"]}{pov}', color=color, linestyle=linestyle)

        # normalised difference
        mid_price_yes_execution, mid_price_no_execution = forward_fill_series(orderbook_with_execution_df["MID_PRICE"],
                                                                              orderbook_df["MID_PRICE"])

        diff = 10000 * (mid_price_yes_execution - mid_price_no_execution) / mid_price_no_execution

        diff = diff.to_frame()
        diff = diff.loc[~diff.index.duplicated(keep='last')]
        diff = diff[diff.columns[0]]  # to series for plotting

        diff.plot(ax=axes[1], label=f'{plot_params_dict["execution_label"]}{pov}', color=color, linestyle=linestyle)


    axes[0].axvspan(pd.Timestamp(plot_params_dict['shade_start_datetime']),
                    pd.Timestamp(plot_params_dict['shade_end_datetime']), alpha=0.2, color='grey')
    axes[1].axvspan(pd.Timestamp(plot_params_dict['shade_start_datetime']),
                    pd.Timestamp(plot_params_dict['shade_end_datetime']), alpha=0.2, color='grey')
    axes[-1].xaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
    axes[-1].xaxis.set_minor_formatter(mdates.DateFormatter("%H:%M"))
    axes[0].xaxis.set_visible(False)
    axes[0].legend()

    axes[-1].set_xlabel('Time', size=15)
    axes[0].set_ylabel('Mid-price ($)', size=15)
    axes[1].set_ylabel('Normalized Difference (bps)', size=15)

    fig.tight_layout()
    fig.subplots_adjust(top=0.7)

    fig.savefig(plot_params_dict["output_file_path"], format='png', dpi=300, transparent=False, bbox_inches='tight',
                pad_inches=0.03)
Esempio n. 2
0
def plot_binned_trade_counts(trades_within_bins_dict, binwidth, output_dir):
    """ Plot binned counts of trade volume. """

    fig, ax = plt.subplots(figsize=(Constants.fig_width, Constants.fig_height))

    ax.set(yscale="log")

    ax.set_ylabel(Constants.binned_trade_counts_ylabel)
    ax.set_xlabel(Constants.binned_trade_counts_xlabel)

    symbols = list(trades_within_bins_dict.keys())
    symbols.sort()
    colors = get_plot_colors(symbols)
    alphas = [1] * len(symbols)

    x_s = []
    for symbol, color, alpha in zip(symbols, colors, alphas):
        binned_trades_counts = trades_within_bins_dict[symbol].copy(deep=True)
        binned_trades_counts = binned_trades_counts / binned_trades_counts.sum(
        )
        x = binned_trades_counts.sort_values()
        x_s.append(x)
        plt.hist(x,
                 bins="sqrt",
                 density=True,
                 label=symbol,
                 color=color,
                 alpha=alpha,
                 histtype="step",
                 linewidth=Constants.binned_count_linewidth)

    xlim = ax.get_xlim()
    ylim = ax.get_ylim()
    xx = np.linspace(*xlim, 200)

    # Plot fitted curves
    for x, symbol, color in zip(x_s, symbols, colors):
        gamma_params = stats.gamma.fit(x.values[x.values > 0], floc=0)
        plt.plot(xx,
                 stats.gamma.pdf(xx, *gamma_params),
                 linestyle="--",
                 color=color,
                 label=f"{symbol} gamma fit",
                 linewidth=Constants.binned_count_linewidth)

    ax.set_ylim(ylim)

    plt.title(
        f"Order volume within time window $\\tau = ${binwidth} seconds, normalized",
        size=Constants.title_font_size)
    plt.legend(fontsize=Constants.legend_font_size)

    fig.savefig(
        f'{output_dir}/{Constants.binned_trade_counts_filename}_tau_{binwidth}.png',
        format='png',
        dpi=300,
        transparent=False,
        bbox_inches='tight',
        pad_inches=0.03)
def plot_interarrival_times(interarrivals_dict, output_dir, scale='log'):
    """ Plots histogram of the interarrival times for symbols. """

    fig, ax = plt.subplots(figsize=(Constants.fig_width, Constants.fig_height))

    if scale == 'log':
        ax.set(xscale="symlog", yscale="log")

    ax.set_ylabel(Constants.interarrival_times_ylabel)
    ax.set_xlabel(Constants.interarrival_times_xlabel)

    symbols = list(interarrivals_dict.keys())
    symbols.sort()
    colors = get_plot_colors(symbols)
    alphas = [1] * len(symbols)

    x_s = []

    for symbol, color, alpha in zip(symbols, colors, alphas):
        interarrival_times_series = interarrivals_dict[symbol]
        x = interarrival_times_series.sort_values()
        x_s.append(x)
        plt.hist(x, bins="sqrt", density=True, label=symbol, color=color, alpha=alpha, histtype="step",
                 linewidth=Constants.interarrival_linewidth)

    ylim = ax.get_ylim()
    xlim = ax.get_xlim()

    xx = np.linspace(*xlim, 200)

    # Plot fitted curves, leave out zeroes for better fit
    for x, symbol, color in zip(x_s, symbols, colors):
        x = x[(x > Constants.interarrival_fit_lower_bound) & (x < Constants.interarrival_fit_upper_bound)]
        weibull_params = stats.weibull_min.fit(x, floc=0)

        x_left = xx[xx < x.min()][1:]
        x_mid = x.to_numpy()
        x_right = xx[xx > x.max()]
        xxx = np.concatenate((x_left, x_mid, x_right))

        plt.plot(xxx, stats.weibull_min.pdf(xxx, *weibull_params), linestyle="--", color=color,
                 label=f"{symbol} Weibull fit", linewidth=Constants.interarrival_linewidth)

    plt.legend(fontsize=Constants.legend_font_size)
    ax.set_ylim(ylim)

    fig.savefig(f'{output_dir}/{Constants.interarrival_times_filename}.png', format='png', dpi=300,
                transparent=False, bbox_inches='tight', pad_inches=0.03)
def get_scatter_plot_params_dict(symbols):
    """ Creates dictionary of parameters used by intraday seasonality plots. """
    colors = get_plot_colors(symbols)
    scatter_styles_sizes = itertools.cycle(Constants.scatter_marker_styles_sizes)
    scatter_plot_params_dict = dict()

    for symbol, color, style_and_size in zip(symbols, colors, scatter_styles_sizes):
        scatter_plot_params_dict.update({
            symbol : {
                'color': color,
                'marker': style_and_size[0],
                'marker_size': style_and_size[1]
            }
        })

    return scatter_plot_params_dict