-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeature.py
238 lines (200 loc) · 7.88 KB
/
feature.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import pandas as pd
import numpy as np
import multiprocessing as mp
import glob
def get_all_stock_ids(train_dir):
paths = glob.glob(f"{train_dir}/*")
return sorted([int(path.split("=")[1]) for path in paths])
def get_cache_file_name(stock_ids, window):
# encode stock list
# every bit represent if a stock is picked or not
# two 64bit integers shall be enough
high = 0
low = 0
for stock_id in stock_ids:
if stock_id < 64:
low += 1 << stock_id
else:
high += 1 << (stock_id - 64)
cache = f"./data/mine/{window}/{hex(high)}_{hex(low)}.csv"
return cache
def realized_volatility(price):
log_return = np.log(price).diff().dropna()
return np.sqrt(np.sum(log_return ** 2))
def cut_by_time(df, window_seconds):
batch_id = (df.seconds_in_bucket / window_seconds).astype(int)
return pd.Series(batch_id, name="batch_id", index=df.index)
def get_features(df, feature_dict):
features = (
df.groupby(["time_id", "batch_id"], group_keys=False)
.agg(feature_dict)
.reset_index()
)
# concat multi-level column
features.columns = ["_".join(col) for col in features.columns]
# now each time id has several rows of features for different time window
# use pandas.pivot to flat these rows
flat_features = features.pivot(index="time_id_", columns="batch_id_")
flat_features.columns = [f"{col[0]}_batch{col[1]}" for col in flat_features.columns]
return flat_features.reset_index()
def vwap(row, bid_idx, ask_idx):
# TODO: multi-level
return (
row[f"bid_price{bid_idx}"] * row[f"ask_size{ask_idx}"]
+ row[f"ask_price{ask_idx}"] * row[f"bid_size{bid_idx}"]
) / (row[f"ask_size{ask_idx}"] + row[f"bid_size{bid_idx}"])
def get_book_features(raw_book, window):
# VWAPs
book = raw_book.copy()
# level 0 price does not change
book["vwap11"] = vwap(book, 1, 1)
# ask level 0 is fully traded
book["vwap12"] = vwap(book, 1, 2)
# bid level 0 is fully traded
book["vwap21"] = vwap(book, 2, 1)
# bid and ask level 0 is fully traded
book["vwap22"] = vwap(book, 2, 2)
book["bid_ask_spread"] = book.ask_price1 - book.bid_price1
book["total_volume_lv1"] = book.ask_size1 + book.bid_size1
book["total_volume_lv12"] = (
book.ask_size1 + book.bid_size1 + book.ask_size2 + book.bid_size2
)
# book flip (cross spread to take orders, extremely aggressive behavior)
book["flip"] = book.ask_price1.shift(-1) <= book.bid_price1
book["batch_id"] = cut_by_time(book, window)
feature_dict = {
# the mean, std of vwap is useless according to feature importance
"vwap11": [realized_volatility],
"vwap12": [realized_volatility],
"vwap21": [realized_volatility],
"vwap22": [realized_volatility],
"bid_ask_spread": ["mean", "std"],
"total_volume_lv1": ["mean", "std", "sum"],
"total_volume_lv12": ["mean", "std", "sum"],
"flip": ["sum"],
"seconds_in_bucket": "count",
}
book_features = get_features(book, feature_dict)
# last book state
last_state = raw_book.drop_duplicates(["time_id"], keep="last").reset_index(
drop=True
)
book_features["last_total_volume_lv1"] = last_state.bid_size1 + last_state.ask_size1
book_features["last_total_volume_lv12"] = (
book_features.last_total_volume_lv1
+ last_state.bid_size2
+ last_state.ask_size2
)
book_features["last_bid_ask_spread"] = last_state.ask_price1 - last_state.bid_price1
return book_features
def get_trade_features(raw_trade, raw_book, window):
trade = raw_trade.rename({"size": "trade_volume"}, axis=1).copy()
trade["per_trade_quantity"] = trade.trade_volume / trade.order_count
# a complex feature, trade_volume/(ask_size + bid_size)
# this may give insight on how much percentage of ToB is taken
raw_book["time_seconds"] = (
raw_book.time_id.astype(int) * 600 + raw_book.seconds_in_bucket
)
trade["time_seconds"] = trade.time_id.astype(int) * 600 + trade.seconds_in_bucket
merged = pd.merge_asof(
trade,
raw_book[
[
"time_id",
"time_seconds",
"bid_size1",
"ask_size1",
"bid_size2",
"ask_size2",
]
],
by="time_id",
on="time_seconds",
)
merged["trade_ratio_lv1"] = merged.trade_volume / (
merged.bid_size1 + merged.ask_size1
)
merged["trade_ratio_lv12"] = merged.trade_volume / (
merged.bid_size1 + merged.ask_size1 + merged.bid_size2 + merged.ask_size2
)
merged["batch_id"] = cut_by_time(merged, window)
feature_dict = {
"trade_volume": ["mean", "std", "sum"],
"order_count": ["mean", "std", "sum"],
"per_trade_quantity": ["mean", "std"],
"trade_ratio_lv1": ["mean", "std"],
"trade_ratio_lv12": ["mean", "std"],
"seconds_in_bucket": "count",
}
return get_features(merged, feature_dict)
# mode = "train" or "test"
def get_one_stock_features(stock_id, window, mode):
book_path = f"./data/book_{mode}.parquet/stock_id={stock_id}"
raw_book = pd.read_parquet(book_path)
book_features = get_book_features(raw_book, window)
trade_path = f"./data/trade_{mode}.parquet/stock_id={stock_id}"
raw_trade = pd.read_parquet(trade_path)
trade_features = get_trade_features(raw_trade, raw_book, window)
# left join to handle "no trade" cases for low liquidity stocks
# it is safe because book update must >= trade update
merged = pd.merge(
book_features,
trade_features,
on=["time_id_"],
how="left",
suffixes=["_book", "_trade"],
)
merged.insert(loc=0, column="stock_id", value=stock_id)
return merged
# use this to get book & trade features
def get_stock_features(stock_ids, window, mode):
with mp.Pool(4) as p:
results = p.starmap(
get_one_stock_features,
zip(stock_ids, [window] * len(stock_ids), [mode] * len(stock_ids)),
)
return pd.concat(results).reset_index(drop=True)
def get_correlation(y_path):
vol_true = pd.read_csv(y_path).pivot(
index="time_id", columns="stock_id", values="target"
)
# correlation is based on the "change rate" of volatility
# instead of the raw volatility, I think it is comparable between stocks
return (vol_true / vol_true.shift(1)).corr()
def get_similar_stock_features(train_data, corr, n, selected_features):
selected_features = set(selected_features)
df = []
for stock_id in train_data.stock_id.unique():
# remove itself
top_n_stocks = corr[[stock_id]].nlargest(n + 1, stock_id).index[1:]
similar_stock_features = (
train_data[train_data.stock_id.isin(top_n_stocks)]
.groupby("time_id_")
.mean()
.reindex(selected_features, axis=1)
)
similar_stock_features["stock_id"] = stock_id
df.append(similar_stock_features)
return (
pd.concat(df)
.rename({col: f"{col}_similar" for col in selected_features}, axis=1)
.reset_index()
)
def get_stock_group_features(train_features, corr, selected_features):
copied_corr = corr.copy()
from sklearn.cluster import KMeans
# clustering = DBSCAN(eps=0.4, min_samples=2).fit(corr.values)
clustering = KMeans(n_clusters=5, random_state=0).fit(copied_corr)
copied_corr["group_id"] = clustering.labels_
merged = train_features.merge(copied_corr[["group_id"]], on="stock_id")
group_features = (
merged.groupby(["time_id_", "group_id"])
.mean()
.reindex(selected_features, axis=1)
.reset_index()
.pivot(index="time_id_", columns="group_id")
)
group_features.columns = [
f"{col[0]}_group{col[1]}" for col in group_features.columns
]
return group_features.reset_index()