Skip to content

Instantly share code, notes, and snippets.

@npalko
Created December 29, 2011 02:09
Show Gist options
  • Select an option

  • Save npalko/1531168 to your computer and use it in GitHub Desktop.

Select an option

Save npalko/1531168 to your computer and use it in GitHub Desktop.
Yahoo IChart
#!/usr/bin/env python
import csv
import datetime
import decimal
import urllib
def get(symbol, start, end, frequency='D'):
controller = 'http://ichart.yahoo.com/table.csv?'
param = {
's' : symbol,
'a' : start.month - 1,
'b' : start.day,
'c' : start.year,
'd' : end.month - 1,
'e' : end.day,
'f' : end.year,
'g' : frequency,
}
def format(i):
for k in ('Adj Close','High','Low','Close','Open'):
i[k] = decimal.Decimal(i[k])
i['Volume'] = int(i['Volume'])
i['Date'] = datetime.datetime.strptime(i['Date'],'%Y-%m-%d').date()
return i
# GET, not POST required by Yahoo! API
it = urllib.urlopen(controller + urllib.urlencode(param))
it = (format(i) for i in csv.DictReader(it))
return it
def demo():
end = datetime.date.today()
start = end - datetime.timedelta(days=10)
it = get('GOOG',start,end,'D')
for i in it:
print i
if __name__ == '__main__':
demo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment