Andrew Channels Dexter Pinion

Wherein I write some stuff that you may like to read. Or not, its up to you really.

July 19, 2004

Acquiring Stock Prices and Exchange Rates

Via a convoluted series of links I came across Ruby/Finance, an in progress port of Perl's Finance::Quote module. My immediate reaction was that this is a module which could usefully be ported to Python. I even entertained the thought of doing it myself, it would be an interesting exercise to learn a bit more about Perl and Ruby. But given my (lack of) progress on the other items on my to-do list it would never get done.

It wouldn't scratch my itch in the short term, either, as I already have perfectly serviceable Python functions to do these things for me. Unlike the Perl and Python code they don't go directly to Yahoo! Finance (the currency converter actually parses the HTML - yeuch) but rely on a SOAP service published by the nice people at (who do the scraping for us).

To get an exchange rate between currencies give this one a whirl;

def getFXRate(currencyFrom, currencyTo):
    "Get conversion rate between <currencyFrom> and <currencyTo>"
    rate = -1
        server = SOAP.SOAPProxy('', \
        rate = server.getRate(currencyFrom, currencyTo)
        print "Exception getting rate between %s and %s" % ( currencyFrom, currencyTo )
    return rate

To get the latest price of a stock, try this function;

def getQuote(symbol):
    "Get latest price for <symbol>"
    price = -1
        server = SOAP.SOAPProxy('', \
        price = server.getQuote(symbol)
        print "Exception getting price for %s" % symbol
    return price

For the functions to work you will need the module from the Python Web Services project.

Posted by Andy Todd at July 19, 2004 09:48 PM


It would seem the fragments don't match their respective introductions.

Handy code though!

Posted by: Richard on July 20, 2004 04:40 AM

You're using an old SOAPpy. SOAPProxy is used as follows:

from SOAPpy import SOAPProxy

Second problem - the service appears to be broken ;)


Posted by: Richard on July 20, 2004 06:07 AM

Richard, thanks for the pointers. I've switched the descriptions round. I use rather than SOAPpy (which is available from CVS) simply because it works.

The service isn't 100%, but it is the web and there are no guarantees of service.

Posted by: Andy Todd on July 20, 2004 09:56 AM

Actually, the version of I use is from the Debian package ( )

Just thought I'd clear that up.

Posted by: Andy Todd on July 20, 2004 01:32 PM

note that yahoo finance exports data as CSV, so parsing html shouldn't be necessary

Posted by: rayg on July 21, 2004 04:30 AM

Ray, indeed they do, but only stock information. The scraping I mentioned is to get currency rates.

Posted by: Andy Todd on July 21, 2004 08:46 AM