From 7e524ccf7a5dab2ed9dde4ff582a6a30eab20537 Mon Sep 17 00:00:00 2001 From: Gourav Kumar Date: Tue, 10 May 2022 09:51:12 +0530 Subject: [PATCH] incorporated eomonth in ffill and bfill More Sharpe tests and ffill/bfill tests --- fincal/fincal.py | 10 ++++++---- tests/test_fincal.py | 34 ++++++++++++++++++++++++++++++++++ tests/test_stats.py | 25 ++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/fincal/fincal.py b/fincal/fincal.py index f10bd20..0d81c05 100644 --- a/fincal/fincal.py +++ b/fincal/fincal.py @@ -145,7 +145,9 @@ class TimeSeries(TimeSeriesCore): res_string: str = "First date: {}\nLast date: {}\nNumber of rows: {}" return res_string.format(self.start_date, self.end_date, total_dates) - def ffill(self, inplace: bool = False, limit: int = 1000, skip_weekends: bool = False) -> TimeSeries | None: + def ffill( + self, inplace: bool = False, limit: int = 1000, skip_weekends: bool = False, eomonth: bool = False + ) -> TimeSeries | None: """Forward fill missing dates in the time series Parameters @@ -164,7 +166,6 @@ class TimeSeries(TimeSeriesCore): Returns a TimeSeries object if inplace is False, otherwise None """ - eomonth: bool = True if self.frequency.days >= AllFrequencies.M.days else False dates_to_fill = create_date_series( self.start_date, self.end_date, self.frequency.symbol, eomonth, skip_weekends=skip_weekends ) @@ -188,7 +189,9 @@ class TimeSeries(TimeSeriesCore): return self.__class__(new_ts, frequency=self.frequency.symbol) - def bfill(self, inplace: bool = False, limit: int = 1000, skip_weekends: bool = False) -> TimeSeries | None: + def bfill( + self, inplace: bool = False, limit: int = 1000, skip_weekends: bool = False, eomonth: bool = False + ) -> TimeSeries | None: """Backward fill missing dates in the time series Parameters @@ -207,7 +210,6 @@ class TimeSeries(TimeSeriesCore): Returns a TimeSeries object if inplace is False, otherwise None """ - eomonth: bool = True if self.frequency.days >= AllFrequencies.M.days else False dates_to_fill = create_date_series( self.start_date, self.end_date, self.frequency.symbol, eomonth, skip_weekends=skip_weekends ) diff --git a/tests/test_fincal.py b/tests/test_fincal.py index 40527f1..8e471da 100644 --- a/tests/test_fincal.py +++ b/tests/test_fincal.py @@ -164,6 +164,40 @@ class TestTimeSeriesBasics: assert "2017-01-15" in bf assert bf["2017-01-15"][1] == bf["2017-01-22"][1] + def test_fill_monthly(self, create_test_data): + ts_data = create_test_data(frequency=AllFrequencies.M, num=10) + ts_data.pop(2) + ts_data.pop(6) + ts = TimeSeries(ts_data, frequency="M") + assert len(ts) == 8 + + ff = ts.ffill() + assert len(ff) == 10 + assert "2017-03-01" in ff + assert ff["2017-03-01"][1] == ff["2017-02-01"][1] + + bf = ts.bfill() + assert len(bf) == 10 + assert "2017-08-01" in bf + assert bf["2017-08-01"][1] == bf["2017-09-01"][1] + + def test_fill_eomonthly(self, create_test_data): + ts_data = create_test_data(frequency=AllFrequencies.M, num=10, eomonth=True) + ts_data.pop(2) + ts_data.pop(6) + ts = TimeSeries(ts_data, frequency="M") + assert len(ts) == 8 + + ff = ts.ffill() + assert len(ff) == 10 + assert "2017-03-31" in ff + assert ff["2017-03-31"][1] == ff["2017-02-28"][1] + + bf = ts.bfill() + assert len(bf) == 10 + assert "2017-08-31" in bf + assert bf["2017-08-31"][1] == bf["2017-09-30"][1] + class TestReturns: def test_returns_calc(self, create_test_data): diff --git a/tests/test_stats.py b/tests/test_stats.py index 7b24872..e178474 100644 --- a/tests/test_stats.py +++ b/tests/test_stats.py @@ -7,7 +7,7 @@ def test_conf(conf_fun): class TestSharpe: - def test_sharpe_daily(self, create_test_data): + def test_sharpe_daily_freq(self, create_test_data): data = create_test_data(num=1305, frequency=fc.AllFrequencies.D, skip_weekends=True) ts = fc.TimeSeries(data, "D") sharpe_ratio = fc.sharpe_ratio( @@ -49,3 +49,26 @@ class TestSharpe: return_period_value=6, ) assert round(sharpe_ratio, 4) == 0.8401 + + def test_sharpe_weekly_freq(self, create_test_data): + data = create_test_data(num=261, frequency=fc.AllFrequencies.W, mu=0.6, sigma=0.7) + ts = fc.TimeSeries(data, "W") + sharpe_ratio = fc.sharpe_ratio( + ts, + risk_free_rate=0.052, + from_date="2017-01-08", + to_date="2021-12-31", + return_period_unit="days", + return_period_value=7, + ) + assert round(sharpe_ratio, 4) == 0.4533 + + sharpe_ratio = fc.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