Wednesday, April 29, 2015

Getting stock price and exchange rate from Yahoo by Python



#download.py gets stock and exchange rates from Yahoo and then saves the data as a csv file in local drive.
#You may change the url and the filepath if necessary.
#Author: David Tsang 28 APR 2015

import urllib

#historical data
def downloadFile(symbol, filename):
    url = "http://real-chart.finance.yahoo.com/table.csv?s=" + symbol
    directory = "C:\\Python27\\Lib\\site-packages\\QSTK\\QSData\\Yahoo\\"
    print "Downloading " + symbol
    filepath = directory + filename + ".csv"
    urllib.urlretrieve (url, filepath)
    print "- Completed"

#latest quote
def quote(symbol, filepath):
    url = "http://download.finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=sl1d1t1c1ohgv&e=.csv"
    #print "Downloading " + symbol
    urllib.urlretrieve (url, filepath)

#exchange rate
def exchrate(symbol, filepath):
    url = "http://download.finance.yahoo.com/d/quotes.csv?s=" + symbol +"=X&f=sl1d1t1c1ohgv&e=.csv"
    print "Downloading " + symbol
    urllib.urlretrieve (url, filepath)

No comments:

Post a Comment

Applying SMA10/20, SMA20/50 as trading signals

This is the comparison for results before and after applying SMA10/20 and SMA20/50 in the stock trader. Background Trading 3 stock ma...