|
| 1 | +"""_summary_ |
| 2 | +
|
| 3 | +Raises: |
| 4 | + ValueError: _description_ |
| 5 | +
|
| 6 | +Returns: |
| 7 | + _type_: _description_ |
| 8 | +""" |
| 9 | + |
| 10 | +from typing import List |
| 11 | +import pandas as pd |
| 12 | +from daycount.convention import Convention |
| 13 | +from daycount.day_count_factor import DayCountFactor |
| 14 | +from enums import DayCountTimePeriod |
| 15 | +from utils import roll_day, roll_month, has_month_end_day |
| 16 | + |
| 17 | + |
| 18 | +class USAppendixJ(Convention): |
| 19 | + """_summary_ |
| 20 | +
|
| 21 | + Args: |
| 22 | + Convention (_type_): _description_ |
| 23 | + """ |
| 24 | + def __init__(self, time_period: DayCountTimePeriod = DayCountTimePeriod.MONTH): |
| 25 | + """_summary_ |
| 26 | +
|
| 27 | + Args: |
| 28 | + time_period (DayCountTimePeriod, optional): _description_. |
| 29 | + Defaults to DayCountTimePeriod.MONTH. |
| 30 | + """ |
| 31 | + super().__init__(use_post_dates=True, |
| 32 | + include_non_financing_flows=True, |
| 33 | + use_xirr_method=True) |
| 34 | + self.time_period = time_period |
| 35 | + |
| 36 | + def compute_factor(self, start: pd.Timestamp, end: pd.Timestamp) -> DayCountFactor: |
| 37 | + """_summary_ |
| 38 | +
|
| 39 | + Args: |
| 40 | + start (pd.Timestamp): _description_ |
| 41 | + end (pd.Timestamp): _description_ |
| 42 | +
|
| 43 | + Raises: |
| 44 | + ValueError: _description_ |
| 45 | +
|
| 46 | + Returns: |
| 47 | + DayCountFactor: _description_ |
| 48 | + """ |
| 49 | + whole_periods = 0 |
| 50 | + initial_drawdown = start |
| 51 | + start_whole_period = end |
| 52 | + principal_operand_log: List[str] = [] |
| 53 | + fractional_operand_log: List[str] = [] |
| 54 | + |
| 55 | + if self.time_period == DayCountTimePeriod.DAY: |
| 56 | + days = (start_whole_period - initial_drawdown).days |
| 57 | + fractional_adjustment = days / 365.0 |
| 58 | + fractional_operand_log.append(DayCountFactor.operands_to_string(days, 365)) |
| 59 | + return DayCountFactor.us_appendix_j( |
| 60 | + principal_factor=0.0, |
| 61 | + fractional_adjustment=fractional_adjustment, |
| 62 | + principal_operand_log=principal_operand_log, |
| 63 | + fractional_operand_log=fractional_operand_log |
| 64 | + ) |
| 65 | + |
| 66 | + while True: |
| 67 | + temp_date = start_whole_period |
| 68 | + if self.time_period == DayCountTimePeriod.YEAR: |
| 69 | + temp_date = roll_month(start_whole_period, -12, end.day) |
| 70 | + elif self.time_period == DayCountTimePeriod.HALF_YEAR: |
| 71 | + temp_date = roll_month(start_whole_period, -6, end.day) |
| 72 | + elif self.time_period == DayCountTimePeriod.QUARTER: |
| 73 | + temp_date = roll_month(start_whole_period, -3, end.day) |
| 74 | + elif self.time_period == DayCountTimePeriod.MONTH: |
| 75 | + temp_date = roll_month(start_whole_period, -1, end.day) |
| 76 | + elif self.time_period == DayCountTimePeriod.FORTNIGHT: |
| 77 | + temp_date = roll_day(start_whole_period, -14) |
| 78 | + elif self.time_period == DayCountTimePeriod.WEEK: |
| 79 | + temp_date = roll_day(start_whole_period, -7) |
| 80 | + else: |
| 81 | + raise ValueError("Unsupported time period") |
| 82 | + |
| 83 | + if not initial_drawdown > temp_date: |
| 84 | + start_whole_period = temp_date |
| 85 | + whole_periods += 1 |
| 86 | + else: |
| 87 | + if self.time_period in [DayCountTimePeriod.YEAR, DayCountTimePeriod.HALF_YEAR, |
| 88 | + DayCountTimePeriod.QUARTER, DayCountTimePeriod.MONTH]: |
| 89 | + if initial_drawdown.day == temp_date.day: |
| 90 | + break |
| 91 | + if (initial_drawdown.month == temp_date.month and |
| 92 | + has_month_end_day(initial_drawdown) and |
| 93 | + has_month_end_day(end)): |
| 94 | + if initial_drawdown.day <= temp_date.day or has_month_end_day(end): |
| 95 | + start_whole_period = initial_drawdown |
| 96 | + whole_periods += 1 |
| 97 | + break |
| 98 | + |
| 99 | + principal_factor = float(whole_periods) if whole_periods > 0 else 0.0 |
| 100 | + if whole_periods > 0: |
| 101 | + principal_operand_log.append(str(whole_periods)) |
| 102 | + |
| 103 | + fractional_adjustment = 0.0 |
| 104 | + if not initial_drawdown > start_whole_period: |
| 105 | + days = (start_whole_period - initial_drawdown).days |
| 106 | + denominator = { |
| 107 | + DayCountTimePeriod.YEAR: 365, |
| 108 | + DayCountTimePeriod.HALF_YEAR: 180, |
| 109 | + DayCountTimePeriod.QUARTER: 90, |
| 110 | + DayCountTimePeriod.MONTH: 30, |
| 111 | + DayCountTimePeriod.FORTNIGHT: 15, |
| 112 | + DayCountTimePeriod.WEEK: 7 |
| 113 | + }.get(self.time_period, 30) |
| 114 | + |
| 115 | + if days > 0: |
| 116 | + fractional_adjustment = days / denominator |
| 117 | + fractional_operand_log.append( |
| 118 | + DayCountFactor.operands_to_string(days, denominator) |
| 119 | + ) |
| 120 | + |
| 121 | + return DayCountFactor.us_appendix_j( |
| 122 | + principal_factor=principal_factor, |
| 123 | + fractional_adjustment=fractional_adjustment, |
| 124 | + principal_operand_log=principal_operand_log, |
| 125 | + fractional_operand_log=fractional_operand_log |
| 126 | + ) |
0 commit comments