Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TradingCompetition2022-main/logger/BusinessLogger.py
5925 views
1
from logger.Logger import Logger, LogData
2
from datetime import datetime
3
4
5
class BusinessLogger(Logger):
6
"""
7
Singleton Class for data logging
8
Collect all event
9
"""
10
ORDER_POSTED = "ORDER_POSTED"
11
ORDER_OPEN_LONG_DONE = "ORDER_OPEN_LONG_DONE"
12
ORDER_CLOSE_LONG_DONE = "ORDER_CLOSE_LONG_DONE"
13
ACCOUNT_STARTED = "ACCOUNT_STARTED"
14
ACCOUNT_BALANCE_NO_MONEY = "ACCOUNT_BALANCE_NO_MONEY"
15
STOCK_IN_WORK = "STOCK_IN_WORK"
16
STOCK_RE_INIT = "STOCK_RE_INIT"
17
STOCK_DEL_FROM_SESS = "STOCK_DEL_FROM_SESSION"
18
MODEL_CREATED = "MODEL_CREATED"
19
CALC_STOP = "CALC_STOP"
20
STOP_LOOS = "STOP_LOOS"
21
TAKE_PROFIT = "TAKE_PROFIT"
22
23
_LOGGER_INSTANCE: object = None
24
log_line_dict = dict(datetime=datetime.now(), msg="")
25
26
def __init__(self, conf_name_print_mode=None):
27
if conf_name_print_mode is None:
28
conf_name_print_mode = 'print_business_log'
29
super().__init__(conf_name_print_mode)
30
31
@classmethod
32
def get_logger_instance(cls):
33
if cls._LOGGER_INSTANCE is None:
34
cls._LOGGER_INSTANCE = BusinessLogger()
35
return cls._LOGGER_INSTANCE
36
37
def add_event(self, event_type, obj, value=None):
38
key = obj.id if hasattr(obj, 'id') else obj
39
if value is None:
40
value = obj
41
log_data = LogData(event_id=event_type, key=key, value=value)
42
self.add_log(log_data)
43
44
def add_event_no_money(self, obj, balance, need_money):
45
self.add_event(BusinessLogger.ACCOUNT_BALANCE_NO_MONEY,
46
obj, dict(balance=balance, need_money=need_money))
47
48