This is a fine problem for an OOP approach. You could, for example, create a class for a particular portfolio stock holding, which has attributes for the company name, number of shares, and share price. You can give it useful functions, like getValue
. Here's an example implementation:
class Holding:
def __init__(self, companyName, numShares, sharePrice):
self.companyName = companyName
self.numShares = numShares
self.sharePrice = sharePrice
def getValue(self):
return self.numShares * self.sharePrice
portfolio = {'AMP':Holding('AMP', 1000, 2.5), 'ANZ':Holding('ANZ', 2000, 17.0), 'BHP':Holding('BHP', 500, 54.30)}
print portfolio['BHP'].sharePrice
# 54.3
print portfolio['AMP'].getValue()
# 2500.0
You can access the attributes of your holdings by attribute name. You could take it to the next level, and write a class for portfolio, too, which could have attributes like "holdingList", and "broker name", and functions like "getTotalValue", and so forth.