def _iterate_contract(self, direction_function_name, rollcycle_name): """ Used for going backward or forwards :param direction_function_name: str, attribute method of a roll cycle, eithier 'next_year_month' or 'previous_year_month' :param rollcycle_name: str, attribute method of self.roll_parameters, eithier 'priced_rollcycle' or 'held_rollcycle' :return: new contractDate object """ self._check_valid_date_in_named_rollcycle(rollcycle_name) rollcycle_to_use = getattr(self.roll_parameters, rollcycle_name) direction_function = getattr(rollcycle_to_use, direction_function_name) current_month_str = self.letter_month() current_year_int = self.year() new_year_int, new_month_str = direction_function(current_year_int, current_month_str) new_month_int = month_from_contract_letter(new_month_str) if self._only_has_month: new_day_number = 0 else: new_day_number=self.day() ## we don't pass expiry date as that will change return contractDateWithRollParameters.contract_date_from_numbers(self.roll_parameters, new_year_int, new_month_int, new_day_number = new_day_number, approx_expiry_offset = self.roll_parameters.approx_expiry_offset)
def yearmonth_inrollcycle_after_date(self, reference_date): """ Returns a tuple (month,year) which is in this roll cycle; and which is just before reference_date :param reference_date: datetime.datetime :return: tuple (int, int) """ relevant_year = reference_date.year relevant_month = reference_date.month roll_cycle_as_list = self.as_list() closest_month_index = bisect_right(roll_cycle_as_list, relevant_month) if closest_month_index==len(roll_cycle_as_list): ## fallen into the next year # go forward one from the last month last_month_in_year_as_str = self.cyclestring[-1] adjusted_year_int, adjusted_month_str = self.next_year_month( relevant_year, last_month_in_year_as_str) adjusted_month_int = month_from_contract_letter(adjusted_month_str) else: adjusted_month_int = roll_cycle_as_list[closest_month_index] adjusted_year_int = relevant_year return (adjusted_year_int, adjusted_month_int)
def yearmonth_inrollcycle_before_date(self, reference_date): """ Returns a tuple (month,year) which is in this roll cycle; and which is just before reference_date :param reference_date: datetime.datetime :return: tuple (int, int) """ relevant_year = reference_date.year relevant_month = reference_date.month roll_cycle_as_list = self.as_list() closest_month_index = bisect_left(roll_cycle_as_list, relevant_month) - 1 if closest_month_index == -1: # We are to the left of, or equal to the first month, go back one first_month_in_year_as_str = self.cyclestring[0] adjusted_year_int, adjusted_month_str = self.previous_year_month( relevant_year, first_month_in_year_as_str) adjusted_month_int = month_from_contract_letter(adjusted_month_str) else: adjusted_month_int = roll_cycle_as_list[closest_month_index] adjusted_year_int = relevant_year return (adjusted_year_int, adjusted_month_int)
def strip_file_names(pathname): # These won't have .csv attached file_names = files_with_extension_in_pathname(pathname) for filename in file_names: identifier = filename.split("_")[0] yearcode = int(identifier[len(identifier)-2:]) monthcode = identifier[len(identifier)-3] if yearcode>50: year = 1900+yearcode else: year = 2000+yearcode month = month_from_contract_letter(monthcode) marketcode = identifier[:len(identifier)-3] instrument = market_map[marketcode] datecode = str(year)+'{0:02d}'.format(month) new_file_name = "%s_%s00.csv" % (instrument, datecode) new_full_name = "%s%s" % (pathname, new_file_name) old_full_name = "%s%s.csv" % (pathname, filename) print("Rename %s to\n %s" % (old_full_name, new_full_name)) os.rename(old_full_name, new_full_name) return None
def yearmonth_inrollcycle_after_date(self, reference_date): """ Returns a tuple (month,year) which is in this roll cycle; and which is just before reference_date :param reference_date: datetime.datetime :return: tuple (int, int) """ relevant_year = reference_date.year relevant_month = reference_date.month roll_cycle_as_list = self.as_list() closest_month_index = bisect_right(roll_cycle_as_list, relevant_month) if closest_month_index==len(roll_cycle_as_list): ## fallen into the next year # go forward one from the last month last_month_in_year_as_str = self.cyclestring[-1] adjusted_year_int, adjusted_month_str = self.next_year_month( relevant_year, last_month_in_year_as_str) adjusted_month_int = month_from_contract_letter(adjusted_month_str) else: adjusted_month_int = roll_cycle_as_list[closest_month_index] adjusted_year_int = relevant_year return (adjusted_year_int, adjusted_month_int)
def _iterate_contract(self, direction_function_name, rollcycle_name): """ Used for going backward or forwards :param direction_function_name: str, attribute method of a roll cycle, eithier 'next_year_month' or 'previous_year_month' :param rollcycle_name: str, attribute method of self.roll_parameters, eithier 'priced_rollcycle' or 'held_rollcycle' :return: new contractDate object """ self._check_valid_date_in_named_rollcycle(rollcycle_name) rollcycle_to_use = getattr(self.roll_parameters, rollcycle_name) direction_function = getattr(rollcycle_to_use, direction_function_name) current_month_str = self.letter_month() current_year_int = self.year() new_year_int, new_month_str = direction_function(current_year_int, current_month_str) new_month_int = month_from_contract_letter(new_month_str) if self._only_has_month: new_day_number = 0 else: new_day_number=self.day() ## we don't pass expiry date as that will change return contractDateWithRollParameters.contract_date_from_numbers(self.roll_parameters, new_year_int, new_month_int, new_day_number = new_day_number, approx_expiry_offset = self.roll_parameters.approx_expiry_offset)
def _yearmonth_inrollcycle_before_dateNOTUSED(self, reference_date): ## FEELS LIKE WE SHOULD BE WORKING IN CONTRACT DATES RATHER THAN TUPLES HERE... ## IS THIS CODE USED?? """ Returns a tuple (month,year) which is in this roll cycle; and which is just before reference_date :param reference_date: datetime.datetime :return: tuple (int, int) """ relevant_year = reference_date.year relevant_month = reference_date.month roll_cycle_as_list = self._as_list() closest_month_index = bisect_left(roll_cycle_as_list, relevant_month) - 1 if closest_month_index == -1: # We are to the left of, or equal to the first month, go back one first_month_in_year_as_str = self._cyclestring[0] adjusted_year_int, adjusted_month_str = self._previous_year_month_given_tuple( relevant_year, first_month_in_year_as_str) adjusted_month_int = month_from_contract_letter(adjusted_month_str) else: adjusted_month_int = roll_cycle_as_list[closest_month_index] adjusted_year_int = relevant_year return (adjusted_year_int, adjusted_month_int)
def __init__(self, cyclestring): assert type(cyclestring) is str self.cyclestring = ''.join(sorted(cyclestring)) self.cycle_as_list = [ month_from_contract_letter(contract_letter) for contract_letter in self.cyclestring ]
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 contract_given_tuple(contract_date: contractDate, year_value:int, month_str: str): if contract_date.only_has_month: new_day_number = 0 else: new_day_number = contract_date.day() month_int = month_from_contract_letter(month_str) date_str = from_contract_numbers_to_contract_string(year_value, month_int, new_day_number) return contractDate(date_str)
def __init__(self, cyclestring): assert type(cyclestring) is str self.cyclestring = ''.join(sorted(cyclestring)) self.cycle_as_list = [month_from_contract_letter(contract_letter) for contract_letter in self.cyclestring] if cyclestring == EMPTY_ROLL_CYCLE_STRING: self._isempty = True else: self._isempty = False
def __init__(self, cyclestring): assert type(cyclestring) is str self.cyclestring = ''.join(sorted(cyclestring)) self.cycle_as_list = [month_from_contract_letter(contract_letter) for contract_letter in self.cyclestring] if cyclestring == EMPTY_ROLL_CYCLE_STRING: self._isempty = True else: self._isempty = False
def _iterate_contract(self, direction_function_name, rollcycle_name): """ 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) direction_function = getattr(rollcycle_to_use, direction_function_name) try: assert self.valid_date_in_named_rollcycle(rollcycle_name) is True except BaseException: raise Exception( "ContractDate %s with %s roll cycle, must be in %s %s" % (self.date, rollcycle_name, str(rollcycle_to_use)) ) current_month_str = self.letter_month() current_year_int = self.year() new_year_int, new_month_str = direction_function( current_year_int, current_month_str ) new_month_int = month_from_contract_letter(new_month_str) if self.only_has_month: new_day_number = 0 else: new_day_number = self.day() # we don't pass expiry date as that will change return contractDateWithRollParameters.contract_date_from_numbers( self.roll_parameters, new_year_int, new_month_int, new_day_number=new_day_number, approx_expiry_offset=self.roll_parameters.approx_expiry_offset, )
def previous_contract_date(self): """ cycle back in contractDate space :return: a new contractDate """ self.check_for_rollcycle() current_month = self.letter_month() if self.rollcycle.month_is_first(current_month): ## first period new_year_number = self.year() - 1 else: new_year_number = self.year() new_month_letter = self.rollcycle.previous_month(current_month) new_month_number = month_from_contract_letter(new_month_letter) return self.contract_date_from_numbers( new_year_number, new_month_number, new_day_number=self.day(), rollcycle_string=self.rollcycle.cyclestring ) ## we don't pass expiry date as that will change
def next_contract_date(self): """ cycle forwards :return: a new contractDate """ self.check_for_rollcycle() current_month = self.letter_month() if self.rollcycle.month_is_last(current_month): ## last period new_year_number = self.year() + 1 else: new_year_number = self.year() new_month_letter = self.rollcycle.next_month(current_month) new_month_number = month_from_contract_letter(new_month_letter) return self.contract_date_from_numbers( new_year_number, new_month_number, new_day_number=self.day(), rollcycle_string=self.rollcycle.cyclestring ) ## we don't pass expiry date as that will change
def yearmonth_inrollcycle_before_date(self, reference_date): """ Returns a tuple (month,year) which is in this roll cycle; and which is just before reference_date :param reference_date: datetime.datetime :return: tuple (int, int) """ relevant_year = reference_date.year relevant_month = reference_date.month roll_cycle_as_list = self.as_list() closest_month_index = bisect_left(roll_cycle_as_list, relevant_month)-1 if closest_month_index==-1: # We are to the left of, or equal to the first month, go back one first_month_in_year_as_str = self.cyclestring[0] adjusted_year_int, adjusted_month_str = self.previous_year_month(relevant_year, first_month_in_year_as_str) adjusted_month_int = month_from_contract_letter(adjusted_month_str) else: adjusted_month_int = roll_cycle_as_list[closest_month_index] adjusted_year_int = relevant_year return (adjusted_year_int, adjusted_month_int)