modified return calculation to include motnhs and days

This commit is contained in:
= 2022-02-24 11:28:37 +05:30
parent 66ccd2a3f8
commit 0fbca4ae4c

View File

@ -1,11 +1,17 @@
from __future__ import annotations from __future__ import annotations
import datetime import datetime
from typing import List, Union from typing import List, Literal, Union
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from .core import AllFrequencies, TimeSeriesCore, _parse_date, _preprocess_match_options from .core import (
AllFrequencies,
TimeSeriesCore,
_interval_to_years,
_parse_date,
_preprocess_match_options,
)
def create_date_series( def create_date_series(
@ -120,7 +126,8 @@ class TimeSeries(TimeSeriesCore):
prior_match: str = "closest", prior_match: str = "closest",
closest: str = "previous", closest: str = "previous",
compounding: bool = True, compounding: bool = True,
years: int = 1, interval_type: Literal['years', 'months', 'days'] = 'years',
interval_value: int = 1,
date_format: str = None date_format: str = None
) -> float: ) -> float:
"""Method to calculate returns for a certain time-period as on a particular date """Method to calculate returns for a certain time-period as on a particular date
@ -172,7 +179,7 @@ class TimeSeries(TimeSeriesCore):
raise ValueError("As on date not found") raise ValueError("As on date not found")
as_on += as_on_delta as_on += as_on_delta
prev_date = as_on - relativedelta(years=years) prev_date = as_on - relativedelta(**{interval_type: interval_value})
while True: while True:
previous = self.data.get(prev_date, None) previous = self.data.get(prev_date, None)
if previous is not None: if previous is not None:
@ -183,6 +190,7 @@ class TimeSeries(TimeSeriesCore):
returns = current / previous returns = current / previous
if compounding: if compounding:
years = _interval_to_years(interval_type, interval_value)
returns = returns ** (1 / years) returns = returns ** (1 / years)
return returns - 1 return returns - 1