-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: symbols_in_namespace() method in namespaces.py
- Loading branch information
1 parent
fe6cc53
commit 20292c6
Showing
22 changed files
with
1,222 additions
and
1,005 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
import okama as ok | ||
import pandas as pd | ||
|
||
x = ok.Portfolio(symbols=['RUB.FX', 'MCFTR.INDX'], ccy='RUB', | ||
first_date='2015-01', last_date='2020-01', inflation=True) | ||
print(x.describe()) | ||
print('\n\n*******************') | ||
print(pd.read_pickle('tests/data/portfolio_description.pkl')) | ||
print(ok.symbols_in_namespace('CBR').info) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from typing import Dict | ||
|
||
from io import StringIO | ||
import json | ||
|
||
import pandas as pd | ||
import numpy as np | ||
|
||
from .api_methods import API | ||
from .namespaces import no_dividends_namespaces | ||
|
||
|
||
class QueryData: | ||
""" | ||
Set of methods to get symbols data from API. | ||
""" | ||
|
||
@staticmethod | ||
def get_symbol_info(symbol: str) -> Dict[str, str]: | ||
json_input = API.get_symbol_info(symbol) | ||
return json.loads(json_input) | ||
|
||
@staticmethod | ||
def csv_to_series(csv_input: str, period: str) -> pd.Series: | ||
ts = pd.read_csv(StringIO(csv_input), | ||
delimiter=',', | ||
index_col=0, | ||
parse_dates=[0], | ||
dtype={1: np.float64}, | ||
engine='python') | ||
if not ts.empty: | ||
ts.index = ts.index.to_period(period.upper()) | ||
ts = ts.squeeze('columns') | ||
return ts | ||
|
||
@staticmethod | ||
def get_macro_ts(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01') -> pd.Series: | ||
""" | ||
Requests API for Macroeconomic indicators time series (monthly data). | ||
- Inflation time series | ||
- Bank rates time series | ||
""" | ||
csv_input = API.get_macro(symbol=symbol, first_date=first_date, last_date=last_date) | ||
return QueryData.csv_to_series(csv_input, period='M') | ||
|
||
@staticmethod | ||
def get_ror(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01', period='M') -> pd.Series: | ||
""" | ||
Requests API for rate of return time series. | ||
""" | ||
csv_input = API.get_ror(symbol=symbol, first_date=first_date, last_date=last_date, period=period) | ||
return QueryData.csv_to_series(csv_input, period) | ||
|
||
@staticmethod | ||
def get_nav(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01', period='M') -> pd.Series: | ||
""" | ||
NAV time series for funds (works for PIF namespace only). | ||
""" | ||
csv_input = API.get_nav(symbol=symbol, first_date=first_date, last_date=last_date, period=period) | ||
return QueryData.csv_to_series(csv_input, period=period) | ||
|
||
@staticmethod | ||
def get_close(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01', period='M') -> pd.Series: | ||
""" | ||
Gets 'close' time series for a ticker. | ||
""" | ||
csv_input = API.get_close(symbol=symbol, first_date=first_date, last_date=last_date, period=period) | ||
return QueryData.csv_to_series(csv_input, period) | ||
|
||
@staticmethod | ||
def get_adj_close(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01', period='M') -> pd.Series: | ||
""" | ||
Gets 'adjusted close' time series for a ticker. | ||
""" | ||
csv_input = API.get_adjusted_close(symbol=symbol, first_date=first_date, last_date=last_date, period=period) | ||
return QueryData.csv_to_series(csv_input, period) | ||
|
||
@staticmethod | ||
def get_dividends(symbol: str, first_date: str = '1913-01-01', last_date: str = '2100-01-01',) -> pd.Series: | ||
""" | ||
Dividends time series daily data (dividend payment day should be considered). | ||
""" | ||
if symbol.split('.', 1)[-1] not in no_dividends_namespaces(): | ||
csv_input = API.get_dividends(symbol, first_date=first_date, last_date=last_date) | ||
ts = QueryData.csv_to_series(csv_input, period='D') | ||
else: | ||
# make empty time series when no dividends | ||
ts = pd.Series(dtype=float) | ||
ts.rename(symbol, inplace=True) | ||
return ts | ||
|
||
@staticmethod | ||
def get_live_price(symbol: str) -> float: | ||
price = API.get_live_price(symbol) | ||
return float(price) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import json | ||
from functools import lru_cache | ||
|
||
import pandas as pd | ||
|
||
from .api_methods import API | ||
from ..settings import default_namespace | ||
|
||
|
||
@lru_cache() | ||
def get_namespaces(): | ||
string_response = API.get_namespaces() | ||
return json.loads(string_response) | ||
|
||
|
||
@lru_cache() | ||
def symbols_in_namespace(namespace: str = default_namespace): | ||
string_response = API.get_symbols_in_namespace(namespace.upper()) | ||
list_of_symbols = json.loads(string_response) | ||
df = pd.DataFrame(list_of_symbols[1:], columns=list_of_symbols[0]) | ||
return df.astype('string', copy=False) | ||
|
||
|
||
@lru_cache() | ||
def get_assets_namespaces(): | ||
string_response = API.get_assets_namespaces() | ||
return json.loads(string_response) | ||
|
||
|
||
@lru_cache() | ||
def get_macro_namespaces(): | ||
string_response = API.get_macro_namespaces() | ||
return json.loads(string_response) | ||
|
||
|
||
@lru_cache() | ||
def no_dividends_namespaces(): | ||
string_response = API.get_no_dividends_namespaces() | ||
return json.loads(string_response) | ||
|
||
|
||
namespaces = get_namespaces() | ||
assets_namespaces = get_assets_namespaces() | ||
macro_namespaces = get_macro_namespaces() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import json | ||
|
||
from .api_methods import API | ||
|
||
|
||
def search(search_string: str) -> json: | ||
string_response = API.search(search_string) | ||
return json.loads(string_response) |
Oops, something went wrong.