migrated tests from fincal to fincal2
This commit is contained in:
parent
d88acc5888
commit
6ffa52f84e
@ -112,6 +112,10 @@ class TestTimeSeriesCore:
|
|||||||
assert isinstance(ts, TimeSeriesCore)
|
assert isinstance(ts, TimeSeriesCore)
|
||||||
assert isinstance(ts, Mapping)
|
assert isinstance(ts, Mapping)
|
||||||
|
|
||||||
|
|
||||||
|
class TestSlicing:
|
||||||
|
data = [("2021-01-01", 220), ("2021-02-01", 230), ("2021-03-01", 240)]
|
||||||
|
|
||||||
def test_getitem(self):
|
def test_getitem(self):
|
||||||
ts = TimeSeriesCore(self.data, frequency="M")
|
ts = TimeSeriesCore(self.data, frequency="M")
|
||||||
assert ts.dates[0] == datetime.datetime(2021, 1, 1, 0, 0)
|
assert ts.dates[0] == datetime.datetime(2021, 1, 1, 0, 0)
|
||||||
@ -165,6 +169,15 @@ class TestTimeSeriesCore:
|
|||||||
assert dates[0] == datetime.datetime(2021, 1, 1, 0, 0)
|
assert dates[0] == datetime.datetime(2021, 1, 1, 0, 0)
|
||||||
assert values[0] == 220
|
assert values[0] == 220
|
||||||
|
|
||||||
|
def test_iloc_slicing(self):
|
||||||
|
ts = TimeSeriesCore(self.data, frequency="M")
|
||||||
|
assert ts.iloc[0] == (datetime.datetime(2021, 1, 1), 220)
|
||||||
|
assert ts.iloc[-1] == (datetime.datetime(2021, 3, 1), 240)
|
||||||
|
|
||||||
|
ts_slice = ts.iloc[0:2]
|
||||||
|
assert isinstance(ts_slice, TimeSeriesCore)
|
||||||
|
assert len(ts_slice) == 2
|
||||||
|
|
||||||
|
|
||||||
class TestTimeSeriesCoreHeadTail:
|
class TestTimeSeriesCoreHeadTail:
|
||||||
data = [
|
data = [
|
||||||
|
@ -1,310 +0,0 @@
|
|||||||
import datetime
|
|
||||||
import os
|
|
||||||
import random
|
|
||||||
from typing import Literal, Sequence
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
from fincal.core import Frequency, Series
|
|
||||||
from fincal.exceptions import DateNotFoundError
|
|
||||||
from fincal.fincal import TimeSeries, create_date_series
|
|
||||||
from fincal.utils import FincalOptions
|
|
||||||
|
|
||||||
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
||||||
sample_data_path = os.path.join(THIS_DIR, "data")
|
|
||||||
|
|
||||||
|
|
||||||
def create_random_test_data(
|
|
||||||
frequency: str,
|
|
||||||
eomonth: bool,
|
|
||||||
n: int,
|
|
||||||
gaps: float,
|
|
||||||
month_position: Literal["start", "middle", "end"],
|
|
||||||
date_as_str: bool,
|
|
||||||
as_outer_type: Literal["dict", "list"] = "list",
|
|
||||||
as_inner_type: Literal["dict", "list", "tuple"] = "tuple",
|
|
||||||
) -> Sequence[tuple]:
|
|
||||||
start_dates = {
|
|
||||||
"start": datetime.datetime(2016, 1, 1),
|
|
||||||
"middle": datetime.datetime(2016, 1, 15),
|
|
||||||
"end": datetime.datetime(2016, 1, 31),
|
|
||||||
}
|
|
||||||
end_date = datetime.datetime(2021, 12, 31)
|
|
||||||
dates = create_date_series(start_dates[month_position], end_date, frequency=frequency, eomonth=eomonth)
|
|
||||||
dates = dates[:n]
|
|
||||||
if gaps:
|
|
||||||
num_gaps = int(len(dates) * gaps)
|
|
||||||
to_remove = random.sample(dates, num_gaps)
|
|
||||||
for i in to_remove:
|
|
||||||
dates.remove(i)
|
|
||||||
if date_as_str:
|
|
||||||
dates = [i.strftime("%Y-%m-%d") for i in dates]
|
|
||||||
|
|
||||||
values = [random.randint(8000, 90000) / 100 for _ in dates]
|
|
||||||
|
|
||||||
data = list(zip(dates, values))
|
|
||||||
if as_outer_type == "list":
|
|
||||||
if as_inner_type == "list":
|
|
||||||
data = [list(i) for i in data]
|
|
||||||
elif as_inner_type == "dict[1]":
|
|
||||||
data = [dict((i,)) for i in data]
|
|
||||||
elif as_inner_type == "dict[2]":
|
|
||||||
data = [dict(date=i, value=j) for i, j in data]
|
|
||||||
elif as_outer_type == "dict":
|
|
||||||
data = dict(data)
|
|
||||||
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
def create_organised_test_data() -> dict:
|
|
||||||
"""Creates organised test data so that output is exactly same in each run"""
|
|
||||||
|
|
||||||
all_dates, all_values = [], []
|
|
||||||
prev_date, prev_number = datetime.datetime(2018, 1, 1), 1000
|
|
||||||
|
|
||||||
for i in range(1, 1000):
|
|
||||||
if i % 5 == 0:
|
|
||||||
prev_date += datetime.timedelta(days=3)
|
|
||||||
else:
|
|
||||||
prev_date += datetime.timedelta(days=1)
|
|
||||||
all_dates.append(prev_date)
|
|
||||||
|
|
||||||
for i in range(1, 1000):
|
|
||||||
rem = i % 7
|
|
||||||
if rem % 2:
|
|
||||||
prev_number -= rem
|
|
||||||
else:
|
|
||||||
prev_number += rem
|
|
||||||
all_values.append(prev_number)
|
|
||||||
|
|
||||||
return dict(zip(all_dates, all_values))
|
|
||||||
|
|
||||||
|
|
||||||
class TestDateSeries:
|
|
||||||
def test_daily(self):
|
|
||||||
start_date = datetime.datetime(2020, 1, 1)
|
|
||||||
end_date = datetime.datetime(2020, 12, 31)
|
|
||||||
d = create_date_series(start_date, end_date, frequency="D")
|
|
||||||
assert len(d) == 366
|
|
||||||
|
|
||||||
start_date = datetime.datetime(2017, 1, 1)
|
|
||||||
end_date = datetime.datetime(2017, 12, 31)
|
|
||||||
d = create_date_series(start_date, end_date, frequency="D")
|
|
||||||
assert len(d) == 365
|
|
||||||
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
create_date_series(start_date, end_date, frequency="D", eomonth=True)
|
|
||||||
|
|
||||||
def test_monthly(self):
|
|
||||||
start_date = datetime.datetime(2020, 1, 1)
|
|
||||||
end_date = datetime.datetime(2020, 12, 31)
|
|
||||||
d = create_date_series(start_date, end_date, frequency="M")
|
|
||||||
assert len(d) == 12
|
|
||||||
|
|
||||||
d = create_date_series(start_date, end_date, frequency="M", eomonth=True)
|
|
||||||
assert datetime.datetime(2020, 2, 29) in d
|
|
||||||
|
|
||||||
start_date = datetime.datetime(2020, 1, 31)
|
|
||||||
d = create_date_series(start_date, end_date, frequency="M")
|
|
||||||
assert datetime.datetime(2020, 2, 29) in d
|
|
||||||
assert datetime.datetime(2020, 8, 31) in d
|
|
||||||
assert datetime.datetime(2020, 10, 30) not in d
|
|
||||||
|
|
||||||
start_date = datetime.datetime(2020, 2, 29)
|
|
||||||
d = create_date_series(start_date, end_date, frequency="M")
|
|
||||||
assert len(d) == 11
|
|
||||||
assert datetime.datetime(2020, 2, 29) in d
|
|
||||||
assert datetime.datetime(2020, 8, 31) not in d
|
|
||||||
assert datetime.datetime(2020, 10, 29) in d
|
|
||||||
|
|
||||||
def test_quarterly(self):
|
|
||||||
start_date = datetime.datetime(2018, 1, 1)
|
|
||||||
end_date = datetime.datetime(2020, 12, 31)
|
|
||||||
d = create_date_series(start_date, end_date, frequency="Q")
|
|
||||||
assert len(d) == 12
|
|
||||||
|
|
||||||
d = create_date_series(start_date, end_date, frequency="Q", eomonth=True)
|
|
||||||
assert datetime.datetime(2020, 4, 30) in d
|
|
||||||
|
|
||||||
start_date = datetime.datetime(2020, 1, 31)
|
|
||||||
d = create_date_series(start_date, end_date, frequency="Q")
|
|
||||||
assert len(d) == 4
|
|
||||||
assert datetime.datetime(2020, 2, 29) not in d
|
|
||||||
assert max(d) == datetime.datetime(2020, 10, 31)
|
|
||||||
|
|
||||||
start_date = datetime.datetime(2020, 2, 29)
|
|
||||||
d = create_date_series(start_date, end_date, frequency="Q")
|
|
||||||
assert datetime.datetime(2020, 2, 29) in d
|
|
||||||
assert datetime.datetime(2020, 8, 31) not in d
|
|
||||||
assert datetime.datetime(2020, 11, 29) in d
|
|
||||||
|
|
||||||
d = create_date_series(start_date, end_date, frequency="Q", eomonth=True)
|
|
||||||
assert datetime.datetime(2020, 11, 30) in d
|
|
||||||
|
|
||||||
|
|
||||||
class TestFincalBasic:
|
|
||||||
def test_creation(self):
|
|
||||||
data = create_random_test_data(
|
|
||||||
frequency="D", eomonth=False, n=50, gaps=0, month_position="start", date_as_str=True
|
|
||||||
)
|
|
||||||
time_series = TimeSeries(data, frequency="D")
|
|
||||||
assert len(time_series) == 50
|
|
||||||
assert isinstance(time_series.frequency, Frequency)
|
|
||||||
assert time_series.frequency.days == 1
|
|
||||||
|
|
||||||
ffill_data = time_series.ffill()
|
|
||||||
assert len(ffill_data) == 50
|
|
||||||
|
|
||||||
data = create_random_test_data(
|
|
||||||
frequency="D", eomonth=False, n=500, gaps=0.1, month_position="start", date_as_str=True
|
|
||||||
)
|
|
||||||
time_series = TimeSeries(data, frequency="D")
|
|
||||||
assert len(time_series) == 450
|
|
||||||
|
|
||||||
def test_fill(self):
|
|
||||||
data = create_random_test_data(
|
|
||||||
frequency="D", eomonth=False, n=500, gaps=0.1, month_position="start", date_as_str=True
|
|
||||||
)
|
|
||||||
time_series = TimeSeries(data, frequency="D")
|
|
||||||
ffill_data = time_series.ffill()
|
|
||||||
assert len(ffill_data) >= 498
|
|
||||||
|
|
||||||
ffill_data = time_series.ffill(inplace=True)
|
|
||||||
assert ffill_data is None
|
|
||||||
assert len(time_series) >= 498
|
|
||||||
|
|
||||||
data = create_random_test_data(
|
|
||||||
frequency="D", eomonth=False, n=500, gaps=0.1, month_position="start", date_as_str=True
|
|
||||||
)
|
|
||||||
time_series = TimeSeries(data, frequency="D")
|
|
||||||
bfill_data = time_series.bfill()
|
|
||||||
assert len(bfill_data) >= 498
|
|
||||||
|
|
||||||
bfill_data = time_series.bfill(inplace=True)
|
|
||||||
assert bfill_data is None
|
|
||||||
assert len(time_series) >= 498
|
|
||||||
|
|
||||||
data = [("2021-01-01", 220), ("2021-01-02", 230), ("2021-03-04", 240)]
|
|
||||||
ts = TimeSeries(data, frequency="D")
|
|
||||||
ff = ts.ffill()
|
|
||||||
assert ff["2021-01-03"][1] == 230
|
|
||||||
|
|
||||||
bf = ts.bfill()
|
|
||||||
assert bf["2021-01-03"][1] == 240
|
|
||||||
|
|
||||||
def test_iloc_slicing(self):
|
|
||||||
data = create_random_test_data(
|
|
||||||
frequency="D", eomonth=False, n=50, gaps=0, month_position="start", date_as_str=True
|
|
||||||
)
|
|
||||||
time_series = TimeSeries(data, frequency="D")
|
|
||||||
assert time_series.iloc[0] is not None
|
|
||||||
assert time_series.iloc[:3] is not None
|
|
||||||
assert time_series.iloc[5:7] is not None
|
|
||||||
assert isinstance(time_series.iloc[0], tuple)
|
|
||||||
assert isinstance(time_series.iloc[10:20], TimeSeries)
|
|
||||||
assert len(time_series.iloc[10:20]) == 10
|
|
||||||
|
|
||||||
def test_key_slicing(self):
|
|
||||||
data = create_random_test_data(
|
|
||||||
frequency="D", eomonth=False, n=50, gaps=0, month_position="start", date_as_str=True
|
|
||||||
)
|
|
||||||
time_series = TimeSeries(data, frequency="D")
|
|
||||||
available_date = time_series.iloc[5][0]
|
|
||||||
assert time_series[available_date] is not None
|
|
||||||
assert isinstance(time_series["dates"], Series)
|
|
||||||
assert isinstance(time_series["values"], Series)
|
|
||||||
assert len(time_series.dates) == 50
|
|
||||||
assert len(time_series.values) == 50
|
|
||||||
|
|
||||||
|
|
||||||
class TestReturns:
|
|
||||||
data = [
|
|
||||||
("2020-01-01", 10),
|
|
||||||
("2020-02-01", 12),
|
|
||||||
("2020-03-01", 14),
|
|
||||||
("2020-04-01", 16),
|
|
||||||
("2020-05-01", 18),
|
|
||||||
("2020-06-01", 20),
|
|
||||||
("2020-07-01", 22),
|
|
||||||
("2020-08-01", 24),
|
|
||||||
("2020-09-01", 26),
|
|
||||||
("2020-10-01", 28),
|
|
||||||
("2020-11-01", 30),
|
|
||||||
("2020-12-01", 32),
|
|
||||||
("2021-01-01", 34),
|
|
||||||
]
|
|
||||||
|
|
||||||
def test_returns_calc(self):
|
|
||||||
ts = TimeSeries(self.data, frequency="M")
|
|
||||||
returns = ts.calculate_returns(
|
|
||||||
"2021-01-01", annual_compounded_returns=False, return_period_unit="years", return_period_value=1
|
|
||||||
)
|
|
||||||
assert returns[1] == 2.4
|
|
||||||
returns = ts.calculate_returns(
|
|
||||||
"2020-04-01", annual_compounded_returns=False, return_period_unit="months", return_period_value=3
|
|
||||||
)
|
|
||||||
assert round(returns[1], 4) == 0.6
|
|
||||||
returns = ts.calculate_returns(
|
|
||||||
"2020-04-01", annual_compounded_returns=True, return_period_unit="months", return_period_value=3
|
|
||||||
)
|
|
||||||
assert round(returns[1], 4) == 5.5536
|
|
||||||
returns = ts.calculate_returns(
|
|
||||||
"2020-04-01", annual_compounded_returns=False, return_period_unit="days", return_period_value=90
|
|
||||||
)
|
|
||||||
assert round(returns[1], 4) == 0.6
|
|
||||||
returns = ts.calculate_returns(
|
|
||||||
"2020-04-01", annual_compounded_returns=True, return_period_unit="days", return_period_value=90
|
|
||||||
)
|
|
||||||
assert round(returns[1], 4) == 5.727
|
|
||||||
returns = ts.calculate_returns(
|
|
||||||
"2020-04-10", annual_compounded_returns=True, return_period_unit="days", return_period_value=90
|
|
||||||
)
|
|
||||||
assert round(returns[1], 4) == 5.727
|
|
||||||
with pytest.raises(DateNotFoundError):
|
|
||||||
ts.calculate_returns("2020-04-10", return_period_unit="days", return_period_value=90, as_on_match="exact")
|
|
||||||
with pytest.raises(DateNotFoundError):
|
|
||||||
ts.calculate_returns("2020-04-10", return_period_unit="days", return_period_value=90, prior_match="exact")
|
|
||||||
|
|
||||||
def test_date_formats(self):
|
|
||||||
ts = TimeSeries(self.data, frequency="M")
|
|
||||||
FincalOptions.date_format = "%d-%m-%Y"
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
ts.calculate_returns(
|
|
||||||
"2020-04-10", annual_compounded_returns=True, return_period_unit="days", return_period_value=90
|
|
||||||
)
|
|
||||||
|
|
||||||
returns1 = ts.calculate_returns(
|
|
||||||
"2020-04-10", return_period_unit="days", return_period_value=90, date_format="%Y-%m-%d"
|
|
||||||
)
|
|
||||||
returns2 = ts.calculate_returns("10-04-2020", return_period_unit="days", return_period_value=90)
|
|
||||||
assert round(returns1[1], 4) == round(returns2[1], 4) == 5.727
|
|
||||||
|
|
||||||
FincalOptions.date_format = "%m-%d-%Y"
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
ts.calculate_returns(
|
|
||||||
"2020-04-10", annual_compounded_returns=True, return_period_unit="days", return_period_value=90
|
|
||||||
)
|
|
||||||
|
|
||||||
returns1 = ts.calculate_returns(
|
|
||||||
"2020-04-10", return_period_unit="days", return_period_value=90, date_format="%Y-%m-%d"
|
|
||||||
)
|
|
||||||
returns2 = ts.calculate_returns("04-10-2020", return_period_unit="days", return_period_value=90)
|
|
||||||
assert round(returns1[1], 4) == round(returns2[1], 4) == 5.727
|
|
||||||
|
|
||||||
def test_limits(self):
|
|
||||||
ts = TimeSeries(self.data, frequency="M")
|
|
||||||
FincalOptions.date_format = "%Y-%m-%d"
|
|
||||||
with pytest.raises(DateNotFoundError):
|
|
||||||
ts.calculate_returns("2020-04-25", return_period_unit="days", return_period_value=90, closest_max_days=10)
|
|
||||||
|
|
||||||
|
|
||||||
class TestVolatility:
|
|
||||||
data = create_organised_test_data()
|
|
||||||
|
|
||||||
def test_volatility_basic(self):
|
|
||||||
ts = TimeSeries(self.data, frequency="D")
|
|
||||||
sd = ts.volatility()
|
|
||||||
assert len(ts) == 999
|
|
||||||
assert round(sd, 6) == 0.057391
|
|
||||||
|
|
||||||
sd = ts.volatility(annualize_volatility=False)
|
|
||||||
assert round(sd, 6) == 0.003004
|
|
@ -1,13 +1,13 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import math
|
import math
|
||||||
import random
|
import random
|
||||||
from unittest import skip
|
from typing import List
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from dateutil.relativedelta import relativedelta
|
from dateutil.relativedelta import relativedelta
|
||||||
from fincal.core import AllFrequencies, Frequency
|
from fincal.core import AllFrequencies, Frequency
|
||||||
from fincal.exceptions import DateNotFoundError
|
from fincal.exceptions import DateNotFoundError
|
||||||
from fincal.fincal import MaxDrawdown, TimeSeries, create_date_series
|
from fincal.fincal import TimeSeries, create_date_series
|
||||||
from fincal.utils import FincalOptions
|
from fincal.utils import FincalOptions
|
||||||
|
|
||||||
|
|
||||||
@ -48,9 +48,14 @@ def create_prices(s0: float, mu: float, sigma: float, num_prices: int) -> list:
|
|||||||
return all_values
|
return all_values
|
||||||
|
|
||||||
|
|
||||||
def create_test_timeseries(
|
def create_test_data(
|
||||||
frequency: Frequency, num: int = 1000, skip_weekends: bool = False, mu: float = 0.1, sigma: float = 0.05
|
frequency: Frequency,
|
||||||
) -> TimeSeries:
|
num: int = 1000,
|
||||||
|
skip_weekends: bool = False,
|
||||||
|
mu: float = 0.1,
|
||||||
|
sigma: float = 0.05,
|
||||||
|
eomonth: bool = False,
|
||||||
|
) -> List[tuple]:
|
||||||
"""Creates TimeSeries data
|
"""Creates TimeSeries data
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
@ -83,15 +88,152 @@ def create_test_timeseries(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
end_date = start_date + relativedelta(**timedelta_dict)
|
end_date = start_date + relativedelta(**timedelta_dict)
|
||||||
dates = create_date_series(start_date, end_date, frequency.symbol, skip_weekends=skip_weekends)
|
dates = create_date_series(start_date, end_date, frequency.symbol, skip_weekends=skip_weekends, eomonth=eomonth)
|
||||||
values = create_prices(1000, mu, sigma, num)
|
values = create_prices(1000, mu, sigma, num)
|
||||||
ts = TimeSeries(dict(zip(dates, values)), frequency=frequency.symbol)
|
ts = list(zip(dates, values))
|
||||||
return ts
|
return ts
|
||||||
|
|
||||||
|
|
||||||
|
class TestDateSeries:
|
||||||
|
def test_daily(self):
|
||||||
|
start_date = datetime.datetime(2020, 1, 1)
|
||||||
|
end_date = datetime.datetime(2020, 12, 31)
|
||||||
|
d = create_date_series(start_date, end_date, frequency="D")
|
||||||
|
assert len(d) == 366
|
||||||
|
|
||||||
|
start_date = datetime.datetime(2017, 1, 1)
|
||||||
|
end_date = datetime.datetime(2017, 12, 31)
|
||||||
|
d = create_date_series(start_date, end_date, frequency="D")
|
||||||
|
assert len(d) == 365
|
||||||
|
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
create_date_series(start_date, end_date, frequency="D", eomonth=True)
|
||||||
|
|
||||||
|
def test_monthly(self):
|
||||||
|
start_date = datetime.datetime(2020, 1, 1)
|
||||||
|
end_date = datetime.datetime(2020, 12, 31)
|
||||||
|
d = create_date_series(start_date, end_date, frequency="M")
|
||||||
|
assert len(d) == 12
|
||||||
|
|
||||||
|
d = create_date_series(start_date, end_date, frequency="M", eomonth=True)
|
||||||
|
assert datetime.datetime(2020, 2, 29) in d
|
||||||
|
|
||||||
|
start_date = datetime.datetime(2020, 1, 31)
|
||||||
|
d = create_date_series(start_date, end_date, frequency="M")
|
||||||
|
assert datetime.datetime(2020, 2, 29) in d
|
||||||
|
assert datetime.datetime(2020, 8, 31) in d
|
||||||
|
assert datetime.datetime(2020, 10, 30) not in d
|
||||||
|
|
||||||
|
start_date = datetime.datetime(2020, 2, 29)
|
||||||
|
d = create_date_series(start_date, end_date, frequency="M")
|
||||||
|
assert len(d) == 11
|
||||||
|
assert datetime.datetime(2020, 2, 29) in d
|
||||||
|
assert datetime.datetime(2020, 8, 31) not in d
|
||||||
|
assert datetime.datetime(2020, 10, 29) in d
|
||||||
|
|
||||||
|
def test_quarterly(self):
|
||||||
|
start_date = datetime.datetime(2018, 1, 1)
|
||||||
|
end_date = datetime.datetime(2020, 12, 31)
|
||||||
|
d = create_date_series(start_date, end_date, frequency="Q")
|
||||||
|
assert len(d) == 12
|
||||||
|
|
||||||
|
d = create_date_series(start_date, end_date, frequency="Q", eomonth=True)
|
||||||
|
assert datetime.datetime(2020, 4, 30) in d
|
||||||
|
|
||||||
|
start_date = datetime.datetime(2020, 1, 31)
|
||||||
|
d = create_date_series(start_date, end_date, frequency="Q")
|
||||||
|
assert len(d) == 4
|
||||||
|
assert datetime.datetime(2020, 2, 29) not in d
|
||||||
|
assert max(d) == datetime.datetime(2020, 10, 31)
|
||||||
|
|
||||||
|
start_date = datetime.datetime(2020, 2, 29)
|
||||||
|
d = create_date_series(start_date, end_date, frequency="Q")
|
||||||
|
assert datetime.datetime(2020, 2, 29) in d
|
||||||
|
assert datetime.datetime(2020, 8, 31) not in d
|
||||||
|
assert datetime.datetime(2020, 11, 29) in d
|
||||||
|
|
||||||
|
d = create_date_series(start_date, end_date, frequency="Q", eomonth=True)
|
||||||
|
assert datetime.datetime(2020, 11, 30) in d
|
||||||
|
|
||||||
|
|
||||||
|
class TestTimeSeriesCreation:
|
||||||
|
def test_creation_with_list_of_tuples(self):
|
||||||
|
ts_data = create_test_data(frequency=AllFrequencies.D, num=50)
|
||||||
|
ts = TimeSeries(ts_data, frequency="D")
|
||||||
|
assert len(ts) == 50
|
||||||
|
assert isinstance(ts.frequency, Frequency)
|
||||||
|
assert ts.frequency.days == 1
|
||||||
|
|
||||||
|
def test_creation_with_string_dates(self):
|
||||||
|
ts_data = create_test_data(frequency=AllFrequencies.D, num=50)
|
||||||
|
ts_data1 = [(dt.strftime("%Y-%m-%d"), val) for dt, val in ts_data]
|
||||||
|
ts = TimeSeries(ts_data1, frequency="D")
|
||||||
|
datetime.datetime(2017, 1, 1) in ts
|
||||||
|
|
||||||
|
ts_data1 = [(dt.strftime("%d-%m-%Y"), val) for dt, val in ts_data]
|
||||||
|
ts = TimeSeries(ts_data1, frequency="D", date_format="%d-%m-%Y")
|
||||||
|
datetime.datetime(2017, 1, 1) in ts
|
||||||
|
|
||||||
|
ts_data1 = [(dt.strftime("%m-%d-%Y"), val) for dt, val in ts_data]
|
||||||
|
ts = TimeSeries(ts_data1, frequency="D", date_format="%m-%d-%Y")
|
||||||
|
datetime.datetime(2017, 1, 1) in ts
|
||||||
|
|
||||||
|
ts_data1 = [(dt.strftime("%m-%d-%Y %H:%M"), val) for dt, val in ts_data]
|
||||||
|
ts = TimeSeries(ts_data1, frequency="D", date_format="%m-%d-%Y %H:%M")
|
||||||
|
datetime.datetime(2017, 1, 1, 0, 0) in ts
|
||||||
|
|
||||||
|
def test_creation_with_list_of_dicts(self):
|
||||||
|
ts_data = create_test_data(frequency=AllFrequencies.D, num=50)
|
||||||
|
ts_data1 = [{"date": dt.strftime("%Y-%m-%d"), "value": val} for dt, val in ts_data]
|
||||||
|
ts = TimeSeries(ts_data1, frequency="D")
|
||||||
|
datetime.datetime(2017, 1, 1) in ts
|
||||||
|
|
||||||
|
def test_creation_with_list_of_lists(self):
|
||||||
|
ts_data = create_test_data(frequency=AllFrequencies.D, num=50)
|
||||||
|
ts_data1 = [[dt.strftime("%Y-%m-%d"), val] for dt, val in ts_data]
|
||||||
|
ts = TimeSeries(ts_data1, frequency="D")
|
||||||
|
datetime.datetime(2017, 1, 1) in ts
|
||||||
|
|
||||||
|
def test_creation_with_dict(self):
|
||||||
|
ts_data = create_test_data(frequency=AllFrequencies.D, num=50)
|
||||||
|
ts_data1 = [{dt.strftime("%Y-%m-%d"): val} for dt, val in ts_data]
|
||||||
|
ts = TimeSeries(ts_data1, frequency="D")
|
||||||
|
datetime.datetime(2017, 1, 1) in ts
|
||||||
|
|
||||||
|
|
||||||
|
class TestTimeSeriesBasics:
|
||||||
|
def test_fill(self):
|
||||||
|
ts_data = create_test_data(frequency=AllFrequencies.D, num=50, skip_weekends=True)
|
||||||
|
ts = TimeSeries(ts_data, frequency="D")
|
||||||
|
ffill_data = ts.ffill()
|
||||||
|
assert len(ffill_data) == 68
|
||||||
|
|
||||||
|
ffill_data = ts.ffill(inplace=True)
|
||||||
|
assert ffill_data is None
|
||||||
|
assert len(ts) == 68
|
||||||
|
|
||||||
|
ts_data = create_test_data(frequency=AllFrequencies.D, num=50, skip_weekends=True)
|
||||||
|
ts = TimeSeries(ts_data, frequency="D")
|
||||||
|
bfill_data = ts.bfill()
|
||||||
|
assert len(bfill_data) == 68
|
||||||
|
|
||||||
|
bfill_data = ts.bfill(inplace=True)
|
||||||
|
assert bfill_data is None
|
||||||
|
assert len(ts) == 68
|
||||||
|
|
||||||
|
data = [("2021-01-01", 220), ("2021-01-02", 230), ("2021-03-04", 240)]
|
||||||
|
ts = TimeSeries(data, frequency="D")
|
||||||
|
ff = ts.ffill()
|
||||||
|
assert ff["2021-01-03"][1] == 230
|
||||||
|
|
||||||
|
bf = ts.bfill()
|
||||||
|
assert bf["2021-01-03"][1] == 240
|
||||||
|
|
||||||
|
|
||||||
class TestReturns:
|
class TestReturns:
|
||||||
def test_returns_calc(self):
|
def test_returns_calc(self):
|
||||||
ts = create_test_timeseries(AllFrequencies.D, skip_weekends=True)
|
ts_data = create_test_data(AllFrequencies.D, skip_weekends=True)
|
||||||
|
ts = TimeSeries(ts_data, "D")
|
||||||
returns = ts.calculate_returns(
|
returns = ts.calculate_returns(
|
||||||
"2020-01-01", annual_compounded_returns=False, return_period_unit="years", return_period_value=1
|
"2020-01-01", annual_compounded_returns=False, return_period_unit="years", return_period_value=1
|
||||||
)
|
)
|
||||||
@ -123,7 +265,8 @@ class TestReturns:
|
|||||||
ts.calculate_returns("2020-04-04", return_period_unit="months", return_period_value=3, prior_match="exact")
|
ts.calculate_returns("2020-04-04", return_period_unit="months", return_period_value=3, prior_match="exact")
|
||||||
|
|
||||||
def test_date_formats(self):
|
def test_date_formats(self):
|
||||||
ts = create_test_timeseries(AllFrequencies.D, skip_weekends=True)
|
ts_data = create_test_data(AllFrequencies.D, skip_weekends=True)
|
||||||
|
ts = TimeSeries(ts_data, "D")
|
||||||
FincalOptions.date_format = "%d-%m-%Y"
|
FincalOptions.date_format = "%d-%m-%Y"
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
ts.calculate_returns(
|
ts.calculate_returns(
|
||||||
@ -150,14 +293,97 @@ class TestReturns:
|
|||||||
|
|
||||||
def test_limits(self):
|
def test_limits(self):
|
||||||
FincalOptions.date_format = "%Y-%m-%d"
|
FincalOptions.date_format = "%Y-%m-%d"
|
||||||
ts = create_test_timeseries(AllFrequencies.D)
|
ts_data = create_test_data(AllFrequencies.D)
|
||||||
|
ts = TimeSeries(ts_data, "D")
|
||||||
with pytest.raises(DateNotFoundError):
|
with pytest.raises(DateNotFoundError):
|
||||||
ts.calculate_returns("2020-11-25", return_period_unit="days", return_period_value=90, closest_max_days=10)
|
ts.calculate_returns("2020-11-25", return_period_unit="days", return_period_value=90, closest_max_days=10)
|
||||||
|
|
||||||
|
|
||||||
|
class TestReturnsAgain:
|
||||||
|
data = [
|
||||||
|
("2020-01-01", 10),
|
||||||
|
("2020-02-01", 12),
|
||||||
|
("2020-03-01", 14),
|
||||||
|
("2020-04-01", 16),
|
||||||
|
("2020-05-01", 18),
|
||||||
|
("2020-06-01", 20),
|
||||||
|
("2020-07-01", 22),
|
||||||
|
("2020-08-01", 24),
|
||||||
|
("2020-09-01", 26),
|
||||||
|
("2020-10-01", 28),
|
||||||
|
("2020-11-01", 30),
|
||||||
|
("2020-12-01", 32),
|
||||||
|
("2021-01-01", 34),
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_returns_calc(self):
|
||||||
|
ts = TimeSeries(self.data, frequency="M")
|
||||||
|
returns = ts.calculate_returns(
|
||||||
|
"2021-01-01", annual_compounded_returns=False, return_period_unit="years", return_period_value=1
|
||||||
|
)
|
||||||
|
assert returns[1] == 2.4
|
||||||
|
returns = ts.calculate_returns(
|
||||||
|
"2020-04-01", annual_compounded_returns=False, return_period_unit="months", return_period_value=3
|
||||||
|
)
|
||||||
|
assert round(returns[1], 4) == 0.6
|
||||||
|
returns = ts.calculate_returns(
|
||||||
|
"2020-04-01", annual_compounded_returns=True, return_period_unit="months", return_period_value=3
|
||||||
|
)
|
||||||
|
assert round(returns[1], 4) == 5.5536
|
||||||
|
returns = ts.calculate_returns(
|
||||||
|
"2020-04-01", annual_compounded_returns=False, return_period_unit="days", return_period_value=90
|
||||||
|
)
|
||||||
|
assert round(returns[1], 4) == 0.6
|
||||||
|
returns = ts.calculate_returns(
|
||||||
|
"2020-04-01", annual_compounded_returns=True, return_period_unit="days", return_period_value=90
|
||||||
|
)
|
||||||
|
assert round(returns[1], 4) == 5.727
|
||||||
|
returns = ts.calculate_returns(
|
||||||
|
"2020-04-10", annual_compounded_returns=True, return_period_unit="days", return_period_value=90
|
||||||
|
)
|
||||||
|
assert round(returns[1], 4) == 5.727
|
||||||
|
with pytest.raises(DateNotFoundError):
|
||||||
|
ts.calculate_returns("2020-04-10", return_period_unit="days", return_period_value=90, as_on_match="exact")
|
||||||
|
with pytest.raises(DateNotFoundError):
|
||||||
|
ts.calculate_returns("2020-04-10", return_period_unit="days", return_period_value=90, prior_match="exact")
|
||||||
|
|
||||||
|
def test_date_formats(self):
|
||||||
|
ts = TimeSeries(self.data, frequency="M")
|
||||||
|
FincalOptions.date_format = "%d-%m-%Y"
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
ts.calculate_returns(
|
||||||
|
"2020-04-10", annual_compounded_returns=True, return_period_unit="days", return_period_value=90
|
||||||
|
)
|
||||||
|
|
||||||
|
returns1 = ts.calculate_returns(
|
||||||
|
"2020-04-10", return_period_unit="days", return_period_value=90, date_format="%Y-%m-%d"
|
||||||
|
)
|
||||||
|
returns2 = ts.calculate_returns("10-04-2020", return_period_unit="days", return_period_value=90)
|
||||||
|
assert round(returns1[1], 4) == round(returns2[1], 4) == 5.727
|
||||||
|
|
||||||
|
FincalOptions.date_format = "%m-%d-%Y"
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
ts.calculate_returns(
|
||||||
|
"2020-04-10", annual_compounded_returns=True, return_period_unit="days", return_period_value=90
|
||||||
|
)
|
||||||
|
|
||||||
|
returns1 = ts.calculate_returns(
|
||||||
|
"2020-04-10", return_period_unit="days", return_period_value=90, date_format="%Y-%m-%d"
|
||||||
|
)
|
||||||
|
returns2 = ts.calculate_returns("04-10-2020", return_period_unit="days", return_period_value=90)
|
||||||
|
assert round(returns1[1], 4) == round(returns2[1], 4) == 5.727
|
||||||
|
|
||||||
|
def test_limits(self):
|
||||||
|
ts = TimeSeries(self.data, frequency="M")
|
||||||
|
FincalOptions.date_format = "%Y-%m-%d"
|
||||||
|
with pytest.raises(DateNotFoundError):
|
||||||
|
ts.calculate_returns("2020-04-25", return_period_unit="days", return_period_value=90, closest_max_days=10)
|
||||||
|
|
||||||
|
|
||||||
class TestVolatility:
|
class TestVolatility:
|
||||||
def test_daily_ts(self):
|
def test_daily_ts(self):
|
||||||
ts = create_test_timeseries(AllFrequencies.D)
|
ts_data = create_test_data(AllFrequencies.D)
|
||||||
|
ts = TimeSeries(ts_data, "D")
|
||||||
assert len(ts) == 1000
|
assert len(ts) == 1000
|
||||||
sd = ts.volatility(annualize_volatility=False)
|
sd = ts.volatility(annualize_volatility=False)
|
||||||
assert round(sd, 6) == 0.002622
|
assert round(sd, 6) == 0.002622
|
||||||
@ -184,7 +410,8 @@ class TestVolatility:
|
|||||||
|
|
||||||
class TestDrawdown:
|
class TestDrawdown:
|
||||||
def test_daily_ts(self):
|
def test_daily_ts(self):
|
||||||
ts = create_test_timeseries(AllFrequencies.D, skip_weekends=True)
|
ts_data = create_test_data(AllFrequencies.D, skip_weekends=True)
|
||||||
|
ts = TimeSeries(ts_data, "D")
|
||||||
mdd = ts.max_drawdown()
|
mdd = ts.max_drawdown()
|
||||||
assert isinstance(mdd, dict)
|
assert isinstance(mdd, dict)
|
||||||
assert len(mdd) == 3
|
assert len(mdd) == 3
|
||||||
@ -197,7 +424,8 @@ class TestDrawdown:
|
|||||||
assert mdd == expeced_response
|
assert mdd == expeced_response
|
||||||
|
|
||||||
def test_weekly_ts(self):
|
def test_weekly_ts(self):
|
||||||
ts = create_test_timeseries(AllFrequencies.W, mu=1, sigma=0.5)
|
ts_data = create_test_data(AllFrequencies.W, mu=1, sigma=0.5)
|
||||||
|
ts = TimeSeries(ts_data, "W")
|
||||||
mdd = ts.max_drawdown()
|
mdd = ts.max_drawdown()
|
||||||
assert isinstance(mdd, dict)
|
assert isinstance(mdd, dict)
|
||||||
assert len(mdd) == 3
|
assert len(mdd) == 3
|
||||||
|
Loading…
Reference in New Issue
Block a user