Skip to content

Commit eb809e0

Browse files
committed
feat: add first_date & last_date to Asset
1 parent 389b9cd commit eb809e0

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

okama/asset.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Optional, Union
22

33
import pandas as pd
44
import numpy as np
@@ -15,15 +15,39 @@ class Asset:
1515
----------
1616
symbol: str, default 'SPY.US'
1717
Symbol is an asset ticker with a namespace after dot. The default value is 'SPY.US' (SPDR S&P 500 ETF Trust).
18+
19+
first_date : str, pd.Timestamp, None, default None
20+
First date of the rate of return time series.
21+
If None, the first available date will be used.
22+
23+
last_date : str, pd.Timestamp, None, default None
24+
Last date of the rate of return time series.
25+
If None, the last available date will be used.
1826
"""
1927

20-
def __init__(self, symbol: str = settings.default_ticker):
28+
def __init__(
29+
self,
30+
symbol: str = settings.default_ticker,
31+
first_date: Union[str, pd.Timestamp, None] = None,
32+
last_date: Union[str, pd.Timestamp, None] = None,
33+
):
2134
if symbol is None or len(str(symbol).strip()) == 0:
2235
raise ValueError("Symbol can not be empty")
2336
self._symbol = str(symbol).strip()
2437
self._check_namespace()
2538
self._get_symbol_data(symbol)
26-
self.ror: pd.Series = data_queries.QueryData.get_ror(symbol)
39+
self._first_date = first_date
40+
self._last_date = last_date
41+
self.ror: pd.Series = data_queries.QueryData.get_ror(
42+
symbol,
43+
first_date=first_date if first_date else "1913-01-01",
44+
last_date=last_date if last_date else "2100-01-01",
45+
period="M"
46+
)
47+
self._set_first_last_dates()
48+
49+
def _set_first_last_dates(self) -> None:
50+
"""Set first_date, last_date, period_length and pl attributes based on ror data."""
2751
self.first_date: pd.Timestamp = self.ror.index[0].to_timestamp()
2852
self.last_date: pd.Timestamp = self.ror.index[-1].to_timestamp()
2953
self.period_length: float = round((self.last_date - self.first_date) / np.timedelta64(365, "D"), ndigits=1)

0 commit comments

Comments
 (0)