From aea6bf9b5739d13d8df8230be5505d12b1e822c3 Mon Sep 17 00:00:00 2001 From: Gourav Kumar Date: Sun, 8 May 2022 21:04:48 +0530 Subject: [PATCH] Some tests for Sharpe ratio Also some corrections based identified during testing --- README.md | 5 ++++- fincal/fincal.py | 4 ++-- tests/test_stats.py | 51 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 tests/test_stats.py diff --git a/README.md b/README.md index b6b5333..69068c9 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,12 @@ Fincal aims to simplify things by allowing you to: ### Fincal features - [x] Sync two TimeSeries - [x] Average rolling return -- [ ] Sharpe ratio +- [x] Sharpe ratio - [ ] Jensen's Alpha - [ ] Beta +- [ ] Sortino ratio +- [ ] Correlation & R-squared +- [ ] Treynor ratio - [x] Max drawdown ### Pending implementation diff --git a/fincal/fincal.py b/fincal/fincal.py index b911a9e..f10bd20 100644 --- a/fincal/fincal.py +++ b/fincal/fincal.py @@ -528,7 +528,7 @@ class TimeSeries(TimeSeriesCore): traded_days = FincalOptions.traded_days if return_period_unit == "months": - sd *= math.sqrt(12) + sd *= math.sqrt(12 / return_period_value) elif return_period_unit == "days": sd *= math.sqrt(traded_days / return_period_value) @@ -805,7 +805,7 @@ def read_csv( nrows: int = -1, delimiter: str = ",", encoding: str = "utf-8", -) -> TimeSeriesCore: +) -> TimeSeries: """Reads Time Series data directly from a CSV file""" data = _preprocess_csv(csv_file_path, delimiter, encoding) diff --git a/tests/test_stats.py b/tests/test_stats.py new file mode 100644 index 0000000..7b24872 --- /dev/null +++ b/tests/test_stats.py @@ -0,0 +1,51 @@ +import fincal as fc + + +def test_conf(conf_fun): + conf_add = conf_fun + assert conf_add(2, 4) == 6 + + +class TestSharpe: + def test_sharpe_daily(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( + ts, + risk_free_rate=0.06, + from_date="2017-02-04", + to_date="2021-12-31", + return_period_unit="months", + return_period_value=1, + ) + assert round(sharpe_ratio, 4) == 1.0502 + + sharpe_ratio = fc.sharpe_ratio( + ts, + risk_free_rate=0.06, + from_date="2017-01-09", + to_date="2021-12-31", + return_period_unit="days", + return_period_value=7, + ) + assert round(sharpe_ratio, 4) == 1.0701 + + sharpe_ratio = fc.sharpe_ratio( + ts, + risk_free_rate=0.06, + from_date="2018-01-02", + to_date="2021-12-31", + return_period_unit="years", + return_period_value=1, + ) + assert round(sharpe_ratio, 4) == 1.4374 + + sharpe_ratio = fc.sharpe_ratio( + ts, + risk_free_rate=0.06, + from_date="2017-07-03", + to_date="2021-12-31", + return_period_unit="months", + return_period_value=6, + ) + assert round(sharpe_ratio, 4) == 0.8401