added sharpe ratio
This commit is contained in:
parent
0f002f3478
commit
336cf41ca8
@ -1,3 +1,4 @@
|
||||
from .core import *
|
||||
from .fincal import *
|
||||
from .statistics import *
|
||||
from .utils import *
|
||||
|
22
fincal/statistics.py
Normal file
22
fincal/statistics.py
Normal 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
51
test.py
@ -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=}")
|
||||
|
Loading…
Reference in New Issue
Block a user