diff --git a/table.py b/table.py index aa1025d..a52335e 100644 --- a/table.py +++ b/table.py @@ -2,30 +2,37 @@ from tabulate import tabulate from decimal import Decimal import yfi as y + def generate_table(stonks): table_data = [] total = 0 for ticker in stonks.keys(): price = y.get_current_price(ticker) shares_count = stonks[ticker] - table_data.append([ticker, shares_count, round(Decimal(price) * Decimal(stonks[ticker]), 2)]) - total += (Decimal(price) * Decimal(stonks[ticker])) + table_data.append( + [ticker, shares_count, round(Decimal(price) * Decimal(stonks[ticker]), 2)] + ) + total += Decimal(price) * Decimal(stonks[ticker]) - table_data.append(['Total', '', round(total, 2)]) + table_data.append(["Total", "", round(total, 2)]) - return str( tabulate(table_data, ['Ticker', 'Shares', 'Value ($)'], tablefmt="pretty") ) + return str( + tabulate(table_data, ["Ticker", "Shares", "Value ($)"], tablefmt="pretty") + ) def watchlist_table(watchlist): table_data = [] - header = ['Ticker', 'Current Price ($)', 'Estimated Price ($)'] + header = ["Ticker", "Current Price ($)", "Estimated Price ($)"] for stonk in watchlist.keys(): - table_data.append([ - stonk, - y.get_current_price(stonk), - watchlist[stonk], - ]) + table_data.append( + [ + stonk, + y.get_current_price(stonk), + watchlist[stonk], + ] + ) - return str( tabulate( table_data, header, tablefmt="pretty" ) ) + return str(tabulate(table_data, header, tablefmt="pretty")) diff --git a/table2.py b/table2.py index 36aa057..5c666e2 100644 --- a/table2.py +++ b/table2.py @@ -3,14 +3,12 @@ import matplotlib, io from datetime import datetime -def generate_table(headers, data, title="", bg='skyblue', border='steelblue'): +def generate_table(headers, data, title="", bg="skyblue", border="steelblue"): timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S") - font = {'family' : 'monospace', - 'weight' : 'normal', - 'size' : 12} + font = {"family": "monospace", "weight": "normal", "size": 12} - matplotlib.rc('font', **font) + matplotlib.rc("font", **font) title_text = title footer_text = timestamp @@ -19,36 +17,35 @@ def generate_table(headers, data, title="", bg='skyblue', border='steelblue'): column_headers = headers - if data: cell_text = [] for row in data: cell_text.append([x for x in row]) else: - cell_text = [["No Data"] + ["" for x in range(len(headers)-1)]] - + cell_text = [["No Data"] + ["" for x in range(len(headers) - 1)]] # Create the figure. Setting a small pad on tight_layout # seems to better regulate white space. Sometimes experimenting # with an explicit figsize here can produce better outcome. dpi = 160 - #image_height = (( 0.5 + ( 0.4125 * ( len(data) + 1) ) ) * dpi) - image_height = 100 + (67 * ( len(data) +1 )) + # image_height = (( 0.5 + ( 0.4125 * ( len(data) + 1) ) ) * dpi) + image_height = 100 + (67 * (len(data) + 1)) image_width = 6 * dpi - plt.figure(linewidth=2, - edgecolor=fig_border, - facecolor=fig_background_color, - tight_layout={'pad':1}, - dpi=dpi, - figsize=(image_width/dpi, image_height/dpi) - ) + plt.figure( + linewidth=2, + edgecolor=fig_border, + facecolor=fig_background_color, + tight_layout={"pad": 1}, + dpi=dpi, + figsize=(image_width / dpi, image_height / dpi), + ) # Add a table at the bottom of the axes - the_table = plt.table(cellText=cell_text, - colLabels=column_headers, - loc='upper center') + the_table = plt.table( + cellText=cell_text, colLabels=column_headers, loc="upper center" + ) # Scaling is the only influence we have over top and bottom cell padding. # Make the rows taller (i.e., make cell y scale larger). @@ -63,10 +60,17 @@ def generate_table(headers, data, title="", bg='skyblue', border='steelblue'): plt.box(on=None) # Add title - plt.suptitle(title_text, x=0.5, y=((image_height - 15)/image_height)) + plt.suptitle(title_text, x=0.5, y=((image_height - 15) / image_height)) # Add footer - plt.figtext(((image_width - 15)/image_width), 1-((image_height - 15)/image_height), footer_text, horizontalalignment='right', size=12, weight='light') + plt.figtext( + ((image_width - 15) / image_width), + 1 - ((image_height - 15) / image_height), + footer_text, + horizontalalignment="right", + size=12, + weight="light", + ) # Force the figure to update, so backends center objects correctly within the figure. # Without plt.draw() here, the title will center on the axes and not the figure. @@ -74,6 +78,6 @@ def generate_table(headers, data, title="", bg='skyblue', border='steelblue'): # Create image. plt.savefig ignores figure edge and face colors, so map them. image_buffer = io.BytesIO() - plt.savefig(image_buffer, format='png') + plt.savefig(image_buffer, format="png") image_buffer.seek(0) return image_buffer diff --git a/yfi.py b/yfi.py index 1345039..98bb914 100644 --- a/yfi.py +++ b/yfi.py @@ -8,21 +8,24 @@ def stock_exists(ticker): except KeyError: return False + def get_current_price(ticker): yf_obj = yf.Ticker(ticker) - todays_data = yf_obj.history(period='1d') - return round(todays_data['Close'][0], 2) + todays_data = yf_obj.history(period="1d") + return round(todays_data["Close"][0], 2) + def get_delta(ticker, return_old_price=False): yf_obj = yf.Ticker(ticker) - old_price = yf_obj.history(interval='1d')['Close'][-2] + old_price = yf_obj.history(interval="1d")["Close"][-2] current_price = get_current_price(ticker) - delta = 100 * ( (current_price / old_price) - 1 ) + delta = 100 * ((current_price / old_price) - 1) if return_old_price: return delta, old_price else: return delta + def get_delta_old_price(ticker): yf_obj = yf.Ticker(ticker) - return yf_obj.history(interval='1d')['Close'][-2] + return yf_obj.history(interval="1d")["Close"][-2]