incorporated eomonth in ffill and bfill

More Sharpe tests and ffill/bfill tests
This commit is contained in:
Gourav Kumar 2022-05-10 09:51:12 +05:30
parent aea6bf9b57
commit 7e524ccf7a
3 changed files with 64 additions and 5 deletions

View File

@ -145,7 +145,9 @@ class TimeSeries(TimeSeriesCore):
res_string: str = "First date: {}\nLast date: {}\nNumber of rows: {}" res_string: str = "First date: {}\nLast date: {}\nNumber of rows: {}"
return res_string.format(self.start_date, self.end_date, total_dates) 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 """Forward fill missing dates in the time series
Parameters Parameters
@ -164,7 +166,6 @@ class TimeSeries(TimeSeriesCore):
Returns a TimeSeries object if inplace is False, otherwise None 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( dates_to_fill = create_date_series(
self.start_date, self.end_date, self.frequency.symbol, eomonth, skip_weekends=skip_weekends 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) 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 """Backward fill missing dates in the time series
Parameters Parameters
@ -207,7 +210,6 @@ class TimeSeries(TimeSeriesCore):
Returns a TimeSeries object if inplace is False, otherwise None 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( dates_to_fill = create_date_series(
self.start_date, self.end_date, self.frequency.symbol, eomonth, skip_weekends=skip_weekends self.start_date, self.end_date, self.frequency.symbol, eomonth, skip_weekends=skip_weekends
) )

View File

@ -164,6 +164,40 @@ class TestTimeSeriesBasics:
assert "2017-01-15" in bf assert "2017-01-15" in bf
assert bf["2017-01-15"][1] == bf["2017-01-22"][1] 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: class TestReturns:
def test_returns_calc(self, create_test_data): def test_returns_calc(self, create_test_data):

View File

@ -7,7 +7,7 @@ def test_conf(conf_fun):
class TestSharpe: 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) data = create_test_data(num=1305, frequency=fc.AllFrequencies.D, skip_weekends=True)
ts = fc.TimeSeries(data, "D") ts = fc.TimeSeries(data, "D")
sharpe_ratio = fc.sharpe_ratio( sharpe_ratio = fc.sharpe_ratio(
@ -49,3 +49,26 @@ class TestSharpe:
return_period_value=6, return_period_value=6,
) )
assert round(sharpe_ratio, 4) == 0.8401 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