In [168]:
# Get FTSE100 index back from 01-Jan-1999
import html5lib
base_url = 'https://www.google.com/finance/historical?cid=12590587&startdate;=Jan%201%2C%201999&' + \
'enddate=Oct%2015%2C%202016#=200&ei;=qVQCWMmRHMf7UKqMn7AM&start;='
ftse = pd.DataFrame()
for page in range(0,85,200): #4485
url = base_url + str(page)
print('getting- ', url[:50] + '...' + url[110:])
ftse = ftse.append(pd.read_html(url, flavor='html5lib', attrs={'class': 'gf-table historical_price'},
parse_dates=[0], header=0)[0])
ftse.set_index('Date')
In [182]:
ftse.head()
Out[182]:
In [56]:
%matplotlib inline
import quandl
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
np.set_printoptions(supress=True)
pd.set_option('display.float_format', lambda x: '%.4f' % x)
barclays = quandl.get("GOOG/LON_BARC", authtoken="TMMe4w92TaVmnEgC1D6z")
barclays['Gain'] = barclays['Close'] - barclays['Open']
barclays['Day'] = barclays.index.dayofweek
barclays = barclays[(barclays.Volume!=0) & (barclays.Day!=5)]
barclays.head()
Out[56]:
In [ ]:
plt.figure(figsize=(20,4))
plt.subplot(1,2,1)
barclays.Close.plot()
ftse.Close.plot()
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [4]:
sns.kdeplot(barclays['Close']);
In [5]:
sns.distplot(barclays['Close'])
Out[5]:
In [6]:
sns.lmplot('Open', 'Close', data=barclays, fit_reg=False)
Out[6]:
In [67]:
barclays_gr = barclays.groupby('Day')
In [75]:
barclays_gr.Gain.mean()
Out[75]:
In [109]:
plt.figure(figsize=(14,4))
plt.subplot(1,2,1)
barclays_gr.Gain.mean().plot(kind='bar')
plt.subplot(1,2,2)
barclays_gr.Volume.mean().plot(kind='bar')
Out[109]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [116]:
import pandas_datareader.data as web
import datetime
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2013, 1, 27)
f1 = web.DataReader("GOOG", 'yahoo', start, end)
#f2 = web.DataReader("INDEXFTSE:UKX", 'google', start, end)
In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
t = np.linspace(0, 5, 100)
x = t + np.random.normal(size=100)
xde = signal.detrend(x)
diff = x - xde
plt.plot(t, x, linewidth=1)
plt.plot(t, xde, linewidth=1)
plt.plot(t, diff, linewidth=1)
Out[1]:
In [ ]:
%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.animation as animation
import requests
import time
import datetime as dt
from IPython.display import display
from ipywidgets import widgets
def get_price(ticker):
resp = requests.get(r"http://finance.yahoo.com/webservice/v1/symbols/{}/quote?format=json" \
.format(ticker), headers={'User-Agent': 'Mozilla/5.0'}).json()
return float(resp['list']['resources'][0]['resource']['fields']['price'])
def animate(frame):
global x, y
x.append( dt.datetime.now() )
price = get_price(ticker)
y.append( price)
ax.set_xlim([max(x) - dt.timedelta(seconds=65), max(x)])
ax.set_ylim([min(y) * 0.99999, max(y) * 1.00001])
line.set_data(x, y)
message = '{} {}'.format(ticker, price)
widget.value = '{} => {}'.format(dt.datetime.now(), message)
time.sleep(2)
return line,
ticker = 'GBPUSD=X'
fig = plt.figure(num='Yahoo Finance Ticker: '+ticker)
ax = plt.axes()
ax.grid(True)
ax.get_yaxis().get_major_formatter().set_useOffset(False)
fig.autofmt_xdate()
time_locator = mdates.SecondLocator(interval=5)
time_fmt = mdates.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_locator(time_locator)
ax.xaxis.set_major_formatter(time_fmt)
line, = ax.plot([], [], lw=1)
x=[]
y=[]
widget = widgets.HTML(value='')
display(widget)
anim = animation.FuncAnimation(fig, animate, repeat=False, frames=30, interval=50, blit=False)
plt.show()
Comments
comments powered by Disqus