Compare commits

...

2 Commits

Author SHA1 Message Date
336cf41ca8 added sharpe ratio 2022-04-29 07:43:06 +05:30
0f002f3478 added mean() method 2022-04-29 07:42:39 +05:30
4 changed files with 58 additions and 24 deletions

View File

@ -1,3 +1,4 @@
from .core import *
from .fincal import *
from .statistics import *
from .utils import *

View File

@ -476,7 +476,7 @@ class TimeSeries(TimeSeriesCore):
Only used when annualizing volatility for a time series with daily frequency.
If not provided, will use the value in FincalOptions.traded_days.
Remaining options are passed on to rolling_return function.
Remaining options are passed on to calculate_rolling_returns function.
Returns:
-------
@ -715,6 +715,7 @@ class TimeSeries(TimeSeriesCore):
This will ensure that both time series have the same frequency and same set of dates.
The frequency will be set to the higher of the two objects.
Dates will be taken from the class on which the method is called.
Values will be taken from the other class.
Parameters:
-----------
@ -751,6 +752,11 @@ class TimeSeries(TimeSeriesCore):
return self.__class__(new_other, frequency=other.frequency.symbol)
def mean(self) -> float:
"""Calculates the mean value of the time series data"""
return statistics.mean(self.values)
def _preprocess_csv(file_path: str | pathlib.Path, delimiter: str = ",", encoding: str = "utf-8") -> List[list]:
"""Preprocess csv data"""

22
fincal/statistics.py Normal file
View File

@ -0,0 +1,22 @@
from .fincal import TimeSeries
def sharpe_ratio(
time_series_data: TimeSeries, risk_free_data: TimeSeries = None, risk_free_rate: float = None, **kwargs
):
pass
if risk_free_data is None and risk_free_rate is None:
raise ValueError("At least one of risk_free_data or risk_free rate is required")
returns_ts = time_series_data.calculate_rolling_returns(**kwargs)
if risk_free_data is not None:
risk_free_data = returns_ts.sync(risk_free_data)
else:
risk_free_data = risk_free_rate
excess_returns = returns_ts - risk_free_data
sd = time_series_data.volatility(**kwargs)
sharpe_ratio = excess_returns.mean() / sd
return sharpe_ratio

51
test.py
View File

@ -1,29 +1,34 @@
# from fincal.core import FincalOptions
from fincal.fincal import TimeSeries
import fincal as fc
data = [
("2022-01-01", 10),
("2022-01-02", 12),
("2022-01-03", 14),
("2022-01-04", 16),
("2022-01-06", 18),
("2022-01-07", 20),
("2022-01-09", 22),
("2022-01-10", 24),
("2022-01-11", 26),
("2022-01-13", 28),
("2022-01-14", 30),
("2022-01-15", 32),
("2022-01-16", 34),
("2022-01-01", 150),
("2022-01-02", 152),
("2022-01-03", 151),
("2022-01-04", 154),
("2022-01-05", 150),
("2022-01-06", 157),
("2022-01-07", 155),
("2022-01-08", 158),
("2022-01-09", 162),
("2022-01-10", 160),
("2022-01-11", 156),
("2022-01-12", 162),
("2023-01-01", 164),
("2023-01-02", 161),
("2023-01-03", 167),
("2023-01-04", 168),
]
ts = TimeSeries(data, frequency="D")
ts = fc.TimeSeries(data, frequency="D", date_format="%Y-%d-%m")
print(ts)
data = [("2022-01-01", 220), ("2022-01-08", 230), ("2022-01-15", 240)]
ts2 = TimeSeries(data, frequency="W")
print(ts2)
synced_ts = ts.sync(ts2)
print("---------\n")
for i in synced_ts:
print(i)
sharpe = fc.sharpe_ratio(
ts,
risk_free_rate=(1 + 0.15) ** (1 / 12) - 1,
from_date="2022-02-01",
to_date="2023-04-01",
frequency="M",
return_period_unit="months",
return_period_value=1,
)
print(f"{sharpe=}")