In [22]:
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
import io
import requests
import time
import seaborn as sns
import string
In [3]:
##This function is modified to retrived most historical stock data from NASDAQ and NYSX
##This function cannot retrieve historical index data since Google Finance does not support downding this type of data
##This function return dataframe
month_exe = {1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:'Jun',7:'Jul',8:'Aug',9:'Sep',10:'Oct',11:'Nov',12:'Dec'}
def google_stocks(symbol, startdate, enddate = None):
startdate = month_exe[startdate[0]] + '%20' + str(startdate[1]) + '%2C%20' + str(startdate[2])
enddate = month_exe[enddate[0]] + '%20' + str(enddate[1]) + '%2C%20' + str(enddate[2])
stock_url = 'https://finance.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
In [5]:
#https://finance.google.com/finance/historical?q=AAPL&startdate=Jul%201%2C%202016&enddate=Jul%201%2C%202017&output=csv
In [143]:
startdate = (7,1,2016)
enddate = (7,1,2017)
In [144]:
Symbol = ['NASDAQ:GOOG','NYSE:UVV','NYSE:BP','NYSE:CLD','NYSE:BHP','NYSE:RIO']
In [145]:
for i in Symbol:
symbol = i
df_name = symbol.split(':')[1] + '_hist'
exec('{} = google_stocks(symbol, startdate, enddate)'.format(df_name))
In [146]:
BP_hist.iloc[0:10]
Out[146]:
In [147]:
BP_hist.Date = pd.to_datetime(BP_hist.Date)
In [148]:
BP_hist.Low.iloc[0:10]
Out[148]:
In [158]:
goog = sns.tsplot(GOOG_hist.Close)
In [159]:
uvv = sns.tsplot(UVV_hist.Close)
In [204]:
for i in ['CLD','BHP','RIO']:
exec('{}_hist["company"] = i'.format(i))
In [205]:
coal_mining = CLD_hist.append(BHP_hist).append(RIO_hist)
In [221]:
coal_mining.head()
Out[221]:
In [234]:
cld = sns.tsplot(CLD_hist.Close)
In [141]:
bhp = sns.tsplot(BHP_hist.Close)
In [142]:
rio = sns.tsplot(RIO_hist.Close)
In [1]:
#https://finance.google.com/finance/historical?q=INDEXSP:.INX&startdate=Jul+1%2C+2016&enddate=Jul+1%2C+2017&output=csv