Python Folio
 

 A super-quick program to price a portfolio, written in Python. Uses Yahoo Finance quotes for prices. It expects a file called folio.csv to be present in the same directory. The file containts a pair of stock tickers and position, pair per line. For example:

IBM, 100
VINIX, 10
FCNTX, 40
 

The above represents a portfolio of 100 shares of IBM, 10 of the VINIX and 40 of FCNTX.

The actual script is:

#!/usr/bin/python
 
 from urllib import urlopen
 import csv
 
 # Portfolio Container: TICKER : [SHARES : POSITION]
 positions={}
 
 # Initialize 'positions' based on contents of folio.csv
 def definePositions():
      reader = csv.reader(open("folio.csv", "r"))   
     for row in reader:
         # Key in positions is uppercase ticker. Value is
         # a list of [0] = shares and [1] = price (initially 0)
         positions[row[0].upper().strip()] = [float(row[1]), 0.00]
 
 # Print put the portfolio and totals. Portfolio needs to have been
 # priced for this to be useful.
 def dumpPositions():
     total = 0.00;
     for x in positions:
         subtotal = positions[x][0]*positions[x][1]
         print x,"\t: %8.2f"%positions[x][0],  \
                   "@ %6.2f"%positions[x][1],  \
               "= %12.2f"%(subtotal)
         total += subtotal
     print "Total\t:%33.2f" % total
 
 # Hit up yahoo for the prices in the portfolio. No error handling
 # and no proxy support
 def priceTickers():
 
     # Build a request string for Yahoo
     # Details: http://www.gummy-stuff.org/Yahoo-data.htm
     url = "http://finance.yahoo.com/d/quotes.csv?s="
     for x in positions: 
         url+=x + "+"
     url += "&f=sl1"  # symbol and last trade price
 
     # Get the response from Yahoo and update portfolio...
     for response_line in urlopen(url).readlines():
 
         # Ticker and price are comma-separated
         pair = response_line.split(',')
 
         # Ticker is enclosed in quotes and we dont want them
         ticker = pair[0].strip('"')
 
         # Update positions for the ticker with the new price
         positions[ticker][1] = float(pair[1].strip())
 
 definePositions()
 priceTickers()
 dumpPositions()