-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraft 1.py
587 lines (467 loc) · 20.7 KB
/
draft 1.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
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
"""
CAPM Beta Algorithmic Trading Case
Rotman BMO Finance Research and Trading Lab, Uniersity of Toronto (C) All rights reserved.
Preamble:
-> Code will have a small start up period; however, trades should only be executed once forward market price is available,
hence there should not be any issue caused.
-> Code only runs effectively if the News articles are formatted as they are now. The only way to get the required new data is by parsing the text.
"""
import re
import signal
import time
import requests
from time import sleep
from sklearn.linear_model import LinearRegression
import pandas as pd
import numpy as np
import numpy.ma as ma
import concurrent.futures
CAPM_vals = {}
expected_return = {}
real_return = {}
profit = {}
open_order_status = "NONE"
order_id = 0
front_running = True
front_running_quantity = 1000
mege_order_size = 100000000
RITM_P_ALL = np.zeros(600)
RITM_R_ALL = np.zeros(600)
aa_P_ALL = np.zeros(600)
aa_LP_ALL = np.zeros(600)
aa_R_ALL = np.zeros(600)
aa_BETA_ALL = np.zeros(600)
aa_BETAMA_ALL = np.zeros(600)
aa_BETAC_ALL = np.zeros(600)
bb_P_ALL = np.zeros(600)
bb_LP_ALL = np.zeros(600)
bb_R_ALL = np.zeros(600)
bb_BETA_ALL = np.zeros(600)
bb_BETAMA_ALL = np.zeros(600)
bb_BETAC_ALL = np.zeros(600)
cc_P_ALL = np.zeros(600)
cc_LP_ALL = np.zeros(600)
cc_R_ALL = np.zeros(600)
cc_BETA_ALL = np.zeros(600)
cc_BETAMA_ALL = np.zeros(600)
cc_BETAC_ALL = np.zeros(600)
# class that passes error message, ends the program
class ApiException(Exception):
pass
# code that lets us shut down if CTRL C is pressed
def signal_handler(signum, frame):
global shutdown
signal.signal(signal.SIGINT, signal.SIG_DFL)
shutdown = True
API_KEY = {"X-API-Key": "114514"}
shutdown = False
session = requests.Session()
session.headers.update(API_KEY)
def get_position(ticker):
sec = session.get("http://localhost:9999/v1/securities", params={"ticker": ticker}).json()
return sec["position"]
def gross_position():
return abs(get_position("ALPHA")) + abs(get_position("GAMMA")) + abs(get_position("THETA"))
def net_position():
return get_position("ALPHA") + get_position("GAMMA") + get_position("THETA")
def get_tick(session):
resp = session.get("http://localhost:9999/v1/case")
if resp.ok:
case = resp.json()
return case["tick"]
raise ApiException("fail - cant get tick")
# code that parses the first and latest news instances for forward market predictions and the risk free rate
# Important: this code only works if the only '%' character is in front of the RISK FREE RATE and the onle '$' character is in front of the forward price suggestions
def get_news(session):
news = session.get("http://localhost:9999/v1/news")
if news.ok:
newsbook = news.json()
for i in range(len(newsbook[-1]["body"])):
if newsbook[-1]["body"][i] == "%":
CAPM_vals["%Rf"] = round(
float(newsbook[-1]["body"][i - 4: i]) / 100, 4
)
latest_news = newsbook[0]
if len(newsbook) > 1:
# print(latest_news["body"])
forward = re.findall(r"\d+\.\d+", latest_news["body"])
if not forward:
forward = re.findall(r"\d+", latest_news["body"])
CAPM_vals["forward"] = float(forward[0])
CAPM_vals["tick"] = int(re.findall(r"\d+", latest_news["body"])[0])
return CAPM_vals
raise ApiException("timeout")
# gets all the price data for all securities
def pop_prices(session):
price_act = session.get("http://localhost:9999/v1/securities")
if price_act.ok:
prices = price_act.json()
return prices
raise ApiException("fail - cant get securities")
# Buy or Sell function, put in your own parameters
def buy_or_sell(session, real_return): # , signal="BUY"):
max_er = max(real_return, key=real_return.get)
min_er = min(real_return, key=real_return.get)
gross_pos = abs(get_position("ALPHA")) + abs(get_position("GAMMA")) + abs(get_position("THETA"))
net_pos = get_position("ALPHA") + get_position("GAMMA") + get_position("THETA")
if real_return[max_er] > 0.6 and gross_pos < 250000 and net_pos < 100000:
r = session.post(
"http://localhost:9999/v1/orders",
params={
"ticker": max_er,
"type": "MARKET",
"quantity": "10000",
"action": "BUY",
},
)
if real_return[min_er] < -0.6 and gross_pos < 250000 and net_pos > -100000:
r = session.post(
"http://localhost:9999/v1/orders",
params={
"ticker": min_er,
"type": "MARKET",
"quantity": "10000",
"action": "SELL",
}
)
def post_market_order(ticker, action, quantity):
r = session.post(
"http://localhost:9999/v1/orders",
params={
"ticker": ticker,
"type": "MARKET",
"quantity": quantity,
"action": action,
},
)
if r.status_code == 429:
wait = r.json()["wait"]
sleep(wait)
post_market_order(ticker, action, quantity)
return r
def post_limit_order(ticker, action, quantity, price):
r = session.post(
"http://localhost:9999/v1/orders",
params={
"ticker": ticker,
"type": "LIMIT",
"quantity": quantity,
"action": action,
"price": price,
},
)
if r.status_code == 429:
wait = r.json()["wait"]
sleep(wait)
post_market_order(ticker, action, quantity)
return r
def pop_last_mega_transacted_price(ticker):
s = session.get(f"http://localhost:9999/v1/securities/book?ticker={ticker}").json()
for order in s['bids']:
if order['quantity'] > 10000:
print("mega order found")
print("order size: ", order['quantity'])
print(order)
if order["quantity_filled"] > 0:
return order['price']
for order in s['asks']:
if order['quantity'] > 10000:
print("mega order found")
print("order size: ", order['quantity'])
print(order)
if order["quantity_filled"] > 0:
return order['price']
raise Exception("No last transacted mega order found")
def setup_portfolio(CAPM_vals, real_return, action):
max_er = max(real_return, key=real_return.get)
min_er = min(real_return, key=real_return.get)
# gross_pos = abs(get_position("ALPHA")) + abs(get_position("GAMMA")) + abs(get_position("THETA"))
# net_pos = get_position("ALPHA") + get_position("GAMMA") + get_position("THETA")
for _ in range(10):
post_market_order(max_er, "BUY" if action == "BUY" else "SELL", 10000)
for _ in range(10):
r = post_market_order(min_er, "SELL" if action == "SELL" else "BUY", 10000)
print(r)
for _ in range(5):
post_market_order(max_er, "BUY" if action == "BUY" else "SELL", 10000)
def close_all_positions(session):
securities = session.get("http://localhost:9999/v1/securities").json()
for sec in securities:
if sec["position"] > 0:
position = sec["position"]
while position > 0:
session.post(
"http://localhost:9999/v1/orders",
params={
"ticker": sec["ticker"],
"type": "MARKET",
"quantity": min(position, 10000),
"action": "SELL",
},
)
position -= 10000
sleep(0.1)
elif sec["position"] < 0:
position = sec["position"]
while position < 0:
session.post(
"http://localhost:9999/v1/orders",
params={
"ticker": sec["ticker"],
"type": "MARKET",
"quantity": min(-position, 10000),
"action": "BUY",
},
)
position += 10000
sleep(0.1)
def get_last_transacted_mega_order_price(ticker):
s = session.get(f"http://localhost:9999/v1/securities/book", params={"ticker": ticker}).json()
for open_order in s['bids']:
if open_order['quantity'] > 10000 and open_order['quantity_filled'] > 0:
return open_order['price']
for open_order in s['asks']:
if open_order['quantity'] > 10000 and open_order['quantity_filled'] > 0:
return open_order['price']
return None
def mega_order_exists(ticker, side):
s = session.get(f"http://localhost:9999/v1/securities/book", params={"ticker": ticker}).json()
for open_order in s[side]:
if open_order['quantity'] > 10000:
return True
return False
def retrieve_mega_order(ticker, side=""):
s = session.get(f"http://localhost:9999/v1/securities/book", params={"ticker": ticker}).json()
for open_order in s['asks']:
if open_order['quantity'] > 10000:
return open_order['price'], open_order['quantity'], open_order['quantity_filled'], open_order['order_id']
def main():
S2aa = 0
S2taa = 0
Windows1 = 5
SH1 = 2
aa = 0.5
id00aa = 0
tt00aa = 0
with requests.Session() as session:
session.headers.update(API_KEY)
holding_position = False
while get_tick(session) < 600 and not shutdown:
# print("running")
tt = get_tick(session)
RITM_P_ALL[tt - 1] = \
session.get("http://localhost:9999/v1/securities", params={"ticker": "RITM"}).json()[0][
"last"]
if tt > 2:
RITM_R_ALL[tt - 1] = RITM_P_ALL[tt - 1] / RITM_P_ALL[tt - 2]
# ALPHA
aa_LP_ALL[tt - 1] = session.get("http://localhost:9999/v1/securities", params={"ticker": "ALPHA"}).json()[0][
"last"]
# Check if mega order exist,
# 1. If mega order does not exist, pass
# 2. if mega order does exist, retrieve price as pp00, size as size00, filled as ff00
# 2.1 If ff00 == 0, pass
# 2.2 If ff00 > 0, aa_P_ALL[tt-1] = pp00, k
if mega_order_exists("ALPHA", "asks"):
pp00, size00, ff00, mega_order_id = retrieve_mega_order("ALPHA")
if ff00 == 0:
aa_P_ALL[tt - 1] = aa_LP_ALL[tt - 1]
if ff00 > 0:
aa_P_ALL[tt - 1] = pp00
else:
aa_P_ALL[tt - 1] = aa_LP_ALL[tt - 1]
# print("aa_P_ALL: ", aa_P_ALL[tt - 1])
if tt > 2:
if np.isnan(aa_P_ALL[tt - 1]):
pp00 = aa_LP_ALL[tt - 1]
else:
pp00 = aa_P_ALL[tt - 1]
if np.isnan(aa_P_ALL[tt - 2]):
pp11 = aa_LP_ALL[tt - 2]
else:
pp11 = aa_P_ALL[tt - 2]
aa_R_ALL[tt - 1] = pp00 / pp11
# print("aa_R_ALL: ", aa_R_ALL[tt - 1])
if tt > Windows1:
sr00 = aa_R_ALL[tt - Windows1:tt]
mr00 = RITM_R_ALL[tt - Windows1:tt]
tmp1 = np.isnan(sr00)
tmp2 = np.isnan(mr00)
# merge tmp1 and tmp2 union set
tmp3 = np.logical_and(~tmp1, ~tmp2)
sr11 = sr00[tmp3]
mr11 = mr00[tmp3]
aa_BETA_ALL[tt - 1] = np.cov(sr11, mr11)[0][1] / np.var(sr11)
if tt > 2 * Windows1:
aa_BETAMA_ALL[tt - 1] = aa_BETA_ALL[tt - Windows1:tt].mean()
if abs(aa_BETA_ALL[tt - 1] - aa_BETAMA_ALL[tt - 1]) >= 0.2:
aa_BETAC_ALL[tt - 1] = aa_BETA_ALL[tt - 2]
if abs(aa_BETA_ALL[tt - 1] - aa_BETAMA_ALL[tt - 1]) < 0.2:
aa_BETAC_ALL[tt - 1] = aa_BETA_ALL[tt - 1]
else:
aa_BETAC_ALL[tt - 1] = aa_BETA_ALL[tt - 1]
# THETA
bb_LP_ALL[tt - 1] = session.get("http://localhost:9999/v1/securities", params={"ticker": "THETA"}).json()[0][
"last"]
# Check if mega order exist,
# 1. If mega order does not exist, pass
# 2. if mega order does exist, retrieve price as pp00, size as size00, filled as ff00
# 2.1 If ff00 == 0, pass
# 2.2 If ff00 > 0, bb_P_ALL[tt-1] = pp00, k
if mega_order_exists("THETA", "asks"):
pp00, size00, ff00, mega_order_id = retrieve_mega_order("THETA")
if ff00 == 0:
bb_P_ALL[tt - 1] = bb_LP_ALL[tt - 1]
if ff00 > 0:
bb_P_ALL[tt - 1] = pp00
else:
bb_P_ALL[tt - 1] = bb_LP_ALL[tt - 1]
# print("bb_P_ALL: ", bb_P_ALL[tt - 1])
if tt > 2:
if np.isnan(bb_P_ALL[tt - 1]):
pp00 = bb_LP_ALL[tt - 1]
else:
pp00 = bb_P_ALL[tt - 1]
if np.isnan(bb_P_ALL[tt - 2]):
pp11 = bb_LP_ALL[tt - 2]
else:
pp11 = bb_P_ALL[tt - 2]
bb_R_ALL[tt - 1] = pp00 / pp11
# print("bb_R_ALL: ", bb_R_ALL[tt - 1])
if tt > Windows1:
sr00 = bb_R_ALL[tt - Windows1:tt]
mr00 = RITM_R_ALL[tt - Windows1:tt]
tmp1 = np.isnan(sr00)
tmp2 = np.isnan(mr00)
# merge tmp1 and tmp2 union set
tmp3 = np.logical_and(~tmp1, ~tmp2)
sr11 = sr00[tmp3]
mr11 = mr00[tmp3]
bb_BETA_ALL[tt - 1] = np.cov(sr11, mr11)[0][1] / np.var(sr11)
if tt > 2 * Windows1:
bb_BETAMA_ALL[tt - 1] = bb_BETA_ALL[tt - Windows1:tt].mean()
if abs(bb_BETA_ALL[tt - 1] - bb_BETAMA_ALL[tt - 1]) >= 0.2:
bb_BETAC_ALL[tt - 1] = bb_BETA_ALL[tt - 2]
if abs(bb_BETA_ALL[tt - 1] - bb_BETAMA_ALL[tt - 1]) < 0.2:
bb_BETAC_ALL[tt - 1] = bb_BETA_ALL[tt - 1]
else:
bb_BETAC_ALL[tt - 1] = bb_BETA_ALL[tt - 1]
# GAMMA
CC_LP_ALL[tt - 1] = session.get("http://localhost:9999/v1/securities", params={"ticker": "GAMMA"}).json()[0][
"last"]
# Check if mega order exist,
# 1. If mega order does not exist, pass
# 2. if mega order does exist, retrieve price as pp00, size as size00, filled as ff00
# 2.1 If ff00 == 0, pass
# 2.2 If ff00 > 0, CC_P_ALL[tt-1] = pp00, k
if mega_order_exists("GAMMA", "asks"):
pp00, size00, ff00, mega_order_id = retrieve_mega_order("GAMMA")
if ff00 == 0:
CC_P_ALL[tt - 1] = CC_LP_ALL[tt - 1]
if ff00 > 0:
CC_P_ALL[tt - 1] = pp00
else:
CC_P_ALL[tt - 1] = CC_LP_ALL[tt - 1]
# print("CC_P_ALL: ", CC_P_ALL[tt - 1])
if tt > 2:
if np.isnan(CC_P_ALL[tt - 1]):
pp00 = CC_LP_ALL[tt - 1]
else:
pp00 = CC_P_ALL[tt - 1]
if np.isnan(CC_P_ALL[tt - 2]):
pp11 = CC_LP_ALL[tt - 2]
else:
pp11 = CC_P_ALL[tt - 2]
CC_R_ALL[tt - 1] = pp00 / pp11
# print("CC_R_ALL: ", CC_R_ALL[tt - 1])
if tt > Windows1:
sr00 = CC_R_ALL[tt - Windows1:tt]
mr00 = RITM_R_ALL[tt - Windows1:tt]
tmp1 = np.isnan(sr00)
tmp2 = np.isnan(mr00)
# merge tmp1 and tmp2 union set
tmp3 = np.logical_and(~tmp1, ~tmp2)
sr11 = sr00[tmp3]
mr11 = mr00[tmp3]
CC_BETA_ALL[tt - 1] = np.cov(sr11, mr11)[0][1] / np.var(sr11)
if tt > 2 * Windows1:
CC_BETAMA_ALL[tt - 1] = CC_BETA_ALL[tt - Windows1:tt].mean()
if abs(CC_BETA_ALL[tt - 1] - CC_BETAMA_ALL[tt - 1]) >= 0.2:
CC_BETAC_ALL[tt - 1] = CC_BETA_ALL[tt - 2]
if abs(CC_BETA_ALL[tt - 1] - CC_BETAMA_ALL[tt - 1]) < 0.2:
CC_BETAC_ALL[tt - 1] = CC_BETA_ALL[tt - 1]
else:
CC_BETAC_ALL[tt - 1] = CC_BETA_ALL[tt - 1]
# ----------------- measure -----------------
if STOCK_BETAC_ALL[tt - 1] != 0:
if S2aa == 1 and tt - S2taa <= 2:
continue
if S2aa == 1 and tt - S2taa > 2 and id00aa != 0:
q00 = session.get("http://localhost:9999/v1/orders/" + str(id00aa)).json()["quantity_filled"]
session.delete("http://localhost:9999/v1/orders/" + str(id00aa))
session.post("http://localhost:9999/v1/orders", params={"ticker": "ALPHA", "type": "MARKET", "quantity": Vt * aa - q00, "action": "SELL"})
S2aa = 0
id00aa = 0
q00 = 0
if S2aa == 0:
if STOCK_BETAC_ALL[tt - 1] * (RITM_P_ALL[tt - 1] - RITM_P_ALL[tt - 3]) > 0:
S2aa = 1
if mega_order_exists("ALPHA", "asks"):
print("here exist mega order")
pp00, size00, ff00, mega_order_id = retrieve_mega_order("ALPHA")
# get orderbook
sec = session.get("http://localhost:9999/v1/securities/book", params={"ticker": "ALPHA"}).json()
Vt = 0
for order in sec['asks']:
if order['quantity'] <= 10000 and order["price"] < pp00 -0.05:
Vt += order['quantity']
print("Vt: ", Vt)
if Vt * aa > 10000:
S2aa = 0
elif Vt > 0:
session.post("http://localhost:9999/v1/orders", params={"ticker": "ALPHA", "type": "MARKET", "quantity": Vt * aa, "action": "BUY"})
r = session.post("http://localhost:9999/v1/orders", params={"ticker": "ALPHA", "type": "LIMIT", "quantity": Vt * aa, "action": "SELL", "price": pp00 - 0.01}).json()
print("limit order: ", r)
id00aa = r["order_id"]
print("id00aa: ", id00aa)
print("tt00aa: ", tt00aa)
print("order info", session.get("http://localhost:9999/v1/orders/" + str(id00aa)).json())
tt00aa = get_tick(session)
S2taa = tt00aa
else:
S2aa = 0
if mega_order_exists("ALPHA", "asks"):
pp11, size11, ff11, mega_order_id11 = retrieve_mega_order("ALPHA")
if pp11 >= pp00:
tt11 = get_tick(session)
if tt11 - tt00aa >= SH1 and id00aa != 0:
q00 = session.get("http://localhost:9999/v1/orders/" + str(id00aa)).json()["quantity_filled"]
session.delete("http://localhost:9999/v1/orders/" + str(id00aa))
session.post("http://localhost:9999/v1/orders", params={"ticker": "ALPHA", "type": "MARKET", "quantity": Vt * aa - q00, "action": "SELL"})
S2aa = 0
id00aa = 0
q00 = 0
else:
continue
else:
q00 = session.get("http://localhost:9999/v1/orders/" + str(id00aa)).json()[
"quantity_filled"]
session.delete("http://localhost:9999/v1/orders/" + str(id00aa))
session.post("http://localhost:9999/v1/orders",
params={"ticker": "ALPHA", "type": "MARKET", "quantity": Vt * aa - q00,
"action": "SELL"})
S2aa = 0
id00aa = 0
q00 = 0
else:
q00 = session.get("http://localhost:9999/v1/orders/" + str(id00aa)).json()["quantity_filled"]
session.delete("http://localhost:9999/v1/orders/" + str(id00aa))
session.post("http://localhost:9999/v1/orders", params={"ticker": "ALPHA", "type": "MARKET", "quantity": Vt * aa - q00, "action": "SELL"})
S2aa = 0
id00aa = 0
q00 = 0
else:
S2aa = 0
sleep(0.2)
if __name__ == "__main__":
main()