In [3]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from util import get_data, plot_data, fill_missing_values
%matplotlib inline
You can download the csv files with the stock data in it from Yahoo Finance (Historical Data) using your browser, the pandas_datareader or you build something yourself with requests.
In [25]:
dates = pd.date_range('2014-01-01', '2014-12-31')
symbols = ['V']
df = get_data(symbols, dates) # will always add SPY (S&P500) for reference
fill_missing_values(df) # fill missing values (NaN)
plot_data(df) # plots the data
In [26]:
visa = df['V'] # extract the visa data from the dataframe
plot_data(visa, title="Visa stock prices")
In [27]:
def r_mean(values, window=20):
"""Return rolling mean of given values, using specified window size."""
return values.rolling(window, center=False).mean()
def r_std(values, window=20):
"""Return rolling mean of given values, using specified window size."""
return values.rolling(window, center=False).std()
def bbands(rm, rstd):
"""Return upper and lower Bollinger Bands."""
upper = rm + 2*rstd
lower = rm - 2*rstd
return (upper, lower)
In [28]:
rm = r_mean(visa)
rstd = r_std(visa)
(upper, lower) = bbands(rm, rstd)
ax = visa.plot(title='Bollinger Bands' + u'\N{REGISTERED SIGN}')
ax.set_ylabel('Price')
ax.set_xlabel('Date')
rm.plot(label='rolling mean', ax=ax)
upper.plot(label='upper band', ax=ax)
lower.plot(label='lower band', ax=ax)
Out[28]:
In [29]:
def get_daily_returns(df):
"""Compute and return the daily return values."""
dr = df.copy()
dr[1:] = (df[1:] / df[:-1].values) - 1
#dr.ix[0, :] = 0 # set daily return for the first day to 0
dr.iloc[0] = 0
return dr
dr = get_daily_returns(df)
plot_data(dr['V'], title="Daily Returns", ylabel="Daily Returns")
In [30]:
m = dr['V'].mean()
std = dr['V'].std()
dr['V'].hist(bins=20)
plt.axvline(m, color='y', linestyle='dashed', linewidth=2)
plt.axvline(std, color='r', linestyle='dashed', linewidth=2)
plt.axvline(-std, color='r', linestyle='dashed', linewidth=2)
Out[30]:
In [31]:
dr.plot(kind='scatter', x='SPY', y='V')
beta, alpha = np.polyfit(dr['SPY'], dr['V'], 1)
print("beta: " + str(beta))
print("alpha: " + str(alpha))
plt.plot(dr['SPY'], beta*dr['SPY'] + alpha, '-', color='r')
Out[31]:
In [32]:
start_val = 1000000
start_date = '2011-1-1'
end_date = '2011-12-31'
symbols = ['SPY', 'XOM', 'GOOG', 'GLD']
allocs = [0.4, 0.4, 0.1, 0.1]
In [33]:
dates = pd.date_range(start_date, end_date)
prices = get_data(symbols, dates)
print(prices.head())
plot_data(prices)
In [34]:
def get_normed_prices(prices):
return prices / prices.iloc[0].values
normed = get_normed_prices(prices)
print(normed.head())
plot_data(normed, title="Normed Price", ylabel="Normed Price")
In [35]:
def get_alloced_prices(allocs):
return normed * allocs
alloced = get_alloced_prices(allocs)
print(alloced.head())
In [36]:
def get_pos_vals(start_val):
return alloced * start_val
pos_vals = get_pos_vals(start_val)
print(pos_vals.head())
In [37]:
def get_portfolio_value(pos_vals):
return pos_vals.sum(axis=1)
port_val = get_portfolio_value(pos_vals)
plot_data(port_val, title="Portfolio Value", ylabel="Value")
In [38]:
port_dr = get_daily_returns(port_val)
plot_data(port_dr, title="Daily Return", ylabel="Daily Return")
In [39]:
dates = pd.date_range(start_date, end_date)
symbols = ['SPY']
df = get_data(symbols, dates)
dr = get_daily_returns(df)
dr['PORTFOLIO'] = port_dr
dr.plot(kind='scatter', x='SPY', y='PORTFOLIO')
beta, alpha = np.polyfit(dr['SPY'], port_dr, 1)
print("beta: " + str(beta))
print("alpha: " + str(alpha))
plt.plot(dr['SPY'], beta*dr['SPY'] + alpha, '-', color='r') # CAPM Equation
Out[39]:
In [40]:
cum_ret = (port_val[-1] / port_val[0]) - 1
print("The Cumulative Return is %s." % cum_ret)
In [41]:
avg_daily_ret = port_dr.mean()
print("The Average Daily Return is %s." % avg_daily_ret)
In [42]:
std_daily_ret = port_dr.std()
print("The Standard Deviation of the Daily Return is %s." % std_daily_ret)
In [43]:
sr = np.sqrt(252) * (avg_daily_ret / std_daily_ret)
print("The Sharp Ratio is %s" % sr)