forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo_trade_limits.py
136 lines (107 loc) · 4.7 KB
/
mongo_trade_limits.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
from syscore.exceptions import missingData
from sysdata.production.trade_limits import (
tradeLimitData,
listOfInstrumentStrategyKeyAndDays,
instrumentStrategyKeyAndDays,
)
from sysobjects.production.tradeable_object import instrumentStrategy
from sysdata.mongodb.mongo_generic import mongoDataWithMultipleKeys
from syslogging.logger import *
LIMIT_STATUS_COLLECTION = "limit_status"
LEGACY_STRATEGY_KEY = "strategy_name"
LEGACY_INSTRUMENT_KEY = "instrument_code"
PERIOD_KEY = "period_days"
INSTRUMENT_STRATEGY_KEY = "instrument_strategy_key"
class mongoTradeLimitData(tradeLimitData):
"""
Read and write data class to get override state data
"""
def __init__(self, mongo_db=None, log=get_logger("mongoTradeLimitData")):
super().__init__(log=log)
self._mongo_data = mongoDataWithMultipleKeys(
LIMIT_STATUS_COLLECTION, mongo_db=mongo_db
)
@property
def mongo_data(self):
return self._mongo_data
def __repr__(self):
return "Data connection for trade limit data, mongodb %s" % (
str(self.mongo_data)
)
def _get_trade_limit_as_dict_or_missing_data(
self, instrument_strategy: instrumentStrategy, period_days: int
) -> dict:
instrument_strategy_key = instrument_strategy.key
dict_of_keys = {
INSTRUMENT_STRATEGY_KEY: instrument_strategy_key,
PERIOD_KEY: period_days,
}
try:
result_dict = self.mongo_data.get_result_dict_for_dict_keys(dict_of_keys)
except missingData:
result_dict = self._get_old_style_trade_limit_as_dict_or_missing_data(
instrument_strategy, period_days
)
return result_dict
def _get_old_style_trade_limit_as_dict_or_missing_data(
self, instrument_strategy: instrumentStrategy, period_days: int
) -> dict:
dict_of_keys = {
LEGACY_INSTRUMENT_KEY: instrument_strategy.instrument_code,
LEGACY_STRATEGY_KEY: instrument_strategy.strategy_name,
PERIOD_KEY: period_days,
}
result_dict = self.mongo_data.get_result_dict_for_dict_keys(dict_of_keys)
result_dict = _from_trade_limit_dict_to_required_dict(result_dict)
return result_dict
def _update_trade_limit_as_dict(self, trade_limit_dict: dict):
instrument_strategy_key = trade_limit_dict.pop("instrument_strategy_key")
period_days = trade_limit_dict.pop("period_days")
dict_of_keys = {
INSTRUMENT_STRATEGY_KEY: instrument_strategy_key,
PERIOD_KEY: period_days,
}
# we do this to avoid blended records old and new
self._delete_old_style_data(instrument_strategy_key, period_days)
self.mongo_data.add_data(dict_of_keys, trade_limit_dict, allow_overwrite=True)
def _delete_old_style_data(self, instrument_strategy_key: str, period_days: int):
instrument_strategy = instrumentStrategy.from_key(instrument_strategy_key)
dict_of_keys = {
LEGACY_STRATEGY_KEY: instrument_strategy.strategy_name,
LEGACY_INSTRUMENT_KEY: instrument_strategy.instrument_code,
PERIOD_KEY: period_days,
}
self.mongo_data.delete_data_without_any_warning(dict_of_keys)
def _get_all_limit_keys(self) -> listOfInstrumentStrategyKeyAndDays:
list_of_result_dicts = self.mongo_data.get_list_of_all_dicts()
list_of_results = [
_from_result_dict_to_isd(result_dict)
for result_dict in list_of_result_dicts
]
list_of_results = listOfInstrumentStrategyKeyAndDays(list_of_results)
return list_of_results
def _from_result_dict_to_isd(result_dict: dict) -> instrumentStrategyKeyAndDays:
if INSTRUMENT_STRATEGY_KEY in result_dict.keys():
## NEW STYLE
instrument_strategy_key = result_dict[INSTRUMENT_STRATEGY_KEY]
else:
## LEGACY
instrument_strategy = instrumentStrategy(
strategy_name=result_dict[LEGACY_STRATEGY_KEY],
instrument_code=result_dict[LEGACY_INSTRUMENT_KEY],
)
instrument_strategy_key = instrument_strategy.key
return instrumentStrategyKeyAndDays(
instrument_strategy_key, result_dict[PERIOD_KEY]
)
def _from_trade_limit_dict_to_required_dict(trade_limit_dict: dict) -> dict:
if INSTRUMENT_STRATEGY_KEY in trade_limit_dict.keys():
## NEW STYLE
return trade_limit_dict
## OLD STYLE
instrument_strategy = instrumentStrategy(
instrument_code=trade_limit_dict.pop(LEGACY_INSTRUMENT_KEY),
strategy_name=trade_limit_dict.pop(LEGACY_STRATEGY_KEY),
)
trade_limit_dict[INSTRUMENT_STRATEGY_KEY] = instrument_strategy.key
return trade_limit_dict