Compare commits

..

No commits in common. "336cf41ca84b880a20738cefbe158ff93aaf7956" and "79cd44d41f4be05aaa052c18a27dd8aafcbb2ade" have entirely different histories.

4 changed files with 24 additions and 58 deletions

View File

@ -1,4 +1,3 @@
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 calculate_rolling_returns function.
Remaining options are passed on to rolling_return function.
Returns:
-------
@ -715,7 +715,6 @@ 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:
-----------
@ -752,11 +751,6 @@ 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"""

View File

@ -1,22 +0,0 @@
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,34 +1,29 @@
# from fincal.core import FincalOptions
import fincal as fc
from fincal.fincal import TimeSeries
data = [
("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),
("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),
]
ts = fc.TimeSeries(data, frequency="D", date_format="%Y-%d-%m")
ts = TimeSeries(data, frequency="D")
print(ts)
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=}")
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)