-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathtradeclassification_c.pyx
More file actions
executable file
·533 lines (421 loc) · 17.1 KB
/
tradeclassification_c.pyx
File metadata and controls
executable file
·533 lines (421 loc) · 17.1 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 30 13:16:59 2017
@author: sjurkatis
"""
#from libc.stdlib cimport malloc, free
import numpy as np
cimport numpy as np
cimport cython
#from cpython cimport array
#import array
DTYPE = np.int
ctypedef np.int_t DTYPE_t
DTYPEf = np.float64
ctypedef np.float64_t DTYPEf_t
@cython.boundscheck(False)
@cython.wraparound(False)
def get_ind(double [:] arr, double [:] time):
cdef:
int n = time.shape[0]
int s = arr.shape[0]
int begin, end, found, k
unsigned int ind = 0
size_t j, i
np.ndarray[DTYPE_t, ndim=1] left = np.zeros(n, dtype=DTYPE)
np.ndarray[DTYPE_t, ndim=1] right = np.zeros(n, dtype=DTYPE)
double [:] tmp
double v
for j in xrange(n):
begin = s
end = 0
found = 0
tmp = arr[ind:]
k = tmp.shape[0]
for i in xrange(k):
v = tmp[i] - time[j]
if not found and v>=0:
found = 1
begin = ind + i
if found:
if v > 0:
ind += i
end = ind
break
elif i==k-1:
end = s
break
left[j] = begin
right[j] = end
return left, right
@cython.boundscheck(False)
@cython.wraparound(False)
def sign_trades_ds1(long[:] P, long[:] V, long[:] Al, long[:] Ar, long[:] Bl, long[:] Br, long[:] runlength, long[:] askp, long[:] bidp, long[:] avdiff, long[:] bvdiff, double[:] atime, double[:] btime, double bar):
cdef:
int n = Al.shape[0]
int anum = askp.shape[0]
int bnum = bidp.shape[0]
int trnum = P.shape[0]
np.ndarray[DTYPE_t, ndim=1] s = np.zeros(trnum, dtype=DTYPE)
np.ndarray[DTYPE_t, ndim=1] c = np.zeros(trnum, dtype=DTYPE)
#np.ndarray[DTYPE_t, ndim=1] ask = np.zeros(trnum, dtype=DTYPE)
#np.ndarray[DTYPE_t, ndim=1] bid = np.zeros(trnum, dtype=DTYPE)
size_t j,i
int al, ar, bl, br, last_ask, last_bid, k, d_a, d_b, tmpind, p, v
np.ndarray[DTYPE_t, ndim=1] discard_a = np.zeros(anum, dtype=DTYPE)
np.ndarray[DTYPE_t, ndim=1] discard_b = np.zeros(bnum, dtype=DTYPE)
int ind = 0
bint av_match, bv_match
double upper, lower
for j in range(n):
al = Al[j]
ar = Ar[j]
bl = Bl[j]
br = Br[j]
last_ask = askp[al]
last_bid = bidp[bl]
for i in range(runlength[j]):
tmpind = ind+i
p = P[tmpind]
v = V[tmpind]
av_match = False
bv_match = False
#if ar>0:
# ask[tmpind] = last_ask
#else:
# ask[tmpind] = -1
#if br>0:
# bid[tmpind] = last_bid
#else:
# bid[tmpind] = -1
for k in range(ar-al):
if discard_a[al+k]==0 and askp[al+k]==p and avdiff[al+k]==v:
av_match = True
d_a = k
break
for k in range(br-bl):
if discard_b[bl+k]==0 and bidp[bl+k]==p and bvdiff[bl+k]==v:
bv_match = True
d_b = k
break
if av_match and not bv_match:
s[tmpind] = 1
c[tmpind] = 1
#ask[tmpind] = p
last_ask = askp[al+d_a]
elif bv_match and not av_match:
s[tmpind] = -1
c[tmpind] = 1
#bid[tmpind] = p
last_bid = bidp[bl+d_b]
elif av_match and bv_match:
if atime[al+d_a] < btime[bl+d_b]:
s[tmpind] = 1
c[tmpind] = 2
#ask[tmpind] = p
discard_a[al+d_a] = 1
last_ask = askp[al+d_a]
elif atime[al+d_a] > btime[bl+d_b]:
s[tmpind] = -1
c[tmpind] = 2
#bid[tmpind] = p
discard_b[bl+d_b] = 1
last_bid = bidp[bl+d_b]
else:
# not av_match and not bv_match:
# most likely hidden order
if last_ask > last_bid:
upper = last_ask*(1-bar) + last_bid*bar
lower = last_ask*bar + last_bid*(1-bar)
#x = (p-last_bid)/(last_ask-last_bid)
if p>upper: #x>1-bar:
s[tmpind] = 1
c[tmpind] = 3
elif p<lower: #x<bar:
s[tmpind] = -1
c[tmpind] = 3
ind +=runlength[j]
return s, c #, ask, bid
@cython.boundscheck(False)
@cython.wraparound(False)
def sign_trades_ds2(long[:] P, long[:] V, long[:] Al, long[:] Ar, long[:] Bl, long[:] Br, long[:] runlength, long[:] askp, long[:] bidp, long[:] avdiff, long[:] bvdiff, double[:] atime, double[:] btime, double bar):
cdef:
int n = Al.shape[0]
int anum = askp.shape[0]
int bnum = bidp.shape[0]
int trnum = P.shape[0]
np.ndarray[DTYPE_t, ndim=1] s = np.zeros(trnum, dtype=DTYPE)
np.ndarray[DTYPE_t, ndim=1] c = np.zeros(trnum, dtype=DTYPE)
size_t j,i,l
int al, ar, bl, br, last_ask, last_bid, k, d_a, d_b, tmpind, p, trV, v
int ind = 0
bint av_match, bv_match
double upper, lower
for j in range(n):
al = Al[j]
ar = Ar[j]
bl = Bl[j]
br = Br[j]
last_ask = askp[al]
last_bid = bidp[bl]
for i in range(runlength[j]):
tmpind = ind+i
p = P[tmpind]
trV = V[tmpind]
av_match = False
bv_match = False
for k in range(ar-al):
if askp[al+k]==p and avdiff[al+k]>=trV:
av_match = True
d_a = k
break
for k in range(br-bl):
if bidp[bl+k]==p and bvdiff[bl+k]>=trV:
bv_match = True
d_b = k
break
if av_match and not bv_match:
s[tmpind] = 1
c[tmpind] = 1
#last_ask = p
elif bv_match and not av_match:
s[tmpind] = -1
c[tmpind] = 1
#last_bid = p
elif av_match and bv_match:
if atime[al+d_a] < btime[bl+d_b]:
s[tmpind] = 1
c[tmpind] = 2
avdiff[al+d_a] -= trV
#last_ask = p
elif atime[al+d_a] > btime[bl+d_b]:
s[tmpind] = -1
c[tmpind] = 2
bvdiff[bl+d_b] -= trV
#last_bid = p
else:
# there are two possibilites for a visible order to not find a
# match:
# (1): a market order trades against the best quote and the next
# best quote, due to the size of the market order. The order
# book, however, records only best quote and volume before
# the market order and the new best quote with corresponding
# volume after the completion of the full market order. That is,
# we have a price match, but not the corresponding volume
# change match.
for k in range(ar-al):
if al+k+1==anum:
break
elif askp[al+k]<p and askp[al+k+1]==p: # this correctly checks also the price that we moved to the next interval
av_match = True
d_a = k+1
break
for k in range(br-bl):
if bl+k+1==bnum:
break
elif bidp[bl+k]>p and bidp[bl+k+1]==p:
bv_match = True
d_b = k+1
break
if av_match and not bv_match:
s[tmpind] = 1
c[tmpind] = 5
#last_ask = p
elif bv_match and not av_match:
s[tmpind] = -1
c[tmpind] = 5
#last_bid = p
elif av_match and bv_match:
if atime[al+d_a] < btime[bl+d_b]:
s[tmpind] = 1
c[tmpind] = 6
#last_ask = p
elif atime[al+d_a] > btime[bl+d_b]:
s[tmpind] = -1
c[tmpind] = 6
#last_bid = p
else:
# if there is no match with a price, there is a second possibility
# (2): The market order goes through the levels 1 to n>2 of
# the order book. The prices between level 1 and n are
# not displayed in the order book. Then all transactions
# taking place between level 1 and n have no price match
for k in range(ar-al):
if al+k+1==anum:
break
elif askp[al+k]<p and askp[al+k+1]>p:
av_match = True
d_a = k+1
break
for k in range(br-bl):
if bl+k+1==bnum:
break
elif bidp[bl+k]>p and bidp[bl+k+1]<p:
bv_match = True
d_b = k+1
break
if av_match and not bv_match:
s[tmpind] = 1
c[tmpind] = 7
#last_ask = p
elif bv_match and not av_match:
s[tmpind] = -1
c[tmpind] = 7
#last_bid = p
elif av_match and bv_match:
if atime[al+d_a] < btime[bl+d_b]:
s[tmpind] = 1
c[tmpind] = 8
#last_ask = p
elif atime[al+d_a] > btime[bl+d_b]:
s[tmpind] = -1
c[tmpind] = 8
#last_bid = p
else: # still no match; must be a hidden order
# not av_match and not bv_match:
# most likely hidden order
if last_ask > last_bid:
upper = last_ask*(1-bar) + last_bid*bar
lower = last_ask*bar + last_bid*(1-bar)
#x = (p-last_bid)/(last_ask-last_bid)
if p>upper: #x>1-bar:
s[tmpind] = 1
c[tmpind] = 3
elif p<lower: #x<bar:
s[tmpind] = -1
c[tmpind] = 3
ind +=runlength[j]
return s, c
@cython.boundscheck(False)
@cython.wraparound(False)
def sign_trades_ds3(long[:] P, long[:] V, long[:] Al, long[:] Ar, long[:] Bl, long[:] Br, long[:] runlength, long[:] askp, long[:] bidp, long[:] askv, long[:] bidv, double[:] atime, double[:] btime, double bar):
cdef:
int n = Al.shape[0]
int anum = askp.shape[0]
int bnum = bidp.shape[0]
int trnum = P.shape[0]
np.ndarray[DTYPE_t, ndim=1] s = np.zeros(trnum, dtype=DTYPE)
np.ndarray[DTYPE_t, ndim=1] c = np.zeros(trnum, dtype=DTYPE)
size_t j,i,l
int al, ar, bl, br, last_ask, last_bid, k, d_a, d_b, tmpind, p, trV
int ind = 0
bint av_match, bv_match
double upper, lower
for j in range(n):
al = Al[j]
ar = Ar[j]
bl = Bl[j]
br = Br[j]
last_ask = askp[al]
last_bid = bidp[bl]
for i in range(runlength[j]):
tmpind = ind+i
p = P[tmpind]
trV = V[tmpind]
av_match = False
bv_match = False
for k in range(ar-al):
if askp[al+k]==p and askv[al+k]>=trV:
av_match = True
d_a = k
break
for k in range(br-bl):
if bidp[bl+k]==p and bidv[bl+k]>=trV:
bv_match = True
d_b = k
break
if av_match and not bv_match:
s[tmpind] = 1
c[tmpind] = 1
elif bv_match and not av_match:
s[tmpind] = -1
c[tmpind] = 1
elif av_match and bv_match:
if atime[al+d_a] < btime[bl+d_b]:
s[tmpind] = 1
c[tmpind] = 2
askv[al+d_a] -=trV
elif atime[al+d_a] > btime[bl+d_b]:
s[tmpind] = -1
c[tmpind] = 2
bidv[bl+d_b] -=trV
else: # still no match; must be a hidden order
# not av_match and not bv_match:
# most likely hidden order
if last_ask > last_bid:
upper = last_ask*(1-bar) + last_bid*bar
lower = last_ask*bar + last_bid*(1-bar)
#x = (p-last_bid)/(last_ask-last_bid)
if p>upper: #x>1-bar:
s[tmpind] = 1
c[tmpind] = 3
elif p<lower: #x<bar:
s[tmpind] = -1
c[tmpind] = 3
ind +=runlength[j]
return s, c
@cython.boundscheck(False)
@cython.wraparound(False)
def tick_rule(long[:] allprices, long[:] prices, long[:] index_p):
cdef:
int n = prices.shape[0]
np.ndarray[DTYPE_t, ndim=1] s = np.zeros(n, dtype=DTYPE)
int t, p, count, sz, lp
long [:] lstp
size_t j
for j in range(n):
count = 0
p = prices[j]
t = index_p[j]
lstp = allprices[:t]
sz = lstp.shape[0]
s[j] = 0
while sz>count:
count += 1
lp = lstp[sz-count]
if p>lp:
s[j] = 1
break
elif p<lp:
s[j] = -1
break
return s #[s[j] for j in range(n)]
@cython.boundscheck(False)
@cython.wraparound(False)
def vol_bin(long[:] x, long w):
cdef:
long vol = 0
long g = 0
int n = x.shape[0]
np.ndarray[DTYPE_t, ndim=1] group = np.zeros(n, dtype=DTYPE)
size_t i
for i in range(n):
vol += x[i]
group[i] = g
if vol>=w:
g+=1
vol = 0
return group
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def concat_runs(long[:] x, bint hj_version=False):
cdef:
int n = x.shape[0]
size_t i, j
double k
size_t count = 0
np.ndarray[DTYPEf_t, ndim=1] interp = np.zeros(sum(x), dtype=DTYPEf)
if hj_version:
for i in range(n):
k = x[i]+1.
for j in range(1,x[i]+1):
interp[count] = (2*j-1)/(2*k)
count += 1
else:
for i in range(n):
k = x[i]+1.
for j in range(1,x[i]+1):
interp[count] = j/k
count += 1
return interp