-
Notifications
You must be signed in to change notification settings - Fork 278
/
latencies.py
323 lines (256 loc) · 11.9 KB
/
latencies.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from numba import float64, int64
from ..reader import COL_LOCAL_TIMESTAMP, COL_EXCH_TIMESTAMP
class ConstantLatency:
r"""
Provides constant order latency. The units of the arguments should match the timestamp units of your
data.
Args:
entry_latency: Order entry latency.
response_latency: Order response latency.
"""
entry_latency: float64
response_latency: float64
def __init__(self, entry_latency, response_latency):
self.entry_latency = entry_latency
self.response_latency = response_latency
def entry(self, timestamp, order, proc):
return self.entry_latency
def response(self, timestamp, order, proc):
return self.response_latency
def reset(self):
pass
class FeedLatency:
r"""
Provides order latency based on feed latency. The units of the arguments should match the timestamp units of your
data.
Order latency is computed as follows:
* feed_latency is calculated as the average latency between the latest feed's latency and the subsequent feed's
latency(by forward-looking).
* If either of these values is unavailable, the available value is used as the sole feed latency.
.. code-block::
entry_latency = feed_latency * entry_latency_mul + entry_latency
response_latency = feed_latency * resp_latency_mul + response_latency
Args:
entry_latency_mul: Multiplier for feed latency to compute order entry latency.
resp_latency_mul: Multiplier for feed latency to compute order response latency.
entry_latency: Offset for order entry latency.
response_latency: Offset for order response latency.
"""
entry_latency_mul: float64
resp_latency_mul: float64
entry_latency: float64
response_latency: float64
def __init__(
self,
entry_latency_mul=1,
resp_latency_mul=1,
entry_latency=0,
response_latency=0
):
self.entry_latency_mul = entry_latency_mul
self.resp_latency_mul = resp_latency_mul
self.entry_latency = entry_latency
self.response_latency = response_latency
def __latency(self, proc):
lat1 = -1
for row_num in range(proc.row_num, -1, -1):
local_timestamp = proc.data[row_num, COL_LOCAL_TIMESTAMP]
exch_timestamp = proc.data[row_num, COL_EXCH_TIMESTAMP]
if local_timestamp != -1 and exch_timestamp != -1:
lat1 = local_timestamp - exch_timestamp
break
lat2 = -1
for row_num in range(proc.next_row_num, len(proc.next_data)):
next_local_timestamp = proc.next_data[row_num, COL_LOCAL_TIMESTAMP]
next_exch_timestamp = proc.next_data[row_num, COL_EXCH_TIMESTAMP]
if next_local_timestamp != -1 and next_exch_timestamp != -1:
lat2 = next_local_timestamp - next_exch_timestamp
break
if lat1 != -1 and lat2 != -1:
return (lat1 + lat2) / 2.0
elif lat1 != -1:
return lat1
elif lat2 != -1:
return lat2
else:
raise ValueError
def entry(self, timestamp, order, proc):
return self.entry_latency + self.entry_latency_mul * self.__latency(proc)
def response(self, timestamp, order, proc):
return self.response_latency + self.resp_latency_mul * self.__latency(proc)
def reset(self):
pass
class ForwardFeedLatency:
r"""
Provides order latency based on feed latency. The units of the arguments should match the timestamp units of your
data.
Order latency is computed as follows:
* the subsequent feed's latency(by forward-looking) is used as the feed latency.
.. code-block::
entry_latency = feed_latency * entry_latency_mul + entry_latency
response_latency = feed_latency * resp_latency_mul + response_latency
Args:
entry_latency_mul: Multiplier for feed latency to compute order entry latency.
resp_latency_mul: Multiplier for feed latency to compute order response latency.
entry_latency: Offset for order entry latency.
response_latency: Offset for order response latency.
"""
entry_latency_mul: float64
resp_latency_mul: float64
entry_latency: float64
response_latency: float64
def __init__(
self,
entry_latency_mul=1,
resp_latency_mul=1,
entry_latency=0,
response_latency=0
):
self.entry_latency_mul = entry_latency_mul
self.resp_latency_mul = resp_latency_mul
self.entry_latency = entry_latency
self.response_latency = response_latency
def __latency(self, proc):
for row_num in range(proc.next_row_num, len(proc.next_data)):
next_local_timestamp = proc.next_data[row_num, COL_LOCAL_TIMESTAMP]
next_exch_timestamp = proc.next_data[row_num, COL_EXCH_TIMESTAMP]
if next_local_timestamp != -1 and next_exch_timestamp != -1:
return next_local_timestamp - next_exch_timestamp
return ValueError
def entry(self, timestamp, order, proc):
return self.entry_latency + self.entry_latency_mul * self.__latency(proc)
def response(self, timestamp, order, proc):
return self.response_latency + self.resp_latency_mul * self.__latency(proc)
def reset(self):
pass
class BackwardFeedLatency:
r"""
Provides order latency based on feed latency. The units of the arguments should match the timestamp units of your
data.
Order latency is computed as follows:
* the latest feed's latency is used as the feed latency.
.. code-block::
entry_latency = feed_latency * entry_latency_mul + entry_latency
response_latency = feed_latency * resp_latency_mul + response_latency
Args:
entry_latency_mul: Multiplier for feed latency to compute order entry latency.
resp_latency_mul: Multiplier for feed latency to compute order response latency.
entry_latency: Offset for order entry latency.
response_latency: Offset for order response latency.
"""
entry_latency_mul: float64
resp_latency_mul: float64
entry_latency: float64
response_latency: float64
def __init__(
self,
entry_latency_mul=1,
resp_latency_mul=1,
entry_latency=0,
response_latency=0
):
self.entry_latency_mul = entry_latency_mul
self.resp_latency_mul = resp_latency_mul
self.entry_latency = entry_latency
self.response_latency = response_latency
def __latency(self, proc):
for row_num in range(proc.row_num, -1, -1):
local_timestamp = proc.data[row_num, COL_LOCAL_TIMESTAMP]
exch_timestamp = proc.data[row_num, COL_EXCH_TIMESTAMP]
if local_timestamp != -1 and exch_timestamp != -1:
return local_timestamp - exch_timestamp
return ValueError
def entry(self, timestamp, order, proc):
return self.entry_latency + self.entry_latency_mul * self.__latency(proc)
def response(self, timestamp, order, proc):
return self.response_latency + self.resp_latency_mul * self.__latency(proc)
def reset(self):
pass
class IntpOrderLatency:
r"""
Provides order latency by interpolating the actual historical order latency. This model provides the most accurate
results. The units of the historical latency data should match the timestamp units of your feed data.
Args:
data (array): An (n, 3) array consisting of three columns: local timestamp when the request was made, exchange
timestamp, and local timestamp when the response was received.
"""
entry_rn: int64
resp_rn: int64
data: float64[:, :]
def __init__(self, data):
self.entry_rn = 0
self.resp_rn = 0
self.data = data
def __intp(self, x, x1, y1, x2, y2):
return (y2 - y1) / (x2 - x1) * (x - x1) + y1
def entry(self, timestamp, order, proc):
if timestamp < self.data[0, 0]:
# Finds a valid latency.
for row_num in range(len(self.data)):
if self.data[row_num, 1] > 0 and self.data[row_num, 0] > 0:
return self.data[row_num, 1] - self.data[row_num, 0]
raise ValueError
if timestamp >= self.data[-1, 0]:
# Finds a valid latency.
for row_num in range(len(self.data) - 1, -1, -1):
if self.data[row_num, 1] > 0 and self.data[row_num, 0] > 0:
return self.data[row_num, 1] - self.data[row_num, 0]
raise ValueError
for row_num in range(self.entry_rn, len(self.data) - 1):
req_local_timestamp = self.data[row_num, 0]
next_req_local_timestamp = self.data[row_num + 1, 0]
if req_local_timestamp <= timestamp < next_req_local_timestamp:
self.entry_rn = row_num
exch_timestamp = self.data[row_num, 1]
next_exch_timestamp = self.data[row_num + 1, 1]
# The exchange may reject an order request due to technical issues such congestion, this is particularly
# common in crypto markets. A timestamp of zero on the exchange represents the occurrence of those kinds
# of errors at that time.
if exch_timestamp <= 0 or next_exch_timestamp <= 0:
resp_timestamp = self.data[row_num, 2]
next_resp_timestamp = self.data[row_num + 1, 2]
lat1 = resp_timestamp - req_local_timestamp
lat2 = next_resp_timestamp - next_req_local_timestamp
# Negative latency indicates that the order is rejected for technical reasons, and its value
# represents the latency that the local experiences when receiving the rejection notification
return -self.__intp(timestamp, req_local_timestamp, lat1, next_req_local_timestamp, lat2)
lat1 = exch_timestamp - req_local_timestamp
lat2 = next_exch_timestamp - next_req_local_timestamp
return self.__intp(timestamp, req_local_timestamp, lat1, next_req_local_timestamp, lat2)
raise ValueError
def response(self, timestamp, order, proc):
if timestamp < self.data[0, 1]:
# Finds a valid latency.
for row_num in range(len(self.data)):
if self.data[row_num, 2] > 0 and self.data[row_num, 1] > 0:
return self.data[row_num, 2] - self.data[row_num, 1]
raise ValueError
if timestamp >= self.data[-1, 1]:
# Finds a valid latency.
for row_num in range(len(self.data) -1, -1, -1):
if self.data[row_num, 2] > 0 and self.data[row_num, 1] > 0:
return self.data[row_num, 2] - self.data[row_num, 1]
raise ValueError
for row_num in range(self.resp_rn, len(self.data) - 1):
exch_timestamp = self.data[row_num, 1]
next_exch_timestamp = self.data[row_num + 1, 1]
if exch_timestamp <= timestamp < next_exch_timestamp:
self.resp_rn = row_num
resp_local_timestamp = self.data[row_num, 2]
next_resp_local_timestamp = self.data[row_num + 1, 2]
lat1 = resp_local_timestamp - exch_timestamp
lat2 = next_resp_local_timestamp - next_exch_timestamp
if exch_timestamp <= 0 and next_exch_timestamp <= 0:
raise ValueError
elif exch_timestamp <= 0:
return lat2
elif next_exch_timestamp <= 0:
return lat1
lat = self.__intp(timestamp, exch_timestamp, lat1, next_exch_timestamp, lat2)
if lat < 0:
raise ValueError('Response latency cannot be negative.')
return lat
raise ValueError
def reset(self):
self.entry_rn = 0
self.resp_rn = 0