30 lines
771 B
Python
30 lines
771 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, Decimal(price) * Decimal(stonks[ticker])])
|
|
total += (Decimal(price) * Decimal(stonks[ticker]))
|
|
|
|
table_data.append(['Total', '', total])
|
|
|
|
return str( tabulate(table_data, ['Ticker', 'Shares', 'Value ($)'], tablefmt="fancy_grid") )
|