dates_trades = pd.to_datetime(trades.index).date # time vector of trades t_k = np.array(list(map(lambda x: x.timestamp(), pd.to_datetime(trades.index)))) p_last = np.array(trades.loc[:, 'price']) # last transaction values delta_q = np.array(trades.loc[:, 'siz']) # flow of traded contracts' sizes delta_sgn = np.array(trades.loc[:, 'aggress']) # trade sign flow match = np.array(trades.loc[:, 'mtch']) # match events # - # ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_volume_cluster_signal-implementation-step01): Process the database t, _, q_ask, p_ask, q_bid, p_bid, t_k, _, p_last, delta_q, _,\ _ = trade_quote_processing(t, dates_quotes, q_ask, p_ask, q_bid, p_bid, t_k, dates_trades, p_last, delta_q, delta_sgn, match) # ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_volume_cluster_signal-implementation-step02): Compute the traded price, the bid/ask prices, the bid/ask sizes and the microprice # + tick_time = np.arange(len(p_last[k_0:k_1 + 1])) i_ = len(tick_time) # last transaction value within the time window as a function of tick time p_last_k = p_last[k_0:k_1 + 1] # traded price # indexes of bid/ask prices near to the traded prices ti = np.zeros(i_, dtype=int) for i in range(i_): ti[i] = np.where(t <= t_k[k_0 + i])[0][-1]
time_trades = np.zeros(len(t_k0)) # time vector of trades for i in range(len(time_trades)): time_trades[i] = t_k0[i].timestamp() p_last = np.array(trades.loc[:, 'price']) # last transaction values delta_q = np.array(trades.loc[:, 'siz']) # flow of traded contracts' sizes delta_sgn = np.array(trades.loc[:, 'aggress']) # trade sign flow match = np.array(trades.loc[:, 'mtch']) # match events # - # ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_high_freq_stock_var-implementation-step01): Process the time series # + # process data time_quotes, _, h_ask, p_ask, h_bid, p_bid, time_trades, _, p_last, delta_q,\ delta_sgn, _ = trade_quote_processing(time_quotes, dates_quotes, h_ask, p_ask, h_bid, p_bid, time_trades, dates_trades, p_last, delta_q, delta_sgn, match) time_quotes = time_quotes.flatten() # index of the first trade within the time window k_0 = np.where(time_trades >= time_quotes[i_0])[0][0] # index of the last trade within the time window k_1 = np.where(time_trades <= time_quotes[i_1])[0][-1] # - # ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_high_freq_stock_var-implementation-step02): Compute the market microstructure variables # + q = np.cumsum(delta_q) # cumulative volume series sgn = np.cumsum(delta_sgn) # cumulative trade sign series