forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_roll_parameters.py
120 lines (92 loc) · 4.41 KB
/
csv_roll_parameters.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
from syscore.constants import arg_not_supplied
from syscore.fileutils import resolve_path_and_filename_for_package
from sysdata.futures.rolls_parameters import rollParametersData
from sysobjects.rolls import rollParameters
from syslogdiag.log_to_screen import logtoscreen
import pandas as pd
ROLLS_DATAPATH = "data.futures.csvconfig"
ROLLS_CONFIG_FILE = "rollconfig.csv"
class allRollParameters(pd.DataFrame):
@classmethod
def read_from_file(allRollParameters, filename):
try:
roll_data = pd.read_csv(filename)
except BaseException:
raise Exception("Can't read file %s" % filename)
try:
roll_data.index = roll_data.Instrument
roll_data.drop(labels="Instrument", axis=1, inplace=True)
except BaseException:
raise Exception("Badly configured file %s" % (filename))
return allRollParameters(roll_data)
def get_list_of_instruments(self) -> list:
return list(self.index)
def get_roll_parameters_for_instrument(
self, instrument_code: str
) -> rollParameters:
config_for_this_instrument = self.loc[instrument_code]
roll_parameters_object = rollParameters(
hold_rollcycle=config_for_this_instrument.HoldRollCycle,
roll_offset_day=config_for_this_instrument.RollOffsetDays,
carry_offset=config_for_this_instrument.CarryOffset,
priced_rollcycle=config_for_this_instrument.PricedRollCycle,
approx_expiry_offset=config_for_this_instrument.ExpiryOffset,
)
return roll_parameters_object
def update_roll_parameters_for_instrument(
self, instrument_code: str, roll_parameters: rollParameters
):
self.at[instrument_code, "HoldRollCycle"] = roll_parameters.hold_rollcycle
self.at[instrument_code, "RollOffsetDays"] = roll_parameters.roll_offset_day
self.at[instrument_code, "CarryOffset"] = roll_parameters.carry_offset
self.at[instrument_code, "PricedRollCycle"] = roll_parameters.priced_rollcycle
self.at[instrument_code, "ExpiryOffset"] = roll_parameters.approx_expiry_offset
def write_to_file(self, filename: str):
self.to_csv(filename, index_label="Instrument")
class csvRollParametersData(rollParametersData):
"""
Get data about instruments from a special configuration used for initialising the system
"""
def __init__(
self, log=logtoscreen("csvRollParametersData"), datapath=arg_not_supplied
):
super().__init__(log=log)
if datapath is arg_not_supplied:
datapath = ROLLS_DATAPATH
config_file = resolve_path_and_filename_for_package(datapath, ROLLS_CONFIG_FILE)
self._config_file = config_file
def __repr__(self):
return "Roll data for initialising system config"
def get_list_of_instruments(self) -> list:
all_roll_parameters = self.get_roll_parameters_all_instruments()
return all_roll_parameters.get_list_of_instruments()
def _get_roll_parameters_without_checking(
self, instrument_code: str
) -> rollParameters:
all_parameters = self.get_roll_parameters_all_instruments()
return all_parameters.get_roll_parameters_for_instrument(instrument_code)
def _delete_roll_parameters_data_without_any_warning_be_careful(
self, instrument_code: str
):
raise NotImplementedError("csv is read only")
def _add_roll_parameters_without_checking_for_existing_entry(
self, instrument_code: str, roll_parameters: rollParameters
):
## We don't normally allow this, but a special case as we safe modify
all_parameters = self.get_roll_parameters_all_instruments()
all_parameters.update_roll_parameters_for_instrument(
instrument_code, roll_parameters
)
all_parameters.write_to_file(self.config_file)
self.log.warn(
"*** WRITTEN NEW ROLL PARAMETERS TO %s - copy to /data/futures/csvconfig/rollconfig.csv NOW ***"
% self.config_file
)
def write_all_roll_parameters_data(self, roll_parameters_df: pd.DataFrame):
all_roll_parameters = allRollParameters(roll_parameters_df)
all_roll_parameters.write_to_file(self.config_file)
def get_roll_parameters_all_instruments(self) -> allRollParameters:
return allRollParameters.read_from_file(self.config_file)
@property
def config_file(self):
return self._config_file