forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_blob.py
353 lines (267 loc) · 12.8 KB
/
data_blob.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
from copy import copy
from sysbrokers.IB.ib_connection import connectionIB
from syscore.objects import arg_not_supplied, get_class_name
from syscore.text import camel_case_split
from sysdata.config.production_config import get_production_config, Config
from sysdata.mongodb.mongo_connection import mongoDb
from sysdata.mongodb.mongo_log import logToMongod
from syslogdiag.logger import logger
from sysdata.mongodb.mongo_IB_client_id import mongoIbBrokerClientIdData
class dataBlob(object):
def __init__(
self,
class_list: list=arg_not_supplied,
log_name: str="",
csv_data_paths: dict=arg_not_supplied,
ib_conn: connectionIB=arg_not_supplied,
mongo_db: mongoDb=arg_not_supplied,
log: logger=arg_not_supplied,
keep_original_prefix: bool=False,
):
"""
Set up of a data pipeline with standard attribute names, logging, links to DB etc
Class names we know how to handle are:
'ib*', 'mongo*', 'arctic*', 'csv*'
data = dataBlob([arcticFuturesContractPriceData, arcticFuturesContractPriceData, mongoFuturesContractData])
.... sets up the following equivalencies:
data.broker_contract_price = ibFuturesContractPriceData(ib_conn, log=log.setup(component="IB-price-data"))
data.db_futures_contract_price = arcticFuturesContractPriceData(mongo_db=mongo_db,
log=log.setup(component="arcticFuturesContractPriceData"))
data.db_futures_contract = mongoFuturesContractData(mongo_db=mongo_db,
log = log.setup(component="mongoFuturesContractData"))
This abstracts the precise data source
:param arg_string: str like a named tuple in the form 'classNameOfData1 classNameOfData2' and so on
:param log_name: logger type to set
:param keep_original_prefix: bool. If True then:
data = dataBlob([arcticFuturesContractPriceData, arcticFuturesContractPriceData, mongoFuturesContractData])
.... sets up the following equivalencies. This is useful if you are copying from one source to another
data.ib_contract_price = ibFuturesContractPriceData(ib_conn, log=log.setup(component="IB-price-data"))
data.arctic_futures_contract_price = arcticFuturesContractPriceData(mongo_db=mongo_db,
log=log.setup(component="arcticFuturesContractPriceData"))
data.mongo_futures_contract = mongoFuturesContractData(mongo_db=mongo_db,
log = log.setup(component="mongoFuturesContractData"))
"""
self._mongo_db = mongo_db
self._ib_conn = ib_conn
self._log = log
self._log_name = log_name
self._csv_data_paths = csv_data_paths
self._keep_original_prefix = keep_original_prefix
self._attr_list = []
if class_list is arg_not_supplied:
# can set up dynamically later
pass
else:
self.add_class_list(class_list)
self._original_data = copy(self)
def __repr__(self):
return "dataBlob with elements: %s" % ",".join(self._attr_list)
def add_class_list(self, class_list: list):
for class_object in class_list:
self.add_class_object(class_object)
def add_class_object(self, class_object):
resolved_instance = self._get_resolved_instance_of_class(class_object)
class_name = get_class_name(class_object)
self._resolve_names_and_add(resolved_instance, class_name)
def _get_resolved_instance_of_class(self, class_object):
class_adding_method = self._get_class_adding_method(class_object)
resolved_instance = class_adding_method(class_object)
return resolved_instance
def _get_class_adding_method(self, class_object):
prefix = self._get_class_prefix(class_object)
class_dict = dict(ib = self._add_ib_class, csv = self._add_csv_class, arctic = self._add_arctic_class,
mongo = self._add_mongo_class)
method_to_add_with = class_dict.get(prefix, None)
if method_to_add_with is None:
error_msg = "Don't know how to handle object named %s" % get_class_name(class_object)
self._raise_and_log_error(error_msg)
return method_to_add_with
def _get_class_prefix(self, class_object) -> str:
class_name = get_class_name(class_object)
split_up_name = camel_case_split(class_name)
prefix = split_up_name[0]
return prefix
def _add_ib_class(self, class_object):
log = self._get_specific_logger(class_object)
try:
resolved_instance = class_object(self.ib_conn, log = log)
except Exception as e:
class_name = get_class_name(class_object)
msg = (
"Error %s couldn't evaluate %s(self.ib_conn, log = self.log.setup(component = %s)) This might be because (a) IB gateway not running, or (b) import is missing\
or (c) arguments don't follow pattern" % (str(e), class_name, class_name))
self._raise_and_log_error(msg)
return resolved_instance
def _add_mongo_class(self, class_object):
log = self._get_specific_logger(class_object)
try:
resolved_instance = class_object(mongo_db=self.mongo_db, log = log)
except Exception as e:
class_name = get_class_name(class_object)
msg = (
"Error '%s' couldn't evaluate %s(mongo_db=self.mongo_db, log = self.log.setup(component = %s)) \
This might be because import is missing\
or arguments don't follow pattern" % (str(e), class_name, class_name))
self._raise_and_log_error(msg)
return resolved_instance
def _add_arctic_class(self, class_object):
log = self._get_specific_logger(class_object)
try:
resolved_instance = class_object(mongo_db=self.mongo_db, log = log)
except Exception as e:
class_name = get_class_name(class_object)
msg = (
"Error %s couldn't evaluate %s(mongo_db=self.mongo_db, log = self.log.setup(component = %s)) \
This might be because import is missing\
or arguments don't follow pattern" % (str(e), class_name, class_name))
self._raise_and_log_error(msg)
return resolved_instance
def _add_csv_class(self, class_object):
datapath = self._get_csv_paths_for_class(class_object)
log = self._get_specific_logger(class_object)
try:
resolved_instance = class_object(datapath = datapath, log = log)
except Exception as e:
class_name = get_class_name(class_object)
msg = (
"Error %s couldn't evaluate %s(datapath = datapath, log = self.log.setup(component = %s)) \
This might be because import is missing\
or arguments don't follow pattern" % (str(e), class_name, class_name))
self._raise_and_log_error(msg)
return resolved_instance
def _get_csv_paths_for_class(self, class_object) -> str:
class_name = get_class_name(class_object)
csv_data_paths = self.csv_data_paths
if csv_data_paths is arg_not_supplied:
self.log.warn("No datapaths provided for .csv, will use defaults (may break in production, should be fine in sim)")
return arg_not_supplied
datapath = csv_data_paths.get(class_name, "")
if datapath == "":
self.log.warn(
"No key for %s in csv_data_paths, will use defaults (may break in production, should be fine in sim)" %
class_name)
return arg_not_supplied
return datapath
@property
def csv_data_paths(self) -> dict:
csv_data_paths = getattr(self, "_csv_data_paths", arg_not_supplied)
return csv_data_paths
def _get_specific_logger(self, class_object):
class_name = get_class_name(class_object)
log = self.log.setup(component = class_name)
return log
def _resolve_names_and_add(self, resolved_instance, class_name: str):
attr_name = self._get_new_name(class_name)
self._add_new_class_with_new_name(resolved_instance, attr_name)
def _get_new_name(self, class_name: str) -> str:
split_up_name = camel_case_split(class_name)
attr_name = identifying_name(
split_up_name, keep_original_prefix=self._keep_original_prefix
)
return attr_name
def _add_new_class_with_new_name(self, resolved_instance, attr_name:str):
already_exists = self._already_existing_class_name(attr_name)
if already_exists:
## not uncommon don't log or would be a sea of span
pass
else:
setattr(self, attr_name, resolved_instance)
self._add_attr_to_list(attr_name)
def _already_existing_class_name(self, attr_name: str):
existing_attr = getattr(self, attr_name, None)
if existing_attr is None:
return False
else:
return True
def _add_attr_to_list(self, new_attr: str):
self._attr_list.append(new_attr)
"""
Following two methods implement context manager
"""
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
def close(self):
if self._ib_conn is not arg_not_supplied:
self.ib_conn.close_connection()
self.db_ib_broker_client_id.release_clientid(
self.ib_conn.client_id())
# No need to explicitly close Mongo connections; handled by Python garbage collection
@property
def ib_conn(self) -> connectionIB:
ib_conn = getattr(self, "_ib_conn", arg_not_supplied)
if ib_conn is arg_not_supplied:
ib_conn = self._get_new_ib_connection()
self._ib_conn = ib_conn
return ib_conn
def _get_new_ib_connection(self) -> connectionIB:
client_id = self._get_next_client_id_for_ib()
ib_conn = connectionIB(client_id,
log=self.log)
return ib_conn
def _get_next_client_id_for_ib(self) -> int:
## default to tracking ID through mongo change if required
self.add_class_object(mongoIbBrokerClientIdData)
client_id = self.db_ib_broker_client_id.return_valid_client_id()
return int(client_id)
@property
def mongo_db(self) -> mongoDb:
mongo_db = getattr(self, "_mongo_db", arg_not_supplied)
if mongo_db is arg_not_supplied:
mongo_db= self._get_new_mongo_db()
self._mongo_db = mongo_db
return mongo_db
def _get_new_mongo_db(self) -> mongoDb:
mongo_db = mongoDb()
return mongo_db
@property
def config(self) -> Config:
config = getattr(self, "_config", None)
if config is None:
config = self._config = get_production_config()
return config
def _raise_and_log_error(self, error_msg: str):
self.log.critical(error_msg)
raise Exception(error_msg)
@property
def log(self):
log = getattr(self, "_log", arg_not_supplied)
if log is arg_not_supplied:
log = logToMongod(self.log_name, mongo_db=self.mongo_db, data = self)
log.set_logging_level("on")
self._log = log
return log
@property
def log_name(self) -> str:
log_name = getattr(self, "_logname", "")
return log_name
source_dict = dict(arctic="db", mongo="db", csv="db", ib="broker")
def identifying_name(split_up_name: list, keep_original_prefix=False)-> str:
"""
Turns sourceClassNameData into broker_class_name or db_class_name
:param split_up_name: list eg source, class,name,data
:return: str, class_name
"""
lower_split_up_name = [x.lower() for x in split_up_name]
data_label = lower_split_up_name.pop(-1) # always 'data'
original_source_label = lower_split_up_name.pop(
0
) # always the source, eg csv, ib, mongo or arctic
try:
assert data_label == "data"
except BaseException:
raise Exception(
"Get_data strings only work if class name ends in ...Data")
if keep_original_prefix:
source_label = original_source_label
else:
try:
source_label = source_dict[original_source_label]
except BaseException:
raise Exception(
"Only works with classes that begin with one of %s"
% str(source_dict.keys())
)
lower_split_up_name = [source_label] + lower_split_up_name
return "_".join(lower_split_up_name)