Expanded sortino function

This commit is contained in:
Gourav Kumar 2022-07-24 08:46:12 +05:30
parent ef68ae0293
commit b1305ca89d

View File

@ -8,7 +8,9 @@ from typing import Literal
from pyfacts.core import date_parser
from .pyfacts import TimeSeries
from .utils import _interval_to_years, covariance
from .utils import _interval_to_years, _preprocess_from_to_date, covariance
# from dateutil.relativedelta import relativedelta
@date_parser(3, 4)
@ -540,10 +542,21 @@ def sortino_ratio(
interval_days = math.ceil(_interval_to_years(return_period_unit, return_period_value) * 365)
if from_date is None:
from_date = time_series_data.start_date + datetime.timedelta(days=interval_days)
if to_date is None:
to_date = time_series_data.end_date
# if from_date is None:
# from_date = time_series_data.start_date + relativedelta(**{return_period_unit: return_period_value})
# if to_date is None:
# to_date = time_series_data.end_date
from_date, to_date = _preprocess_from_to_date(
from_date,
to_date,
time_series_data,
False,
return_period_unit,
return_period_value,
as_on_match,
prior_match,
closest,
)
if risk_free_data is None and risk_free_rate is None:
raise ValueError("At least one of risk_free_data or risk_free rate is required")
@ -566,7 +579,8 @@ def sortino_ratio(
annualized_average_rr = (1 + average_rr) ** (365 / interval_days) - 1
excess_returns = annualized_average_rr - risk_free_rate
sd = statistics.stdev([i for i in average_rr_ts.values if i < 0])
my_list = [i for i in average_rr_ts.values if i < 0]
sd = statistics.stdev(my_list) # [i for i in average_rr_ts.values if i < 0])
sd *= math.sqrt(365 / interval_days)
sortino_ratio_value = excess_returns / sd