stock-tracker-discord/table.py
2021-02-13 06:03:42 +00:00

30 lines
787 B
Python

from tabulate import tabulate
from decimal import Decimal
import yfinance as yf
def get_current_price(ticker):
yf_obj = yf.Ticker(ticker)
todays_data = yf_obj.history(period='1d')
return round(todays_data['Close'][0], 2)
stonks = {
'MSFT': 4.3,
'AAPL': 13,
'GME' : 212
}
def generate_table(stonks):
table_data = []
total = 0
for ticker in stonks.keys():
price = 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(['Total', '', round(total, 2)])
return str( tabulate(table_data, ['Ticker', 'Shares', 'Value ($)'], tablefmt="pretty") )