-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event.py
296 lines (260 loc) · 11.9 KB
/
Event.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
class Event:
def __init__(self, data):
self.author = 'AllOptions'
self.data = data
self.event_list = [] # magnitude of event
self.event_init = [] # when event was initiated
self.decay = [] # identifier of event
self.constant = 10 ** 6 # adjust dt to millisecond (increases numerical accuracy)
# self.espd = 1.0 / 2.0
self.event_type = ''
self.decay_type = ''
self.event_limit = 4.0 * 10 ** 9 # 10 sec old events are removed
self.delay_roundtrip = 10.0 * 10 ** 4 # start event 10 micros after it is observed
# Event specific parameters
self.eZero_trig = 200 # trigger event traded volumes are greater than 20
self.paraImp = [] # paramters for Impulse
self.paraD = [] # paramters for Decay
self.fCol = (0, 1) # column of features to use
self.mxEvent = 1.25 # |sum events| <= mxEvent
self.lots_traded_trigger = 5
self.LT_trig1 = 200
self.LT_trig2 = 500
self.LT_trig3 = 1000
self.LT_trig4 = 1500
self.LT_decay1 = 2 # 1 - 2 * milliseconds (0 @ 0.5 milliseconds)
self.LT_decay2 = 2
self.LT_decay3 = 2
self.LT_decay4 = 1 # 1 - 1 * milliseconds (0 @ 1.0 milliseconds)
self.LT_resp1 = 0.1
self.LT_resp2 = 0.2
self.LT_resp3 = 0.4
self.LT_resp4 = 1.0
# self.e2_trig1 = 10 # trigger: volumes are greater than 20
# self.e2_trig2 = 30 # end of first trigger, start of second
# self.e2_resp1 = 0.5 # size of shift
# self.e2_resp2 = 1.0
# self.e2_mean_rev1 = 0.5 # mean reversion
# self.e2_mean_rev2 = 0.5
# self.e2_tabletop = 0.0 # delay before signal decay starts 200.000 nanoseconds / 200 micro
# self.e2_spread = False # adjust with spread
# self.e2_voladj = False # adjust with traded volume
def get_events(self, e):
# Check for old event, remove event
for i in range(len(self.event_init)-1, -1, -1):
if self.data.tgates[e] - self.event_init[i] > self.event_limit:
del self.event_list[i]
del self.event_init[i]
del self.decay[i]
# Account for decay in events and sum total impulse
if self.decay_type == 'linear':
out = self.linear_decay(e)
elif self.decay_type == 'exp':
out = self.exponential_decay(e)
elif self.decay_type == 'LongMemory':
out = self.LongMemory(e) # CHECK, Currently no LM term included.
elif self.decay_type == 'LastTrade':
out = self.Last_Trade_decay(e)
else:
print('WARNING: decay_type not recognised')
# Check for new event, add event
if self.event_type == 'eZero':
self.eZero(self.data.vols[e], self.data.sellbuys[e], self.data.tgates[e], self.data.features[e, self.fCol])
elif self.event_type == 'evntLT':
self.evntLT(self.data.vols[e], self.data.sellbuys[e], self.data.tgates[e])
if self.event_type == 'eOmniPresent':
self.eOmniPresent(self.data.vols[e], self.data.sellbuys[e], self.data.tgates[e], self.data.features[e, self.fCol])
return out
""" Events """
def eZero(self, trade_vol, sellbuy, timestamp, features):
# Event is triggered if traded volume >= trigger.
if trade_vol >= self.eZero_trig:
if sellbuy == 'BUY':
bs = 1
else:
bs = -1
# Compute Impulse
if len(self.paraImp) > 2: # Multiple regression
impulse = self.paraImp[0] # constant/intercept term
for p in range(1, len(self.paraImp)):
impulse += self.paraImp[p] * features[p-1]
impulse = bs * impulse
elif len(self.paraImp) == 1: # Constant term only
impulse = bs * self.paraImp[0]
else:
impulse = bs * (self.paraImp[0] + self.paraImp[1] * features) # Simple regression
# Compute Decay
if len(self.paraD) > 2:
decay = self.paraD[0]
for p in range(1, len(self.paraD)):
decay += self.paraD[p] * features[p-1]
elif len(self.paraD) == 1:
decay = self.paraD[0]
else:
decay = self.paraD[0] + self.paraD[1] * features
self.event_list.append(impulse)
self.decay.append(decay)
self.event_init.append(timestamp)
def eOmniPresent(self, trade_vol, sellbuy, timestamp, features):
# Event is triggered if traded volume >= trigger.
if trade_vol >= self.lots_traded_trigger:
if sellbuy == 'BUY':
bs = 1.0
else:
bs = -1.0
# Compute Impulse
if len(self.paraImp) > 2: # Multiple regression
impulse = self.paraImp[0] # constant/intercept term
for p in range(1, len(self.paraImp)):
impulse += self.paraImp[p] * features[p-1]
impulse = bs * impulse
elif len(self.paraImp) == 1: # Constant term only
impulse = bs * self.paraImp[0]
else:
impulse = bs * (self.paraImp[0] + self.paraImp[1] * features) # Simple regression
# Compute Decay
decay = self.paraD
self.event_list.append(impulse)
self.decay.append(decay)
self.event_init.append(timestamp)
def evntLT(self, trade_vol, sellbuy, timestamp):
# LT event.
if trade_vol >= self.LT_trig1:
if sellbuy == 'BUY':
bs = 1
else:
bs = -1
if self.LT_trig2 > trade_vol >= self.LT_trig1: # Bucket 1
self.event_list.append(bs * self.LT_resp1)
self.event_init.append(timestamp)
self.decay.append(self.LT_decay1)
elif self.LT_trig3 > trade_vol >= self.LT_trig2: # Bucket 2
self.event_list.append(bs * self.LT_resp2)
self.event_init.append(timestamp)
self.decay.append(self.LT_decay2)
elif self.LT_trig4 > trade_vol >= self.LT_trig3: # Bucket 3
self.event_list.append(bs * self.LT_resp3)
self.event_init.append(timestamp)
self.decay.append(self.LT_decay3)
elif trade_vol >= self.LT_trig4: # Bucket 4
self.event_list.append(bs * self.LT_resp4)
self.event_init.append(timestamp)
self.decay.append(self.LT_decay4)
""" Decay """
def LongMemory(self, e):
# Long Memory does not include long memory yet. To be included
from math import exp
out = 0.0
for i in range(len(self.event_init)): # Go through all events, and discount
dt = (self.data.tgates[e] - self.event_init[i]) / self.constant
if dt < 0:
print('WARNING: TIMESTAMPS NOT IN ORDER, SEE Event.LongMemory, started: ' +
str(self.event_init[i]) + ' evaluated: ' + str(self.data.tgates[e]))
if dt > self.delay_roundtrip / self.constant: # Only consider signals older than 10 micros,
if self.decay[i] < 0: # Error handling: opt tries negative values
out += 10.0 # Gives a penalty
else:
out += exp(-self.decay[i] * (dt - self.delay_roundtrip / self.constant)) * self.event_list[i]
# Cap the total amount of adjustment allowed.
if out > self.mxEvent:
out = self.mxEvent
if out < -self.mxEvent:
out = -self.mxEvent
return out
def exponential_decay(self, e):
from math import exp
out = 0.0
for i in range(len(self.event_init)):
dt = (self.data.tgates[e] - self.event_init[i]) / self.constant
if self.decay[i] < 0: # Error handling, opt tries negative values
self.decay[i] = 0
if dt <= self.e2_tabletop: # Decay starts after 0.2: 200,000 nano-sec
out += self.event_list[i]
else:
out += exp(-self.decay[i] * dt) * self.event_list[i]
# Cap the total amount of adjustment allowed.
if out > self.mxEvent:
out = self.mxEvent
if out < -self.mxEvent:
out = -self.mxEvent
return out
def linear_decay(self, e):
out = 0.0
for i in range(len(self.event_init)):
dt = (self.data.tgates[e] - self.event_init[i]) / self.constant
out += max((1 - self.decay[i] * dt), 0.0) * self.event_list[i]
# Cap the total amount of adjustment allowed.
if out > self.mxEvent:
out = self.mxEvent
if out < -self.mxEvent:
out = -self.mxEvent
return out
def Last_Trade_decay(self, e):
# Last Trade assumes linear decay, and only cares about last trade.
if len(self.event_init) > 0:
dt = (self.data.tgates[e] - self.event_init[-1]) / self.constant
out = max((1 - self.decay[-1] * dt), 0.0) * self.event_list[-1]
else:
out = 0.0
# Cap the total amount of adjustment allowed.
if out > self.mxEvent:
out = self.mxEvent
if out < -self.mxEvent:
out = -self.mxEvent
return out
# def e2_large_trade(self, trade_vol, sellbuy, timestamp, price, theo):
# # Event is triggered if volume1 >= trigger1 < volume2 >= trigger2.
# if self.e2_voladj is True:
# voladj = trade_vol
# else:
# voladj = 1
#
# if self.e2_spread is False:
#
# if self.e2_trig2 >= trade_vol >= self.e2_trig1:
#
# if sellbuy == 'BUY':
# self.event_list.append(self.e2_resp1 * voladj)
# else:
# self.event_list.append(-self.e2_resp1 * voladj)
#
# self.event_init.append(timestamp)
# self.decay.append(self.e2_mean_rev1)
# elif trade_vol > self.e2_trig2:
# if sellbuy == 'BUY':
# self.event_list.append(self.e2_resp2 * voladj)
# else:
# self.event_list.append(-self.e2_resp2 * voladj)
# self.event_init.append(timestamp)
# self.decay.append(self.e2_mean_rev2)
#
# elif self.e2_spread is True:
# if self.e2_trig2 >= trade_vol >= self.e2_trig1:
# cross = self.e2_resp1 * abs(price - theo)
#
# if sellbuy == 'BUY':
# self.event_list.append(cross * voladj)
# else:
# self.event_list.append(-cross * voladj)
#
# self.event_init.append(timestamp)
# self.decay.append(self.e2_mean_rev1)
#
# elif trade_vol > self.e2_trig2:
# cross = self.e2_resp2 * abs(price - theo)
# if sellbuy == 'BUY':
# self.event_list.append(cross * voladj)
# else:
# self.event_list.append(-cross * voladj)
# self.event_init.append(timestamp)
# self.decay.append(self.e2_mean_rev2)
# else:
# print('WARNING: self.e2_spread not set correctly \n')
#
# def smart_mid(self, ask, askvol, bid, bidvol):
# """ Returns smart P """
# if bid - ask > self.espd * 2.0 :
# mid = 0.5 * (bid + ask)
# else:
# mid = (bid * askvol + ask * bidvol) / (bidvol + askvol)
# return mid