In [1]:
import pandas as pd
from pandas import Series,DataFrame
import numpy as np
In [2]:
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline
In [3]:
from pandas_datareader import data as wb
In [4]:
from datetime import datetime
In [5]:
from __future__ import division
In [6]:
tech_list=['AAPL','GOOG','MSFT','AMZN']
In [7]:
end=datetime.now()
start=datetime(end.year-1,end.month,end.day)

for stock in tech_list:
    globals()[stock]=wb.DataReader(stock,'google',start,end)
In [8]:
AAPL.describe()
Out[8]:
Open High Low Close Volume
count 252.000000 252.000000 252.000000 252.000000 2.520000e+02
mean 129.805714 130.702341 129.014246 129.956349 2.961358e+07
std 17.074552 17.104038 16.940850 17.022823 1.386619e+07
min 102.650000 105.720000 102.530000 103.130000 1.147592e+07
25% 113.680000 114.505000 112.675000 113.565000 2.144606e+07
50% 131.500000 132.330000 131.170000 132.080000 2.591687e+07
75% 144.475000 145.557500 143.530000 144.590000 3.342159e+07
max 159.280000 161.830000 159.110000 161.060000 1.123403e+08
In [9]:
AAPL.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 252 entries, 2016-08-10 to 2017-08-09
Data columns (total 5 columns):
Open      252 non-null float64
High      252 non-null float64
Low       252 non-null float64
Close     252 non-null float64
Volume    252 non-null int64
dtypes: float64(4), int64(1)
memory usage: 11.8 KB
In [10]:
AAPL['Close'].plot(legend=True,figsize=(10,4))
Out[10]:
<matplotlib.axes._subplots.AxesSubplot at 0xb5b4be0>
In [11]:
AAPL['Volume'].plot(legend=True,figsize=(10,4))
Out[11]:
<matplotlib.axes._subplots.AxesSubplot at 0xb8834a8>
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [50]:
ma_day=[10,20,50]

for ma in ma_day:
    
    column_name="MA for %s days" %(str(ma))
    
    AAPL[column_name]=Series.rolling(AAPL['Close'],ma).mean()
In [51]:
AAPL[['Close','MA for 10 days','MA for 20 days','MA for 50 days']].plot(subplots=False,figsize=(10,4))
Out[51]:
<matplotlib.axes._subplots.AxesSubplot at 0x18c9dcc0>
In [52]:
AAPL['Daily Return']=AAPL['Close'].pct_change()

AAPL['Daily Return'].plot(figsize=(10,4),legend=True,linestyle='--',marker='o')
Out[52]:
<matplotlib.axes._subplots.AxesSubplot at 0x18f85c18>
In [53]:
sns.distplot(AAPL['Daily Return'].dropna(),bins=100,color='purple')
Out[53]:
<matplotlib.axes._subplots.AxesSubplot at 0x19135978>
In [54]:
AAPL['Daily Return'].hist(bins=100)
Out[54]:
<matplotlib.axes._subplots.AxesSubplot at 0x19201f28>
In [55]:
closing_df=wb.DataReader(tech_list,'google',start,end)['Close']
In [56]:
closing_df.head()
Out[56]:
AAPL AMZN GOOG MSFT
Date
2016-08-10 108.00 768.56 784.68 58.02
2016-08-11 107.93 771.24 784.85 58.30
2016-08-12 108.18 772.56 783.22 57.94
2016-08-15 109.48 768.49 782.44 58.12
2016-08-16 109.38 764.04 777.14 57.44
In [57]:
tech_rets=closing_df.pct_change()
In [58]:
tech_rets.head()
Out[58]:
AAPL AMZN GOOG MSFT
Date
2016-08-10 NaN NaN NaN NaN
2016-08-11 -0.000648 0.003487 0.000217 0.004826
2016-08-12 0.002316 0.001712 -0.002077 -0.006175
2016-08-15 0.012017 -0.005268 -0.000996 0.003107
2016-08-16 -0.000913 -0.005791 -0.006774 -0.011700
In [59]:
sns.jointplot('GOOG','GOOG',tech_rets,kind='scatter',color='seagreen')
Out[59]:
<seaborn.axisgrid.JointGrid at 0x18e075c0>
In [60]:
sns.jointplot('GOOG','MSFT',tech_rets,kind='scatter')
Out[60]:
<seaborn.axisgrid.JointGrid at 0x1a140240>
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [61]:
tech_rets.head()
Out[61]:
AAPL AMZN GOOG MSFT
Date
2016-08-10 NaN NaN NaN NaN
2016-08-11 -0.000648 0.003487 0.000217 0.004826
2016-08-12 0.002316 0.001712 -0.002077 -0.006175
2016-08-15 0.012017 -0.005268 -0.000996 0.003107
2016-08-16 -0.000913 -0.005791 -0.006774 -0.011700
In [62]:
sns.pairplot(tech_rets.dropna())
Out[62]:
<seaborn.axisgrid.PairGrid at 0x1a72f748>
In [63]:
returns_fig=sns.PairGrid(tech_rets.dropna())

returns_fig.map_upper(plt.scatter,color='purple')

returns_fig.map_lower(sns.kdeplot,cmap='cool_d')

returns_fig.map_diag(plt.hist,bins=30)
Out[63]:
<seaborn.axisgrid.PairGrid at 0x19f152e8>
In [64]:
returns_fig=sns.PairGrid(closing_df)

returns_fig.map_upper(plt.scatter,color='purple')

returns_fig.map_lower(sns.kdeplot,cmap='cool_d')

returns_fig.map_diag(plt.hist,bins=30)
Out[64]:
<seaborn.axisgrid.PairGrid at 0x1c46b240>
In [65]:
sns.heatmap(tech_rets.dropna().corr(),annot=True)
Out[65]:
<matplotlib.axes._subplots.AxesSubplot at 0x210ec358>
In [66]:
sns.heatmap(closing_df.dropna().corr(),annot=True)
Out[66]:
<matplotlib.axes._subplots.AxesSubplot at 0x21755f60>
In [ ]:
 
In [ ]:
 
In [ ]:
 

Risk Analysis

In [67]:
rets=tech_rets.dropna()
In [73]:
area=np.pi*20

plt.scatter(rets.mean(),rets.std(),s=area)

plt.xlabel('Expected Return')
plt.ylabel('Risk')

for label,x,y in zip(rets.columns,rets.mean(),rets.std()):
    plt.annotate(
    label,
    xy=(x,y),xytext=(50,50),
    textcoords='offset points',ha='right',va='bottom',
    arrowprops=dict(arrowstyle='-',connectionstyle='arc3,rad=-0.3'))
In [ ]:
 
In [ ]:
 
In [ ]:
 

Value at Risk

In [74]:
sns.distplot(AAPL['Daily Return'].dropna(),bins=100,color='purple')
Out[74]:
<matplotlib.axes._subplots.AxesSubplot at 0x21b96d30>
In [75]:
rets.head()
Out[75]:
AAPL AMZN GOOG MSFT
Date
2016-08-11 -0.000648 0.003487 0.000217 0.004826
2016-08-12 0.002316 0.001712 -0.002077 -0.006175
2016-08-15 0.012017 -0.005268 -0.000996 0.003107
2016-08-16 -0.000913 -0.005791 -0.006774 -0.011700
2016-08-17 -0.001463 0.000772 0.003564 0.002089
In [76]:
rets['AAPL'].quantile(0.05)
Out[76]:
-0.015138161347655832

Monte Carlo Simulation

In [78]:
days=365

dt=1/days

mu=rets.mean()['GOOG']

sigma=rets.std()['GOOG']
In [82]:
def stock_monte_carlo(start_price,days,mu,sigma):
    
    price=np.zeros(days)
    price[0]=start_price
    
    shock=np.zeros(days)
    drift=np.zeros(days)
    
    for x in xrange(1,days):
        
        shock[x]=np.random.normal(loc=mu*dt,scale=sigma*np.sqrt(dt))
        
        drift[x]=mu*dt
        
        price[x]=price[x-1]+(price[x-1]*(drift[x]+shock[x]))
                             
    return price
In [83]:
GOOG.head()
Out[83]:
Open High Low Close Volume
Date
2016-08-10 783.75 786.81 782.78 784.68 786363
2016-08-11 785.00 789.75 782.97 784.85 975113
2016-08-12 781.50 783.40 780.40 783.22 740498
2016-08-15 783.75 787.49 780.11 782.44 938186
2016-08-16 780.30 780.98 773.44 777.14 1028047
In [86]:
start_price=540.74

for run in xrange(100):
    plt.plot(stock_monte_carlo(start_price,days,mu,sigma))
    
plt.xlabel('Days')
plt.ylabel('Price')
plt.title('Monte Carlo Analysis for Google')
Out[86]:
<matplotlib.text.Text at 0x22c73048>
In [88]:
runs=10000

simulations=np.zeros(runs)

for run in xrange(runs):
    simulations[run]=stock_monte_carlo(start_price,days,mu,sigma)[days-1]
In [89]:
q=np.percentile(simulations,1)

plt.hist(simulations,bins=200)

plt.figtext(0.6,0.8,s="Start price: $%.2f" %start_price)

plt.figtext(0.6,0.7,"Mean final price: $%.2f" % simulations.mean())

plt.figtext(0.6,0.6,"VaR(0.99): $%.2f" % (start_price-q,))

plt.figtext(0.15,0.6,"q(0.99): $%.2f" % q)

plt.axvline(x=q,linewidth=4,color='r')

plt.title(u"Final price distribution for Google Stock after %s days" % days, weight='bold');
In [ ]: