def test_compare_order_metrics(self): sim_root = self.config.sim_root + self.sim_st.date().isoformat( ) + "/" + self.sim_st.time().isoformat() + "/" all_sims = DataLoader().load_sim_data(sim_root) all_sim_limit_orders = list( map(lambda sim: DataSplitter.get_limit_orders(sim[0].compute()), all_sims)) all_sim_market_orders = list( map(lambda sim: DataSplitter.get_market_orders(sim[0].compute()), all_sims)) all_sim_trades = list(map(lambda sim: sim[1].compute(), all_sims)) all_sim_cancels = list(map(lambda sim: sim[2].compute(), all_sims)) feed_df = DataLoader().load_feed( self.config.real_root, self.sim_st, self.sim_st + timedelta(seconds=self.config.simulation_window), self.config.product) real_orders = DataSplitter.get_orders(feed_df) real_limit_orders = DataSplitter.get_limit_orders(real_orders) real_market_orders = DataSplitter.get_market_orders(real_orders) real_trades = DataSplitter.get_trades(feed_df) real_trades['size'] = pd.to_numeric(real_trades['remaining_size']) real_cancels = DataSplitter.get_cancellations(feed_df) real_cancels['size'] = pd.to_numeric(real_cancels['remaining_size']) print("Order Buy/Sell limit metrics") Evaluation.compare_order_metrics(real_limit_orders, all_sim_limit_orders) print("Order Buy/Sell market metrics") Evaluation.compare_order_metrics(real_market_orders, all_sim_market_orders) print("Cancel metrics") Evaluation.compare_order_metrics(real_cancels, all_sim_cancels) print("Trade metrics") Evaluation.compare_metrics(real_trades, all_sim_trades)
def load_split_data(real_root, start_time, end_time, product): feed_df = DataLoader().load_feed(real_root, start_time, end_time, product) feed_df = DataSplitter.get_product(product, feed_df) orders_df = DataSplitter.get_orders(feed_df) trades_df = DataSplitter.get_trades(feed_df) cancels_df = DataSplitter.get_cancellations(feed_df) return orders_df, trades_df, cancels_df
def graph_order_cancel_relative_price_distribution(self, feed_df): trades_df = DataSplitter.get_trades(feed_df) cancels_df = DataSplitter.get_cancellations(feed_df) self.graph_relative_price_distribution(trades_df, cancels_df)
def test_orders_per_minute_windowed(self): product = "LTC-USD" root = "/Users/jamesprince/project-data/data/consolidated-feed/" st = datetime.datetime(2018, 5, 17, 0, 0, 0) et = datetime.datetime(2018, 5, 17, 23, 59, 59) feed_df = DataLoader.load_feed(root + product + "/", st, et, product) orders = DataSplitter.get_orders(feed_df) limit_orders = DataSplitter.get_limit_orders(orders) market_orders = DataSplitter.get_market_orders(orders) trades = DataSplitter.get_trades(feed_df) cancels = DataSplitter.get_cancellations(feed_df) print("Total limit orders: " + str(len(limit_orders))) print("Total market orders: " + str(len(market_orders))) print("Total trades: " + str(len(trades))) print("Total cancels: " + str(len(cancels))) # total_vol = trades['remaining_size'].sum() # print("Total traded volume: " + str(total_vol)) window_minutes = 60 step_minutes = 5 times = [] num_limit_orders = [] num_market_orders = [] num_trades = [] num_cancels = [] traded_vols = [] for i in range(0, int((24 * 60) / step_minutes - 1)): window_st = st + datetime.timedelta(seconds=i * step_minutes * 60) window_et = window_st + datetime.timedelta(seconds=window_minutes * 60) limit_orders_this_window = DataSplitter.get_between( limit_orders, window_st, window_et) market_orders_this_window = DataSplitter.get_between( market_orders, window_st, window_et) trades_this_window = DataSplitter.get_between( trades, window_st, window_et) cancels_this_window = DataSplitter.get_between( cancels, window_st, window_et) times.append(window_st) num_limit_orders.append(len(limit_orders_this_window)) num_market_orders.append(len(market_orders_this_window)) num_trades.append(len(trades_this_window)) num_cancels.append(len(cancels_this_window)) # vol_this_window = trades_this_window['remaining_size'].sum() # traded_vols.append(vol_this_window) Statistics.plot_metric_daily_comparison(times, num_limit_orders, num_cancels, "LTC-USD", st, step_minutes, window_minutes, "Limit Orders", "Cancels") Statistics.plot_metric_daily(times, num_limit_orders, "LTC-USD", st, step_minutes, window_minutes, "Limit Orders") Statistics.plot_metric_daily(times, num_market_orders, "LTC-USD", st, step_minutes, window_minutes, "Market Orders") Statistics.plot_metric_daily(times, num_trades, "LTC-USD", st, step_minutes, window_minutes, "Trades") Statistics.plot_metric_daily(times, num_cancels, "LTC-USD", st, step_minutes, window_minutes, "Cancels") Statistics.plot_metric_daily(times, traded_vols, "LTC-USD", st, step_minutes, window_minutes, "Traded Volume")
def test_real_spread_plot(self): plt.figure(figsize=(12, 8)) product = "LTC-USD" root = "/Users/jamesprince/project-data/data/consolidated-feed/" st = datetime.datetime(2018, 5, 17, 1, 0, 0) et = datetime.datetime(2018, 5, 17, 1, 5, 0) feed_df = DataLoader.load_feed(root + product + "/", st, et, product) conf = configparser.ConfigParser() conf.read("../config/backtest.ini") config = BacktestConfig(conf) ob_seq, ob_state = reconstruct_orderbook(config, st, logging.getLogger("test")) orderbook_evo = OrderBookEvolutor(ob_state, st, ob_seq) res_df = orderbook_evo.evolve_orderbook(feed_df) res_df['seconds'] = ( res_df['time'] - res_df['time'].iloc[0]).apply(lambda x: x.total_seconds()) print(res_df) limit_orders = DataSplitter.get_limit_orders_from_feed(feed_df) limit_orders['seconds'] = ( limit_orders['time'] - limit_orders['time'].iloc[0]).apply(lambda x: x.total_seconds()) buy_limit_orders = DataSplitter.get_side("buy", limit_orders) sell_limit_orders = DataSplitter.get_side("sell", limit_orders) cancels = DataSplitter.get_cancellations(feed_df) # print(cancels) cancels_merged = cancels.merge(limit_orders, on='order_id', how='left') # print(cancels_merged) cancels_merged['price'] = cancels_merged['price_x'] cancels_merged['side'] = cancels_merged['side_x'] cancels_merged['seconds'] = (cancels_merged['time_x'] - cancels_merged['time_x'].iloc[0] ).apply(lambda x: x.total_seconds()) cancels_merged['lifetime'] = abs(cancels_merged['time_x'] - cancels_merged['time_y']).dropna() print(cancels_merged) median_idx = int(len(cancels_merged['lifetime']) / 2) print(cancels_merged['lifetime'].sort_values().iloc[median_idx]) buy_cancels = DataSplitter.get_side("buy", cancels_merged) sell_cancels = DataSplitter.get_side("sell", cancels_merged) plt.plot(buy_limit_orders['seconds'], buy_limit_orders['price'], 'r+', label="Buy limit orders") plt.plot(sell_limit_orders['seconds'], sell_limit_orders['price'], 'b+', label="Sell limit orders") # plt.plot(buy_cancels['seconds'], buy_cancels['price'], 'r+', label="Buy side cancels") # plt.plot(sell_cancels['seconds'], sell_cancels['price'], 'b+', label="Sell side cancels") plt.plot(res_df['seconds'], res_df['best_bid'], label='Best bid price') plt.plot(res_df['seconds'], res_df['best_ask'], label='Best ask price') start_price = res_df['midprice'].iloc[0] plt.ylim(start_price - 5, start_price + 5) plt.legend() plt.show()