Spaces:
Runtime error
Runtime error
Commit
•
7af995e
1
Parent(s):
7afcee3
Upload 2 files
Browse files- requirements.txt +7 -0
- sp500-app.py +82 -0
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==0.68.1
|
2 |
+
pandas==0.25.3
|
3 |
+
base58==2.0.0
|
4 |
+
matplotlib==3.1.3
|
5 |
+
numpy==1.18.1
|
6 |
+
yfinance==0.1.54
|
7 |
+
lxml==4.5.1
|
sp500-app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import base64
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import numpy as np
|
6 |
+
import yfinance as yf
|
7 |
+
|
8 |
+
st.title('S&P 500 App')
|
9 |
+
|
10 |
+
st.markdown("""
|
11 |
+
This app retrieves the list of the **S&P 500** (from Wikipedia) and its corresponding **stock closing price** (year-to-date)!
|
12 |
+
* **Python libraries:** base64, pandas, streamlit, yfinance, numpy, matplotlib
|
13 |
+
* **Data source:** [Wikipedia](https://en.wikipedia.org/wiki/List_of_S%26P_500_companies).
|
14 |
+
""")
|
15 |
+
|
16 |
+
st.sidebar.header('User Input Features')
|
17 |
+
|
18 |
+
# Web scraping of S&P 500 data
|
19 |
+
#
|
20 |
+
@st.cache
|
21 |
+
def load_data():
|
22 |
+
url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
|
23 |
+
html = pd.read_html(url, header = 0)
|
24 |
+
df = html[0]
|
25 |
+
return df
|
26 |
+
|
27 |
+
df = load_data()
|
28 |
+
sector = df.groupby('GICS Sector')
|
29 |
+
|
30 |
+
# Sidebar - Sector selection
|
31 |
+
sorted_sector_unique = sorted( df['GICS Sector'].unique() )
|
32 |
+
selected_sector = st.sidebar.multiselect('Sector', sorted_sector_unique, sorted_sector_unique)
|
33 |
+
|
34 |
+
# Filtering data
|
35 |
+
df_selected_sector = df[ (df['GICS Sector'].isin(selected_sector)) ]
|
36 |
+
|
37 |
+
st.header('Display Companies in Selected Sector')
|
38 |
+
st.write('Data Dimension: ' + str(df_selected_sector.shape[0]) + ' rows and ' + str(df_selected_sector.shape[1]) + ' columns.')
|
39 |
+
st.dataframe(df_selected_sector)
|
40 |
+
|
41 |
+
# Download S&P500 data
|
42 |
+
# https://discuss.streamlit.io/t/how-to-download-file-in-streamlit/1806
|
43 |
+
def filedownload(df):
|
44 |
+
csv = df.to_csv(index=False)
|
45 |
+
b64 = base64.b64encode(csv.encode()).decode() # strings <-> bytes conversions
|
46 |
+
href = f'<a href="data:file/csv;base64,{b64}" download="SP500.csv">Download CSV File</a>'
|
47 |
+
return href
|
48 |
+
|
49 |
+
st.markdown(filedownload(df_selected_sector), unsafe_allow_html=True)
|
50 |
+
|
51 |
+
# https://pypi.org/project/yfinance/
|
52 |
+
|
53 |
+
data = yf.download(
|
54 |
+
tickers = list(df_selected_sector[:10].Symbol),
|
55 |
+
period = "ytd",
|
56 |
+
interval = "1d",
|
57 |
+
group_by = 'ticker',
|
58 |
+
auto_adjust = True,
|
59 |
+
prepost = True,
|
60 |
+
threads = True,
|
61 |
+
proxy = None
|
62 |
+
)
|
63 |
+
|
64 |
+
# Plot Closing Price of Query Symbol
|
65 |
+
def price_plot(symbol):
|
66 |
+
df = pd.DataFrame(data[symbol].Close)
|
67 |
+
df['Date'] = df.index
|
68 |
+
fig = plt.figure()
|
69 |
+
plt.fill_between(df.Date, df.Close, color='skyblue', alpha=0.3)
|
70 |
+
plt.plot(df.Date, df.Close, color='skyblue', alpha=0.8)
|
71 |
+
plt.xticks(rotation=90)
|
72 |
+
plt.title(symbol, fontweight='bold')
|
73 |
+
plt.xlabel('Date', fontweight='bold')
|
74 |
+
plt.ylabel('Closing Price', fontweight='bold')
|
75 |
+
return st.pyplot(fig)
|
76 |
+
|
77 |
+
num_company = st.sidebar.slider('Number of Companies', 1, 5)
|
78 |
+
|
79 |
+
if st.button('Show Plots'):
|
80 |
+
st.header('Stock Closing Price')
|
81 |
+
for i in list(df_selected_sector.Symbol)[:num_company]:
|
82 |
+
price_plot(i)
|