Some tests for Sharpe ratio

Also some corrections based identified during testing
This commit is contained in:
Gourav Kumar 2022-05-08 21:04:48 +05:30
parent 68d854cb3f
commit aea6bf9b57
3 changed files with 57 additions and 3 deletions

View File

@ -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

View File

@ -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)

51
tests/test_stats.py Normal file
View File

@ -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