Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
98 lines (79 sloc) 3.3 KB
import as web
import datetime as dt
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.widgets as wd
def plot_data(stkname, fig, topplt, botplt, sidplt):
#Get data from yahoo
#Calculate olling mean, mean and current value of stock
#Also calculate length of data
startdate =, 1, 1)
stkdata = web.DataReader(stkname, 'yahoo', startdate)
stklen = len(stkdata.index)
enddate =[stklen-1])
stkrolmean = pd.ewma(stkdata['Close'], 60)
stkmean = stkdata['Close'].mean(1).round(2)
stkcur = stkdata['Close'][stklen-1]
stkmax = stkdata['Close'].max(1)
stkmin = stkdata['Close'].min(1)
#Decoration for annotation of latest trading value
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
#Clear all axes
#Top plot: Closing data, mean and rolling mean
topplt.plot(stkdata.index, stkdata['Close'], stkdata.index,
stkmean*np.ones(stklen), stkdata.index, stkrolmean,)
topplt.set_title('{} Stock Price from {} to {}'.format(stkname,
startdate, enddate))
topymin, topymax = topplt.get_ylim()
topplt.text(0.05, 0.95, 'Trading price on {}: ${}'.format(enddate,
stkcur), transform=topplt.transAxes, fontsize=14,
verticalalignment='top', bbox=props)
topplt.fill_between(stkdata.index, stkdata['Close'],
(topymin+0.01)*np.ones(stklen), alpha=0.5)
topplt.legend(('Close', 'Mean', 'EWMA'), 'lower right', shadow=True,
fancybox=True, fontsize=8)
#Bottom plot: Bar Graph, trading volume, stkdata['Volume'])
botplt.set_title('{} Trading Volume'.format(stkname))
#Side plot: histogram of 'high-low'
sidplt.hist(stkdata['High']-stkdata['Low'], bins=50, normed=True)
sidplt.set_title('Stock Value Variation')
sidplt.text(0.70, 0.50, '{} Trading Value Stats\nMean:${}\nHighest:${}'
'\nLowest:${}'.format(stkname, stkmean, stkmax, stkmin),
transform=sidplt.transAxes, fontsize=12,
verticalalignment='top', horizontalalignment='center',
#Remove xticklabels on top plot
plt.setp(topplt.get_xticklabels(), visible=False)
return fig
def setup():
#Setup figure
#Top, Bottom, Side with top and bottom plot sharing x axis
fig = plt.figure()
top = plt.subplot(221)
bot = plt.subplot(223, sharex=top)
sid = plt.subplot(122)
stklst = sorted(('AMZN', 'GE', 'GOOG', 'MSFT', 'YHOO', 'EBAY'))
fig = plot_data(stklst[0], fig, top, bot, sid)
#Setup for radio bottoms
axcolor = 'lightgoldenrodyellow'
ylen = len(stklst)/50.0
prop_radio = plt.axes([0.95, 1-ylen, 0.048, ylen], axisbg=axcolor)
radio = wd.RadioButtons(prop_radio, stklst)
return [fig, top, bot, sid, radio]
if __name__ == "__main__":
fig, top, bot, sid, radio = setup()
#Setup multicursor between top and bottom plot
multi = wd.MultiCursor(fig.canvas, (top, bot), color='r', lw=2)
def stocksel(label):
plot_data(label, fig, top, bot, sid)
#Show plot