Created
October 7, 2013 08:52
-
-
Save alcedo/6864697 to your computer and use it in GitHub Desktop.
Yahoo python downloader wrapper. Refer to documentations: http://victorliew.quora.com/3-Ways-to-programatically-download-stuff-from-Yahoo-Financials (YQL, Yahoo Finance)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import os | |
| import time | |
| from datetime import datetime | |
| import urllib2 | |
| """ | |
| Use this function to allow applications to communicate via HTTP to the internet | |
| """ | |
| def setProxy(): | |
| #configure Credit- Suisse specific Agent here. | |
| internal_proxy_host = 'http://myproxy.com:8080' | |
| internal_proxy_protocol = 'http' | |
| proxy = urllib2.ProxyHandler({internal_proxy_protocol : internal_proxy_host}) | |
| opener = urllib2.build_opener(proxy) | |
| opener.addheaders = [('User-agent', 'Mozilla/5.0')] | |
| return opener | |
| """ | |
| This function downloads historical data for a particular ticker | |
| sample usage: get_historical_data("AAPL", datetime.strptime("20 Nov 00", "%d %b %y"), datetime.strptime("20 Nov 13", "%d %b %y")) | |
| """ | |
| def get_historical_data(symbol,fromDate, toDate): | |
| urlQuery = 'http://ichart.yahoo.com/table.csv?a='+ str(fromDate.month -1) +'&c='+ str(fromDate.year) +'&b=' + str(fromDate.day) + '&e='+ str(toDate.day) + '&d='+ str(toDate.month-1) +'&g=d&f=' + str(toDate.year) + '&s=' + symbol + '&ignore=.csv' | |
| print "Query: " + urlQuery | |
| opener = setProxy() | |
| f = opener.open(urlQuery).read() | |
| print f | |
| """ | |
| This function Get spot price + earnings per share. | |
| for other details, refer to: http://www.gummy-stuff.org/Yahoo-data.htm | |
| """ | |
| def get_spot_data(symbol): | |
| numberOfAttempts = 0 | |
| urlQuery = 'http://download.finance.yahoo.com/d/quotes.csv?s='+symbol+'&f=l1e&e=.cs' | |
| print "Query: " + urlQuery | |
| opener = setProxy() | |
| data = opener.open(urlQuery).read() | |
| print symbol + "," + data | |
| """ | |
| Extract tickers from file list. format of file should be: | |
| <header in first line> | |
| <ticker1><seperatpr><bla bla data> | |
| <ticker2><seperatpr><bla bla data> | |
| """ | |
| def read_ticker_from_file(file_path, seperator): | |
| file = open(file_path) | |
| file.readline() #ignore header | |
| tickers = [] | |
| for line in file: | |
| data = line.split(seperator) | |
| tickers.append(data[0]) | |
| return tickers | |
| """ | |
| Program entry point goes here. | |
| """ | |
| def main(argv=None): | |
| print "Running Script " + sys.argv[0] | |
| #set ticker input file path | |
| ticker_file_path = "//gbl.ad.hedani.net//home_ap$//jliew4$//Desktop//QuantResearchPlatform//public//python downloader//NASDAQ_TICKER.txt" | |
| ticker_list = read_ticker_from_file(ticker_file_path, '|') | |
| count = 1 | |
| for ticker in ticker_list: | |
| get_spot_data(ticker) | |
| # test top 10 tickers for now. | |
| if count > 10: | |
| break | |
| count += 1 | |
| return 0 | |
| if __name__ == "__main__": | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment