Compare commits

...

4 Commits

4 changed files with 38 additions and 24 deletions

View File

@ -328,11 +328,17 @@ class TimeSeries(TimeSeriesCore):
as_on_delta, prior_delta = _preprocess_match_options(as_on_match, prior_match, closest)
current = _find_closest_date(self, as_on, closest_max_days, as_on_delta, if_not_found)
prev_date = as_on - relativedelta(**{return_period_unit: return_period_value})
if current[1] != str("nan"):
previous = _find_closest_date(self, prev_date, closest_max_days, prior_delta, if_not_found)
if current[1] == str("nan") or previous[1] == str("nan"):
if (
current[1] == str("nan")
or previous[1] == str("nan")
or current[0] == str("nan")
or previous[0] == str("nan")
):
return as_on, float("NaN")
returns = current[1] / previous[1]
@ -593,7 +599,7 @@ class TimeSeries(TimeSeriesCore):
kwargs["to_date"] = kwargs.get("to_date", self.end_date)
rr = self.calculate_rolling_returns(**kwargs)
mean_rr = statistics.mean(rr.values)
mean_rr = statistics.mean(filter(lambda x: str(x) != "nan", rr.values))
if annualise_returns:
mean_rr = (1 + mean_rr) ** (1 / years) - 1

View File

@ -574,8 +574,10 @@ def sortino_ratio(
"closest": closest,
"date_format": date_format,
}
average_rr_ts = time_series_data.calculate_rolling_returns(**common_params, annual_compounded_returns=False)
average_rr = statistics.mean(average_rr_ts.values)
average_rr_ts = time_series_data.calculate_rolling_returns(
**common_params, annual_compounded_returns=False, if_not_found="nan"
)
average_rr = statistics.mean(filter(lambda x: str(x) != "nan", average_rr_ts.values))
annualized_average_rr = (1 + average_rr) ** (365 / interval_days) - 1
excess_returns = annualized_average_rr - risk_free_rate

View File

@ -188,8 +188,14 @@ def _find_closest_date(
"""
if delta.days < 0 and date < min(data.data):
if if_not_found == "nan":
return float("NaN"), float("NaN")
else:
raise DateOutOfRangeError(date, "min")
if delta.days > 0 and date > max(data.data):
if if_not_found == "nan":
return float("NaN"), float("NaN")
else:
raise DateOutOfRangeError(date, "max")
row: tuple = data.get(date, None)

View File

@ -117,25 +117,25 @@ class TestSortino:
)
assert round(sortino, 4) == -5.5233
# sharpe_ratio = pft.sharpe_ratio(
# ts,
# risk_free_rate=0.052,
# from_date="2017-02-05",
# to_date="2021-12-31",
# return_period_unit="months",
# return_period_value=1,
# )
# assert round(sharpe_ratio, 4) == 0.4898
sortino = pft.sortino_ratio(
ts,
risk_free_rate=0.052,
from_date="2017-02-05",
to_date="2021-12-31",
return_period_unit="months",
return_period_value=1,
)
assert round(sortino, 4) == -1.93
# sharpe_ratio = pft.sharpe_ratio(
# ts,
# risk_free_rate=0.052,
# from_date="2018-01-01",
# to_date="2021-12-31",
# return_period_unit="months",
# return_period_value=12,
# )
# assert round(sharpe_ratio, 4) == 0.3199
sortino = pft.sortino_ratio(
ts,
risk_free_rate=0.052,
from_date="2018-01-01",
to_date="2021-12-31",
return_period_unit="months",
return_period_value=12,
)
assert round(sortino, 4) == -3.9805
class TestBeta: