-
Notifications
You must be signed in to change notification settings - Fork 0
/
Security_Risk_Return.py
100 lines (86 loc) · 2.78 KB
/
Security_Risk_Return.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 9 20:25:07 2019
@author: Darrell
"""
#%% CAPSTONE 1
#USER DECISIONS
ticker='VOO' # watchlist: CRM, VOO, SPY
hp = 5 # holding period (years)
#%% import libraries
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
import numpy as np
import pandas as pd
import pandas_datareader.data as web
from scipy import mean, std
from scipy.stats import skew, kurtosis
style.use('ggplot')
#%% download data
## from web
start = dt.datetime(2000,1,1)#2000,1,1
end = dt.datetime.today()
df = web.DataReader(ticker,'yahoo',start,end)
df_benchmark = web.DataReader('SPY','yahoo',df.index[1],end)
#df.to_csv('TSLA.csv')
## from csv
#df = pd.read_csv('VOO.csv', parse_dates = True, index_col = 0)
#%% moving average
df['20ma'] = df['Adj Close'].rolling(window = 20).mean()
df.dropna(inplace = True)
# historical rate of return
#eR = (df['20ma']['2019'][0]/df['20ma']['2012'][0])**(1/7)
#%% rolling return
returns_cum = df['Adj Close'].pct_change(periods=(252*hp), axis=0)
returns_cum.dropna(inplace = True)
returns_1year = (1.0 + returns_cum)**(1/hp) -1
# benchmark
returns_cum_b = df_benchmark['Adj Close'].pct_change(periods=(252*hp), axis=0)
returns_cum_b.dropna(inplace = True)
returns_1year_b = (1.0 + returns_cum_b)**(1/hp) -1
eR = mean(returns_1year)
sR = std(returns_1year)
eRL = eR - 1.96*sR
eRU = eR + 1.96*sR
#%% figures
#df['Adj Close'].plot()
#plt.show
ax1 = plt.subplot2grid((10,1),(0,0),rowspan=5)
ax2 = plt.subplot2grid((10,1),(6,0),rowspan=5,sharex=ax1)
ax1.plot(df.index, df['Adj Close'])
ax1.plot(df.index, df['20ma'])
ax1.set_ylabel('Price (USD)')
ax1.set_title(ticker)
if ticker=='VOO':
cost_basis=272.91
diff=dt.date.today()-dt.date(2019,7,2)
ndays=diff.days
ax1.axhline(y=cost_basis, color='grey', linestyle='-')
ax1.axvline(x='2019-07-02', color='grey', linestyle='-')
print('Your current earnings gross of fees =',5*(df['Adj Close'][-1]-cost_basis))
print('Your current EAR gross of fees =',
100*((df['Adj Close'][-1]/cost_basis)**(365/ndays)-1),
'%')
#ax2.bar(df.index, df['Volume'])
ax2.plot(returns_1year*100, color = 'b')
ax2.axhline(y=eR*100, color='r', linestyle='-')
ax2.axhline(y=0, color='k', linestyle='-')
# benchmark
ax2.plot(returns_1year_b*100, color = 'grey')
ax2.set_ylabel('Trailing Return (%)')
ax2.set_xlabel('Date')
#ax2.xlabel('Date')
#ax2.legend(['rolling return','mean level'])
#ax2.ylabel('Effective Annual Return')
#plt.figure(0)
plt.show()
#plt.plot(returns_1year, color = 'b')
#plt.axhline(y=eR, color='r', linestyle='-')
#plt.axhline(y=0, color='k', linestyle='-')
#plt.xlabel('Date')
#plt.legend(['rolling return','mean level'])
#plt.ylabel('Effective Annual Return')
#plt.figure(1)