-
Notifications
You must be signed in to change notification settings - Fork 390
/
email_stock.py
121 lines (83 loc) · 3.07 KB
/
email_stock.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from tiingo import TiingoClient
import smtplib
from alpha_vantage.timeseries import TimeSeries
import matplotlib.pyplot as plt
from yahoo_fin import stock_info as si
import pandas as pd
from pandas_datareader import DataReader
import numpy as np
import datetime
import pprint
import json
config = {}
me = 'johnbroe23@gmail.com'
you = 'shashankv323@gmail.com'
password = 'fantasyforlife3'
config['session'] = True
config['api_key'] = "b89559c7df932f3d1173a58e423f0c1634f61651"
client = TiingoClient(config)
ticker = "AAPL"
start_date = datetime.datetime.now() - datetime.timedelta(days=365)
end_date = datetime.date.today()
# price
price = si.get_live_price('{}'.format(ticker))
price = round(price, 2)
# volatility, momentum, beta, alpha, r_squared
df = DataReader(ticker,'yahoo',start_date, end_date)
dfb = DataReader('^GSPC','yahoo',start_date, end_date)
rts = df.resample('M').last()
rbts = dfb.resample('M').last()
dfsm = pd.DataFrame({'s_adjclose' : rts['Adj Close'],
'b_adjclose' : rbts['Adj Close']},
index=rts.index)
dfsm[['s_returns','b_returns']] = dfsm[['s_adjclose','b_adjclose']]/\
dfsm[['s_adjclose','b_adjclose']].shift(1) -1
dfsm = dfsm.dropna()
covmat = np.cov(dfsm["s_returns"],dfsm["b_returns"])
beta = covmat[0,1]/covmat[1,1]
alpha= np.mean(dfsm["s_returns"])-beta*np.mean(dfsm["b_returns"])
ypred = alpha + beta * dfsm["b_returns"]
SS_res = np.sum(np.power(ypred-dfsm["s_returns"],2))
SS_tot = covmat[0,0]*(len(dfsm)-1) # SS_tot is sample_variance*(n-1)
r_squared = 1. - SS_res/SS_tot
volatility = np.sqrt(covmat[0,0])
momentum = np.prod(1+dfsm["s_returns"].tail(12).values) -1
prd = 12.
alpha = alpha*prd
volatility = volatility*np.sqrt(prd)
beta = round(beta, 2)
alpha = round(alpha, 2)
r_squared = round(r_squared, 2)
volatility = round(volatility, 2)
momentum = round(momentum, 2)
# Sharpe Ratio
x = 5000
y = (x)
stock_df = df
stock_df['Norm return'] = stock_df['Adj Close'] / stock_df.iloc[0]['Adj Close']
allocation = float(x/y)
stock_df['Allocation'] = stock_df['Norm return'] * allocation
stock_df['Position'] = stock_df['Allocation'] * x
pos = [df['Position']]
val = pd.concat(pos, axis=1)
val.columns = ['WMT Pos']
val['Total Pos'] = val.sum(axis=1)
val.tail(1)
val['Daily Return'] = val['Total Pos'].pct_change(1)
Sharpe_Ratio = val['Daily Return'].mean() / val['Daily Return'].std()
A_Sharpe_Ratio = (252**0.5) * Sharpe_Ratio
A_Sharpe_Ratio = round(A_Sharpe_Ratio, 2)
#fundamental data
fundamental_data = si.get_quote_table(ticker)
fundamental_data = pprint.pformat(fundamental_data)
fundamental_data = fundamental_data.strip('{}')
'''
if price > 0:
message = '{ticker} has the following: \ncurrent price - {} \nbeta(1Y) - {} \nalpha(1Y) - {} \nvolatility(1Y) - {} \nmomentum(1Y) - {} \nsharpe ratio(1Y) - {} \n{}'.format(ticker, price, beta, alpha, volatility, momentum, A_Sharpe_Ratio, fundamental_data)
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(me, password)
s.sendmail(me, you, message)
s.quit()
print('Email sent.')
'''