Professional Documents
Culture Documents
Data
I will be gathering data using pandas datareader for the following banks:
Bank of America
CitiGroup
Goldman Sachs
JPMorgan Chase
Morgan Stanley
Wells Fargo
I'll use a list of tickers to help with any looping I may need to do
I will concat these DataFrames into one big multi-indexed DataFrame to work off of.
In [110]: bank_stocks = pd.concat([BAC, C, GS, JPM, MS, WFC], axis =1, keys=tickers)
In [111]: bank_stocks.head()
PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Out[111]:
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-
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
01-03
2006-
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
01-04
2006-
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
01-05
2006-
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
01-06
2006-
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
01-09
5 rows 30 columns
In [114]: bank_stocks.head()
Out[114]:
Bank
BAC C ... MS WFC
Ticker
Stock
Open High Low Close Volume Open High Low Close Volume ... Open High Low Close Volume Open High Low Close Volume
Info
Date
2006-
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
01-03
2006-
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
01-04
2006-
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
01-05
2006-
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
01-06
2006-
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
01-09
5 rows 30 columns
I want to find the max Close price for each bank's stock throughout the time period I
collected from Google
PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Out[118]: Bank Ticker
BAC 54.90
C 564.10
GS 247.92
JPM 70.08
MS 89.30
WFC 58.52
dtype: float64
Now I'll create a new empty DataFrame called returns. This dataframe will contain the
returns for each bank's stock. returns are typically defined by:
$$r_t = \frac{p_t - p_{t-1}}{p_{t-1}} = \frac{p_t}{p_{t-1}} - 1$$
I can use pandas pct_change() method on the Close column to create a column
representing this return value.
Out[124]:
BAC Return C Return GS Return JPM Return MS Return WFC Return
Date
PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Citigroup stands out like a sore thumb here. A massive drop in stock value in a very
short time. This was right around the time their Stock crashed.
I can also figure out on what dates each bank stock had the best and worst single day
returns.
In [136]: returns.idxmin()
#index minimum values
Out[136]: 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]
2009-01-20 seemed to be a terrible day for the these major bank's stocks... just so
happened to be Inauguration Day
PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
In [41]: returns.idxmax()
Out[41]: 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]
Now looking at the best returns; Citigroup's worst return and best return were 3 days
apart from eachother. A quick google search shows the this was directly following their
announcement of their 1-for-10 reverse stock split. This provided a reduction in the
number of their traded shares which in turn resulted in an increase in the par value
(earnings per share).
Looking at the standard deviation of the returns, it should show us a quick breakdown
of the riskiest investments over these years
In [126]: returns.std()
Out[126]: BAC Return 0.036650
C Return 0.179969
GS Return 0.025346
JPM Return 0.027656
MS Return 0.037820
WFC Return 0.030233
dtype: float64
The Riskiest investment over these years was Citi by a long shot
Morgan Stanely was a riskier investment over 2015 after Citi rebounded with with Bank
of America nearly identical.
Below is a distplot using seaborn of the 2015 returns for Morgan Stanley
C:\Users\jman0\Anaconda3\lib\site-packages\statsmodels\nonparametric\kdetools
.py:20: VisibleDeprecationWarning:
PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Out[128]: <matplotlib.axes._subplots.AxesSubplot at 0x15e75e0f668>
C:\Users\jman0\Anaconda3\lib\site-packages\statsmodels\nonparametric\kdetools
.py:20: VisibleDeprecationWarning:
Just to play around more with the datae, I'll make a line plot showing Close price for
PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
each bank for the entire index of time using three different methods
In [130]: bank_stocks.xs('Close',axis=1,level=-1).plot(figsize=(12,4))
Out[130]: <matplotlib.axes._subplots.AxesSubplot at 0x15e7de0beb8>
In [132]: # plotly
bank_stocks.xs(key='Close',axis=1,level=-1).iplot()
#Level can be EITHER level=-1 or 'Stock Info"
PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
I can even take the rolling 30 day average against the Close Price for Bank Of America's
stock for the year 2008
In [133]: plt.figure(figsize=(16,4))
bank_stocks.ix['2008-01-01': '2008-12-31']['BAC']['Close'].plot(label='BAC Cl
ose')
bank_stocks.ix['2008-01-01': '2008-12-31']['BAC']['Close'].rolling(window=30)
.mean().plot(label='30 Day Avg')
plt.legend()
Below shows a heatmap and a clustermap of the correlation between the stocks Close
Price.
PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
In [134]: sns.heatmap(data=bank_stocks.xs(key='Close', level = -1, axis=1).corr(),cmap=
'coolwarm',annot=True)
Out[134]: <matplotlib.axes._subplots.AxesSubplot at 0x15e7e247588>
C:\Users\jman0\Anaconda3\lib\site-packages\matplotlib\cbook.py:136: Matplotli
bDeprecationWarning:
The axisbg attribute was deprecated in version 2.0. Use facecolor instead.
PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
To explore cufflinks a bit more, I want to try some new types of advaned plots.
In [104]: BAC.ix['2015-01-01':'2016-01-01'].iplot(kind='candle')
C:\Users\jman0\Anaconda3\lib\site-packages\plotly\tools.py:1422: UserWarning:
PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
This Candlestick plot of the stock shows the rise and fall of the stock for each day.
PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
In [139]: BAC['Close'].ix['2015-01-01':'2016-01-01'].ta_plot(study='boll')
PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
This shows the standard devation of the stock price over 2015
PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com