Advertisement
tetris555

portfolio

Dec 17th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. # market data
  2. market_current = {'TSLA': 662, 'GOOG': 2053, 'AAPL': 122, 'FB': 291, 'NFLX': 535}
  3. positions = {'FB': 15, 'GOOG': 5, 'TSLA': 9}
  4. market_2021 = {'TSLA': 985, 'GOOG': 2965, 'AAPL': 180, 'FB': 340, 'NFLX': 595}
  5.  
  6.  
  7. class Portfolio:
  8.  
  9.     def __init__(self, holdings):
  10.         self.holdings = holdings
  11.  
  12.     def add_to_portfolio(self, name, shares):
  13.         self.holdings[name] = shares
  14.  
  15.     def performance(self, market_today, year='now'):
  16.         total = 0
  17.         report = ''
  18.         for stock in self.holdings:
  19.             report += f"{stock:4} {self.holdings[stock]:4} pcs {market_today[stock] * self.holdings[stock]:10,.2f} USD\n"
  20.             total += market_today[stock] * self.holdings[stock]
  21.         print(f"Stock portfolio value (total) {year}: {total:,.2f} USD")
  22.         print(report)
  23.  
  24.  
  25.  
  26. p1 = Portfolio(positions)
  27. # p1.add_to_portfolio('AAPL', 12)
  28.  
  29. p1.performance(market_2021, 2021)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement