forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 1
/
contracts.py
345 lines (243 loc) · 11.4 KB
/
contracts.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import datetime
from dataclasses import dataclass
from syscore.objects import arg_not_supplied, missing_contract
from syslogdiag.logger import logger
from sysobjects.contract_dates_and_expiries import contractDate, expiryDate
from sysobjects.instruments import futuresInstrument
NO_ROLL_CYCLE_PASSED = object()
@dataclass
class parametersForFuturesContract:
sampling: bool = False
def as_dict(self) -> dict:
keys = self.__dataclass_fields__.keys()
self_as_dict = dict([(key, getattr(self, key)) for key in keys])
return self_as_dict
@classmethod
def from_dict(parametersForFuturesContract, input_dict):
keys = parametersForFuturesContract.__dataclass_fields__.keys()
args_list = [input_dict.get(key, None) for key in keys ]
args_list = [value for value in args_list if value is not None]
return parametersForFuturesContract(*args_list)
def contract_key_from_code_and_id(instrument_code, contract_id):
contract = contract_from_code_and_id(instrument_code, contract_id)
return contract.key
def contract_from_code_and_id(instrument_code, contract_id):
return futuresContract(instrument_code, contract_id)
def contract_from_key(contract_key):
instrument_code, contract_id = get_code_and_id_from_contract_key(contract_key)
return contract_from_code_and_id(instrument_code, contract_id)
class futuresContract(object):
"""
Define an individual futures contract
This is a combination of an instrument_object and contract_date object
"""
def __init__(self, instrument_object: futuresInstrument,
contract_date_object: contractDate,
parameter_object: parametersForFuturesContract = arg_not_supplied,
simple: bool = False):
"""
futuresContract(futuresInstrument, contractDate)
OR
futuresContract("instrument_code", "yyyymm")
:param instrument_object: str or futuresInstrument
:param contract_date_object: contractDate or contractDateWithRollParameters or str
"""
if not simple:
instrument_object, contract_date_object = _resolve_args_for_futures_contract(instrument_object, contract_date_object)
if parameter_object is arg_not_supplied:
parameter_object = parametersForFuturesContract()
self._instrument = instrument_object
self._contract_date = contract_date_object
self._params = parameter_object
@classmethod
def from_two_strings(futuresContract, instrument_code: str,
contract_date_str: str):
instrument_object = futuresInstrument(instrument_code)
contract_date = contractDate(contract_date_str, simple=True)
return futuresContract(instrument_object,
contract_date,
simple=True)
def specific_log(self, log):
new_log = log.setup(instrument_code = self.instrument_code, contract_date = self.date_str)
return new_log
@property
def instrument(self):
return self._instrument
@property
def contract_date(self):
return self._contract_date
@property
def params(self):
return self._params
def __repr__(self):
return self.key
def __eq__(self, other):
instruments_match = self.instrument == other.instrument
contracts_match = self.date_str == other.date_str
if instruments_match and contracts_match:
return True
else:
return False
@property
def key(self):
return get_contract_key_from_code_and_id(self.instrument_code, self.date_str)
@property
def currently_sampling(self):
return self.params.sampling
def sampling_on(self):
self.params.sampling = True
def sampling_off(self):
self.params.sampling = False
def log(self, log: logger):
return log.setup(instrument_code =self.instrument_code, contract_date = self.date_str)
def as_dict(self):
"""
Turn into a dict. We only include instrument_code from the instrument_object, the rest would be found elsewhere
plus we have all the results from as_dict on the contract_date
:return: dict
"""
contract_date_dict = self.contract_date.as_dict()
instrument_dict = self.instrument.as_dict()
contract_params_dict = self.params.as_dict()
joint_dict = dict(
contract_date_dict=contract_date_dict,
instrument_dict=instrument_dict,
contract_params=contract_params_dict,
)
return joint_dict
@classmethod
def create_from_dict(futuresContract, futures_contract_dict):
"""
:param futures_contract_dict: The result of running .as_dict on a futuresContract.
:return: futuresContract object
"""
contract_date_dict = futures_contract_dict["contract_date_dict"]
instrument_dict = futures_contract_dict["instrument_dict"]
contract_params_dict = futures_contract_dict["contract_params"]
contract_date_object = contractDate.create_from_dict(
contract_date_dict)
instrument_object = futuresInstrument.create_from_dict(instrument_dict)
parameter_object = parametersForFuturesContract.from_dict(contract_params_dict)
return futuresContract(
instrument_object, contract_date_object, parameter_object= parameter_object
)
@property
def instrument_code(self):
return self.instrument.instrument_code
@property
def date_str(self):
return self.contract_date.date_str
@property
def date(self):
return self.contract_date.as_date
@property
def expiry_date(self):
return self.contract_date.expiry_date
def expired(self):
expiry_date = self.expiry_date
if expiry_date<datetime.datetime.now():
return True
else:
return False
def update_single_expiry_date(self, new_expiry_date: expiryDate):
self.contract_date.update_single_expiry_date(new_expiry_date)
def is_spread_contract(self):
return self.contract_date.is_spread_contract
def new_contract_with_replaced_instrument_object(
self, new_instrument_object):
contract_date_object = self.contract_date
params = self.params
return futuresContract(new_instrument_object, contract_date_object, parameter_object=params)
def update_expiry_dates_one_at_a_time_with_method(self, method, **kwargs):
as_list_of_individual_contracts = self.as_list_of_individual_contracts()
new_expiries = [method(single_contract, **kwargs) for single_contract in as_list_of_individual_contracts]
for contract_index, expiry_date in enumerate(new_expiries):
if expiry_date is missing_contract:
## whole thing is buggered
return missing_contract
else:
self.update_nth_expiry_date(contract_index, expiry_date)
return self
def update_nth_expiry_date(self, contract_index: int, expiry_date: expiryDate):
self.contract_date.update_nth_expiry_date(contract_index, expiry_date)
def as_list_of_individual_contracts(self) -> list:
return [self.new_contract_with_nth_contract_date(contract_index)
for contract_index in range(len(self.list_of_single_contract_dates()))]
def new_contract_with_first_contract_date(self):
return self.new_contract_with_nth_contract_date(0)
def new_contract_with_nth_contract_date(self, contract_index: int):
new_contract_date_object = self.contract_date.nth_single_contract_as_contract_date(contract_index)
return self.new_contract_with_replaced_contract_date_object(new_contract_date_object)
def new_contract_with_replaced_contract_date_object(self, new_contract_date_object: contractDate):
instrument_object = self.instrument
params = self.params
return futuresContract(instrument_object, new_contract_date_object, params)
def list_of_single_contract_dates(self) -> list:
return self.contract_date.list_of_single_contract_dates
def _resolve_args_for_futures_contract(instrument_object, contract_date_object) -> tuple:
if isinstance(instrument_object, str):
instrument_object = futuresInstrument(instrument_object)
if isinstance(contract_date_object, list) or isinstance(contract_date_object, str) \
or isinstance(contract_date_object, dict):
contract_date_object = contractDate(contract_date_object)
return instrument_object, contract_date_object
def key_contains_instrument_code(contract_key, instrument_code):
key_instrument_code, contract_id = get_code_and_id_from_contract_key(contract_key)
if key_instrument_code == instrument_code:
return True
else:
return False
def get_contract_key_from_code_and_id(instrument_code, contract_id):
return instrument_code + "/" + contract_id
def get_code_and_id_from_contract_key(contract_key):
return contract_key.split("/")
class listOfFuturesContracts(list):
"""
List of futuresContracts
"""
def unique_list_of_instrument_codes(self):
list_of_instruments = [
contract.instrument_code for contract in self]
# will contain duplicates, make unique
unique_list_of_instruments = list(set(list_of_instruments))
return unique_list_of_instruments
def contract_date_str_for_contracts_in_list_for_instrument_code(self, instrument_code: str) -> list:
list_of_contracts = self.contracts_in_list_for_instrument_code(instrument_code)
list_of_date_str = list_of_contracts.list_of_dates()
list_of_date_str = list(set(list_of_date_str))
return list_of_date_str
def contracts_in_list_for_instrument_code(self, instrument_code: str):
list_of_contracts = [
contract
for contract in self
if contract.instrument_code == instrument_code
]
list_of_contracts = listOfFuturesContracts(list_of_contracts)
return list_of_contracts
def currently_sampling(self):
contracts_currently_sampling = [
contract for contract in self if contract.currently_sampling
]
return listOfFuturesContracts(contracts_currently_sampling)
def list_of_dates(self) -> list:
# Return list of contract_date identifiers
contract_dates = [contract.date_str for contract in self]
return contract_dates
def as_dict(self) ->dict:
contract_dates_keys = self.list_of_dates()
contract_values = self
contract_dict = dict([(key, value) for key, value in zip(
contract_dates_keys, contract_values)])
return contract_dict
def difference(self, another_contract_list):
self_contract_dates = set(self.list_of_dates())
another_contract_list_dates = set(another_contract_list.list_of_dates())
list_of_differential_dates =self_contract_dates.difference(another_contract_list_dates)
list_of_contracts = self._subset_of_list_from_list_of_dates(list_of_differential_dates)
return list_of_contracts
def _subset_of_list_from_list_of_dates(self, list_of_dates):
self_as_dict = self.as_dict()
list_of_contracts = [
self_as_dict[contract_date] for contract_date in list_of_dates
]
return listOfFuturesContracts(list_of_contracts)