Skip to content

Commit 20292c6

Browse files
committed
feat: symbols_in_namespace() method in namespaces.py
1 parent fe6cc53 commit 20292c6

22 files changed

+1222
-1005
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<img src="https://img.shields.io/badge/python-v3-brightgreen.svg"
55
alt="python"></a> &nbsp;
66
<a href="https://pypi.org/project/okama/">
7-
<img src="https://img.shields.io/badge/pypi-v0.92-brightgreen.svg"
7+
<img src="https://img.shields.io/badge/pypi-v0.93-brightgreen.svg"
88
alt="pypi"></a> &nbsp;
99
<a href="https://opensource.org/licenses/MIT">
1010
<img src="https://img.shields.io/badge/license-MIT-brightgreen.svg"
@@ -84,10 +84,13 @@ x.wealth_indexes.plot()
8484
![](../images/images/readmi03.jpg?raw=true)
8585

8686
### 2. Create a dividend stocks portfolio with base currency EUR
87+
8788
```python
89+
import okama.portfolio
90+
8891
weights = [0.3, 0.2, 0.2, 0.2, 0.1]
8992
assets = ['T.US', 'XOM.US', 'FRE.XETR', 'SNW.XETR', 'LKOH.MOEX']
90-
pf = ok.Portfolio(assets, weights=weights, ccy='EUR')
93+
pf = okama.portfolio.Portfolio(assets, weights=weights, ccy='EUR')
9194
print(pf)
9295
```
9396
![](../images/images/readmi04.jpg?raw=true)

main.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
import okama as ok
2-
import pandas as pd
32

4-
x = ok.Portfolio(symbols=['RUB.FX', 'MCFTR.INDX'], ccy='RUB',
5-
first_date='2015-01', last_date='2020-01', inflation=True)
6-
print(x.describe())
7-
print('\n\n*******************')
8-
print(pd.read_pickle('tests/data/portfolio_description.pkl'))
3+
print(ok.symbols_in_namespace('CBR').info)

notebooks/01 howto.ipynb

Lines changed: 366 additions & 186 deletions
Large diffs are not rendered by default.

okama/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
2121
"""
2222

23-
from okama.assets import Asset, AssetList, Portfolio
23+
from okama.assets import Asset, AssetList
24+
from okama.portfolio import Portfolio
2425
from okama.macro import Inflation, Rate
25-
from okama.frontier import EfficientFrontier
26-
from okama.frontier_reb import EfficientFrontierReb
26+
from okama.frontier.multi_period import EfficientFrontierReb
2727
from okama.plots import Plots
28-
from okama.data import QueryData, API, search, namespaces, assets_namespaces, macro_namespaces
28+
from okama.api.data_queries import QueryData
29+
from okama.api.search import search
30+
from okama.api.api_methods import API
31+
from okama.api.namespaces import namespaces, assets_namespaces, macro_namespaces, symbols_in_namespace
2932
from okama.helpers import Float, Frame, Rebalance, Date
3033
import okama.settings
3134

32-
__version__ = '0.92'
35+
__version__ = '0.93'

okama/api/__init__.py

Whitespace-only changes.
Lines changed: 5 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,6 @@
1-
from typing import Dict
2-
from functools import lru_cache
3-
4-
from io import StringIO
5-
import json
6-
71
import requests
8-
import pandas as pd
9-
import numpy as np
10-
11-
from .settings import default_ticker
12-
13-
14-
def search(search_string: str) -> json:
15-
string_response = API.search(search_string)
16-
return json.loads(string_response)
17-
18-
19-
@lru_cache()
20-
def get_namespaces():
21-
string_response = API.get_namespaces()
22-
return json.loads(string_response)
23-
24-
25-
@lru_cache()
26-
def get_assets_namespaces():
27-
string_response = API.get_assets_namespaces()
28-
return json.loads(string_response)
29-
30-
31-
@lru_cache()
32-
def get_macro_namespaces():
33-
string_response = API.get_macro_namespaces()
34-
return json.loads(string_response)
352

36-
37-
@lru_cache()
38-
def no_dividends_namespaces():
39-
string_response = API.get_no_dividends_namespaces()
40-
return json.loads(string_response)
41-
42-
43-
class QueryData:
44-
"""
45-
Set of methods to select a source and get_ts the data.
46-
"""
47-
48-
@staticmethod
49-
def get_symbol_info(symbol: str) -> Dict[str, str]:
50-
json_input = API.get_symbol_info(symbol)
51-
return json.loads(json_input)
52-
53-
@staticmethod
54-
def csv_to_series(csv_input: str, period: str) -> pd.Series:
55-
ts = pd.read_csv(StringIO(csv_input),
56-
delimiter=',',
57-
index_col=0,
58-
parse_dates=[0],
59-
dtype={1: np.float64},
60-
engine='python')
61-
if not ts.empty:
62-
ts.index = ts.index.to_period(period.upper())
63-
ts = ts.squeeze('columns')
64-
return ts
65-
66-
@staticmethod
67-
def get_macro_ts(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01') -> pd.Series:
68-
"""
69-
Requests API for Macroeconomic indicators time series (monthly data).
70-
- Inflation time series
71-
- Bank rates time series
72-
"""
73-
csv_input = API.get_macro(symbol=symbol, first_date=first_date, last_date=last_date)
74-
return QueryData.csv_to_series(csv_input, period='M')
75-
76-
@staticmethod
77-
def get_ror(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01', period='M') -> pd.Series:
78-
"""
79-
Requests API for rate of return time series.
80-
"""
81-
csv_input = API.get_ror(symbol=symbol, first_date=first_date, last_date=last_date, period=period)
82-
return QueryData.csv_to_series(csv_input, period)
83-
84-
@staticmethod
85-
def get_nav(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01', period='M') -> pd.Series:
86-
"""
87-
NAV time series for funds (works for PIF namespace only).
88-
"""
89-
csv_input = API.get_nav(symbol=symbol, first_date=first_date, last_date=last_date, period=period)
90-
return QueryData.csv_to_series(csv_input, period=period)
91-
92-
@staticmethod
93-
def get_close(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01', period='M') -> pd.Series:
94-
"""
95-
Gets 'close' time series for a ticker.
96-
"""
97-
csv_input = API.get_close(symbol=symbol, first_date=first_date, last_date=last_date, period=period)
98-
return QueryData.csv_to_series(csv_input, period)
99-
100-
@staticmethod
101-
def get_adj_close(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01', period='M') -> pd.Series:
102-
"""
103-
Gets 'adjusted close' time series for a ticker.
104-
"""
105-
csv_input = API.get_adjusted_close(symbol=symbol, first_date=first_date, last_date=last_date, period=period)
106-
return QueryData.csv_to_series(csv_input, period)
107-
108-
@staticmethod
109-
def get_dividends(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01',) -> pd.Series:
110-
"""
111-
Dividends time series daily data (dividend payment day should be considered).
112-
"""
113-
if symbol.split('.', 1)[-1] not in no_dividends_namespaces():
114-
csv_input = API.get_dividends(symbol, first_date=first_date, last_date=last_date)
115-
ts = QueryData.csv_to_series(csv_input, period='D')
116-
else:
117-
# make empty time series when no dividends
118-
ts = pd.Series(dtype=float)
119-
ts.rename(symbol, inplace=True)
120-
return ts
121-
122-
@staticmethod
123-
def get_live_price(symbol: str) -> float:
124-
price = API.get_live_price(symbol)
125-
return float(price)
3+
from ..settings import default_ticker, default_namespace
1264

1275

1286
class API:
@@ -240,6 +118,10 @@ def get_macro(cls,
240118
def get_namespaces(cls):
241119
return cls.connect(endpoint=cls.endpoint_namespaces, symbol='')
242120

121+
@classmethod
122+
def get_symbols_in_namespace(cls, namespace: str = default_namespace):
123+
return cls.connect(endpoint=cls.endpoint_namespaces, symbol=namespace)
124+
243125
@classmethod
244126
def get_assets_namespaces(cls):
245127
return cls.connect(endpoint=cls.endpoint_assets_namespaces, symbol='')
@@ -263,8 +145,3 @@ def search(cls, search_string: str):
263145
@classmethod
264146
def get_live_price(cls, symbol: str):
265147
return cls.connect(endpoint=cls.endpoint_live_price, symbol=symbol)
266-
267-
268-
namespaces = get_namespaces()
269-
assets_namespaces = get_assets_namespaces()
270-
macro_namespaces = get_macro_namespaces()

okama/api/data_queries.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from typing import Dict
2+
3+
from io import StringIO
4+
import json
5+
6+
import pandas as pd
7+
import numpy as np
8+
9+
from .api_methods import API
10+
from .namespaces import no_dividends_namespaces
11+
12+
13+
class QueryData:
14+
"""
15+
Set of methods to get symbols data from API.
16+
"""
17+
18+
@staticmethod
19+
def get_symbol_info(symbol: str) -> Dict[str, str]:
20+
json_input = API.get_symbol_info(symbol)
21+
return json.loads(json_input)
22+
23+
@staticmethod
24+
def csv_to_series(csv_input: str, period: str) -> pd.Series:
25+
ts = pd.read_csv(StringIO(csv_input),
26+
delimiter=',',
27+
index_col=0,
28+
parse_dates=[0],
29+
dtype={1: np.float64},
30+
engine='python')
31+
if not ts.empty:
32+
ts.index = ts.index.to_period(period.upper())
33+
ts = ts.squeeze('columns')
34+
return ts
35+
36+
@staticmethod
37+
def get_macro_ts(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01') -> pd.Series:
38+
"""
39+
Requests API for Macroeconomic indicators time series (monthly data).
40+
- Inflation time series
41+
- Bank rates time series
42+
"""
43+
csv_input = API.get_macro(symbol=symbol, first_date=first_date, last_date=last_date)
44+
return QueryData.csv_to_series(csv_input, period='M')
45+
46+
@staticmethod
47+
def get_ror(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01', period='M') -> pd.Series:
48+
"""
49+
Requests API for rate of return time series.
50+
"""
51+
csv_input = API.get_ror(symbol=symbol, first_date=first_date, last_date=last_date, period=period)
52+
return QueryData.csv_to_series(csv_input, period)
53+
54+
@staticmethod
55+
def get_nav(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01', period='M') -> pd.Series:
56+
"""
57+
NAV time series for funds (works for PIF namespace only).
58+
"""
59+
csv_input = API.get_nav(symbol=symbol, first_date=first_date, last_date=last_date, period=period)
60+
return QueryData.csv_to_series(csv_input, period=period)
61+
62+
@staticmethod
63+
def get_close(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01', period='M') -> pd.Series:
64+
"""
65+
Gets 'close' time series for a ticker.
66+
"""
67+
csv_input = API.get_close(symbol=symbol, first_date=first_date, last_date=last_date, period=period)
68+
return QueryData.csv_to_series(csv_input, period)
69+
70+
@staticmethod
71+
def get_adj_close(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01', period='M') -> pd.Series:
72+
"""
73+
Gets 'adjusted close' time series for a ticker.
74+
"""
75+
csv_input = API.get_adjusted_close(symbol=symbol, first_date=first_date, last_date=last_date, period=period)
76+
return QueryData.csv_to_series(csv_input, period)
77+
78+
@staticmethod
79+
def get_dividends(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01',) -> pd.Series:
80+
"""
81+
Dividends time series daily data (dividend payment day should be considered).
82+
"""
83+
if symbol.split('.', 1)[-1] not in no_dividends_namespaces():
84+
csv_input = API.get_dividends(symbol, first_date=first_date, last_date=last_date)
85+
ts = QueryData.csv_to_series(csv_input, period='D')
86+
else:
87+
# make empty time series when no dividends
88+
ts = pd.Series(dtype=float)
89+
ts.rename(symbol, inplace=True)
90+
return ts
91+
92+
@staticmethod
93+
def get_live_price(symbol: str) -> float:
94+
price = API.get_live_price(symbol)
95+
return float(price)

okama/api/namespaces.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import json
2+
from functools import lru_cache
3+
4+
import pandas as pd
5+
6+
from .api_methods import API
7+
from ..settings import default_namespace
8+
9+
10+
@lru_cache()
11+
def get_namespaces():
12+
string_response = API.get_namespaces()
13+
return json.loads(string_response)
14+
15+
16+
@lru_cache()
17+
def symbols_in_namespace(namespace: str = default_namespace):
18+
string_response = API.get_symbols_in_namespace(namespace.upper())
19+
list_of_symbols = json.loads(string_response)
20+
df = pd.DataFrame(list_of_symbols[1:], columns=list_of_symbols[0])
21+
return df.astype('string', copy=False)
22+
23+
24+
@lru_cache()
25+
def get_assets_namespaces():
26+
string_response = API.get_assets_namespaces()
27+
return json.loads(string_response)
28+
29+
30+
@lru_cache()
31+
def get_macro_namespaces():
32+
string_response = API.get_macro_namespaces()
33+
return json.loads(string_response)
34+
35+
36+
@lru_cache()
37+
def no_dividends_namespaces():
38+
string_response = API.get_no_dividends_namespaces()
39+
return json.loads(string_response)
40+
41+
42+
namespaces = get_namespaces()
43+
assets_namespaces = get_assets_namespaces()
44+
macro_namespaces = get_macro_namespaces()

okama/api/search.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import json
2+
3+
from .api_methods import API
4+
5+
6+
def search(search_string: str) -> json:
7+
string_response = API.search(search_string)
8+
return json.loads(string_response)

0 commit comments

Comments
 (0)