67 Tracking Your Stock Portfolio


#67 Tracking Your Stock Portfolio

A more complex task for the shell is to keep track of the overall value of your stock portfolio. While this might actually be too depressing to see each time you log in, the building blocks are quite informative and valuable on their own.

Like Script #66, this solution is built from two different scripts, one that extracts the most recently traded value of a given stock, and a second script that reads and calculates running totals for a portfolio of stocks.

The Code

 #!/bin/sh # getstock - Given a stock ticker symbol, returns its current value #    from the Lycos website. url="http://finance.lycos.com/qc/stocks/quotes.aspx?symbols=" if [ $# -ne 1 ] ; then   echo "Usage: $(basename 
 #!/bin/sh # getstock - Given a stock ticker symbol, returns its current value # from the Lycos website. url="http://finance.lycos.com/qc/stocks/quotes.aspx?symbols=" if [ $# -ne 1 ] ; then echo "Usage: $(basename $0) stocksymbol" >&2 exit 1 fi value="$(lynx -dump "$url$1"  grep 'Last price:'  \ awk -F: 'NF > 1 && $(NF) != "N/A" { print $(NF) }')" if [ -z $value ] ; then echo "error: no value found for ticker symbol $1." >&2 exit 1 fi echo $value exit 0 
) stocksymbol" >&2 exit 1 fi value="$(lynx -dump "$url" grep 'Last price:' \ awk -F: 'NF > 1 && $(NF) != "N/A" { print $(NF) }')" if [ -z $value ] ; then echo "error: no value found for ticker symbol ." >&2 exit 1 fi echo $value exit 0

The second script is the wrapper that allows users to create a rudimentary data file with stock name , stock ticker symbol, and the number of shares held, and then have the valuation of their entire portfolio calculated based on the latest (well, 15-minute-delayed) quotes for each stock in the file:

 #!/bin/sh # portfolio - Calculates the value of each stock in your holdings, #   then calculates the value of your overall portfolio, based on #   the latest stock market position. scriptbc="$HOME/bin/scriptbc"   # tweak this as needed portfolio="$HOME/.portfolio" if [ ! -f $portfolio ] ; then   echo "$(basename 
 #!/bin/sh # portfolio - Calculates the value of each stock in your holdings, # then calculates the value of your overall portfolio, based on # the latest stock market position. scriptbc="$HOME/bin/scriptbc" # tweak this as needed portfolio="$HOME/.portfolio" if [ ! -f $portfolio ] ; then echo "$(basename $0): No portfolio to check? ($portfolio)" >&2 exit 1 fi while read holding do eval $(echo $holding  \ awk -F\ '{print "name=\""$1"\"; ticker=\""$2"\"; hold=\""$3"\""}') if [ ! -z "$ticker" ] ; then value="$(getstock $ticker)" totval="$($scriptbc ${value:-0} \* $hold)" echo "$name is trading at $value (your $hold shares = $totval)" sumvalue="$($scriptbc ${sumvalue:-0} + $totval)" fi done < $ portfolio echo "Total portfolio value: $sumvalue" exit 0 
): No portfolio to check? ($portfolio)" >&2 exit 1 fi while read holding do eval $(echo $holding \ awk -F\ '{print "name=\"""\"; ticker=\"""\"; hold=\"""\""}') if [ ! -z "$ticker" ] ; then value="$(getstock $ticker)" totval="$($scriptbc ${value:-0} \* $hold)" echo "$name is trading at $value (your $hold shares = $totval)" sumvalue="$($scriptbc ${sumvalue:-0} + $totval)" fi done < $ portfolio echo "Total portfolio value: $sumvalue" exit 0

How It Works

The getstock script is one of the most straightforward in this chapter. It emulates a method=get query to Lycos Finance and then extracts the value of a single stock specified as the command argument by finding the line in the web page that indicates "Last Price:" and extracting the subsequent price.

The wrapper script portfolio calculates the value of all stocks in a portfolio, using the information stored in the portfolio data file, which is organized as a simple text file with stock name, ticker symbol, and the number of shares held. For parsing simplicity, the data file fields are separated by a symbol, a character that's not likely to show up in a company name. The portfolio script extracts the value of each these fields, calculates the current value of each stock by calling getstock , and then multiplies that by the shares held to ascertain the total value of that stock. Sum them up, and you have the portfolio value.

The eval command on the first line of the while loop in portfolio is the trickiest element of the script:

 eval $(echo $holding  \        awk -F\ '{print "name=\"""\"; ticker=\"""\"; hold=\"""\""}') 

Within the subshell, awk parses a line from the portfolio database, splitting it into three fields, and then outputs them in name = value format. Then the call to eval , within which the awk call is contained, forces the script to evaluate the awk output as if it were entered directly into the shell. For example, for the Apple holdings in the portfolio shown in the next section, the subshell result would be

 name="Apple Computer"; ticker="AAPL"; hold="500" 

Once evaluated by eval , the three variables name , ticker , and hold would then actually be instantiated with the values specified. The rest of the script can then reference these three values by name, without any additional fiddling.

Running the Script

The getstock script isn't intended to be run directly, though given a stock ticker symbol, it'll return the current trading price. The portfolio script requires a separate data file that contains stock name, stock ticker symbol, and the number of shares held. Here's a sample of how that might look:

 $  cat ~/.portfolio  # format is company name, ticker symbol, holdings Apple ComputerAAPL500 Cable & WirelessCWP100 IntelINTC300 Jupiter MediaJUPM50 eBayEBAY200 MicrosoftMSFT200 QualcommQCOM100 

The Results

 $  portfolio  Apple Computer is trading at 22.61 (your 500 shares = 11305.00) Cable & Wireless is trading at 5.63 (your 100 shares = 563.00) Intel is trading at 28.59 (your 300 shares = 8577.00) Jupiter Media is trading at 3.95 (your 50 shares = 197.50) eBay is trading at 55.41 (your 200 shares = 11082.00) Microsoft is trading at 26.52 (your 200 shares = 5304.00) Qualcomm is trading at 41.33 (your 100 shares = 4133.00) Total portfolio value: 41161.50 

Hacking the Script

Obvious areas for improvement would be to add support for overseas exchange holdings, to allow dynamic lookup of ticker symbols by specifying specific stock names , and ” if you're a real gambler who can handle seeing your occasional losses ” to include the original purchase price for each stock as a fourth field in the portfolio file and then compute not only the current portfolio value but the difference in value against the original purchase price of each stock in the portfolio.




Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net