Repository navigation
Expand file tree
/
Copy pathfeatures.py
More file actions
227 lines (165 loc) · 5.55 KB
/
Copy pathfeatures.py
File metadata and controls
227 lines (165 loc) · 5.55 KB
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
import math
def spread(ob, stats):
return stats.best_offer_price - stats.best_bid_price
## is the market locked or crossed?
def locked(ob, stats):
return stats.best_offer_price == stats.best_bid_price
def crossed(ob, stats):
return stats.best_offer_price < stats.best_bid_price
## difference between the best level and 5 levels away
def bid_range(ob, stats):
last_idx = min(4, len(ob.bids) - 1)
return ob.bids[0].price - ob.bids[last_idx].price
def offer_range(ob, stats):
last_idx = min(4, len(ob.offers) - 1)
return ob.offers[last_idx].price - ob.offers[0].price
def bid_slope(ob, stats):
last_idx = min(4, len(ob.bids) - 1)
first_price = ob.bids[0].price
cumulative_vol = 0
total = 0.0
for i in xrange(last_idx):
bid = ob.bids[i+1]
cumulative_vol += bid.size
delta_p = bid.price - first_price
total += delta_p / cumulative_vol
return total / (last_idx+1)
def offer_slope(ob, stats):
last_idx = min(4, len(ob.offers) - 1)
first_price = ob.offers[0].price
cumulative_vol = 0
total = 0.0
for i in xrange(last_idx):
offer = ob.offers[i+1]
cumulative_vol += offer.size
delta_p = offer.price - first_price
total += delta_p / cumulative_vol
return total / (last_idx+1)
def best_bid(orderBook, stats):
return stats.best_bid_price
def best_offer(orderBook, stats):
return stats.best_offer_price
def midprice(orderBook, stats):
return (stats.best_offer_price + stats.best_bid_price ) / 2
def total_added_volume(ob, stats):
return ob.stats.added_bid_volume + ob.stats.added_offer_volume
def net_volume(ob, stats):
return ob.added_bid_volume + ob.added_offer_volume \
- ob.deleted_bid_volume - ob.deleted_offer_volume
## tr8dr's insertion flow
def bid_tr8dr(ob, stats):
return stats.bid_tr8dr
def offer_tr8dr(ob, stats):
return stats.offer_tr8dr
def tr8dr(ob, stats):
return stats.bid_tr8dr + stats.offer_tr8dr
## Canceled = deleted from secondary levels
def canceled_bid_volume(ob, stats):
return stats.canceled_bid_volume
def canceled_bid_count(ob, stats):
return stats.canceled_bid_count
def canceled_offer_volume(ob, stats):
return stats.canceled_offer_volume
def canceled_offer_count(ob, stats):
return stats.canceled_offer_count
def total_canceled_volume(ob, stats):
return stats.canceled_bid_volume + stats.canceled_offer_volume
## Filled = deleted from best level
def filled_bid_volume(ob, stats):
return stats.filled_bid_volume
def filled_bid_count(ob, stats):
return stats.filled_bid_count
def filled_offer_volume(ob, stats):
return stats.filled_offer_volume
def filled_offer_count(ob, stats):
return stats.filled_offer_count
def total_filled_volume(ob, stats):
return stats.filled_bid_volume + stats.filled_offer_volume
## Add
def added_offer_volume(ob, stats):
return stats.added_offer_volume
def added_offer_count(ob, stats):
return stats.added_offer_count
def added_bid_volume(ob, stats):
return stats.added_bid_volume
def added_bid_count(ob, stats):
return stats.added_bid_count
def added_best_offer_volume(ob, stats):
return stats.added_best_offer_volume
def added_best_offer_count(ob, stats):
return stats.added_best_offer_count
def added_best_bid_volume(ob, stats):
return stats.added_best_bid_volume
def added_best_bid_count(ob, stats):
return stats.added_best_bid_count
## Delete
def deleted_offer_volume(ob, stats):
return stats.deleted_offer_volume
def deleted_offer_count(ob, stats):
return stats.deleted_offer_count
def deleted_bid_volume(ob, stats):
return stats.deleted_bid_volume
def deleted_bid_count(ob, stats):
return stats.deleted_bid_count
def total_deleted_volume(ob, stats):
return stats.deleted_offer_volume + stats.deleted_bid_volume
#############
def millisecond_timestamp(orderBook, stats):
return orderBook.last_update_time
def second_timestamp(orderBook, stats):
return orderBook.last_update_time / 1000.0
def message_count(orderBook, stats):
return len(orderBook.actions)
# cache results
def bid_volume(orderBook, stats):
bidVolume = 0.0
for order in orderBook.bids:
bidVolume += order.size
return bidVolume
# cache results
def offer_volume(ob, stats):
offerVolume = 0.0
for order in ob.offers:
offerVolume += order.size
return offerVolume
def best_offer_volume(ob, stats):
return stats.best_offer_volume
def best_bid_volume(ob, stats):
return stats.best_bid_volume
# volume weighted price of first five bids
def bid_vwap(ob, stats):
p = 0
v = 0
for (i, order) in enumerate(ob.bids):
p += order.price * order.size
v += order.size
if i >= 4: break
return p / v
def offer_vwap(ob, stats):
p = 0
v = 0
for (i, order) in enumerate(ob.offers):
p += order.price * order.size
v += order.size
if i >= 4: break
return p / v
def fraction_of_second(orderBook, stats):
return (orderBook.last_update_time % 1000.0) / 1000.0
def nth_digit(x,n):
x = abs(x)
int_x = int(x)
if int_x != 0: x -= int_x
if x == 0: return 0
else:
# add very small constant to keep from rounding down
return int( 0.000001 +
x / (10 ** int(math.log10(x) - n))
) % 10
# like nth digit, but don't floor at the end.
# so nth_digit_tail(3.14,1) = 1.4
def nth_digit_tail(x,n):
x = abs(x)
int_x = int(x)
if int_x != 0: x -= int_x
if x == 0: return 0
else: return (x / (10 ** int(math.log10(x) - n))) % 10