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 www.xmethods.net (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
    try:
        server = SOAP.SOAPProxy('http://services.xmethods.net/soap', \
                         namespace='urn:xmethods-CurrencyExchange')
        rate = server.getRate(currencyFrom, currencyTo)
    except:
        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
    try:
        server = SOAP.SOAPProxy('http://services.xmethods.net/soap', \
                        namespace='urn:xmethods-delayed-quotes')
        price = server.getQuote(symbol)
    except:
        print "Exception getting price for %s" % symbol
    return price

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

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

Comments

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
...
SOAPProxy(...)

Second problem - the service appears to be broken ;)

java.lang.NullPointerException
at net.xmethods.services.currencyexchange.CurrencyExchange.getRate(CurrencyExchange.java:202)

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

Richard, thanks for the pointers. I've switched the descriptions round. I use SOAP.py 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 SOAP.py I use is from the Debian package ( http://packages.debian.org/testing/python/python-soappy )

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