Skip to content

Commit

Permalink
add percentage change to portfolio overview and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AramKoorn committed Nov 13, 2021
1 parent cb9afdd commit 95e09f2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion tenbagger/src/textui/apps/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def on_load(self, event):


if __name__ == '__main__':
port = Portfolio('aram')
port = Portfolio('my_portfolio')
port.unification()

OverviewPortfolio.run(portfolio=port, log="textual.log")
22 changes: 15 additions & 7 deletions tenbagger/src/textui/widgets/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rich.table import Table
from rich import box
from rich.panel import Panel
from tenbagger.src.utils.utilities import Ticker


class SummaryPortfolio(Widget):
Expand Down Expand Up @@ -35,15 +36,19 @@ def __init__(self, portfolio):
super().__init__(portfolio)
self.portfolio = portfolio

# def on_mount(self):
# self.set_interval(30, self.refresh)
# Get last prices to show % change
self.last_prices = {}
self.curr_prices = {}
for ticker in self.portfolio.df.ticker:
tick = Ticker(ticker)
self.last_prices[ticker] = tick.get_last_day_close()
self.curr_prices[ticker] = tick.last_price()

@staticmethod
def generate_table(portfolio):
def generate_table(self, portfolio):

# Update table
prev_prices = dict(zip(list(portfolio.df.ticker), list(portfolio.df.price)))
portfolio.pulse()
#portfolio.pulse()
df = portfolio.df

remove_col = ['circulatingSupply', 'date', 'type']
Expand Down Expand Up @@ -81,8 +86,11 @@ def generate_table(portfolio):
table.add_column(col, style="bold white")
for row in df.values.tolist():
cur_row = dict(zip(list(df), row))
bool = cur_row["Price"] >= prev_prices[cur_row['Ticker']]
cur_row['Price'] = f'[bright_green]{cur_row["Price"]:.2f}' if bool else f'[bright_red]{cur_row["Price"]:.2f}'
diff_price = (self.curr_prices[cur_row['Ticker']] - self.last_prices[cur_row["Ticker"]]) / self.last_prices[cur_row["Ticker"]]
if diff_price >=0:
cur_row['Price'] = f"{cur_row['Price']:.2f} [bright_green](+{diff_price:.2%})"
else:
cur_row['Price'] = f"{cur_row['Price']:.2f} [bright_red]({diff_price:.2%})"
table.add_row(*[str(j) for j in list(cur_row.values())])

# Some formatting
Expand Down
2 changes: 1 addition & 1 deletion tenbagger/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.7.2"
__version__ = "0.7.3"
37 changes: 20 additions & 17 deletions test/utilities/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ def test_make_percentage():

assert_frame_equal(left=desired.reset_index(drop=True), right=res.reset_index(drop=True))


def test_read_yaml():
cfg = read_yaml('user_data/env/environment.yaml')
assert "CURRENCY" in cfg.keys()


class TestTickerInfo:
def test_stonk_info(self):
info = Ticker('IBM').get_info()
Expand All @@ -30,6 +32,7 @@ def test_info_crypto(self):
info = Ticker('eth-usd').get_info()
assert info['quoteType']['quoteType'] == 'CRYPTOCURRENCY'


class TestTickerStonk:
def setup(self):
self.ticker = Ticker('ibm')
Expand All @@ -43,20 +46,21 @@ def test_info(self):

def test_overview(self):
overview = self.ticker.overview()
overview = list(overview.Description)
assert overview == ['price',
'MarketCap',
'Shares Outstanding',
'Dividend Yield',
'trailingAnnualDividendYield',
'Short Percentage of Float',
'Trailing EPS',
'52 week low',
'52 week High',
'heldPercentInsiders',
'earningsQuarterlyGrowth',
'priceToSalesTrailing12Months',
'fair_value']
assert list(overview) == [
'Ticker',
'price',
'MarketCap',
'Shares Outstanding',
'Dividend Yield',
'trailingAnnualDividendYield',
'Short Percentage of Float',
'Trailing EPS',
'52 week low',
'52 week High',
'heldPercentInsiders',
'earningsQuarterlyGrowth',
'priceToSalesTrailing12Months',
'fair_value']


class TestTickerCrypto:
Expand All @@ -69,6 +73,5 @@ def test_last_price(self):

def test_overview(self):
overview = self.ticker.overview()
overview = list(overview.Description)
assert overview == ['price', 'MarketCap', '52 week low', '52 week High']

overview = list(overview)
assert overview == ['Ticker', 'price', 'MarketCap', '52 week low', '52 week High']

0 comments on commit 95e09f2

Please sign in to comment.