Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save clintmjohnson/2ab673ba31489e6aa0ef694b9d9f892e to your computer and use it in GitHub Desktop.
Save clintmjohnson/2ab673ba31489e6aa0ef694b9d9f892e to your computer and use it in GitHub Desktop.
Historic Stock Price Data Download to CSV
import pandas as pd
import io
import requests
import time
def google_stocks(symbol, startdate=(1, 1, 2005), enddate=None):
startdate = str(startdate[0]) + '+' + str(startdate[1]) + '+' + str(startdate[2])
if not enddate:
enddate = time.strftime("%m+%d+%Y")
else:
enddate = str(enddate[0]) + '+' + str(enddate[1]) + '+' + str(enddate[2])
stock_url = "http://www.google.com/finance/historical?q=" + symbol + \
"&startdate=" + startdate + "&enddate=" + enddate + "&output=csv"
raw_response = requests.get(stock_url).content
stock_data = pd.read_csv(io.StringIO(raw_response.decode('utf-8')))
return stock_data
if __name__ == '__main__':
stocks = ['NERV','NDRM','ADRO','COLL','ZEAL','TOCA','ATNX']
for stock in stocks:
stockdata = google_stocks(stock)
stockdata.to_csv('Historical_Stock.csv',mode='a',header=False)
#with open('Historical_Stock.csv','a') as f:
# stockdata.to_csv(f,header=False)
# print(stockdata)
#apple_truncated = google_stocks(, enddate=(1, 1, 2006))
#print(apple_truncated)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment