forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo_order_stack.py
141 lines (102 loc) · 4.41 KB
/
mongo_order_stack.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
from syscore.exceptions import missingData
from sysdata.mongodb.mongo_generic import mongoDataWithSingleKey
from syslogging.logger import *
from sysexecution.order_stacks.order_stack import orderStackData
from sysexecution.orders.named_order_objects import missing_order
from sysexecution.orders.base_orders import Order
from sysexecution.orders.instrument_orders import instrumentOrder
from sysexecution.order_stacks.instrument_order_stack import instrumentOrderStackData
from sysexecution.orders.contract_orders import contractOrder
from sysexecution.order_stacks.contract_order_stack import contractOrderStackData
from sysexecution.orders.broker_orders import brokerOrder
from sysexecution.order_stacks.broker_order_stack import brokerOrderStackData
ORDER_ID_STORE_KEY = "_ORDER_ID_STORE_KEY"
MAX_ORDER_KEY = "max_order_id"
class mongoOrderStackData(orderStackData):
"""
Read and write data class to get roll state data
"""
def _collection_name(self):
raise NotImplementedError("Need to inherit for a specific data type")
def _order_class(self):
return Order
def __init__(self, mongo_db=None, log=get_logger("mongoOrderStackData")):
# Not needed as we don't store anything in _state attribute used in parent class
# If we did have _state would risk breaking if we forgot to override methods
# super().__init__()
collection_name = self._collection_name()
self._mongo_data = mongoDataWithSingleKey(
collection_name, "order_id", mongo_db=mongo_db
)
super().__init__(log=log)
@property
def mongo_data(self):
return self._mongo_data
def __repr__(self):
return "%s: %s with %d active orders" % (
self._name,
str(self.mongo_data),
self.number_of_orders_on_stack(),
)
def get_order_with_id_from_stack(self, order_id: int):
try:
result_dict = self.mongo_data.get_result_dict_for_key(order_id)
except missingData:
return missing_order
order_class = self._order_class()
order = order_class.from_dict(result_dict)
return order
def _get_list_of_all_order_ids(self) -> list:
order_ids = self.mongo_data.get_list_of_keys()
try:
order_ids.pop(order_ids.index(ORDER_ID_STORE_KEY))
except:
pass
return order_ids
def _change_order_on_stack_no_checking(self, order_id: int, order):
order_as_dict = order.as_dict()
self.mongo_data.add_data(order_id, order_as_dict, allow_overwrite=True)
def _put_order_on_stack_no_checking(self, order: Order):
order_as_dict = order.as_dict()
self.mongo_data.add_data(
int(order.order_id), order_as_dict, allow_overwrite=False
)
# ORDER ID
def _get_next_order_id(self) -> int:
max_orderid = self._get_current_max_order_id()
new_orderid = int(max_orderid + 1)
self._update_max_order_id(new_orderid)
return new_orderid
def _get_current_max_order_id(self) -> int:
try:
result_dict = self.mongo_data.get_result_dict_for_key(ORDER_ID_STORE_KEY)
except missingData:
orderid = self._create_and_return_max_order_id()
return orderid
order_id = result_dict[MAX_ORDER_KEY]
return int(order_id)
def _update_max_order_id(self, max_order_id: int):
self.mongo_data.add_data(
ORDER_ID_STORE_KEY, {MAX_ORDER_KEY: max_order_id}, allow_overwrite=True
)
def _create_and_return_max_order_id(self):
first_order_id = 1
self._update_max_order_id(first_order_id)
return first_order_id
def _remove_order_with_id_from_stack_no_checking(self, order_id):
self.mongo_data.delete_data_without_any_warning(order_id)
class mongoInstrumentOrderStackData(mongoOrderStackData, instrumentOrderStackData):
def _collection_name(self):
return "INSTRUMENT_ORDER_STACK"
def _order_class(self):
return instrumentOrder
class mongoContractOrderStackData(mongoOrderStackData, contractOrderStackData):
def _collection_name(self):
return "CONTRACT_ORDER_STACK"
def _order_class(self):
return contractOrder
class mongoBrokerOrderStackData(mongoOrderStackData, brokerOrderStackData):
def _collection_name(self):
return "BROKER_ORDER_STACK"
def _order_class(self):
return brokerOrder