-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
182 lines (142 loc) · 6.12 KB
/
app.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
# coding: utf-8
# In[109]:
import numpy as np
import pandas as pd
import html5lib
import bs4
import seaborn as sns
import matplotlib.pyplot as plt
from yahoofinancials import YahooFinancials
import yfinance as yf
import scipy.stats as stats
import datetime
from datetime import date
import streamlit as st
import statsmodels.api as sm
from statsmodels import regression
from PIL import Image
# In[6]:
image= Image.open("banner.jpg")
st.image(image,use_column_width=True)
st.write("""
This web app analyzes the risk and returns of any partuculat stocks in the database used. it visually analyze the time plot of the stock
,the volatility clustering, descrption of the return, Beta , Value at Risk and Alpha of the Stock.
Database: Yahoo Finance
""")
plt.style.use("bmh")
# In[31]:
## Loading dataset
#### Image ---------
## Enter Ticker
st.sidebar.subheader("Company")
ticker = st.sidebar.text_input("Ticker","AMZN")
## Select Period
st.sidebar.subheader(" Period")
st.sidebar.write("Enter Start and End Date")
start_date= st.sidebar.date_input("Start date", datetime.date(2017,1,1))
end_date= st.sidebar.date_input("End date", date.today())
## Loading dataset
try:
stock = yf.download(ticker,start =start_date,end=end_date)
stockinfo = yf.Ticker(ticker)
share = YahooFinancials('AAPL')
# Benchmark index is S&P 500
benchmark = yf.download("SPY",start =start_date,end=end_date)
## Company _Name
company_name =stockinfo.info["longName"]
st.header(company_name)
if st.button("About"):
info = stockinfo.info['longBusinessSummary']
st.write(info)
# View the dataset
st.subheader("The Stock Information")
st.write("The Open price is the first price traded in a given time period and the last price traded is the Close Price. The High and Low can happen any time in-between these two extremes.")
st.write("OHLC for the past 5 days")
st.dataframe(stock.tail(5).sort_index(ascending=False))
### Time plot
st.subheader("Time plot")
st.write(" A display of closing stock price over a given time period")
st.line_chart(stock["Close"])
## volatility clustering plot
st.subheader(" Daily returns of the Closing Price")
st.write("The price return is the rate of return on an investment portfolio, where the return measure takes into account only the capital appreciation of the portfolio, while the income generated by the assets in the portfolio, in the form of interest and dividends, is ignored.")
returns = stock['Close'].pct_change().dropna()
st.line_chart(returns)
st.subheader("Descriptive Summary of the daily returns ")
summ_return =pd.DataFrame({"Minimum":returns.min(),"Maximum":returns.max(),"Average":returns.mean(),"Variance":returns.var(),
"Kurtosis":returns.kurtosis(),"Skewness": returns.skew(),"Volatility(%)":(returns.std()*100)},index=["Value"])
st.dataframe(summ_return)
# Benchmark retuns
benchmark_returns = benchmark["Close"].pct_change().dropna()
## The Density Distribution of Daily Returns
fig, ax = plt.subplots(figsize=(8,3))
plt.title("Histogram of returns")
plt.xlabel("daily returns")
plt.ylabel("Frequncy")
ax.hist(returns, bins =100,histtype='stepfilled')
st.pyplot(fig)
### Performance Metrics
## Calculate Beta Value
# Regression Method.
X = benchmark_returns.values
Y = returns.values
def linreg(x,y):
x = sm.add_constant(x)
model = regression.linear_model.OLS(y,x).fit()
# remove the constant
x=x[:,1]
return model.params[0],model.params[1]
alpha, beta = linreg(X,Y)
#beta = stockinfo.info['beta']
## Value at Risk Using Variance - Covaraince method
mean = returns.mean()
sigma = returns.std()
VaR= (-1 *stats.norm.ppf(0.05,mean,sigma))*100
#risk = pd.DataFrame({"Beta":beta,"Value at Risk":VaR},index=['Value'])
#st.dataframe(risk)
st.subheader("Beta")
st.write("""
Beta is a relative volatility measure that estimate the movements of the changes in prices(Volatility)of a particular security /assets to the overall stock market. It measures the exposure of an assets to the general markets movements.
The benchmark index used is the S&P 500
""")
st.dataframe(pd.DataFrame({"Beta":beta},index=['Value']))
st.subheader("Value at Risk")
st.write("""
Value at Risk(VaR) is a statistical tool that measures the maximum potential loss of an investment over a given period of time, a 95% VaR is calculated uin this program.
A 95% VaR implies that, at 95% confidence level there is a chance that our portfolio will loss some var% or more of it portfolio value in a single day.
""")
st.dataframe(pd.DataFrame({"Value at Risk":VaR},index=['Value']))
st.subheader("Alpha")
st.write("""
Alpha measures the amount that the investment has returned in comparison to the market index or other broad benchmark that it is compared against.It measures the performance of an index against a benchmark
""")
st.dataframe(pd.DataFrame({"Alpha":alpha},index=['Value']))
except:
st.write("""
The ticker you entered is invalid.
If valid, Consider Using a more recent start date for the Ticker you are trying to Analyze(e.g 2019/01/01) to View the Beta, Value at Risk and Alpha Value of the stock.
""")
# In[107]:
#fig1,ax1 = plt.subplots(figsize=(8,4))
#ax1.plot(stock.Close,label=ticker)
#ax1.plot(benchmark.Close,label="S&P500")
#plt.legend()
#st.pyplot(fig1)
if st.sidebar.button("Developers"):
st.sidebar.write("""
Name:E E Ajaegbu
""")
st.sidebar.write("""
Name:Shyambhu Mukherjee[minor fixes]
""")
st.sidebar.write("""
gmail: ajaegbu35@gmail.com
""")
st.sidebar.write("""
gmail: shyambhu20@gmail.com
""")
st.sidebar.write("""
github: https://github.com/EEAjaegbu
github: https://github.com/shyamcody
""")