Quote.Fetching (Historical)
Google Historical Data {US & HK/US}
http://ichart1.finance.vip.sp1.yahoo.com/table.csv?a=10&b=3&c=2008&q=q&y=0&z=file&x=.csv&s=%200005.HK HK/US data -> Daily, >200 days, csv file [table.csv] <-- slow, 1 day delay!! a=start-month {10, 0=Jan} b=start-day {3} c=start-year {2008} d=end-month{10, 0=Jan} e=end-day{3} f=end-year{2008} OUTFILE Date,Open,High,Low,Close,Volume,Adj Close 2008-11-06,92.10,92.20,89.80,90.65,21517100,90.65 2008-11-05,96.00,96.45,92.80,94.95,20609200,94.95 2008-11-04,93.00,94.25,90.00,92.60,18837600,92.60 2008-11-03,93.00,95.00,92.25,92.50,23126000,92.50 http://finance.google.com/finance/historical? q=YHOO&startdate=Nov+6%2C+2008&enddate=Nov+8%2C+2008&histperiod=daily&output=csv US data -> Daily, >200 days, csv file [data.csv] {BUG??} http://finance.google.com/finance/historical? q=HKG:0005&output=.csv&start=0&num=200 HK/US data -> Daily, latest 200 day max (or change start=201,401), HTML histperiod=daily weekly startdate=Jan++2%2C+1970 enddate=May+10%2C+2007 output=csv OUTFILE Date,Open,High,Low,Close,Volume 7-Nov-08,12.45,12.50,11.65,12.20,47293147 6-Nov-08,14.84,14.89,13.75,13.96,44566410 5-Nov-08,13.21,14.84,13.15,13.92,71290756 Example: http://finance.google.com/finance/historical?q=YHOO&output=csv US data only -> Daily, 1 year period, data.csv file Yahoo! Historical Data {US/HK} ichart1.finance.vip.sp1.yahoo.com (69.147.86.173) URI: /table.csv?s=%200005.HK&a=10&b=3&c=2008&d=10&e=8&f=2008&g=d&q=q&y=0&z=file&x=.csv Download Historical Quotes from Yahoo! http://ichart.finance.yahoo.com/table.csv?s=INTC&a=06&b=9&c=1986&d=2&e=5&f=2008&g=d The URL string above will download daily historical data for INTC (Intel) from 6th of July 1986 (Intel went IPO) until 5th March 2008 into a file call table.csv. http://ichart.finance.yahoo.com/table.csv - The default URL to download historical stock quotes, it won't work if you change the 'table.csv' to something else. s - This is where you can specify your stock quote, if you want to download stock quote for Microsoft, just enter it as 's=MSFT' a - This parameter is to get the input for the start month. '00' is for January, '01' is for February and so on. b - This parameter is to get the input for the start day, this one quite straight forward, '1' is for day one of the month, '2' is for second day of the month and so on. c - This parameter is to get the input for the start year d - This parameter is to get the input for end month, and again '00' is for January, '02' is for February and so on. e - This parameter is to get the input for the end day f - This parameter is to get the input for the end year g - This parameter is to specify the interval of the data you want to download. 'd' is for daily, 'w' is for weekly and 'm' is for monthly prices. The default is 'daily' if you ignore this parameter. With all the parameters above, you can now construct a URL to download historical prices for any stock quotes you want. But if you are going to download all historical prices for a stock quotes from day one onward (eg: Intel), you don't need to crack your head to look for information such as when is Intel went IPO. You just need to ignore the start and end date as follow: eg: http://ichart.finance.yahoo.com/table.csv?s=INTC If you only specify the start date and ignore the end date, it will download everything right from the start date until the most current prices. eg: http://ichart.finance.yahoo.com/table.csv?s=INTC&a=00&b=1&c=2000 Destination: 66.96.133.7 (66.96.133.7) GET /download/mklist/HONGKONG.txt HTTP/1.1\r\n Interesting site with many Excel template http://www./content/view/27/40/ http://itrade./ Python - Google fetch {run on Google Apps} --------------------- import urllib from datetime import datetime from threading import Thread from Queue import Queue base_url="http://ichart.finance.yahoo.com/table.csv?" def get_historical(symbols,start=None,end=None,threads=0): if isinstance(symbols,str): return get_historical_single(symbols,start,end) quotes={} if threads: def quoter(): while True: data = q.get() quotes[data[0]]=get_historical_single(data[0],data[1],data[2]) q.task_done() q = Queue() for i in range(threads): t = Thread(target=quoter) t.setDaemon(True) t.start() for sym in symbols: q.put((sym,start,end)) q.join() else: for sym in symbols: quotes[sym]=get_historical_single(sym,start,end) return quotes def get_historical_single(symbol,start=None,end=None): full_url=base_url+"&s="+symbol if start: full_url+="&a=%i&b=%i&c=%i"%(start.month-1,start.day,start.year) if end: full_url+="&d=%i&e=%i&f=%i"%(end.month-1,end.day,end.year) full_url+="&g=d" quotes={} quotes['raw']=[] quotes['by_date']={} quotes['dates']=[] quotes['opens']=[] quotes['highs']=[] quotes['lows']=[] quotes['closes']=[] quotes['volumes']=[] quotes['adjusted_closes']=[] quotes_lines=urllib.urlopen(actual_url).read().split('\n')[1:-1] for quote_line in quotes_lines: #quote_line structure: Date,Open,High,Low,Close,Volume,Adj Close splt_q=quote_line.split(',') date=datetime(*(map(int,splt_q[0].split('-')))) op=float(splt_q[1]) hi=float(splt_q[2]) lo=float(splt_q[3]) close=float(splt_q[4]) vol=int(splt_q[5]) adj_close=float(splt_q[6]) quote=dict(date=date,open=op,high=hi,low=lo,close=close,volume=vol,adj_close=adj_close) quotes['raw'].append(quote) quotes['by_date'][date]=quote quotes['dates'].append(date) quotes['opens'].append(op) quotes['highs'].append(hi) quotes['lows'].append(lo) quotes['closes'].append(close) quotes['volumes'].append(volume) quotes['adjusted_closes'].append(adj_close) return quotes if __name__ == '__main__': start_date=datetime(2005,1,1) symbols=['F.MI','AAPL','IBM','GOOG'] quotes=get_historical(symbols,start_date=start_date,threads=4) for k in symbols: |
|
来自: Tim You > 《My.Finance.Site》