In [45]:
from pandas_datareader import data, wb
import pandas as pd
import numpy as np
import datetime
%matplotlib inline
from ipywidgets import interactive
from IPython.display import Audio, display
In [46]:
# Widget related imports
import ipywidgets as widgets
from IPython.display import display, clear_output, Javascript
from traitlets import Unicode

# nbconvert related imports
from nbconvert import get_export_names, export_by_name
from nbconvert.writers import FilesWriter
from nbformat import read, NO_CONVERT
from nbconvert.utils.exceptions import ConversionException
In [47]:
notebook_name = widgets.Text()
In [48]:
js = """IPython.notebook.kernel.widget_manager.get_model('%s').then(function(model) {
    model.set('value', IPython.notebook.notebook_name);
    model.save();
});
""" % notebook_name.model_id
display(Javascript(data=js))
In [49]:
filename = notebook_name.value
filename
Out[49]:
'Financial analysis.ipynb'
In [50]:
exporter_names = widgets.Dropdown(options=get_export_names(), value='html')
export_button = widgets.Button(description="Export")
download_link = widgets.HTML(visible=False)
In [51]:
start = datetime.datetime(2006, 1, 1)
end = datetime.datetime(2016, 12, 1)
In [52]:
start
Out[52]:
datetime.datetime(2006, 1, 1, 0, 0)
In [53]:
#Bank of America
BAC = data.DataReader("BAC", 'google', start, end)

# CitiGroup
C = data.DataReader("c", 'google', start, end)

#Goldman sachs
GS = data.DataReader("GS", 'google', start, end)

#JPMorgan Chase
JPM = data.DataReader("JPM", 'google', start, end)

# Morgan Stanley
MS = data.DataReader("MS", 'google', start, end)

# Wells Fargo
WFC = data.DataReader("WFC", 'google', start, end)
In [54]:
# Could also do this for a Panel Object
df = data.DataReader(['BAC', 'C', 'GS', 'JPM', 'MS', 'WFC'],'google', start, end)
In [55]:
tickers = ['BAC', 'C', 'GS', 'JPM', 'MS', 'WFC']
In [56]:
tickers
Out[56]:
['BAC', 'C', 'GS', 'JPM', 'MS', 'WFC']
In [57]:
bank_stocks = pd.concat([BAC, C, GS, JPM, MS, WFC],axis=1)
In [58]:
bank_stocks
Out[58]:
Open High Low Close Volume Open High Low Close Volume ... Open High Low Close Volume Open High Low Close Volume
Date
2006-01-03 46.92 47.18 46.15 47.08 16296700 490.00 493.80 481.10 492.90 1537660 ... 57.17 58.49 56.74 58.31 5377000 31.60 31.98 31.20 31.90 11016400
2006-01-04 47.00 47.24 46.45 46.58 17757900 488.60 491.00 483.50 483.80 1871020 ... 58.70 59.28 58.35 58.35 7977800 31.80 31.82 31.36 31.53 10871000
2006-01-05 46.58 46.83 46.32 46.64 14970900 484.40 487.80 484.00 486.20 1143160 ... 58.55 58.59 58.02 58.51 5778000 31.50 31.56 31.31 31.50 10158000
2006-01-06 46.80 46.91 46.35 46.57 12599800 488.80 489.00 482.00 486.20 1370250 ... 58.77 58.85 58.05 58.57 6889800 31.58 31.78 31.38 31.68 8403800
2006-01-09 46.72 46.97 46.36 46.60 15620000 486.00 487.40 483.00 483.90 1680740 ... 58.63 59.29 58.62 59.19 4144500 31.68 31.82 31.56 31.68 5619600
2006-01-10 46.40 46.51 45.88 46.21 15634800 483.00 485.50 480.80 485.40 1365960 ... 58.85 59.22 58.58 59.22 4713600 31.55 31.68 31.44 31.65 7157800
2006-01-11 46.06 46.25 45.75 46.10 14745100 495.80 495.80 485.80 489.80 1684440 ... 59.15 59.88 58.84 59.57 5837900 31.65 31.90 31.58 31.82 8049600
2006-01-12 46.22 46.23 45.71 45.80 10546600 491.00 491.00 488.80 490.30 1230060 ... 59.63 59.78 59.27 59.47 3817700 31.70 31.79 31.43 31.44 10763600
2006-01-13 45.83 46.00 45.68 45.80 10791300 491.00 491.90 487.30 489.20 940930 ... 59.57 59.67 59.06 59.38 3430900 31.55 31.80 31.51 31.62 6366000
2006-01-17 45.40 45.58 45.00 45.31 14606100 485.10 487.00 482.70 484.30 1237830 ... 59.45 59.46 58.68 58.81 2624700 31.46 31.58 31.20 31.30 10690600
2006-01-18 45.32 45.65 44.92 45.18 10911300 484.30 486.70 481.10 483.80 1218910 ... 58.55 58.90 58.05 58.58 2904300 31.30 31.84 31.30 31.67 10798000
2006-01-19 45.88 45.88 44.85 45.14 14584000 485.60 485.80 477.00 479.40 1696500 ... 58.75 59.98 58.71 59.35 3813000 31.80 31.82 31.27 31.30 8713400
2006-01-20 45.00 45.15 44.17 44.19 25164400 472.10 474.00 456.30 456.90 4781930 ... 59.30 59.55 57.41 57.85 5446100 31.31 31.31 30.75 30.75 18175400
2006-01-23 43.50 44.39 43.49 43.96 23459900 460.00 463.80 457.00 460.00 2025500 ... 57.70 58.69 57.70 58.48 3801000 30.76 30.97 30.75 30.80 7047400
2006-01-24 44.08 44.68 44.04 44.24 13500300 462.90 463.60 459.90 460.10 2083740 ... 58.82 59.45 58.62 58.69 3813600 30.82 30.98 30.66 30.80 8232400
2006-01-25 44.15 44.65 44.15 44.63 15132000 461.40 463.70 460.10 462.30 1591940 ... 59.00 59.42 58.55 59.17 4011700 30.84 31.06 30.73 31.00 8580800
2006-01-26 45.00 45.16 44.67 44.90 18449500 465.50 475.50 464.50 470.10 1988600 ... 61.11 60.50 59.49 59.97 5790400 31.24 31.50 31.18 31.28 8598000
2006-01-27 44.90 45.11 44.61 44.80 12250800 470.10 473.70 466.00 468.70 1412760 ... 60.00 61.00 59.79 60.78 3700100 31.28 31.60 31.04 31.35 7640000
2006-01-30 44.75 45.35 44.25 44.48 14816000 468.70 469.90 466.60 468.20 1057630 ... 61.45 61.85 60.64 60.67 4246600 31.35 31.36 31.15 31.24 5756400
2006-01-31 44.65 44.73 44.12 44.23 19151000 468.30 470.50 465.50 465.80 1887280 ... 60.50 61.91 60.49 61.45 5178400 31.12 31.30 31.10 31.18 11763400
2006-02-01 44.10 44.24 43.86 43.95 15854800 465.90 467.20 461.10 463.30 1844970 ... 61.10 61.96 60.89 61.83 3586000 30.95 30.98 30.75 30.92 8224400
2006-02-02 43.95 43.97 43.37 43.44 14745300 459.00 461.00 451.00 451.80 2325470 ... 61.65 62.14 60.85 61.27 3469800 30.82 30.90 30.60 30.65 9831600
2006-02-03 43.30 43.55 42.75 43.09 17097100 450.70 456.10 448.10 450.60 1666510 ... 60.60 62.15 60.36 61.44 3961600 30.42 30.72 30.31 30.52 7532400
2006-02-06 43.40 43.65 43.24 43.37 12554600 452.60 456.10 450.90 451.70 1147430 ... 61.26 61.91 61.23 61.73 2629500 30.42 30.60 30.34 30.52 7290000
2006-02-07 43.60 43.87 43.46 43.49 13917200 452.00 453.80 450.00 450.50 1207780 ... 61.35 61.54 60.27 60.53 3811900 30.50 30.68 30.45 30.56 8097000
2006-02-08 43.55 43.72 43.34 43.67 12564600 453.30 455.30 450.70 453.60 1051370 ... 61.15 61.36 60.80 61.27 2719000 30.50 30.72 30.40 30.66 5928000
2006-02-09 43.75 44.05 43.70 43.83 13766200 455.00 461.00 454.30 457.90 1357740 ... 61.27 61.90 61.10 61.18 2714000 30.60 30.82 30.50 30.62 7052600
2006-02-10 43.77 44.02 43.41 43.92 9039000 457.00 460.70 452.50 459.60 1272030 ... 61.00 61.14 59.38 60.31 4483600 30.64 30.97 30.57 30.86 6501200
2006-02-13 43.70 43.96 43.30 43.70 8329500 460.60 462.30 454.10 456.80 1158300 ... 60.20 60.58 59.39 59.81 4104900 30.78 30.88 30.60 30.82 5401400
2006-02-14 43.92 44.49 43.73 44.16 17049500 457.80 462.50 457.10 461.20 1518040 ... 59.81 60.46 59.21 60.07 3715100 30.95 31.58 30.89 31.32 8263600
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2016-10-20 16.45 16.63 16.41 16.56 88081209 49.46 49.90 49.21 49.58 13522551 ... 32.84 33.19 32.58 32.90 10650671 44.97 45.29 44.66 44.93 27210558
2016-10-21 16.46 16.67 16.40 16.67 77954926 49.07 49.63 48.98 49.57 12728627 ... 32.60 33.52 32.54 33.44 13910338 44.74 45.30 44.66 45.09 21490816
2016-10-24 16.75 16.80 16.61 16.77 68244817 49.82 49.88 49.52 49.58 10753462 ... 33.57 33.68 33.34 33.38 9221301 45.45 45.68 45.29 45.52 19303808
2016-10-25 16.71 16.82 16.68 16.72 49350278 49.49 49.80 49.30 49.59 9790517 ... 33.39 33.59 33.20 33.35 7912666 45.47 45.80 45.41 45.72 20402012
2016-10-26 16.64 16.87 16.62 16.87 58730367 49.27 50.06 49.21 50.01 13960034 ... 33.15 33.65 33.06 33.59 9112972 45.46 46.17 45.44 46.15 25181900
2016-10-27 16.95 17.10 16.86 16.91 97937253 50.32 50.60 49.90 49.93 19181449 ... 33.82 34.00 33.58 33.82 12608435 46.42 46.81 46.26 46.41 25661020
2016-10-28 16.95 16.98 16.50 16.68 115123601 49.98 50.12 48.95 49.56 17780277 ... 33.69 34.03 33.17 33.52 10183535 46.57 46.64 45.88 46.23 20882042
2016-10-31 16.68 16.73 16.50 16.50 70387630 49.74 49.86 49.14 49.15 15173447 ... 33.71 33.88 33.49 33.57 8687190 46.40 46.56 46.00 46.01 20115933
2016-11-01 16.56 16.75 16.33 16.61 88587059 49.40 49.67 48.40 48.94 20691054 ... 33.68 33.83 32.91 33.36 8988743 46.12 46.28 45.60 45.99 20020196
2016-11-02 16.46 16.56 16.28 16.48 119202034 48.63 48.72 48.16 48.54 15824060 ... 33.12 33.17 32.73 32.92 8900448 45.38 45.51 45.07 45.24 19566574
2016-11-03 16.48 16.67 16.45 16.48 69690374 48.53 48.99 48.02 48.19 16224279 ... 33.07 33.23 32.77 32.81 8172454 45.33 45.79 45.25 45.34 14982746
2016-11-04 16.53 16.71 16.35 16.55 82437373 48.11 48.80 47.70 48.17 14152548 ... 32.82 33.21 32.56 32.78 7897846 45.36 45.42 44.49 44.60 27391609
2016-11-07 16.86 17.04 16.85 17.01 91581902 49.26 49.84 49.21 49.82 19357566 ... 33.57 34.00 33.57 34.00 11328047 45.22 45.69 45.21 45.40 25769393
2016-11-08 16.82 17.10 16.71 17.00 95842770 49.42 50.02 49.04 49.91 17548250 ... 33.81 34.28 33.54 34.10 7370494 45.18 45.82 44.87 45.54 19462621
2016-11-09 17.66 18.05 17.40 17.97 319516881 50.07 51.94 49.47 51.59 59210448 ... 34.70 36.91 34.47 36.52 28311033 46.33 48.61 46.00 47.99 67615042
2016-11-10 18.26 18.99 18.25 18.76 304986449 52.05 54.20 51.74 53.61 52067278 ... 36.72 38.94 36.72 38.02 38587204 48.93 51.93 48.79 51.63 100988797
2016-11-11 18.64 19.03 18.63 19.02 212952487 52.76 53.16 51.60 52.83 41858608 ... 37.37 38.75 37.34 38.49 22845455 51.09 51.99 50.73 51.73 46594895
2016-11-14 19.41 20.20 19.40 20.08 320959885 53.04 54.89 53.00 54.68 43113101 ... 38.88 39.88 38.78 39.35 25618994 51.91 54.06 51.91 53.22 59154429
2016-11-15 19.79 20.18 19.60 20.16 190293076 53.99 55.47 53.71 55.45 33433808 ... 38.81 40.04 38.40 40.00 18608046 52.38 52.59 51.54 52.59 39437914
2016-11-16 19.78 19.96 19.68 19.75 126734547 54.21 55.00 54.21 54.63 24200100 ... 39.01 39.48 38.96 39.19 18234923 51.79 52.17 51.36 51.68 26996205
2016-11-17 19.76 20.22 19.70 20.08 167472107 54.60 55.98 54.36 55.45 24579011 ... 39.02 39.97 38.72 39.97 12650072 51.39 52.90 50.96 52.49 29133825
2016-11-18 20.06 20.14 19.87 20.00 120620238 55.38 55.84 54.98 55.46 24989950 ... 40.00 40.66 39.89 40.43 16413329 52.45 52.98 51.99 52.82 29673414
2016-11-21 20.10 20.35 20.01 20.33 117479106 55.96 55.99 55.14 55.54 20410449 ... 40.44 40.80 40.11 40.56 10122257 52.85 52.98 52.02 52.12 23587173
2016-11-22 20.45 20.47 20.12 20.30 104771816 55.66 56.15 55.43 56.10 21975907 ... 40.56 40.77 39.97 40.54 12341112 52.43 52.58 51.81 52.22 22816942
2016-11-23 20.42 20.66 20.20 20.56 110963157 56.30 56.83 55.81 56.69 19913463 ... 40.50 41.18 40.22 41.04 9853631 52.44 52.63 51.95 52.16 20022318
2016-11-25 20.62 20.88 20.50 20.86 62610385 56.60 56.80 56.19 56.78 11488291 ... 41.10 41.20 40.79 41.10 4027471 52.40 52.62 52.04 52.62 9635745
2016-11-28 20.67 20.85 20.25 20.30 108651231 55.86 56.35 55.43 55.47 23645006 ... 40.63 41.00 40.34 40.44 10123600 52.12 52.48 51.39 51.58 26518494
2016-11-29 20.38 20.54 20.25 20.29 114216829 55.50 55.94 55.32 55.52 15725383 ... 40.44 40.92 40.29 40.60 8602864 51.56 52.13 51.56 51.86 16267266
2016-11-30 20.77 21.19 20.72 21.12 191699579 56.23 56.72 56.06 56.39 24395062 ... 41.02 41.64 41.01 41.36 14726566 52.48 53.16 52.30 52.92 26567203
2016-12-01 21.42 21.94 21.34 21.50 199233944 56.77 57.57 56.54 57.27 27592511 ... 41.76 42.54 41.58 42.16 14053940 53.13 54.47 53.06 54.34 35623052

2749 rows รƒโ€” 30 columns

In [59]:
bank_stocks = pd.concat([BAC, C, GS, JPM, MS, WFC], axis=1,keys=tickers)
In [60]:
bank_stocks
Out[60]:
BAC C ... MS WFC
Open High Low Close Volume Open High Low Close Volume ... Open High Low Close Volume Open High Low Close Volume
Date
2006-01-03 46.92 47.18 46.15 47.08 16296700 490.00 493.80 481.10 492.90 1537660 ... 57.17 58.49 56.74 58.31 5377000 31.60 31.98 31.20 31.90 11016400
2006-01-04 47.00 47.24 46.45 46.58 17757900 488.60 491.00 483.50 483.80 1871020 ... 58.70 59.28 58.35 58.35 7977800 31.80 31.82 31.36 31.53 10871000
2006-01-05 46.58 46.83 46.32 46.64 14970900 484.40 487.80 484.00 486.20 1143160 ... 58.55 58.59 58.02 58.51 5778000 31.50 31.56 31.31 31.50 10158000
2006-01-06 46.80 46.91 46.35 46.57 12599800 488.80 489.00 482.00 486.20 1370250 ... 58.77 58.85 58.05 58.57 6889800 31.58 31.78 31.38 31.68 8403800
2006-01-09 46.72 46.97 46.36 46.60 15620000 486.00 487.40 483.00 483.90 1680740 ... 58.63 59.29 58.62 59.19 4144500 31.68 31.82 31.56 31.68 5619600
2006-01-10 46.40 46.51 45.88 46.21 15634800 483.00 485.50 480.80 485.40 1365960 ... 58.85 59.22 58.58 59.22 4713600 31.55 31.68 31.44 31.65 7157800
2006-01-11 46.06 46.25 45.75 46.10 14745100 495.80 495.80 485.80 489.80 1684440 ... 59.15 59.88 58.84 59.57 5837900 31.65 31.90 31.58 31.82 8049600
2006-01-12 46.22 46.23 45.71 45.80 10546600 491.00 491.00 488.80 490.30 1230060 ... 59.63 59.78 59.27 59.47 3817700 31.70 31.79 31.43 31.44 10763600
2006-01-13 45.83 46.00 45.68 45.80 10791300 491.00 491.90 487.30 489.20 940930 ... 59.57 59.67 59.06 59.38 3430900 31.55 31.80 31.51 31.62 6366000
2006-01-17 45.40 45.58 45.00 45.31 14606100 485.10 487.00 482.70 484.30 1237830 ... 59.45 59.46 58.68 58.81 2624700 31.46 31.58 31.20 31.30 10690600
2006-01-18 45.32 45.65 44.92 45.18 10911300 484.30 486.70 481.10 483.80 1218910 ... 58.55 58.90 58.05 58.58 2904300 31.30 31.84 31.30 31.67 10798000
2006-01-19 45.88 45.88 44.85 45.14 14584000 485.60 485.80 477.00 479.40 1696500 ... 58.75 59.98 58.71 59.35 3813000 31.80 31.82 31.27 31.30 8713400
2006-01-20 45.00 45.15 44.17 44.19 25164400 472.10 474.00 456.30 456.90 4781930 ... 59.30 59.55 57.41 57.85 5446100 31.31 31.31 30.75 30.75 18175400
2006-01-23 43.50 44.39 43.49 43.96 23459900 460.00 463.80 457.00 460.00 2025500 ... 57.70 58.69 57.70 58.48 3801000 30.76 30.97 30.75 30.80 7047400
2006-01-24 44.08 44.68 44.04 44.24 13500300 462.90 463.60 459.90 460.10 2083740 ... 58.82 59.45 58.62 58.69 3813600 30.82 30.98 30.66 30.80 8232400
2006-01-25 44.15 44.65 44.15 44.63 15132000 461.40 463.70 460.10 462.30 1591940 ... 59.00 59.42 58.55 59.17 4011700 30.84 31.06 30.73 31.00 8580800
2006-01-26 45.00 45.16 44.67 44.90 18449500 465.50 475.50 464.50 470.10 1988600 ... 61.11 60.50 59.49 59.97 5790400 31.24 31.50 31.18 31.28 8598000
2006-01-27 44.90 45.11 44.61 44.80 12250800 470.10 473.70 466.00 468.70 1412760 ... 60.00 61.00 59.79 60.78 3700100 31.28 31.60 31.04 31.35 7640000
2006-01-30 44.75 45.35 44.25 44.48 14816000 468.70 469.90 466.60 468.20 1057630 ... 61.45 61.85 60.64 60.67 4246600 31.35 31.36 31.15 31.24 5756400
2006-01-31 44.65 44.73 44.12 44.23 19151000 468.30 470.50 465.50 465.80 1887280 ... 60.50 61.91 60.49 61.45 5178400 31.12 31.30 31.10 31.18 11763400
2006-02-01 44.10 44.24 43.86 43.95 15854800 465.90 467.20 461.10 463.30 1844970 ... 61.10 61.96 60.89 61.83 3586000 30.95 30.98 30.75 30.92 8224400
2006-02-02 43.95 43.97 43.37 43.44 14745300 459.00 461.00 451.00 451.80 2325470 ... 61.65 62.14 60.85 61.27 3469800 30.82 30.90 30.60 30.65 9831600
2006-02-03 43.30 43.55 42.75 43.09 17097100 450.70 456.10 448.10 450.60 1666510 ... 60.60 62.15 60.36 61.44 3961600 30.42 30.72 30.31 30.52 7532400
2006-02-06 43.40 43.65 43.24 43.37 12554600 452.60 456.10 450.90 451.70 1147430 ... 61.26 61.91 61.23 61.73 2629500 30.42 30.60 30.34 30.52 7290000
2006-02-07 43.60 43.87 43.46 43.49 13917200 452.00 453.80 450.00 450.50 1207780 ... 61.35 61.54 60.27 60.53 3811900 30.50 30.68 30.45 30.56 8097000
2006-02-08 43.55 43.72 43.34 43.67 12564600 453.30 455.30 450.70 453.60 1051370 ... 61.15 61.36 60.80 61.27 2719000 30.50 30.72 30.40 30.66 5928000
2006-02-09 43.75 44.05 43.70 43.83 13766200 455.00 461.00 454.30 457.90 1357740 ... 61.27 61.90 61.10 61.18 2714000 30.60 30.82 30.50 30.62 7052600
2006-02-10 43.77 44.02 43.41 43.92 9039000 457.00 460.70 452.50 459.60 1272030 ... 61.00 61.14 59.38 60.31 4483600 30.64 30.97 30.57 30.86 6501200
2006-02-13 43.70 43.96 43.30 43.70 8329500 460.60 462.30 454.10 456.80 1158300 ... 60.20 60.58 59.39 59.81 4104900 30.78 30.88 30.60 30.82 5401400
2006-02-14 43.92 44.49 43.73 44.16 17049500 457.80 462.50 457.10 461.20 1518040 ... 59.81 60.46 59.21 60.07 3715100 30.95 31.58 30.89 31.32 8263600
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2016-10-20 16.45 16.63 16.41 16.56 88081209 49.46 49.90 49.21 49.58 13522551 ... 32.84 33.19 32.58 32.90 10650671 44.97 45.29 44.66 44.93 27210558
2016-10-21 16.46 16.67 16.40 16.67 77954926 49.07 49.63 48.98 49.57 12728627 ... 32.60 33.52 32.54 33.44 13910338 44.74 45.30 44.66 45.09 21490816
2016-10-24 16.75 16.80 16.61 16.77 68244817 49.82 49.88 49.52 49.58 10753462 ... 33.57 33.68 33.34 33.38 9221301 45.45 45.68 45.29 45.52 19303808
2016-10-25 16.71 16.82 16.68 16.72 49350278 49.49 49.80 49.30 49.59 9790517 ... 33.39 33.59 33.20 33.35 7912666 45.47 45.80 45.41 45.72 20402012
2016-10-26 16.64 16.87 16.62 16.87 58730367 49.27 50.06 49.21 50.01 13960034 ... 33.15 33.65 33.06 33.59 9112972 45.46 46.17 45.44 46.15 25181900
2016-10-27 16.95 17.10 16.86 16.91 97937253 50.32 50.60 49.90 49.93 19181449 ... 33.82 34.00 33.58 33.82 12608435 46.42 46.81 46.26 46.41 25661020
2016-10-28 16.95 16.98 16.50 16.68 115123601 49.98 50.12 48.95 49.56 17780277 ... 33.69 34.03 33.17 33.52 10183535 46.57 46.64 45.88 46.23 20882042
2016-10-31 16.68 16.73 16.50 16.50 70387630 49.74 49.86 49.14 49.15 15173447 ... 33.71 33.88 33.49 33.57 8687190 46.40 46.56 46.00 46.01 20115933
2016-11-01 16.56 16.75 16.33 16.61 88587059 49.40 49.67 48.40 48.94 20691054 ... 33.68 33.83 32.91 33.36 8988743 46.12 46.28 45.60 45.99 20020196
2016-11-02 16.46 16.56 16.28 16.48 119202034 48.63 48.72 48.16 48.54 15824060 ... 33.12 33.17 32.73 32.92 8900448 45.38 45.51 45.07 45.24 19566574
2016-11-03 16.48 16.67 16.45 16.48 69690374 48.53 48.99 48.02 48.19 16224279 ... 33.07 33.23 32.77 32.81 8172454 45.33 45.79 45.25 45.34 14982746
2016-11-04 16.53 16.71 16.35 16.55 82437373 48.11 48.80 47.70 48.17 14152548 ... 32.82 33.21 32.56 32.78 7897846 45.36 45.42 44.49 44.60 27391609
2016-11-07 16.86 17.04 16.85 17.01 91581902 49.26 49.84 49.21 49.82 19357566 ... 33.57 34.00 33.57 34.00 11328047 45.22 45.69 45.21 45.40 25769393
2016-11-08 16.82 17.10 16.71 17.00 95842770 49.42 50.02 49.04 49.91 17548250 ... 33.81 34.28 33.54 34.10 7370494 45.18 45.82 44.87 45.54 19462621
2016-11-09 17.66 18.05 17.40 17.97 319516881 50.07 51.94 49.47 51.59 59210448 ... 34.70 36.91 34.47 36.52 28311033 46.33 48.61 46.00 47.99 67615042
2016-11-10 18.26 18.99 18.25 18.76 304986449 52.05 54.20 51.74 53.61 52067278 ... 36.72 38.94 36.72 38.02 38587204 48.93 51.93 48.79 51.63 100988797
2016-11-11 18.64 19.03 18.63 19.02 212952487 52.76 53.16 51.60 52.83 41858608 ... 37.37 38.75 37.34 38.49 22845455 51.09 51.99 50.73 51.73 46594895
2016-11-14 19.41 20.20 19.40 20.08 320959885 53.04 54.89 53.00 54.68 43113101 ... 38.88 39.88 38.78 39.35 25618994 51.91 54.06 51.91 53.22 59154429
2016-11-15 19.79 20.18 19.60 20.16 190293076 53.99 55.47 53.71 55.45 33433808 ... 38.81 40.04 38.40 40.00 18608046 52.38 52.59 51.54 52.59 39437914
2016-11-16 19.78 19.96 19.68 19.75 126734547 54.21 55.00 54.21 54.63 24200100 ... 39.01 39.48 38.96 39.19 18234923 51.79 52.17 51.36 51.68 26996205
2016-11-17 19.76 20.22 19.70 20.08 167472107 54.60 55.98 54.36 55.45 24579011 ... 39.02 39.97 38.72 39.97 12650072 51.39 52.90 50.96 52.49 29133825
2016-11-18 20.06 20.14 19.87 20.00 120620238 55.38 55.84 54.98 55.46 24989950 ... 40.00 40.66 39.89 40.43 16413329 52.45 52.98 51.99 52.82 29673414
2016-11-21 20.10 20.35 20.01 20.33 117479106 55.96 55.99 55.14 55.54 20410449 ... 40.44 40.80 40.11 40.56 10122257 52.85 52.98 52.02 52.12 23587173
2016-11-22 20.45 20.47 20.12 20.30 104771816 55.66 56.15 55.43 56.10 21975907 ... 40.56 40.77 39.97 40.54 12341112 52.43 52.58 51.81 52.22 22816942
2016-11-23 20.42 20.66 20.20 20.56 110963157 56.30 56.83 55.81 56.69 19913463 ... 40.50 41.18 40.22 41.04 9853631 52.44 52.63 51.95 52.16 20022318
2016-11-25 20.62 20.88 20.50 20.86 62610385 56.60 56.80 56.19 56.78 11488291 ... 41.10 41.20 40.79 41.10 4027471 52.40 52.62 52.04 52.62 9635745
2016-11-28 20.67 20.85 20.25 20.30 108651231 55.86 56.35 55.43 55.47 23645006 ... 40.63 41.00 40.34 40.44 10123600 52.12 52.48 51.39 51.58 26518494
2016-11-29 20.38 20.54 20.25 20.29 114216829 55.50 55.94 55.32 55.52 15725383 ... 40.44 40.92 40.29 40.60 8602864 51.56 52.13 51.56 51.86 16267266
2016-11-30 20.77 21.19 20.72 21.12 191699579 56.23 56.72 56.06 56.39 24395062 ... 41.02 41.64 41.01 41.36 14726566 52.48 53.16 52.30 52.92 26567203
2016-12-01 21.42 21.94 21.34 21.50 199233944 56.77 57.57 56.54 57.27 27592511 ... 41.76 42.54 41.58 42.16 14053940 53.13 54.47 53.06 54.34 35623052

2749 rows รƒโ€” 30 columns

In [61]:
bank_stocks.columns.names = ['Bank Ticker','Stock Info']
In [62]:
bank_stocks.head()
Out[62]:
Bank Ticker BAC C ... MS WFC
Stock Info Open High Low Close Volume Open High Low Close Volume ... Open High Low Close Volume Open High Low Close Volume
Date
2006-01-03 46.92 47.18 46.15 47.08 16296700 490.0 493.8 481.1 492.9 1537660 ... 57.17 58.49 56.74 58.31 5377000 31.60 31.98 31.20 31.90 11016400
2006-01-04 47.00 47.24 46.45 46.58 17757900 488.6 491.0 483.5 483.8 1871020 ... 58.70 59.28 58.35 58.35 7977800 31.80 31.82 31.36 31.53 10871000
2006-01-05 46.58 46.83 46.32 46.64 14970900 484.4 487.8 484.0 486.2 1143160 ... 58.55 58.59 58.02 58.51 5778000 31.50 31.56 31.31 31.50 10158000
2006-01-06 46.80 46.91 46.35 46.57 12599800 488.8 489.0 482.0 486.2 1370250 ... 58.77 58.85 58.05 58.57 6889800 31.58 31.78 31.38 31.68 8403800
2006-01-09 46.72 46.97 46.36 46.60 15620000 486.0 487.4 483.0 483.9 1680740 ... 58.63 59.29 58.62 59.19 4144500 31.68 31.82 31.56 31.68 5619600

5 rows รƒโ€” 30 columns

In [63]:
bank_stocks.xs(key='Close',axis=1,level='Stock Info').max()
Out[63]:
Bank Ticker
BAC     54.90
C      564.10
GS     247.92
JPM     81.79
MS      89.30
WFC     58.52
dtype: float64
In [64]:
bank_stocks['BAC'].max()
Out[64]:
Stock Info
Open      5.504000e+01
High      5.508000e+01
Low       5.482000e+01
Close     5.490000e+01
Volume    1.226791e+09
dtype: float64
In [65]:
returns = pd.DataFrame()
In [66]:
for tick in tickers:
    returns[tick+' Return'] = bank_stocks[tick]['Close'].pct_change()
returns.head()
Out[66]:
BAC Return C Return GS Return JPM Return MS Return WFC Return
Date
2006-01-03 NaN NaN NaN NaN NaN NaN
2006-01-04 -0.010620 -0.018462 -0.013812 -0.014183 0.000686 -0.011599
2006-01-05 0.001288 0.004961 -0.000393 0.003029 0.002742 -0.000951
2006-01-06 -0.001501 0.000000 0.014169 0.007046 0.001025 0.005714
2006-01-09 0.000644 -0.004731 0.012030 0.016242 0.010586 0.000000
In [67]:
#returns[1:]
import seaborn as sns
sns.pairplot(returns[1:])
Out[67]:
<seaborn.axisgrid.PairGrid at 0x2de90056128>
In [68]:
returns.min()
Out[68]:
BAC Return   -0.289694
C Return     -0.899107
GS Return    -0.189596
JPM Return   -0.207274
MS Return    -0.258929
WFC Return   -0.238223
dtype: float64
In [69]:
# Worst Drop (4 of them on Inauguration day)
returns.idxmin()
Out[69]:
BAC Return   2009-01-20
C Return     2011-05-06
GS Return    2009-01-20
JPM Return   2009-01-20
MS Return    2008-10-09
WFC Return   2009-01-20
dtype: datetime64[ns]
In [70]:
# Best Single Day Gain
# citigroup stock split in May 2011, but also JPM day after inauguration.
returns.idxmax()
Out[70]:
BAC Return   2009-04-09
C Return     2011-05-09
GS Return    2008-11-24
JPM Return   2009-01-21
MS Return    2008-10-13
WFC Return   2008-07-16
dtype: datetime64[ns]
In [71]:
returns.std() # Citigroup riskiest
Out[71]:
BAC Return    0.035580
C Return      0.172306
GS Return     0.024761
JPM Return    0.026872
MS Return     0.036707
WFC Return    0.029264
dtype: float64
In [72]:
returns.head()
Out[72]:
BAC Return C Return GS Return JPM Return MS Return WFC Return
Date
2006-01-03 NaN NaN NaN NaN NaN NaN
2006-01-04 -0.010620 -0.018462 -0.013812 -0.014183 0.000686 -0.011599
2006-01-05 0.001288 0.004961 -0.000393 0.003029 0.002742 -0.000951
2006-01-06 -0.001501 0.000000 0.014169 0.007046 0.001025 0.005714
2006-01-09 0.000644 -0.004731 0.012030 0.016242 0.010586 0.000000
In [73]:
returns.loc['2015-01-01':'2015-12-31'].std() # Very similar risk profiles, but Morgan Stanley or BofA
Out[73]:
BAC Return    0.016163
C Return      0.015289
GS Return     0.014046
JPM Return    0.014017
MS Return     0.016249
WFC Return    0.012591
dtype: float64
In [74]:
sns.distplot(returns.loc['2015-01-01':'2015-12-31']['MS Return'],color='green',bins=100)
Out[74]:
<matplotlib.axes._subplots.AxesSubplot at 0x2de90056a58>
In [75]:
sns.distplot(returns.loc['2008-01-01':'2008-12-31']['C Return'],color='red',bins=100)
Out[75]:
<matplotlib.axes._subplots.AxesSubplot at 0x2de92b835c0>
In [76]:
import matplotlib.pyplot as plt
import plotly.graph_objs as go
import plotly.plotly as py
import plotly.offline as offline
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline

# Optional Plotly Method Imports
import plotly
import cufflinks as cf
cf.go_offline()

offline.init_notebook_mode()
IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_data_rate_limit`.
In [77]:
import matplotlib.pyplot as plt
import plotly.graph_objs as go
import plotly.plotly as py
import plotly.offline as offline
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline

# Optional Plotly Method Imports
import plotly
import cufflinks as cf
cf.go_offline()
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_data_rate_limit`.
In [78]:
import matplotlib.pyplot as plt
import plotly.graph_objs as go
import plotly.plotly as py
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline
import cufflinks as cf
init_notebook_mode(connected=True)
In [79]:
init_notebook_mode(connected=True)
In [80]:
py.sign_in("dattatele","93reCjIosFgiul4JuaIb")
In [81]:
for tick in tickers:
    bank_stocks[tick]['Close'].plot(figsize=(12,4),label=tick)
plt.legend()
Out[81]:
<matplotlib.legend.Legend at 0x2de92c0af28>
In [82]:
bank_stocks.xs(key='Close',axis=1,level='Stock Info').plot()
Out[82]:
<matplotlib.axes._subplots.AxesSubplot at 0x2de92e28e80>
In [83]:
bank_stocks.xs(key='Close',axis=1,level='Stock Info').iplot()
200820102012201420160100200300400500Export to plot.ly ยป
BACCGSJPMMSWFC
In [84]:
banK_stock_df = bank_stocks.xs(key='Close',axis=1,level='Stock Info')
In [85]:
# plotly
banK_stock_df.iplot()
200820102012201420160100200300400500Export to plot.ly ยป
BACCGSJPMMSWFC
In [86]:
plt.figure(figsize=(12,6))
BAC['Close'].loc['2008-01-01':'2009-01-01'].rolling(window=30).mean().plot(label='30 Day Avg')
BAC['Close'].loc['2008-01-01':'2009-01-01'].plot(label='BAC CLOSE')
plt.legend()
Out[86]:
<matplotlib.legend.Legend at 0x2de92c49da0>
In [87]:
sns.heatmap(bank_stocks.xs(key='Close',axis=1,level='Stock Info').corr(),annot=True)
Out[87]:
<matplotlib.axes._subplots.AxesSubplot at 0x2de93f31780>
In [88]:
sns.clustermap(bank_stocks.xs(key='Close',axis=1,level='Stock Info').corr(),annot=True)
Out[88]:
<seaborn.matrix.ClusterGrid at 0x2de94015ef0>
In [89]:
close_corr = bank_stocks.xs(key='Close',axis=1,level='Stock Info').corr()
close_corr.iplot(kind='heatmap',colorscale='rdylbu')
BACCGSJPMMSWFCBACCGSJPMMSWFCExport to plot.ly ยป
00.20.40.60.81
In [90]:
BAC[['Open', 'High', 'Low', 'Close']].loc['2015-01-01':'2016-01-01'].iplot(kind='candle')
Mar 2015May 2015Jul 2015Sep 2015Nov 201514.51515.51616.51717.51818.5Export to plot.ly ยป
In [91]:
MS['Close'].loc['2015-01-01':'2016-01-01'].ta_plot(study='sma',periods=[13,21,55],title='Simple Moving Averages')
Mar 2015May 2015Jul 2015Sep 2015Nov 20153234363840Export to plot.ly ยป
Simple Moving AveragesCloseSMASMASMA
In [92]:
BAC['Close'].loc['2015-01-01':'2016-01-01'].ta_plot(study='boll')
Mar 2015May 2015Jul 2015Sep 2015Nov 20151516171819Export to plot.ly ยป
CloseSMAUPPERLOWER
In [93]:
file_writer = FilesWriter()

def export(name, nb):

    # Get a unique key for the notebook and set it in the resources object.
    notebook_name = name[:name.rfind('.')]
    resources = {}
    resources['unique_key'] = notebook_name
    resources['output_files_dir'] = '%s_files' % notebook_name

    # Try to export
    try:
        output, resources = export_by_name(exporter_names.value, nb)
    except ConversionException as e:
        download_link.value = "<br>Could not export notebook!"
    else:
        write_results = file_writer.write(output, resources, notebook_name=notebook_name)

        download_link.value = "<br>Results: <a href='files/{filename}'><i>\"{filename}\"</i></a>".format(filename=write_results)
        download_link.visible = True

def handle_export(widget):
    with open(filename, 'r') as f:
        export(filename, read(f, NO_CONVERT))

export_button.on_click(handle_export)
In [94]:
display(exporter_names, export_button, download_link)
C:\Anaconda3\lib\site-packages\ipykernel_launcher.py:13: DeprecationWarning:

export_by_name is deprecated since nbconvert 5.0. Instead, use export(get_exporter(format_name), nb, **kw)).

In [ ]:

Skype Backpack