From b1305ca89d3eda564e178fc2bffb9d7965b49ea5 Mon Sep 17 00:00:00 2001 From: Gourav Kumar Date: Sun, 24 Jul 2022 08:46:12 +0530 Subject: [PATCH] Expanded sortino function --- pyfacts/statistics.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/pyfacts/statistics.py b/pyfacts/statistics.py index 58694a7..c2a1d02 100644 --- a/pyfacts/statistics.py +++ b/pyfacts/statistics.py @@ -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