-
Notifications
You must be signed in to change notification settings - Fork 0
/
kabuka.py
82 lines (71 loc) · 2.11 KB
/
kabuka.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
from re import S
from turtle import st
import pandas as pd
import yfinance as yf
import altair as alt
import streamlit as st
st.title("米国株価可視化アプリ")
st.sidebar.write("""
# GAFA株価
こちらは株価可視化ツールです。以下のオプションから表示日数を指定できます。
""")
st.sidebar.write("""
## 表示日数選択
""")
days = st.sidebar.slider("日数",1,50,20)
st.write(
f"""
### 過去**{days}日間**のGAFA株価
"""
)
@st.cache
def get_data(days,tickers):
df = pd.DataFrame()
for company in tickers.keys():
tkr = yf.Ticker(tickers[company])
hist = tkr.history(period = f'{days}d')
hist.index = hist.index.strftime('%d %B %Y')
hist = hist [['Close']]
hist.columns = [company]
hist = hist.T
hist.index.name = 'Name'
df = pd.concat([df,hist])
return df
try:
st.sidebar.write("""
## 株価の範囲指定
""")
ymin,ymax = st.sidebar.slider("範囲を指定してください",0.0,3500.0,(0.0,3500.0))
tickers = {
'apple': 'AAPL',
'amazon':'AMZN',
'google':'GOOGL',
"microsoft":'MSFT',
'netflix':'NFLX'
}
df = get_data(days,tickers)
companies = st.multiselect(
"会社名を選択してください。",
list(df.index),
['google','amazon']
)
if not companies:
st.error("少なくとも一社は選んでください")
else:
data = df.loc[companies]
st.write("## 株価(USD)", data.sort_index())
data = data.T.reset_index()
data = pd.melt(data,id_vars = ["Date"]).rename(columns = {'value':'Stock Prices(USD)'})
chart = (
alt.Chart(data)
.mark_line(opacity = 0.8,clip = True)
.encode(
x = "Date:T",
y = alt.Y("Stock Prices(USD):Q",stack = None,scale = alt.Scale(domain = [ymin,ymax])),
color = "Name:N")
)
st.altair_chart(chart,use_container_width = True)
except:
st.error(
"おっと何かエラーが起きているようです。"
)