Compare commits
5 Commits
c4e1d8b586
...
db8f73d5c6
Author | SHA1 | Date | |
---|---|---|---|
db8f73d5c6 | |||
7b65f6ff3f | |||
18b60bd608 | |||
0fec9abac0 | |||
c35bc35529 |
@ -327,12 +327,18 @@ class TimeSeries(TimeSeriesCore):
|
|||||||
|
|
||||||
as_on_delta, prior_delta = _preprocess_match_options(as_on_match, prior_match, closest)
|
as_on_delta, prior_delta = _preprocess_match_options(as_on_match, prior_match, closest)
|
||||||
|
|
||||||
current = _find_closest_date(self.data, as_on, closest_max_days, as_on_delta, if_not_found)
|
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})
|
prev_date = as_on - relativedelta(**{return_period_unit: return_period_value})
|
||||||
if current[1] != str("nan"):
|
if current[1] != str("nan"):
|
||||||
previous = _find_closest_date(self.data, prev_date, closest_max_days, prior_delta, if_not_found)
|
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")
|
return as_on, float("NaN")
|
||||||
|
|
||||||
returns = current[1] / previous[1]
|
returns = current[1] / previous[1]
|
||||||
@ -593,7 +599,7 @@ class TimeSeries(TimeSeriesCore):
|
|||||||
kwargs["to_date"] = kwargs.get("to_date", self.end_date)
|
kwargs["to_date"] = kwargs.get("to_date", self.end_date)
|
||||||
|
|
||||||
rr = self.calculate_rolling_returns(**kwargs)
|
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:
|
if annualise_returns:
|
||||||
mean_rr = (1 + mean_rr) ** (1 / years) - 1
|
mean_rr = (1 + mean_rr) ** (1 / years) - 1
|
||||||
|
|
||||||
|
@ -574,8 +574,10 @@ def sortino_ratio(
|
|||||||
"closest": closest,
|
"closest": closest,
|
||||||
"date_format": date_format,
|
"date_format": date_format,
|
||||||
}
|
}
|
||||||
average_rr_ts = time_series_data.calculate_rolling_returns(**common_params, annual_compounded_returns=False)
|
average_rr_ts = time_series_data.calculate_rolling_returns(
|
||||||
average_rr = statistics.mean(average_rr_ts.values)
|
**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
|
annualized_average_rr = (1 + average_rr) ** (365 / interval_days) - 1
|
||||||
|
|
||||||
excess_returns = annualized_average_rr - risk_free_rate
|
excess_returns = annualized_average_rr - risk_free_rate
|
||||||
|
@ -166,7 +166,7 @@ def _preprocess_from_to_date(
|
|||||||
|
|
||||||
if from_date is None:
|
if from_date is None:
|
||||||
expected_start_date = time_series.start_date + relativedelta(**{return_period_unit: return_period_value})
|
expected_start_date = time_series.start_date + relativedelta(**{return_period_unit: return_period_value})
|
||||||
from_date = _find_closest_date(time_series.data, expected_start_date, 999, as_on_match, "fail")[0]
|
from_date = _find_closest_date(time_series, expected_start_date, 999, as_on_match, "fail")[0]
|
||||||
|
|
||||||
if to_date is None:
|
if to_date is None:
|
||||||
to_date = time_series.end_date
|
to_date = time_series.end_date
|
||||||
@ -181,16 +181,26 @@ def _find_closest_date(
|
|||||||
delta: datetime.timedelta,
|
delta: datetime.timedelta,
|
||||||
if_not_found: Literal["fail", "nan"],
|
if_not_found: Literal["fail", "nan"],
|
||||||
) -> Tuple[datetime.datetime, float]:
|
) -> Tuple[datetime.datetime, float]:
|
||||||
"""Helper function to find data for the closest available date"""
|
"""Helper function to find data for the closest available date
|
||||||
|
|
||||||
if delta.days < 0 and date < min(data):
|
data:
|
||||||
raise DateOutOfRangeError(date, "min")
|
TimeSeries data
|
||||||
if delta.days > 0 and date > max(data):
|
"""
|
||||||
raise DateOutOfRangeError(date, "max")
|
|
||||||
|
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)
|
row: tuple = data.get(date, None)
|
||||||
if row is not None:
|
if row is not None:
|
||||||
return date, row
|
return row
|
||||||
|
|
||||||
if delta and limit_days != 0:
|
if delta and limit_days != 0:
|
||||||
return _find_closest_date(data, date + delta, limit_days - 1, delta, if_not_found)
|
return _find_closest_date(data, date + delta, limit_days - 1, delta, if_not_found)
|
||||||
|
@ -117,25 +117,25 @@ class TestSortino:
|
|||||||
)
|
)
|
||||||
assert round(sortino, 4) == -5.5233
|
assert round(sortino, 4) == -5.5233
|
||||||
|
|
||||||
# sharpe_ratio = pft.sharpe_ratio(
|
sortino = pft.sortino_ratio(
|
||||||
# ts,
|
ts,
|
||||||
# risk_free_rate=0.052,
|
risk_free_rate=0.052,
|
||||||
# from_date="2017-02-05",
|
from_date="2017-02-05",
|
||||||
# to_date="2021-12-31",
|
to_date="2021-12-31",
|
||||||
# return_period_unit="months",
|
return_period_unit="months",
|
||||||
# return_period_value=1,
|
return_period_value=1,
|
||||||
# )
|
)
|
||||||
# assert round(sharpe_ratio, 4) == 0.4898
|
assert round(sortino, 4) == -1.93
|
||||||
|
|
||||||
# sharpe_ratio = pft.sharpe_ratio(
|
sortino = pft.sortino_ratio(
|
||||||
# ts,
|
ts,
|
||||||
# risk_free_rate=0.052,
|
risk_free_rate=0.052,
|
||||||
# from_date="2018-01-01",
|
from_date="2018-01-01",
|
||||||
# to_date="2021-12-31",
|
to_date="2021-12-31",
|
||||||
# return_period_unit="months",
|
return_period_unit="months",
|
||||||
# return_period_value=12,
|
return_period_value=12,
|
||||||
# )
|
)
|
||||||
# assert round(sharpe_ratio, 4) == 0.3199
|
assert round(sortino, 4) == -3.9805
|
||||||
|
|
||||||
|
|
||||||
class TestBeta:
|
class TestBeta:
|
||||||
|
Loading…
Reference in New Issue
Block a user