forked from pst-group/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollcalendars_from_arcticprices_to_csv.py
More file actions
executable file
·108 lines (76 loc) · 3.5 KB
/
Copy pathrollcalendars_from_arcticprices_to_csv.py
File metadata and controls
executable file
·108 lines (76 loc) · 3.5 KB
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
from syscore.objects import arg_not_supplied
from sysdata.arctic.arctic_futures_per_contract_prices import (
arcticFuturesContractPriceData,
)
from sysdata.mongodb.mongo_roll_data import mongoRollParametersData
from sysobjects.roll_calendars import rollCalendar
from sysdata.csv.csv_roll_calendars import csvRollCalendarData
from sysproduction.data.prices import get_valid_instrument_code_from_user
"""
Generate a 'best guess' roll calendar based on some price data for individual contracts
"""
def build_and_write_roll_calendar(
instrument_code, output_datapath=arg_not_supplied, check_before_writing=True
):
if output_datapath is arg_not_supplied:
print("*** WARNING *** This will overwrite the provided roll calendar. Might be better to use a temporary directory!")
else:
print("Writing to %s" % output_datapath)
artic_prices = arcticFuturesContractPriceData()
mongo_rollparameters = mongoRollParametersData()
csv_roll_calendars = csvRollCalendarData(output_datapath)
dict_of_all_futures_contract_prices = artic_prices.get_all_prices_for_instrument(
instrument_code)
dict_of_futures_contract_prices = dict_of_all_futures_contract_prices.final_prices()
roll_parameters_object = mongo_rollparameters.get_roll_parameters(
instrument_code)
# might take a few seconds
print("Prepping roll calendar... might take a few seconds")
roll_calendar = rollCalendar.create_from_prices(
dict_of_futures_contract_prices, roll_parameters_object
)
# checks - this might fail
roll_calendar.check_if_date_index_monotonic()
# this should never fail
roll_calendar.check_dates_are_valid_for_prices(
dict_of_futures_contract_prices
)
# Write to csv
# Will not work if an existing calendar exists
if check_before_writing:
check_happy_to_write = input(
"Are you ok to write this csv to path %s? [might be worth writing and hacking manually] (yes/other)?" % csv_roll_calendars.datapath
)
else:
check_happy_to_write = "yes"
if check_happy_to_write == "yes":
print("Adding roll calendar")
csv_roll_calendars.add_roll_calendar(instrument_code, roll_calendar, ignore_duplication=True)
else:
print("Not writing")
return roll_calendar
def check_saved_roll_calendar(
instrument_code, input_datapath=None
):
if input_datapath is None:
print("This will check the roll calendar in the default directory : are you are that's what you want to do?")
csv_roll_calendars = csvRollCalendarData(input_datapath)
roll_calendar = csv_roll_calendars.get_roll_calendar(instrument_code)
artic_prices = arcticFuturesContractPriceData()
dict_of_all_futures_contract_prices = artic_prices.get_all_prices_for_instrument(
instrument_code)
dict_of_futures_contract_prices = dict_of_all_futures_contract_prices.final_prices()
print(roll_calendar)
# checks - this might fail
roll_calendar.check_if_date_index_monotonic()
# this should never fail
roll_calendar.check_dates_are_valid_for_prices(
dict_of_futures_contract_prices
)
return roll_calendar
if __name__ == "__main__":
input("Will overwrite existing prices are you sure?! CTL-C to abort")
instrument_code = get_valid_instrument_code_from_user(source='single')
## MODIFY DATAPATH IF REQUIRED
#build_and_write_roll_calendar(instrument_code, output_datapath=arg_not_supplied)
build_and_write_roll_calendar("KR3", output_datapath='/home/rob/')