import pandas as pd
from bokeh.charts import TimeSeries, show, output_file, vplot, output_notebook
output_notebook()
# read in the S&P 500 Index Fund from the Yahoo Finance API
GSPC = pd.read_csv("https://ichart.yahoo.com/table.csv?s=^GSPC",
parse_dates=['Date'])
FBIDX = pd.read_csv("https://ichart.yahoo.com/table.csv?s=FBIDX",
parse_dates=['Date'])
NDX = pd.read_csv("https://ichart.yahoo.com/table.csv?s=^NDX",
parse_dates=['Date'])
SO = pd.read_csv("https://ichart.yahoo.com/table.csv?s=SO",
parse_dates=['Date'])
# add column data for correct printing of dates in hovertool
GSPC['Str Date'] = GSPC['Date'].map(lambda x: x.strftime('%Y-%m-%d'))
FBIDX['Str Date'] = FBIDX['Date'].map(lambda x: x.strftime('%Y-%m-%d'))
NDX['Str Date'] = NDX['Date'].map(lambda x: x.strftime('%Y-%m-%d'))
SO['Str Date'] = SO['Date'].map(lambda x: x.strftime('%Y-%m-%d'))
# show sample data
print('GSPC')
print(GSPC.head())
print()
print('FBIDX')
print(FBIDX.head())
print()
print('NDX')
print(NDX.head())
print()
print('SO')
print(SO.head())
GSPC Date Open High Low Close Volume \ 0 2016-05-25 2078.929932 2094.729980 2078.929932 2090.540039 3859160000 1 2016-05-24 2052.649902 2079.669922 2052.649902 2076.060059 3627340000 2 2016-05-23 2052.229980 2055.580078 2047.260010 2048.040039 3055480000 3 2016-05-20 2041.880005 2058.350098 2041.880005 2052.320068 3507650000 4 2016-05-19 2044.209961 2044.209961 2025.910034 2040.040039 3846770000 Adj Close Str Date 0 2090.540039 2016-05-25 1 2076.060059 2016-05-24 2 2048.040039 2016-05-23 3 2052.320068 2016-05-20 4 2040.040039 2016-05-19 () FBIDX Date Open High Low Close Volume Adj Close Str Date 0 2016-05-25 11.74 11.74 11.74 11.74 0 11.74 2016-05-25 1 2016-05-24 11.75 11.75 11.75 11.75 0 11.75 2016-05-24 2 2016-05-23 11.76 11.76 11.76 11.76 0 11.76 2016-05-23 3 2016-05-20 11.76 11.76 11.76 11.76 0 11.76 2016-05-20 4 2016-05-19 11.76 11.76 11.76 11.76 0 11.76 2016-05-19 () NDX Date Open High Low Close Volume \ 0 2016-05-25 4459.259766 4488.520020 4453.049805 4476.430176 1784420000 1 2016-05-24 4381.339844 4449.209961 4380.459961 4444.299805 1942280000 2 2016-05-23 4364.729980 4382.399902 4353.359863 4355.100098 1865110000 3 2016-05-20 4330.069824 4379.979980 4329.569824 4362.899902 1970520000 4 2016-05-19 4319.859863 4331.580078 4284.370117 4315.580078 1805890000 Adj Close Str Date 0 4476.430176 2016-05-25 1 4444.299805 2016-05-24 2 4355.100098 2016-05-23 3 4362.899902 2016-05-20 4 4315.580078 2016-05-19 () SO Date Open High Low Close Volume Adj Close \ 0 2016-05-25 48.419998 48.770000 48.189999 48.549999 3825300 48.549999 1 2016-05-24 48.310001 48.540001 48.049999 48.459999 4159200 48.459999 2 2016-05-23 48.759998 48.830002 48.070000 48.090000 3789000 48.090000 3 2016-05-20 48.820000 48.860001 48.380001 48.650002 4090000 48.650002 4 2016-05-19 47.990002 48.689999 47.619999 48.669998 4039400 48.669998 Str Date 0 2016-05-25 1 2016-05-24 2 2016-05-23 3 2016-05-20 4 2016-05-19
data = dict(
Date = GSPC['Date'],
GSPC = GSPC['Adj Close'],
FBIDX = FBIDX['Adj Close'],
NDX = NDX['Adj Close'],
SO = SO['Adj Close'],
SDate = GSPC['Str Date'],
)
print(type(data['Date'][0]))
print(type(data['GSPC'][0]))
<class 'pandas.tslib.Timestamp'> <type 'numpy.float64'>
from bokeh.models import HoverTool
hover = HoverTool(
tooltips = [
("Date", "@SDate"),
("Adj Close", " $y{$0.00}"),
]
)
ts_stocks = TimeSeries(data,
x = 'Date', y = ['GSPC', 'FBIDX', 'NDX', 'SO'],
color = ['cadetblue', 'firebrick', 'limegreen', 'hotpink'],
tools=['box_zoom', 'pan', 'save', hover, 'resize', 'reset', 'wheel_zoom'],
title = 'Stock Time Series',
ylabel = 'Stock Prices', legend=True
)
show(ts_stocks)
<bokeh.io._CommsHandle at 0x7f061217add0>