-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
153 lines (121 loc) · 4.22 KB
/
utils.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
from datetime import datetime
import json
import time
import boto3
import requests
def get_secret(id_secret):
"""
Gets a secret from AWS
"""
client_secrets = boto3.client("secretsmanager")
resp = client_secrets.get_secret_value(SecretId=id_secret)
assert (
resp["ResponseMetadata"]["HTTPStatusCode"] == 200
), "Secrets get failed with non-200 status"
return json.loads(resp["SecretString"])
def put_secret(id_secret, secret):
"""
Puts a secret in AWS
"""
client_secrets = boto3.client("secretsmanager")
resp = client_secrets.put_secret_value(
SecretId=id_secret, SecretString=json.dumps(secret)
)
assert (
resp["ResponseMetadata"]["HTTPStatusCode"] == 200
), "Secrets put failed with non-200 status"
return True
def generate_experiment(
accounts=["Robinhood_Noise", "Robinhood_Alpha"], lag=10, n_orders=1
):
today = datetime.now().strftime("%Y-%m-%d")
i_obs = 0
obs = []
# Create trades starting lag seconds from now
ts_start = int(time.time_ns() + lag * 1e9)
ts_open = ts_start
for i in range(n_orders):
for account in accounts:
for order_type in ["limit"]:
ob = {
"id": i_obs,
"date_open": today,
"account": account,
"symbol": "SPY",
"ts_open_utc_ns": ts_open,
"ts_close_utc_ns": int(ts_open + 5.5 * 60 * 1e9),
"order_size": 1, # Dollars
}
if order_type == "limit":
ob.update({"order_type": order_type, "limit_price": -500})
elif order_type == "market":
ob.update(
{
"order_type": order_type,
}
)
elif order_type == "marketable_limit":
ob.update(
{
"order_type": order_type,
}
)
else:
raise NotImplementedError(f"Unsupported order type {order_type}")
obs.append(ob)
# Each trade will be seperated by 60 seconds
ts_open += int(10 * 1e9)
i_obs += 1
return obs
class RobustEncoder(json.JSONEncoder):
"""
JSONEncoder that is robust to objects lacking a `to_json` method. Attempts
to serialize such objects using the __dict__ attribute and falls back to
a `str` representation when that fails.
"""
def default(self, obj):
try:
return json.JSONEncoder.default(self, obj)
except TypeError:
try:
return obj.__dict__
except Exception as e:
print(f"Unexpected {type(e)} when encoding {obj}")
return str(obj)
class TelegramHook:
"""
Minimal integration with Telegram to send status updates
"""
def __init__(self, logger, id_secret: str, id_chat: str) -> None:
self.logger = logger
secrets = get_secret(id_secret)
self.bot_token = secrets["telegram_hook"]
self.id_chat = id_chat
def send_message(self, msg: str) -> bool:
uri = f"https://api.telegram.org/bot{self.bot_token}/sendMessage"
params = {"chat_id": self.id_chat, "parse_mode": "Markdown", "text": msg}
try:
resp = requests.get(uri, params=params)
assert resp.status_code == 200
return True
except AssertionError:
self.logger.error("Received non-200 status code from Telegram API")
except Exception as e:
self.logger.error(f"Unexpected {type(e)} when calling Telegram API")
return False
class PrintLogger:
"""
Hack to print log events to stdout
"""
def __init__(self) -> None:
pass
def debug(self, output):
self._print("DEBUG", output)
def info(self, output):
self._print("INFO", output)
def warn(self, output):
self._print("WARN", output)
def error(self, output):
self._print("ERROR", output)
def _print(self, level, output):
print(f"{level} - {output}")