(dataframes, n_lob_levels, chosen_model)
| 235 | |
| 236 | |
| 237 | def preprocess_data(dataframes, n_lob_levels, chosen_model): |
| 238 | dataframes = reset_indexes(dataframes) |
| 239 | |
| 240 | # take only the first n_lob_levels levels of the orderbook and drop the others |
| 241 | dataframes[1] = dataframes[1].iloc[:, :n_lob_levels * cst.LEN_LEVEL] |
| 242 | |
| 243 | # take the indexes of the dataframes that are of type |
| 244 | # 2 (partial deletion), 5 (execution of a hidden limit order), |
| 245 | # 6 (cross trade), 7 (trading halt) and drop it |
| 246 | indexes_to_drop = dataframes[0][dataframes[0]["event_type"].isin([2, 5, 6, 7])].index |
| 247 | dataframes[0] = dataframes[0].drop(indexes_to_drop) |
| 248 | dataframes[1] = dataframes[1].drop(indexes_to_drop) |
| 249 | |
| 250 | dataframes = reset_indexes(dataframes) |
| 251 | |
| 252 | # drop index column in messages |
| 253 | dataframes[0] = dataframes[0].drop(columns=["order_id"]) |
| 254 | |
| 255 | # do the difference of time row per row in messages and subsitute the values with the differences |
| 256 | # Store the initial value of the "time" column |
| 257 | first_time = dataframes[0]["time"].values[0] |
| 258 | # Calculate the difference using diff |
| 259 | dataframes[0]["time"] = dataframes[0]["time"].diff() |
| 260 | # Set the first value directly |
| 261 | dataframes[0].iat[0, dataframes[0].columns.get_loc("time")] = first_time - 34200 |
| 262 | |
| 263 | # add depth column to messages |
| 264 | dataframes[0]["depth"] = 0 |
| 265 | |
| 266 | # we compute the depth of the orders with respect to the orderbook |
| 267 | # Extract necessary columns |
| 268 | prices = dataframes[0]["price"].values |
| 269 | directions = dataframes[0]["direction"].values |
| 270 | event_types = dataframes[0]["event_type"].values |
| 271 | bid_sides = dataframes[1].iloc[:, 2::4].values |
| 272 | ask_sides = dataframes[1].iloc[:, 0::4].values |
| 273 | |
| 274 | # Initialize depth array |
| 275 | depths = np.zeros(dataframes[0].shape[0], dtype=int) |
| 276 | |
| 277 | # Compute the depth of the orders with respect to the orderbook |
| 278 | for j in range(1, len(prices)): |
| 279 | order_price = prices[j] |
| 280 | direction = directions[j] |
| 281 | event_type = event_types[j] |
| 282 | |
| 283 | index = j if event_type == 1 else j - 1 |
| 284 | |
| 285 | if direction == 1: |
| 286 | bid_price = bid_sides[index, 0] |
| 287 | depth = (bid_price - order_price) // 100 |
| 288 | else: |
| 289 | ask_price = ask_sides[index, 0] |
| 290 | depth = (order_price - ask_price) // 100 |
| 291 | |
| 292 | depths[j] = max(depth, 0) |
| 293 | |
| 294 | # Assign the computed depths back to the DataFrame |
no test coverage detected