Skip to content

Commit

Permalink
Merge pull request #20 from nenkoru/dev
Browse files Browse the repository at this point in the history
Use importing best practice & Ignore Python cache files
  • Loading branch information
chilango74 committed Feb 7, 2022
2 parents 9dd19f8 + 77c0bfe commit e308260
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/docs/source/
/docs/stubs/
!/main_notebook.ipynb

__pycache__
*.pyc
33 changes: 16 additions & 17 deletions okama/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import pandas as pd
import numpy as np

from .common.helpers.helpers import Frame
from .settings import default_ticker, PeriodLength, _MONTHS_PER_YEAR
from .api.data_queries import QueryData
from .api.namespaces import get_assets_namespaces
from okama import settings
from okama.api import data_queries, namespaces
from okama.common.helpers import helpers


class Asset:
Expand All @@ -19,21 +18,21 @@ class Asset:
Symbol is an asset ticker with namespace after dot. The default value is "SPY.US" (SPDR S&P 500 ETF Trust).
"""

def __init__(self, symbol: str = default_ticker):
def __init__(self, symbol: str = settings.default_ticker):
if symbol is None or len(str(symbol).strip()) == 0:
raise ValueError("Symbol can not be empty")
self._symbol = str(symbol).strip()
self._check_namespace()
self._get_symbol_data(symbol)
self.ror: pd.Series = QueryData.get_ror(symbol)
self.ror: pd.Series = data_queries.QueryData.get_ror(symbol)
self.first_date: pd.Timestamp = self.ror.index[0].to_timestamp()
self.last_date: pd.Timestamp = self.ror.index[-1].to_timestamp()
self.period_length: float = round(
(self.last_date - self.first_date) / np.timedelta64(365, "D"), ndigits=1
)
self.pl = PeriodLength(
self.ror.shape[0] // _MONTHS_PER_YEAR,
self.ror.shape[0] % _MONTHS_PER_YEAR,
self.pl = settings.PeriodLength(
self.ror.shape[0] // settings._MONTHS_PER_YEAR,
self.ror.shape[0] % settings._MONTHS_PER_YEAR,
)

def __repr__(self):
Expand All @@ -53,14 +52,14 @@ def __repr__(self):

def _check_namespace(self):
namespace = self._symbol.split(".", 1)[-1]
allowed_namespaces = get_assets_namespaces()
allowed_namespaces = namespaces.get_assets_namespaces()
if namespace not in allowed_namespaces:
raise ValueError(
f"{namespace} is not in allowed assets namespaces: {allowed_namespaces}"
)

def _get_symbol_data(self, symbol) -> None:
x = QueryData.get_symbol_info(symbol)
x = data_queries.QueryData.get_symbol_info(symbol)
self.ticker: str = x["code"]
self.name: str = x["name"]
self.country: str = x["country"]
Expand Down Expand Up @@ -94,7 +93,7 @@ def price(self) -> Optional[float]:
float, None
Live price of the asset. Returns None if not defined.
"""
return QueryData.get_live_price(self.symbol)
return data_queries.QueryData.get_live_price(self.symbol)

@property
def close_daily(self):
Expand All @@ -106,7 +105,7 @@ def close_daily(self):
Series
Time series of close price historical data (daily).
"""
return QueryData.get_close(self.symbol, period='D')
return data_queries.QueryData.get_close(self.symbol, period='D')

@property
def close_monthly(self):
Expand All @@ -127,7 +126,7 @@ def close_monthly(self):
>>> x.close_monthly.plot()
>>> plt.show()
"""
return Frame.change_period_to_month(self.close_daily)
return helpers.Frame.change_period_to_month(self.close_daily)

@property
def adj_close(self):
Expand All @@ -143,7 +142,7 @@ def adj_close(self):
Series
Time series of adjusted close price historical data (daily).
"""
return QueryData.get_adj_close(self.symbol, period='D')
return data_queries.QueryData.get_adj_close(self.symbol, period='D')

@property
def dividends(self) -> pd.Series:
Expand Down Expand Up @@ -171,7 +170,7 @@ def dividends(self) -> pd.Series:
2021-03-25 0.5264
Freq: D, Name: VNQ.US, Length: 66, dtype: float64
"""
div = QueryData.get_dividends(self.symbol)
div = data_queries.QueryData.get_dividends(self.symbol)
if div.empty:
# Zero time series for assets where dividend yield is not defined.
index = pd.date_range(
Expand All @@ -188,5 +187,5 @@ def nav_ts(self) -> Optional[pd.Series]:
Return NAV time series (monthly) for mutual funds.
"""
if self.exchange == "PIF":
return QueryData.get_nav(self.symbol)
return data_queries.QueryData.get_nav(self.symbol)
return np.nan

0 comments on commit e308260

Please sign in to comment.