forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive_update_roll_status.py
563 lines (435 loc) · 18.1 KB
/
interactive_update_roll_status.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
"""
Roll adjusted and multiple prices for a given contract, after checking that we do not have positions
NOTE: this does not update the roll calendar .csv files stored elsewhere. Under DRY the sole source of production
roll info is the multiple prices series
"""
from dataclasses import dataclass
import numpy as np
from syscore.interactive import print_menu_of_values_and_get_response, get_and_convert
from syscore.objects import success, failure, status, named_object
from syscore.text import landing_strip, print_with_landing_strips_around
from sysdata.data_blob import dataBlob
from sysobjects.contracts import futuresContract
from sysobjects.production.roll_state import (
default_state,
roll_adj_state,
explain_roll_state_str,
allowable_roll_state_from_current_and_position,
RollState,
no_roll_state,
)
from sysproduction.reporting.report_configs import roll_report_config
from sysproduction.reporting.reporting_functions import run_report_with_data_blob
from sysproduction.data.positions import diagPositions, updatePositions
from sysproduction.data.contracts import dataContracts
from sysproduction.data.prices import diagPrices, get_valid_instrument_code_from_user
from sysproduction.reporting.data.rolls import (
rollingAdjustedAndMultiplePrices,
relative_volume_in_forward_contract_versus_price,
)
no_change_required = named_object("No roll required")
EXIT_CODE = "EXIT"
def interactive_update_roll_status():
with dataBlob(log_name="Interactive_Update-Roll-Status") as data:
function_to_call = get_rolling_master_function()
function_to_call(data)
def get_rolling_master_function():
MANUAL_INPUT = "Manually input instrument codes and manually decide when to roll"
MENU_OPTIONS = [
MANUAL_INPUT,
"Cycle through instrument codes automatically, but manually decide when to roll",
"Cycle through instrument codes automatically, auto decide when to roll, manually confirm rolls",
"Cycle through instrument codes automatically, auto decide when to roll, automatically roll",
]
function_list = [
update_roll_status_manual_cycle,
update_roll_status_auto_cycle_manual_decide,
update_roll_status_auto_cycle_manual_confirm,
update_roll_status_full_auto,
]
print("How do you want to do your rolls today?")
selection = print_menu_of_values_and_get_response(
MENU_OPTIONS, default_str=MANUAL_INPUT
)
selection_idx = MENU_OPTIONS.index(selection)
function_to_call = function_list[selection_idx]
return function_to_call
@dataclass
class RollDataWithStateReporting(object):
instrument_code: str
original_roll_status: RollState
position_priced_contract: int
allowable_roll_states_as_list_of_str: list
days_until_roll: int
relative_volume: float
@property
def original_roll_status_as_string(self):
return self.original_roll_status.name
def display_roll_query_banner(self):
print(landing_strip(80))
print("Current State: %s" % self.original_roll_status)
print(
"Current position in priced contract %d (if zero can Roll Adjusted prices)"
% self.position_priced_contract
)
print("")
print("These are your options:")
print("")
for state_number, state in enumerate(self.allowable_roll_states_as_list_of_str):
print("%s: %s" % (state, explain_roll_state_str(state)))
print("")
def update_roll_status_manual_cycle(data: dataBlob):
do_another = True
while do_another:
instrument_code = get_valid_instrument_code_from_user(
data=data, allow_exit=True, exit_code=EXIT_CODE
)
if instrument_code is EXIT_CODE:
# belt and braces
do_another = False
else:
manually_report_and_update_roll_state_for_code(data, instrument_code)
return success
def update_roll_status_auto_cycle_manual_decide(data: dataBlob):
days_ahead = get_days_ahead_to_consider_when_auto_cycling()
instrument_list = get_list_of_instruments_to_auto_cycle(data, days_ahead=days_ahead)
for instrument_code in instrument_list:
manually_report_and_update_roll_state_for_code(
data=data, instrument_code=instrument_code
)
return success
def update_roll_status_auto_cycle_manual_confirm(data: dataBlob):
days_ahead = get_days_ahead_to_consider_when_auto_cycling()
auto_parameters = get_auto_roll_parameters()
instrument_list = get_list_of_instruments_to_auto_cycle(data, days_ahead=days_ahead)
for instrument_code in instrument_list:
roll_data = setup_roll_data_with_state_reporting(data, instrument_code)
roll_state_required = auto_selected_roll_state_instrument(
data=data, roll_data=roll_data, auto_parameters=auto_parameters
)
if roll_state_required is no_change_required:
warn_not_rolling(instrument_code, auto_parameters)
else:
modify_roll_state(
data=data,
instrument_code=instrument_code,
original_roll_state=roll_data.original_roll_status,
roll_state_required=roll_state_required,
confirm_adjusted_price_change=True,
)
def update_roll_status_full_auto(data: dataBlob):
days_ahead = get_days_ahead_to_consider_when_auto_cycling()
instrument_list = get_list_of_instruments_to_auto_cycle(data, days_ahead=days_ahead)
auto_parameters = get_auto_roll_parameters()
for instrument_code in instrument_list:
roll_data = setup_roll_data_with_state_reporting(data, instrument_code)
roll_state_required = auto_selected_roll_state_instrument(
data=data, roll_data=roll_data, auto_parameters=auto_parameters
)
if roll_state_required is no_change_required:
warn_not_rolling(instrument_code, auto_parameters)
else:
modify_roll_state(
data=data,
instrument_code=instrument_code,
original_roll_state=roll_data.original_roll_status,
roll_state_required=roll_state_required,
confirm_adjusted_price_change=False,
)
def get_days_ahead_to_consider_when_auto_cycling() -> int:
days_ahead = get_and_convert(
"How many days ahead should I look for expiries?",
type_expected=int,
allow_default=True,
default_value=10,
)
return days_ahead
def get_list_of_instruments_to_auto_cycle(data: dataBlob, days_ahead: int = 10) -> list:
diag_prices = diagPrices()
list_of_potential_instruments = (
diag_prices.get_list_of_instruments_in_multiple_prices()
)
instrument_list = [
instrument_code
for instrument_code in list_of_potential_instruments
if include_instrument_in_auto_cycle(
data=data, instrument_code=instrument_code, days_ahead=days_ahead
)
]
print_with_landing_strips_around(
"Identified following instruments that are near expiry %s"
% str(instrument_list)
)
return instrument_list
def include_instrument_in_auto_cycle(
data: dataBlob, instrument_code: str, days_ahead: int = 10
) -> bool:
days_until_expiry = days_until_earliest_expiry(data, instrument_code)
return days_until_expiry <= days_ahead
def days_until_earliest_expiry(data: dataBlob, instrument_code: str) -> int:
data_contracts = dataContracts(data)
carry_days = data_contracts.days_until_carry_expiry(instrument_code)
roll_days = data_contracts.days_until_roll(instrument_code)
price_days = data_contracts.days_until_price_expiry(instrument_code)
return min([carry_days, roll_days, price_days])
@dataclass
class autoRollParameters:
min_volume: float
manual_prompt_for_position: bool
state_when_position_held: RollState
def get_auto_roll_parameters() -> autoRollParameters:
min_volume = get_and_convert(
"Minimum relative volume before rolling",
type_expected=float,
allow_default=True,
default_value=0.1,
)
manual_prompt_for_position_str = input(
"Manually prompt for state if have position? (n / *anything for yes*)"
)
if manual_prompt_for_position_str == "n":
manual_prompt_for_position = False
else:
manual_prompt_for_position = True
if manual_prompt_for_position:
state_when_position_held = no_change_required
else:
state_when_position_held = get_state_to_use_for_held_position()
auto_parameters = autoRollParameters(
min_volume=min_volume,
manual_prompt_for_position=manual_prompt_for_position,
state_when_position_held=state_when_position_held,
)
return auto_parameters
STATE_OPTIONS = [RollState.Passive, RollState.Force, RollState.Force_Outright]
STATE_OPTIONS_AS_STR = [str(state) for state in STATE_OPTIONS]
def get_state_to_use_for_held_position() -> RollState:
print(
"Choose state to automatically assume if we have a position in priced contract AND roll state is currently NO ROLL"
)
select_state_for_position_held = print_menu_of_values_and_get_response(
STATE_OPTIONS_AS_STR, default_str=STATE_OPTIONS_AS_STR[0]
)
state_when_position_held = STATE_OPTIONS[
STATE_OPTIONS_AS_STR.index(select_state_for_position_held)
]
return state_when_position_held
def auto_selected_roll_state_instrument(
data: dataBlob,
roll_data: RollDataWithStateReporting,
auto_parameters: autoRollParameters,
) -> RollState:
if roll_data.relative_volume < auto_parameters.min_volume:
print_with_landing_strips_around(
"For %s relative volume of %f is less than minimum of %s : NOT AUTO ROLLING"
% (
roll_data.instrument_code,
roll_data.relative_volume,
auto_parameters.min_volume,
)
)
return no_change_required
no_position_held = roll_data.position_priced_contract == 0
if no_position_held:
print_with_landing_strips_around(
"No position held, auto rolling adjusted price for %s"
% roll_data.instrument_code
)
return roll_adj_state
if auto_parameters.manual_prompt_for_position:
run_roll_report(data, roll_data.instrument_code)
roll_state_required = get_roll_state_required(roll_data)
return roll_state_required
original_roll_status = roll_data.original_roll_status
if original_roll_status is no_roll_state:
roll_state_required = auto_parameters.state_when_position_held
print_with_landing_strips_around(
"Automatically changing state from %s to %s for %s"
% (original_roll_status, roll_state_required, roll_data.instrument_code)
)
else:
print_with_landing_strips_around(
"Roll status already set to %s for %s: not changing"
% (original_roll_status, roll_data.instrument_code)
)
return no_change_required
return roll_state_required
def warn_not_rolling(instrument_code: str, auto_parameters: autoRollParameters):
print_with_landing_strips_around(
"\n NOT rolling %s as doesn't meet auto parameters %s\n"
% (instrument_code, str(auto_parameters))
)
def manually_report_and_update_roll_state_for_code(
data: dataBlob, instrument_code: str
):
run_roll_report(data, instrument_code)
manually_update_roll_state_for_code(data, instrument_code)
def manually_update_roll_state_for_code(data: dataBlob, instrument_code: str):
# First get the roll info
# This will also update to console
data.log.setup(instrument_code=instrument_code)
roll_data = setup_roll_data_with_state_reporting(data, instrument_code)
roll_state_required = get_roll_state_required(roll_data)
modify_roll_state(
data=data,
instrument_code=instrument_code,
original_roll_state=roll_data.original_roll_status,
roll_state_required=roll_state_required,
confirm_adjusted_price_change=True,
)
return success
def run_roll_report(data: dataBlob, instrument_code: str):
config = roll_report_config.new_config_with_modified_output("console")
config.modify_kwargs(instrument_code=instrument_code)
report_results = run_report_with_data_blob(config, data)
if report_results is failure:
raise Exception("Can't run roll report, so can't change status")
def get_roll_state_required(roll_data: RollDataWithStateReporting) -> RollState:
invalid_input = True
while invalid_input:
roll_data.display_roll_query_banner()
roll_state_required_as_str = print_menu_of_values_and_get_response(
roll_data.allowable_roll_states_as_list_of_str
)
if roll_state_required_as_str != roll_data.original_roll_status_as_string:
# check if changing
print("")
check = input(
"Changing roll state for %s from %s to %s, are you sure y/n to try again/<RETURN> to exit: "
% (
roll_data.instrument_code,
roll_data.original_roll_status_as_string,
roll_state_required_as_str,
)
)
print("")
if check == "y":
# happy
return RollState[roll_state_required_as_str]
elif check == "":
print("Okay, we're done")
return no_change_required
else:
print("OK. Choose again.")
# back to top of loop
continue
else:
print("No change")
return no_change_required
def setup_roll_data_with_state_reporting(
data: dataBlob, instrument_code: str
) -> RollDataWithStateReporting:
diag_positions = diagPositions(data)
diag_contracts = dataContracts(data)
original_roll_status = diag_positions.get_roll_state(instrument_code)
priced_contract_date = diag_contracts.get_priced_contract_id(instrument_code)
contract = futuresContract(instrument_code, priced_contract_date)
position_priced_contract = int(diag_positions.get_position_for_contract(contract))
allowable_roll_states = allowable_roll_state_from_current_and_position(
original_roll_status, position_priced_contract
)
days_until_roll = diag_contracts.days_until_roll(instrument_code)
relative_volume = relative_volume_in_forward_contract_versus_price(
data=data, instrument_code=instrument_code
)
if np.isnan(relative_volume):
relative_volume = 0.0
roll_data_with_state = RollDataWithStateReporting(
instrument_code=instrument_code,
original_roll_status=original_roll_status,
position_priced_contract=position_priced_contract,
allowable_roll_states_as_list_of_str=allowable_roll_states,
days_until_roll=days_until_roll,
relative_volume=relative_volume,
)
return roll_data_with_state
def modify_roll_state(
data: dataBlob,
instrument_code: str,
original_roll_state: RollState,
roll_state_required: RollState,
confirm_adjusted_price_change: bool = True,
):
if roll_state_required is no_change_required:
return
if roll_state_required is original_roll_state:
return
update_positions = updatePositions(data)
update_positions.set_roll_state(instrument_code, roll_state_required)
if roll_state_required is roll_adj_state:
state_change_to_roll_adjusted_prices(
data=data,
instrument_code=instrument_code,
original_roll_state=original_roll_state,
confirm_adjusted_price_change=confirm_adjusted_price_change,
)
def state_change_to_roll_adjusted_prices(
data: dataBlob,
instrument_code: str,
original_roll_state: RollState,
confirm_adjusted_price_change: bool = True,
):
# Going to roll adjusted prices
update_positions = updatePositions(data)
roll_result = roll_adjusted_and_multiple_prices(
data=data,
instrument_code=instrument_code,
confirm_adjusted_price_change=confirm_adjusted_price_change,
)
if roll_result is success:
# Return the state back to default (no roll) state
data.log.msg(
"Successful roll! Returning roll state of %s to %s"
% (instrument_code, default_state)
)
update_positions.set_roll_state(instrument_code, default_state)
else:
data.log.msg(
"Something has gone wrong with rolling adjusted of %s! Returning roll state to previous state of %s"
% (instrument_code, original_roll_state)
)
update_positions.set_roll_state(instrument_code, original_roll_state)
def roll_adjusted_and_multiple_prices(
data: dataBlob, instrument_code: str, confirm_adjusted_price_change: bool = True
) -> status:
"""
Roll multiple and adjusted prices
THE POSITION MUST BE ZERO IN THE PRICED CONTRACT! WE DON'T CHECK THIS HERE
:param data: dataBlob
:param instrument_code: str
:return:
"""
print(landing_strip(80))
print("")
print("Rolling adjusted prices!")
print("")
try:
rolling_adj_and_mult_object = rollingAdjustedAndMultiplePrices(
data, instrument_code
)
# this will also do the roll calculations
rolling_adj_and_mult_object.compare_old_and_new_prices()
except Exception as e:
print("Error %s when trying to calculate roll prices" % str(e))
return failure
if confirm_adjusted_price_change:
confirm_roll = input(
"Confirm roll adjusted prices for %s are you sure y/n:" % instrument_code
)
if confirm_roll != "y":
print(
"\nUSER DID NOT WANT TO ROLL: Setting roll status back to previous state"
)
return failure
else:
print_with_landing_strips_around("AUTO ROLLING - NO USER CONFIRMATION REQUIRED")
try:
rolling_adj_and_mult_object.write_new_rolled_data()
except Exception as e:
data.log.warn(
"%s went wrong when rolling: Going to roll-back to original multiple/adjusted prices"
% e
)
rolling_adj_and_mult_object.rollback()
return failure
return success