From 18b60bd608d30f590fbbc4fa1f709721df3c92c6 Mon Sep 17 00:00:00 2001 From: Gourav Kumar Date: Sun, 11 Sep 2022 15:12:37 +0530 Subject: [PATCH] sortino filters out nan in rolling returns --- pyfacts/statistics.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pyfacts/statistics.py b/pyfacts/statistics.py index c2a1d02..bd3a336 100644 --- a/pyfacts/statistics.py +++ b/pyfacts/statistics.py @@ -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