forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorders.py
229 lines (182 loc) · 7.86 KB
/
orders.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
import datetime
from syscore.constants import arg_not_supplied
from sysexecution.orders.named_order_objects import missing_order, no_parent
from sysdata.production.historic_orders import (
brokerHistoricOrdersData,
contractHistoricOrdersData,
strategyHistoricOrdersData,
)
from sysdata.data_blob import dataBlob
from sysobjects.fills import ListOfFills
from sysexecution.order_stacks.broker_order_stack import brokerOrderStackData
from sysexecution.order_stacks.contract_order_stack import contractOrderStackData
from sysexecution.order_stacks.instrument_order_stack import instrumentOrderStackData
from sysexecution.orders.contract_orders import contractOrder
from sysexecution.orders.broker_orders import (
brokerOrder,
brokerOrderWithParentInformation,
)
from sysexecution.orders.instrument_orders import instrumentOrder
from sysexecution.orders.list_of_orders import listOfOrders
from sysobjects.production.tradeable_object import instrumentStrategy, futuresContract
from sysproduction.data.production_data_objects import (
get_class_for_data_type,
INSTRUMENT_ORDER_STACK_DATA,
CONTRACT_ORDER_STACK_DATA,
BROKER_HISTORIC_ORDERS_DATA,
STRATEGY_HISTORIC_ORDERS_DATA,
CONTRACT_HISTORIC_ORDERS_DATA,
BROKER_ORDER_STACK_DATA,
)
class dataOrders(object):
def __init__(self, data: dataBlob = arg_not_supplied):
# Check data has the right elements to do this
if data is arg_not_supplied:
data = dataBlob()
data.add_class_list(
[
get_class_for_data_type(INSTRUMENT_ORDER_STACK_DATA),
get_class_for_data_type(CONTRACT_ORDER_STACK_DATA),
get_class_for_data_type(BROKER_ORDER_STACK_DATA),
get_class_for_data_type(STRATEGY_HISTORIC_ORDERS_DATA),
get_class_for_data_type(CONTRACT_HISTORIC_ORDERS_DATA),
get_class_for_data_type(BROKER_HISTORIC_ORDERS_DATA),
]
)
self._data = data
@property
def data(self) -> dataBlob:
return self._data
@property
def db_strategy_historic_orders_data(self) -> strategyHistoricOrdersData:
return self.data.db_strategy_historic_orders
@property
def db_contract_historic_orders_data(self) -> contractHistoricOrdersData:
return self.data.db_contract_historic_orders
@property
def db_broker_historic_orders_data(self) -> brokerHistoricOrdersData:
return self.data.db_broker_historic_orders
@property
def db_instrument_stack_data(self) -> instrumentOrderStackData:
return self.data.db_instrument_order_stack
@property
def db_contract_stack_data(self) -> contractOrderStackData:
return self.data.db_contract_order_stack
@property
def db_broker_stack_data(self) -> brokerOrderStackData:
return self.data.db_broker_order_stack
def add_historic_orders_to_data(
self,
instrument_order: instrumentOrder,
list_of_contract_orders: listOfOrders,
list_of_broker_orders: listOfOrders,
):
self.add_historic_instrument_order_to_data(instrument_order)
for contract_order in list_of_contract_orders:
self.add_historic_contract_order_to_data(contract_order)
for broker_order in list_of_broker_orders:
self.add_historic_broker_order_to_data(broker_order)
def add_historic_instrument_order_to_data(self, instrument_order: instrumentOrder):
self.db_strategy_historic_orders_data.add_order_to_data(instrument_order)
def add_historic_contract_order_to_data(self, contract_order: contractOrder):
self.db_contract_historic_orders_data.add_order_to_data(contract_order)
def add_historic_broker_order_to_data(self, broker_order: brokerOrder):
self.db_broker_historic_orders_data.add_order_to_data(broker_order)
def get_historic_broker_order_ids_in_date_range(
self,
period_start: datetime.datetime,
period_end: datetime.datetime = arg_not_supplied,
) -> list:
# remove split orders
order_id_list = (
self.db_broker_historic_orders_data.get_list_of_order_ids_in_date_range(
period_start, period_end=period_end
)
)
return order_id_list
def get_historic_contract_order_ids_in_date_range(
self, period_start: datetime.datetime, period_end: datetime.datetime
) -> list:
order_id_list = (
self.db_contract_historic_orders_data.get_list_of_order_ids_in_date_range(
period_start, period_end
)
)
return order_id_list
def get_historic_instrument_order_ids_in_date_range(
self, period_start: datetime.datetime, period_end: datetime.datetime
) -> list:
order_id_list = (
self.db_strategy_historic_orders_data.get_list_of_order_ids_in_date_range(
period_start, period_end
)
)
return order_id_list
def get_historic_instrument_order_from_order_id(
self, order_id: int
) -> instrumentOrder:
order = self.db_strategy_historic_orders_data.get_order_with_orderid(order_id)
return order
def get_historic_contract_order_from_order_id(self, order_id: int) -> contractOrder:
order = self.db_contract_historic_orders_data.get_order_with_orderid(order_id)
return order
def get_historic_broker_order_from_order_id(self, order_id: int) -> brokerOrder:
order = self.db_broker_historic_orders_data.get_order_with_orderid(order_id)
return order
def get_fills_history_for_contract(
self, futures_contract: futuresContract
) -> ListOfFills:
## We get this from broker fills, as they have leg by leg information
list_of_fills = (
self.db_broker_historic_orders_data.get_fills_history_for_contract(
futures_contract
)
)
return list_of_fills
def get_fills_history_for_instrument_strategy(
self, instrument_strategy: instrumentStrategy
) -> ListOfFills:
list_of_fills = self.db_strategy_historic_orders_data.get_fills_history_for_instrument_strategy(
instrument_strategy
)
return list_of_fills
def get_historic_broker_order_from_order_id_with_execution_data(
self, order_id: int
) -> brokerOrderWithParentInformation:
order = self.get_historic_broker_order_from_order_id(order_id)
contract_order = self.get_parent_contract_order_for_historic_broker_order_id(
order_id
)
instrument_order = (
self.get_parent_instrument_order_for_historic_broker_order_id(order_id)
)
augmented_order = brokerOrderWithParentInformation.create_augmented_order(
order, contract_order=contract_order, instrument_order=instrument_order
)
return augmented_order
def get_parent_contract_order_for_historic_broker_order_id(
self, order_id: int
) -> contractOrder:
broker_order = self.get_historic_broker_order_from_order_id(order_id)
contract_order_id = broker_order.parent
if contract_order_id is no_parent:
return missing_order
contract_order = self.get_historic_contract_order_from_order_id(
contract_order_id
)
return contract_order
def get_parent_instrument_order_for_historic_broker_order_id(
self, order_id: int
) -> instrumentOrder:
contract_order = self.get_parent_contract_order_for_historic_broker_order_id(
order_id
)
if contract_order is missing_order:
return missing_order
instrument_order_id = contract_order.parent
if instrument_order_id is no_parent:
return missing_order
instrument_order = self.get_historic_instrument_order_from_order_id(
instrument_order_id
)
return instrument_order