forked from streamlit/streamlit-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_app.py
165 lines (132 loc) · 6.4 KB
/
streamlit_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
import pandas as pd
import numpy as np
import twstock
from datetime import datetime, timedelta
import streamlit as st
import pytz
from dash import Dash, html, dash_table, dcc, callback, Output, Input
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go
tw = pytz.timezone('Asia/Taipei')
now = datetime.strftime(datetime.now().astimezone(tw),'%Y%m%d')
before = datetime.now().astimezone(tw) - timedelta(days=90)
stockyear,stockmonth = int(before.year) ,int(before.month)
st.set_page_config(layout="wide")
st.title('Stock Visualiztion Dashboard :sunglasses:')
# if "visibility" not in st.session_state:
# st.session_state.visibility = "visible"
# st.session_state.disabled = False
# Using "with" notation
st.sidebar.write("""#### Choose your Stock""")
with st.form(key ='Form1'):
with st.sidebar:
# input_stock = st.sidebar.text_input('Please enter the stock name',
# label_visibility= 'hidden',
# placeholder='輸入股票代碼')
input_stock = st.sidebar.text_input(
"Please enter the stock name 👇",
# label_visibility=st.session_state.visibility,
# disabled=st.session_state.disabled,
placeholder='股票代碼',
)
# stock_number = st.sidebar.number_input('Please enter the stock name',
# value=1.382,
# placeholder='倍率',
# label_visibility= 'hidden')
stock_number = st.sidebar.number_input(
"Please enter the magnification 👇",
value=1.382,
placeholder='倍率',
)
submitted = st.form_submit_button(label = 'Search Money🔎')
def get_history_stock_price(input_stock,stock_number,syear =stockyear,smonth=stockmonth,before=before):
stock = twstock.Stock(input_stock)
target_price = stock.fetch_from(syear, month=smonth)
stock_df = pd.DataFrame(target_price)
stock_df = stock_df[['date','open','high','low','close' ]]
stock_df['高關價'] = np.around(stock_df['low']+(stock_df['high'] - stock_df['low'])*stock_number,2)
stock_df['中關價'] = np.around((stock_df['high'] + stock_df['low'])/2,2)
stock_df['低關價'] = np.around(stock_df['high']-(stock_df['high'] - stock_df['low'])*stock_number,2)
stock_df['高關價'] = stock_df['高關價'].shift(periods=1)
stock_df['中關價'] = stock_df['中關價'].shift(periods=1)
stock_df['低關價'] = stock_df['低關價'].shift(periods=1)
stock_df = stock_df[stock_df['date']>datetime.strftime(before,'%Y-%m-%d')]
stock_df['date'] =stock_df['date'].apply(lambda x :datetime.strftime(x,'%Y-%m-%d'))
fig = make_subplots(
rows=2, cols=1,
shared_xaxes=True,
vertical_spacing=0.03,
specs=[[{"type": "table"}],
[{"type": "scatter"}]]
)
fig.add_trace(
go.Scatter(
x=stock_df["date"],
y=stock_df["close"],
text=stock_df["close"],
mode ='lines+markers+text'
),
row=2, col=1
)
fig.update_layout(xaxis = {'type' : 'category','tickangle':-90})
if datetime.now().astimezone(tw).weekday()<5 and datetime.now().astimezone(tw).hour<14:
temp = twstock.realtime.get(input_stock)
real_time = pd.DataFrame({'date':datetime.strftime(datetime.now(),'%Y-%m-%d'),
'open':float(temp.get('realtime').get('open')),
'high':float(temp.get('realtime').get('high')),
'low':float(temp.get('realtime').get('low')),
'close':0},index = [0])
stock_df = pd.concat([stock_df, real_time], ignore_index = True)
# stock_df = stock_df.sort_values(by= ['date'],ascending=True)
stock_df['高關價'].iloc[-1] = np.around(stock_df['low'].iloc[-2] +(stock_df['high'].iloc[-2] - stock_df['low'].iloc[-2])*stock_number,2)
stock_df['中關價'].iloc[-1] = np.around((stock_df['high'].iloc[-2] + stock_df['low'].iloc[-2])/2,2)
stock_df['低關價'].iloc[-1] = np.around(stock_df['high'].iloc[-2]-(stock_df['high'].iloc[-2] - stock_df['low'].iloc[-2])*stock_number,2)
elif datetime.now().astimezone(tw).hour>14 or datetime.now().astimezone(tw).weekday()>=5:
temp = twstock.realtime.get(input_stock)
real_time = pd.DataFrame({'date':datetime.strftime(datetime.now()+timedelta(days=1, hours=3),'%Y-%m-%d'),
'open':0,
'high':0,
'low':0,
'close':0}, index=[0])
stock_df = pd.concat([stock_df, real_time], ignore_index = True)
stock_df['高關價'].iloc[-1] = np.around(stock_df['low'].iloc[-2] +(stock_df['high'].iloc[-2] - stock_df['low'].iloc[-2])*stock_number,2)
stock_df['中關價'].iloc[-1] = np.around((stock_df['high'].iloc[-2] + stock_df['low'].iloc[-2])/2,2)
stock_df['低關價'].iloc[-1] = np.around(stock_df['high'].iloc[-2]-(stock_df['high'].iloc[-2] - stock_df['low'].iloc[-2])*stock_number,2)
stock_df = stock_df.sort_values(by= ['date'],ascending=False)
fig.add_trace(
go.Table(
header=dict(
values=["日期", "開盤價", "最高價",
"最低價", "收盤價",'高關價','中關價','低關價'],
font=dict(size=20),
align="left"
),
cells=dict(
values=[stock_df[k].tolist() for k in stock_df.columns],
align = "left")
),
row=1, col=1
)
fig.update_layout(
height=700,
title_text=f'{twstock.codes[input_stock].name}/{input_stock}',
title_font_size=45,
showlegend=False,
width = 1100,
margin=dict(
l=00,
r=10,
b=10,
t=80
))
st.plotly_chart(fig, use_container_width=True)
return None
def main(input_stock,stock_number,stockyear,stockmonth,before):
input_stock = input_stock.split(',')
if submitted:
for stock_name in input_stock:
get_history_stock_price(stock_name,stock_number,syear =stockyear,smonth=stockmonth,before=before)
return None
if __name__ == '__main__':
main(input_stock,stock_number,stockyear,stockmonth,before)