diff --git a/main.py b/main.py index 74b7bfa..accd907 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,3 @@ import okama as ok -ccy = 'RUB' -ls = ['RGBITR.INDX', 'MCFTR.INDX', 'SPY.US', 'PGJ.US', 'GLD.US'] -cons_weights=[0.65, 0.06, 0.19, 0.05, 0.05] -port_cons_no_infl = ok.Portfolio(ls, ccy=ccy, weights=cons_weights, inflation=False) -print(port_cons_no_infl.percentile_from_history(years=8)) +print(ok.Asset('SPY.US')) diff --git a/okama/api/api_methods.py b/okama/api/api_methods.py index 693ce26..fbb7307 100644 --- a/okama/api/api_methods.py +++ b/okama/api/api_methods.py @@ -47,14 +47,18 @@ def connect( params = {"first_date": first_date, "last_date": last_date, "period": period} session.mount("https://", adapter) session.mount("http://", adapter) - r = session.get(request_url, params=params, verify=False, timeout=cls.default_timeout) - if r.status_code != requests.codes.ok: - raise Exception( - f"Error fetching data for {symbol}:", + try: + r = session.get(request_url, params=params, verify=False, timeout=cls.default_timeout) + r.raise_for_status() + except requests.exceptions.HTTPError as errh: + if r.status_code == 404: + raise requests.exceptions.HTTPError(f"{symbol} is not found in the database.") from errh + raise requests.exceptions.HTTPError( + f"HTTP error fetching data for {symbol}:", r.status_code, r.reason, request_url, - ) + ) from errh return r.text @classmethod diff --git a/tests/test_asset_list.py b/tests/test_asset_list.py index 5ebef57..bb7a4cc 100644 --- a/tests/test_asset_list.py +++ b/tests/test_asset_list.py @@ -203,8 +203,8 @@ def test_get_cumulative_return(self, input_data, expected1, expected2, expected3 cumulative_testdata2 = [ ("YTD", -0.0614, -0.0674), - (1, -0.1383, 0.1863), - (None, -0.1912, 0.2530), + (1, -0.1383, 0.1745), + (None, -0.1912, 0.2406), ] @mark.parametrize( diff --git a/tests/test_portfolio.py b/tests/test_portfolio.py index 71e1a38..dc77932 100644 --- a/tests/test_portfolio.py +++ b/tests/test_portfolio.py @@ -179,7 +179,7 @@ def test_get_cagr_real_no_inflation_exception(portfolio_no_inflation): @mark.parametrize( "period, real, expected", - [("YTD", False, 0.01505), (1, False, 0.12269), (2, True, 0.1381)], + [("YTD", False, 0.01505), (1, False, 0.12269), (2, True, 0.1268)], ) def test_cumulative_return(portfolio_rebalanced_month, period, real, expected): assert portfolio_rebalanced_month.get_cumulative_return(