-
Notifications
You must be signed in to change notification settings - Fork 852
/
Copy pathrolls.py
448 lines (344 loc) · 14.6 KB
/
rolls.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
from syscore.genutils import np_convert
import datetime
from copy import copy
from syscore.dateutils import month_from_contract_letter, FUTURES_MONTH_LIST
from sysobjects.contract_dates_and_expiries import contractDate, contract_given_tuple
forward = 1
backwards = -1
class rollCycle(object):
"""
A cycle determining how one contract rolls to the next
Only works with monthly contracts
"""
def __init__(self, cyclestring: str):
assert isinstance(cyclestring, str)
self._cyclestring = "".join(sorted(cyclestring))
def __repr__(self):
return self.cyclestring
def __len__(self) -> int:
return len(self.cyclestring)
def __eq__(self, other):
return self.cyclestring == other.cyclestring
@property
def cyclestring(self):
return self._cyclestring
def iterate_contract_date(
self, direction: int, contract_date: contractDate
) -> contractDate:
year_value, month_str = contract_date.date_str_to_year_month()
if direction == forward:
new_year_value, new_month_str = self._next_year_month_given_tuple(
year_value, month_str
)
elif direction == backwards:
new_year_value, new_month_str = self._previous_year_month_given_tuple(
year_value, month_str
)
else:
raise Exception(
"Direction %d has to be %s or %s" % (direction, forward, backwards)
)
return contract_given_tuple(contract_date, new_year_value, new_month_str)
def _previous_year_month_given_tuple(
self, year_value: int, month_str: str
) -> (int, str):
"""
Returns a tuple (year, month: str)
:param month_str: str
:param year_value: int
:return: tuple (int, str)
"""
new_month_as_str = self._previous_month(month_str)
if self._month_is_first(month_str):
year_value = year_value - 1
return year_value, new_month_as_str
def _next_year_month_given_tuple(
self, year_value: int, month_str: str
) -> (int, str):
"""
Returns a tuple (year, month: str)
:param month_str: str
:param year_value: int
:return: tuple (int, str)
"""
new_month_as_str = self._next_month(month_str)
if self._month_is_last(month_str):
year_value = year_value + 1
return year_value, new_month_as_str
def _next_month(self, current_month: str) -> str:
"""
Move one month forward in expiry cycle
:param current_month: Current month as a str
:return: new month as str
"""
return self._offset_month(current_month, 1)
def _previous_month(self, current_month: str) -> str:
"""
Move one month back in expiry cycle
:param current_month: Current month as a str
:return: new month as str
"""
return self._offset_month(current_month, -1)
def _offset_month(self, current_month: str, offset: int) -> str:
"""
Move a number of months in the expiry cycle
:param current_month: Current month as a str
:param offset: number of months to go forwards or backwards
:return: new month as str
"""
current_index = self._where_month(current_month)
len_cycle = len(self._cyclestring)
new_index = current_index + offset
cycled_index = new_index % len_cycle
return self.cyclestring[cycled_index]
def _where_month(self, current_month: str) -> int:
"""
Return the index value (0 is first) of month in expiry
:param current_month: month as str
:return: int
"""
if not self.check_is_month_in_rollcycle(current_month):
raise Exception("%s not in cycle %s" % (current_month, self._cyclestring))
return self.cyclestring.index(current_month)
def _month_is_first(self, current_month: str) -> bool:
"""
Is this the first month in the expiry cycle?
:param current_month: month as str
:return: bool
"""
return self._where_month(current_month) == 0
def _month_is_last(self, current_month: str) -> bool:
"""
Is this the last month in the expiry cycle?
:param current_month: month as str
:return: bool
"""
return self._where_month(current_month) == len(self._cyclestring) - 1
def _as_list(self) -> list:
"""
:return: list with int values referring to month numbers eg January =12 etc
"""
return [
month_from_contract_letter(contract_letter)
for contract_letter in self.cyclestring
]
def check_is_month_in_rollcycle(self, current_month: str) -> bool:
"""
Is current_month in our expiry cycle?
:param current_month: month as str
:return: bool
"""
if current_month in self._cyclestring:
return True
else:
return False
GLOBAL_ROLLCYCLE = rollCycle("".join(FUTURES_MONTH_LIST))
class rollParameters(object):
"""
A rollParameters object contains information about roll cycles and how we hold contracts
When combined with a contractDate we get a rollWithData which we can use to manipulate the contractDate
according to the rules of rollParameters
"""
def __init__(
self,
hold_rollcycle: str,
priced_rollcycle: str,
roll_offset_day: int = 0,
carry_offset: int = -1,
approx_expiry_offset: int = 0,
):
"""
:param hold_rollcycle: The rollcycle which we actually want to hold, str
:param priced_rollcycle: The entire rollcycle for which prices are available, str
:param roll_offset_day: The day, relative to the expiry date, when we usually roll; int
:param carry_offset: The number of contracts forward or backwards we look for to define carry in the priced roll cycle; int
:param approx_expiry_offset: The offset, relative to the 1st of the contract month, when an expiry date usually occurs; int
"""
self._hold_rollcycle = rollCycle(hold_rollcycle)
self._priced_rollcycle = rollCycle(priced_rollcycle)
self._global_rollcycle = GLOBAL_ROLLCYCLE
self._roll_offset_day = int(roll_offset_day)
self._carry_offset = int(carry_offset)
self._approx_expiry_offset = int(approx_expiry_offset)
@property
def roll_offset_day(self):
return self._roll_offset_day
@property
def carry_offset(self):
return self._carry_offset
@property
def approx_expiry_offset(self):
return self._approx_expiry_offset
def __eq__(self, other):
return (
(self.hold_rollcycle == other.hold_rollcycle)
& (self.priced_rollcycle == self.priced_rollcycle)
& (self.global_rollcycle == other.global_rollcycle)
& (self.roll_offset_day == other.roll_offset_day)
& (self.carry_offset == other.carry_offset)
& (self.approx_expiry_offset == other.approx_expiry_offset)
)
def __repr__(self):
dict_rep = self.as_dict()
str_rep = ", ".join(
["%s:%s" % (key, str(dict_rep[key])) for key in dict_rep.keys()]
)
return "Rollcycle parameters " + str_rep
@property
def priced_rollcycle(self) -> rollCycle:
return self._priced_rollcycle
@property
def hold_rollcycle(self) -> rollCycle:
return self._hold_rollcycle
@property
def global_rollcycle(self):
return self._global_rollcycle
@classmethod
def create_from_dict(rollData, roll_data_dict: dict):
futures_instrument_roll_data = rollData(**roll_data_dict)
return futures_instrument_roll_data
def as_dict(self) -> dict:
return dict(
hold_rollcycle=self.hold_rollcycle.cyclestring,
priced_rollcycle=self.priced_rollcycle.cyclestring,
roll_offset_day=self.roll_offset_day,
carry_offset=self.carry_offset,
approx_expiry_offset=self.approx_expiry_offset,
)
def rolls_per_year_in_hold_cycle(self) -> int:
hold_cycle = self.hold_rollcycle
return len(hold_cycle)
class contractDateWithRollParameters(object):
""" """
def __init__(self, contract_date: contractDate, roll_parameters: rollParameters):
"""
Roll data plus a specific contract date means we can do things like iterate the roll cycle etc
"""
self._roll_parameters = roll_parameters
self._contract_date = contract_date
@property
def roll_parameters(self):
return self._roll_parameters
@property
def contract_date(self):
return self._contract_date
@property
def date_str(self):
return self.contract_date.date_str
def __repr__(self):
return "%s with roll parameters %s" % (
str(self.contract_date),
str(self.roll_parameters),
)
def next_priced_contract(self):
contract = self._closest_previous_valid_priced_contract()
return contract._iterate_contract(forward, "priced_rollcycle")
def previous_priced_contract(self):
contract = self._closest_next_valid_priced_contract()
return contract._iterate_contract(backwards, "priced_rollcycle")
def next_held_contract(self):
contract = self._closest_previous_valid_held_contract()
return contract._iterate_contract(forward, "hold_rollcycle")
def previous_held_contract(self):
contract = self._closest_next_valid_held_contract()
return contract._iterate_contract(backwards, "hold_rollcycle")
def _closest_next_valid_priced_contract(self):
# returns current contract if a valid priced contract, or next one in
# cycle that is
valid_contract_to_return = self
while not valid_contract_to_return._valid_date_in_priced_rollcycle():
valid_contract_to_return = valid_contract_to_return._next_month_contract()
return valid_contract_to_return
def _closest_previous_valid_priced_contract(self):
# returns current contract if a valid priced contract, or previous one
# in cycle that is
valid_contract_to_return = self
while not valid_contract_to_return._valid_date_in_priced_rollcycle():
valid_contract_to_return = (
valid_contract_to_return._previous_month_contract()
)
return valid_contract_to_return
def _closest_next_valid_held_contract(self):
# returns current contract if a valid held contract, or next one in
# cycle that is
valid_contract_to_return = self
while not valid_contract_to_return._valid_date_in_hold_rollcycle():
valid_contract_to_return = valid_contract_to_return._next_month_contract()
return valid_contract_to_return
def _closest_previous_valid_held_contract(self):
# returns current contract if a valid held contract, or previous one in
# cycle that is
valid_contract_to_return = self
while not valid_contract_to_return._valid_date_in_hold_rollcycle():
valid_contract_to_return = (
valid_contract_to_return._previous_month_contract()
)
return valid_contract_to_return
def _next_month_contract(self):
return self._iterate_contract(forward, "global_rollcycle")
def _previous_month_contract(self):
return self._iterate_contract(backwards, "global_rollcycle")
def _iterate_contract(self, direction: int, rollcycle_name: str):
"""
Used for going backward or forwards
:param direction_function_name: str, attribute method of a roll cycle, either 'next_year_month' or 'previous_year_month'
:param rollcycle_name: str, attribute method of self.roll_parameters, either 'priced_rollcycle' or 'held_rollcycle'
:return: new contractDate object
"""
rollcycle_to_use = getattr(self.roll_parameters, rollcycle_name)
try:
assert self._valid_date_in_named_rollcycle(rollcycle_name) is True
except BaseException:
raise Exception(
"ContractDate %s must be in %s %s"
% (str(self.contract_date), rollcycle_name, str(rollcycle_to_use))
)
new_contract_date = rollcycle_to_use.iterate_contract_date(
direction, self.contract_date
)
existing_roll_parameters = self.roll_parameters
new_contract_date_with_roll_data_object = contractDateWithRollParameters(
new_contract_date, existing_roll_parameters
)
return new_contract_date_with_roll_data_object
def _valid_date_in_priced_rollcycle(self) -> bool:
return self._valid_date_in_named_rollcycle("priced_rollcycle")
def _valid_date_in_hold_rollcycle(self) -> bool:
return self._valid_date_in_named_rollcycle("hold_rollcycle")
def _valid_date_in_named_rollcycle(self, rollcycle_name: str) -> bool:
relevant_rollcycle = getattr(self.roll_parameters, rollcycle_name)
current_month = self.contract_date.letter_month()
return relevant_rollcycle.check_is_month_in_rollcycle(current_month)
def carry_contract(self):
if self.roll_parameters.carry_offset == -1:
return self.previous_priced_contract()
elif self.roll_parameters.carry_offset == 1:
return self.next_priced_contract()
else:
raise Exception("carry_offset needs to be +1 or -1")
@property
def desired_roll_date(self) -> datetime.datetime:
return self.contract_date.expiry_date + datetime.timedelta(
days=np_convert(self.roll_parameters.roll_offset_day)
)
def get_contracts_from_recently_to_contract_date(self):
"""
Returns all the unexpired contracts between now and the contract date
We go back 3 months in case of a mismatch between roll parameters and actual expiries when setting up data
:return: list of contractDate
"""
datetime_now = datetime.datetime.now() - datetime.timedelta(100)
contract_dates = []
current_contract_date_with_roll_parameters = copy(self)
while (
current_contract_date_with_roll_parameters.contract_date.expiry_date
>= datetime_now
):
current_contract_date = (
current_contract_date_with_roll_parameters.contract_date
)
contract_dates.append(current_contract_date)
current_contract_date_with_roll_parameters = (
current_contract_date_with_roll_parameters.previous_priced_contract()
)
return contract_dates