Hi guys,
I'm new to programming and am trying to get better by writing "useful" things. Here is a very simple stock portfolio tracker that I started yesterday. It is not very complex and I feel like I'm not really improving my skills since it uses mostly beginner concepts, but I'd welcome any comments, particularly about the structure and/or improvements that I could make. I haven't added exception handling yet but will be doing that next. Also, I welcome any suggestions for things to do/add to the program that will be a bit more of a challenge...
So far, I plan on adding functions for looking up the current price of a ticker. I'm struggling with this because I don't know HTML. Someone suggested that I use BeautifulSoup to parse HTML of the stock page of interest, but I still end up with a huge string of HTML and am having trouble "locating" the stock price on the page. I also plan to add a GUI with WXPython, which is new to me as well. Again, I'd appreciate any comments...
import urllib2
import cPickle
from BeautifulSoup import BeautifulSoup
def introToProgram():
print "Welcome to the stock portfolio program."
print "What would you like to do? Here are your options:"
def getQuote():
#This is not functional yet...
ticker=raw_input('Enter the ticker symbol: ')
ticker='http://finance.yahoo.com/q?s='+ticker
tickerhtml=urllib2.urlopen(ticker)
soup = BeautifulSoup(tickerhtml)
print ticker, " last traded at ",price
def menuPrompt():
print """Type 'v' to view all holdings
Type 'a' to add a new stock to portfolio
Type 'r' to remove a stock from portfolio
Type 'd' if you would like to view detailed
information about a ticker
Type 's' to save your portfolio to file
Type 'o' to open an existing portfolio
Type 'q' to quit"""
selection=raw_input("Enter selection: ")
return selection
def viewHoldings(portfolio):
if portfolio==[]:
print 'There are no stocks in your portfolio'
return
for company in portfolio:
print 'Company: ',company[0]
print 'Initial price: ' ,company[1]
print 'Number of shares: ', company[2]
print 'Initial amount invested: ', company[3]
print
def addStock(portfolio):
name=raw_input('Enter the ticker symbol of the new stock to add: ')
price=input('What price per share did you pay? :')
shares=input('How many shares did you buy?')
amount=price*shares
newstock=[name,price,shares,amount]
print "You want to add the following: "
viewHoldings([newstock])
confirmation=raw_input("Please confirm by pressing y")
if confirmation =='y' or confirmation =='Y':
portfolio.append(newstock)
return portfolio
else:
print "Nothing added"
return
def removeStock(portfolio):
ticker=raw_input('Enter ticker symbol of stock you want to remove: ')
tickerindex=0
for stock in portfolio:
if ticker in stock:
break
tickerindex=tickerindex+1
portfolio.remove(tickerindex)
return portfolio
def detailedTicker():
pass
#Add later
def savePortfolio(portfolio):
portfolioname=raw_input("What would you like to name this portfolio?")
print "Your portfolio will be saved as a file named", portfolioname
portfoliofile= portfolioname+".txt"
f=open(portfoliofile,'w')
cPickle.dump(portfolio,f)
f.close()
def openPortfolio():
portfolioname=raw_input("What is the file name?")
portfoliofile= portfolioname+".txt"
f = open(portfoliofile,'r')
portfolio = cPickle.load(f)
print portfolio
return portfolio
def main ():
portfolio=[]
command=0
cash=0
introToProgram()
while command!='q' :
command=menuPrompt()
if command=='v':
viewHoldings(portfolio)
elif command=='a':
portfolio=addStock(portfolio)
elif command=='r':
removeStock(portfolio)
elif command=='d':
detailedTicker()
elif command=='s':
savePortfolio(portfolio)
elif command=='o':
portfolio=openPortfolio()
elif command=='q':
print "Goodbye"
else:
print "Invalid selection, try again: "
print
print
main ()