|
2 | 2 | from datetime import datetime, timedelta |
3 | 3 |
|
4 | 4 | import numpy as np |
5 | | -import pandas as pd |
| 5 | +import polars as pl |
6 | 6 | from hftbacktest import EXCH_EVENT, LOCAL_EVENT |
7 | 7 | from numba import njit |
8 | 8 |
|
@@ -46,20 +46,24 @@ def generate_order_latency_nb(data, order_latency, mul_entry, offset_entry, mul_ |
46 | 46 |
|
47 | 47 | def generate_order_latency(feed_file, output_file=None, mul_entry=1, offset_entry=0, mul_resp=1, offset_resp=0): |
48 | 48 | data = np.load(feed_file)['data'] |
49 | | - df = pd.DataFrame(data) |
50 | | - df = df[(df['ev'] & EXCH_EVENT == EXCH_EVENT) | (df['ev'] & LOCAL_EVENT == LOCAL_EVENT)] |
51 | | - s = (df['local_ts'] / 1_000_000_000).astype(int) |
52 | | - df = df.groupby(s).last() |
53 | | - data = df.to_records(index=False) |
| 49 | + df = pl.DataFrame(data) |
| 50 | + |
| 51 | + df = df.filter( |
| 52 | + (pl.col('ev') & EXCH_EVENT == EXCH_EVENT) & (pl.col('ev') & LOCAL_EVENT == LOCAL_EVENT) |
| 53 | + ).with_columns( |
| 54 | + pl.col('local_ts').alias('ts') |
| 55 | + ).group_by_dynamic( |
| 56 | + 'ts', every='1000000000i' |
| 57 | + ).agg( |
| 58 | + pl.col('exch_ts').last(), |
| 59 | + pl.col('local_ts').last() |
| 60 | + ).drop('ts') |
| 61 | + |
| 62 | + data = df.to_numpy(structured=True) |
54 | 63 |
|
55 | 64 | order_latency = np.zeros( |
56 | 65 | len(data), |
57 | | - dtype=[ |
58 | | - ('req_ts', '<i8'), |
59 | | - ('exch_ts', '<i8'), |
60 | | - ('resp_ts', '<i8'), |
61 | | - ('_padding', '<i8') |
62 | | - ] |
| 66 | + dtype=[('req_ts', 'i8'), ('exch_ts', 'i8'), ('resp_ts', 'i8'), ('_padding', 'i8')] |
63 | 67 | ) |
64 | 68 | generate_order_latency_nb(data, order_latency, mul_entry, offset_entry, mul_resp, offset_resp) |
65 | 69 |
|
|
0 commit comments