-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathfeatures.py
167 lines (127 loc) · 5.92 KB
/
features.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
import glob
import datetime
import os
import numpy as np
import pandas as pd
path = 'data/*.csv'
def std_rush_order_feature(df_buy, time_freq, rolling_freq):
df_buy = df_buy.groupby(df_buy.index).count()
df_buy[df_buy == 1] = 0
df_buy[df_buy > 1] = 1
buy_volume = df_buy.groupby(pd.Grouper(freq=time_freq))['btc_volume'].sum()
buy_count = df_buy.groupby(pd.Grouper(freq=time_freq))['btc_volume'].count()
buy_volume.drop(buy_volume[buy_count == 0].index, inplace=True)
buy_volume.dropna(inplace=True)
rolling_diff = buy_volume.rolling(window=rolling_freq).std()
results = rolling_diff.pct_change()
return results
def avg_rush_order_feature(df_buy, time_freq, rolling_freq):
df_buy = df_buy.groupby(df_buy.index).count()
df_buy[df_buy == 1] = 0
df_buy[df_buy > 1] = 1
buy_volume = df_buy.groupby(pd.Grouper(freq=time_freq))['btc_volume'].sum()
buy_count = df_buy.groupby(pd.Grouper(freq=time_freq))['btc_volume'].count()
buy_volume.drop(buy_volume[buy_count == 0].index, inplace=True)
buy_volume.dropna(inplace=True)
rolling_diff = buy_volume.rolling(window=rolling_freq).mean()
results = rolling_diff.pct_change()
return results
def std_trades_feature(df_buy_rolling, rolling_freq):
buy_volume = df_buy_rolling['price'].count()
buy_volume.drop(buy_volume[buy_volume == 0].index, inplace=True)
buy_volume.dropna(inplace=True)
rolling_diff = buy_volume.rolling(window=rolling_freq).std()
results = rolling_diff.pct_change()
return results
def std_volume_feature(df_buy_rolling, rolling_freq):
buy_volume = df_buy_rolling['btc_volume'].sum()
buy_volume.drop(buy_volume[buy_volume == 0].index, inplace=True)
buy_volume.dropna(inplace=True)
rolling_diff = buy_volume.rolling(window=rolling_freq).std()
results = rolling_diff.pct_change()
return results
def avg_volume_feature(df_buy_rolling, rolling_freq):
buy_volume = df_buy_rolling['btc_volume'].sum()
buy_volume.drop(buy_volume[buy_volume == 0].index, inplace=True)
buy_volume.dropna(inplace=True)
rolling_diff = buy_volume.rolling(window=rolling_freq).mean()
results = rolling_diff.pct_change()
return results
def std_price_feature(df_buy_rolling, rolling_freq):
buy_volume = df_buy_rolling['price'].mean()
buy_volume.dropna(inplace=True)
rolling_diff = buy_volume.rolling(window=rolling_freq).std()
results = rolling_diff.pct_change()
return results
def avg_price_feature(df_buy_rolling):
buy_volume = df_buy_rolling['price'].mean()
buy_volume.dropna(inplace=True)
rolling_diff = buy_volume.rolling(window=10).mean()
results = rolling_diff.pct_change()
return results
def avg_price_max(df_buy_rolling):
buy_volume = df_buy_rolling['price'].max()
buy_volume.dropna(inplace=True)
rolling_diff = buy_volume.rolling(window=10).mean()
results = rolling_diff.pct_change()
return results
def chunks_time(df_buy_rolling):
# compute any kind of aggregation
buy_volume = df_buy_rolling['price'].max()
buy_volume.dropna(inplace=True)
#the index contains time info
return buy_volume.index
def build_features(file, coin, time_freq, rolling_freq, index):
df = pd.read_csv(file)
df["time"] = pd.to_datetime(df['timestamp'].astype(np.int64), unit='ms')
df = df.reset_index().set_index('time')
df_buy = df[df['side'] == 'buy']
df_buy_grouped = df_buy.groupby(pd.Grouper(freq=time_freq))
date = chunks_time(df_buy_grouped)
results_df = pd.DataFrame(
{'date': date,
'pump_index': index,
'std_rush_order': std_rush_order_feature(df_buy, time_freq, rolling_freq).values,
'avg_rush_order': avg_rush_order_feature(df_buy, time_freq, rolling_freq).values,
'std_trades': std_trades_feature(df_buy_grouped, rolling_freq).values,
'std_volume': std_volume_feature(df_buy_grouped, rolling_freq).values,
'avg_volume': avg_volume_feature(df_buy_grouped, rolling_freq).values,
'std_price': std_price_feature(df_buy_grouped, rolling_freq).values,
'avg_price': avg_price_feature(df_buy_grouped),
'avg_price_max': avg_price_max(df_buy_grouped).values,
'hour_sin': np.sin(2 * np.pi * date.hour/23),
'hour_cos': np.cos(2 * np.pi * date.hour/23),
'minute_sin': np.sin(2 * np.pi * date.minute / 59),
'minute_cos': np.cos(2 * np.pi * date.minute / 59),
})
results_df['symbol'] = coin
results_df['gt'] = 0
return results_df.dropna()
def build_features_multi(time_freq, rolling_freq):
files = glob.glob(path)
all_results_df = pd.DataFrame()
count = 0
pumps = pd.read_csv('pump_telegram.csv')
pumps = pumps[pumps['exchange'] == 'binance']
for f in files:
print(f)
coin_date, time = os.path.basename(f[:f.rfind('.')]).split(' ')
coin, date = coin_date.split('_')
skip_pump = len(pumps[(pumps['symbol'] == coin) & (pumps['date'] == date) & (pumps['hour'] == time.replace('.', ':'))]) == 0
if skip_pump:
continue
results_df = build_features(f, coin, time_freq, rolling_freq, count)
date_datetime = datetime.datetime.strptime(date + ' ' + time, '%Y-%m-%d %H.%M')
# We consider 24 hours before and 24 hours after the pump
results_df = results_df[(results_df['date'] >= date_datetime - datetime.timedelta(hours=24)) & (results_df['date'] <= date_datetime + datetime.timedelta(hours=24))]
all_results_df = pd.concat([all_results_df, results_df])
count += 1
all_results_df.to_csv('features/features_{}.csv'.format(time_freq), index=False, float_format='%.3f')
def compute_features():
build_features_multi(time_freq='25S', rolling_freq=900)
build_features_multi(time_freq='15S', rolling_freq=900)
build_features_multi(time_freq='5S', rolling_freq=700)
if __name__ == '__main__':
start = datetime.datetime.now()
compute_features()
print(datetime.datetime.now() - start)