forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistoric_contract_positions.py
312 lines (259 loc) · 10.6 KB
/
historic_contract_positions.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
import datetime
from typing import List
import pandas as pd
from syscore.constants import arg_not_supplied
from syscore.exceptions import missingData
from sysdata.base_data import baseData
from sysobjects.contracts import futuresContract, listOfFuturesContracts
from sysobjects.production.positions import listOfContractPositions, contractPosition
class contractPositionData(baseData):
"""
Store and retrieve the instrument positions held in a particular instrument and contract
These are *not* strategy specific. Strategies only know about instruments and don't care how their
position is implemented.
"""
def __repr__(self):
return "contractPositionData object"
def get_current_position_for_contract_object(
self, contract_object: futuresContract
):
try:
position_series = self.get_position_as_series_for_contract_object(
contract_object
)
except missingData:
return 0.0
if len(position_series) == 0:
return 0.0
return position_series.iloc[-1]
def update_position_for_contract_object(
self,
contract_object: futuresContract,
position: int,
date: datetime.datetime = arg_not_supplied,
):
if date is arg_not_supplied:
date = datetime.datetime.now()
self._update_position_for_contract_object_with_date_supplied(
contract_object=contract_object, position=position, date_index=date
)
def delete_last_position_for_contract_object(
self, contract_object: futuresContract, are_you_sure=False
):
if are_you_sure:
self._delete_last_position_for_contract_object_without_checking(
contract_object=contract_object
)
def get_list_of_instruments_with_any_position(self):
list_of_contracts = self.get_list_of_contracts()
return list_of_contracts.unique_list_of_instrument_codes()
def get_list_of_instruments_with_current_positions(self) -> list:
list_of_current_positions = (
self.get_all_current_positions_as_list_with_contract_objects()
)
instrument_list = list_of_current_positions.unique_list_of_instruments()
return instrument_list
def get_list_of_contract_date_str_with_any_position_for_instrument(
self, instrument_code: str
) -> List[str]:
## doesn't exclude zeros
list_of_contracts = self.get_list_of_contracts_for_instrument_code(
instrument_code
)
list_of_date_str = list_of_contracts.list_of_dates()
return list_of_date_str
def get_list_of_contract_date_str_with_any_position_for_instrument_in_date_range(
self,
instrument_code,
start_date: datetime.datetime,
end_date: datetime.datetime,
) -> List[str]:
list_of_contracts = self.get_list_of_contracts_for_instrument_code(
instrument_code
)
list_of_contracts_with_position = [
contract
for contract in list_of_contracts
if self.any_positions_for_contract_in_date_range(
contract, start_date, end_date
)
]
list_of_date_str_with_position = [
contract.date_str for contract in list_of_contracts_with_position
]
return list_of_date_str_with_position
def get_all_current_positions_as_df(self) -> pd.DataFrame:
# excludes zeros
return self.get_all_current_positions_as_list_with_contract_objects().as_pd_df()
def get_all_current_positions_as_list_with_contract_objects(
self,
) -> listOfContractPositions:
# excludes zeros
list_of_contracts = self.get_list_of_contracts() ## includes zeros
current_positions = []
for contract in list_of_contracts:
position = self.get_current_position_for_contract_object(contract)
if position == 0:
continue
position_object = contractPosition(position, contract)
current_positions.append(position_object)
list_of_current_positions = listOfContractPositions(current_positions)
return list_of_current_positions
def get_list_of_contracts_for_instrument_code(
self, instrument_code: str
) -> listOfFuturesContracts:
## doesn't remove zero positions
list_of_contracts = self.get_list_of_contracts()
list_of_contracts_for_code = (
list_of_contracts.contracts_in_list_for_instrument_code(instrument_code)
)
return list_of_contracts_for_code
def any_positions_for_contract_in_date_range(
self,
contract: futuresContract,
start_date: datetime.datetime,
end_date: datetime.datetime,
) -> bool:
try:
series_of_positions = self.get_position_as_series_for_contract_object(
contract
)
except missingData:
return False
any_positions = any_positions_since_start_date(
series_of_positions, start_date, end_date
)
return any_positions
def _update_position_for_contract_object_with_date_supplied(
self,
contract_object: futuresContract,
position: int,
date_index: datetime.datetime,
):
new_position_series = pd.Series([position], index=[date_index])
try:
current_series = self.get_position_as_series_for_contract_object(
contract_object
)
self._update_position_for_contract_object_with_date_and_existing_data(
contract_object=contract_object,
current_series=current_series,
new_position_series=new_position_series,
)
except missingData:
## no existing data
## no need to update, just write the new series
self._write_updated_position_series_for_contract_object(
contract_object=contract_object,
updated_series=new_position_series,
)
def _update_position_for_contract_object_with_date_and_existing_data(
self,
contract_object: futuresContract,
current_series: pd.Series,
new_position_series: pd.Series,
):
try:
assert new_position_series.index[0] > current_series.index[-1]
except:
error_msg = "Adding a position which is older than the last position!"
self.log.critical(error_msg)
raise Exception(error_msg)
updated_series = current_series._append(new_position_series)
self._write_updated_position_series_for_contract_object(
contract_object=contract_object, updated_series=updated_series
)
def _delete_last_position_for_contract_object_without_checking(
self, contract_object: futuresContract
):
try:
current_series = self.get_position_as_series_for_contract_object(
contract_object
)
self._delete_last_position_for_contract_object_without_checking_with_current_data(
contract_object=contract_object, current_series=current_series
)
except missingData:
## no existing data can't delete
self.log.warning(
"Can't delete last position for %s, as none present"
% str(contract_object)
)
def _delete_last_position_for_contract_object_without_checking_with_current_data(
self, contract_object: futuresContract, current_series: pd.Series
):
updated_series = current_series.drop(current_series.index[-1])
if len(updated_series) == 0:
self._delete_position_series_for_contract_object_without_checking(
contract_object
)
else:
self._write_updated_position_series_for_contract_object(
contract_object=contract_object, updated_series=updated_series
)
def any_current_position_for_contract(self, contract: futuresContract) -> bool:
position = self.get_current_position_for_contract_object(contract)
if position == 0:
return False
else:
return True
def overwrite_position_series_for_contract_object_without_checking(
self, contract_object: futuresContract, updated_series: pd.Series
):
self._write_updated_position_series_for_contract_object(
contract_object=contract_object, updated_series=updated_series
)
def _write_updated_position_series_for_contract_object(
self, contract_object: futuresContract, updated_series: pd.Series
):
raise NotImplementedError
def _delete_position_series_for_contract_object_without_checking(
self, contract_object: futuresContract
):
raise NotImplementedError
def get_position_as_series_for_contract_object(
self, contract_object: futuresContract
) -> pd.Series:
raise NotImplementedError
def get_list_of_contracts(self) -> listOfFuturesContracts:
## doesn't remove zero positions
raise NotImplementedError
def any_positions_since_start_date(
position_series: pd.Series,
start_date: datetime.datetime,
end_date: datetime.datetime,
) -> bool:
"""
Any positions held in a given date range
Either:
- position at start was non zero, and we didn't trade (return True)
- position at start was zero and we did change our position (return True)
- position at start was zero and we didn't trade (return False)
:param position_series: pd.DataFrame with one column, position
:param start_date: datetime
:param end_date: datetime
:return: bool
"""
no_positions_in_series = len(position_series) == 0
if no_positions_in_series:
return False
positions_during = position_series[start_date:end_date]
position_at_start = _infer_position_at_start(
position_series=position_series, start_date=start_date
)
no_initial_position = position_at_start == 0
no_trading_done_in_period = len(positions_during) == 0
if no_trading_done_in_period and no_initial_position:
return False
return True
def _infer_position_at_start(
position_series: pd.Series, start_date: datetime.datetime
) -> int:
positions_before_start = position_series[:start_date]
no_positions_before_start = len(positions_before_start) == 0
if no_positions_before_start:
position_at_start = 0
else:
last_position_before_start = positions_before_start.iloc[-1]
position_at_start = last_position_before_start
return position_at_start