66import matplotlib .pyplot as plt
77
88from .validators import validate_integer
9- from ..common .helpers .helpers import Frame , Float
9+ from ..common .helpers .helpers import Frame , Float , Date
1010from ..macro import Inflation
1111from ..asset import Asset
1212from ..settings import default_ticker , PeriodLength , _MONTHS_PER_YEAR
@@ -61,7 +61,7 @@ def __init__(
6161 self .assets_first_dates ,
6262 self .assets_last_dates ,
6363 self .assets_ror ,
64- ) = self ._make_list (ls = self ._list_of_asset_like_objects ).values ()
64+ ) = self ._make_list (ls = self ._list_of_asset_like_objects , first_date = first_date , last_date = last_date ).values ()
6565 if first_date :
6666 self .first_date = max (self .first_date , pd .to_datetime (first_date ))
6767 self .assets_ror = self .assets_ror [self .first_date :]
@@ -101,7 +101,7 @@ def __repr__(self):
101101 def __len__ (self ):
102102 return len (self .symbols )
103103
104- def _make_list (self , ls : list ) -> dict :
104+ def _make_list (self , ls : list , first_date , last_date ) -> dict :
105105 """
106106 Make an asset list from a list of symbols.
107107 """
@@ -117,21 +117,26 @@ def _make_list(self, ls: list) -> dict:
117117 df = pd .DataFrame ()
118118 for i , x in enumerate (ls ):
119119 asset = x if hasattr (x , 'symbol' ) and hasattr (x , 'ror' ) else Asset (x )
120- asset_obj_dict .update ({asset .symbol : asset })
120+ if asset .pl .years == 0 and asset .pl .months <= 2 :
121+ raise ValueError (f'{ asset .symbol } period length is { asset .pl .months } . It should be at least 3 months.' )
122+ asset_first_date = max (asset .first_date , pd .to_datetime (first_date )) if first_date else asset .first_date
123+ asset_last_date = min (asset .last_date , pd .to_datetime (last_date )) if last_date else asset .last_date
124+ if Date .get_difference_in_months (asset_last_date , asset_first_date ).n < 2 :
125+ raise ValueError (f'{ asset .symbol } historical data period length is too short. '
126+ f'It must be at least 3 months.' )
127+ asset_obj_dict [asset .symbol ] = asset
121128 if i == 0 : # required to use pd.concat below (df should not be empty).
122129 df = self ._make_ror (asset , currency_name )
123130 else :
124131 new = self ._make_ror (asset , currency_name )
125132 df = pd .concat ([df , new ], axis = 1 , join = "inner" , copy = "false" )
126- currencies .update ({asset .symbol : asset .currency })
127- names .update ({asset .symbol : asset .name })
128- first_dates .update ({asset .symbol : asset .first_date })
129- last_dates .update ({asset .symbol : asset .last_date })
130- # Add currency to the date range dict
131- first_dates .update ({currency_name : currency_first_date })
132- last_dates .update ({currency_name : currency_last_date })
133- currencies .update ({"asset list" : currency_name })
134-
133+ currencies [asset .symbol ] = asset .currency
134+ names [asset .symbol ] = asset .name
135+ first_dates [asset .symbol ] = asset .first_date
136+ last_dates [asset .symbol ] = asset .last_date
137+ first_dates [currency_name ] = currency_first_date
138+ last_dates [currency_name ] = currency_last_date
139+ currencies ["asset list" ] = currency_name
135140 first_dates_sorted : list = sorted (first_dates .items (), key = lambda y : y [1 ])
136141 last_dates_sorted : list = sorted (last_dates .items (), key = lambda y : y [1 ])
137142 if isinstance (df , pd .Series ):
0 commit comments