-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
227 lines (179 loc) · 6.66 KB
/
server.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import datetime
from finlab.data import Data
import sys
# ----------------------
# get all strategies
# ----------------------
import os
snames = [py for py in os.listdir('strategies') if py[-3:] == '.py' and py != '__init__.py']
strategies = {}
for s in snames:
print('strategies.' + s[:-3])
strategies[s[:-3]] = getattr(__import__('strategies.' + s[:-3]), s[:-3]).strategy
# ----------------------
# dataframe to html
# ----------------------
def generate_table(dataframe, max_rows=10):
"""
將 dataframe 讀進來,轉換成 html 中的 table
"""
table_value = []
# 對於每個rows
for i in range(min(len(dataframe), max_rows)):
row = []
# rows 中的每個元素
for col in dataframe.columns:
# 假如此 column 的最後兩個字為「漲跌」則根據正負顯示顏色
if col[-2:] == '漲跌':
color = 'red' if dataframe.iloc[i][col] >= 0 else 'green'
row.append(html.Td(dataframe.iloc[i][col], style={'color': color}))
else:
row.append(html.Td(dataframe.iloc[i][col]))
table_value.append(html.Tr(row))
return html.Table(
# Table Header
[html.Tr([html.Th(col) for col in dataframe.columns])] +
# Table Body
table_value,
className='table table-striped',
)
# ------------------------
# simulation for strategy
# ------------------------
def simulation(strategy, data, date):
"""
給定一個策略,還有日期,我們產生策略當天的股票清單
並且產生股票從該日期到最近期的漲跌幅狀況(dataframe)
並output出 dataframe 、 曲線圖 還有最近期晚上的日期
"""
# record the original date for data
org_date = data.date
# get the stock list on the "date"
data.date = date
slist = strategy(data).index
# select a subset of price
data.date = datetime.datetime.now().date()
ndays = (datetime.datetime.now().date() - date).days
prices = data.get('收盤價', ndays+10)
prices = prices[slist][date+datetime.timedelta(days=1):]
df = pd.DataFrame()
if not prices.empty:
# 製作 dataframe
buy_price = prices.iloc[0]
current_price = prices.iloc[-1]
yesterday_price = prices.iloc[-2]
today_gain = (prices.iloc[-1] / prices.iloc[-2] - 1)*100
total_gain = (prices.iloc[-1] / prices.iloc[0] - 1)*100
df = pd.DataFrame({
'買入股價': buy_price,
'今日股價': current_price,
'昨日股價': yesterday_price,
'今日漲跌': today_gain,
'至今漲跌': total_gain,
})
df = df[['買入股價', '昨日股價', '今日股價', '今日漲跌', '至今漲跌']]
# equality
eq = (prices/prices.bfill().iloc[0]).mean(axis=1)
last_day = prices.index[-1]
else:
# 製作 dataframe
prices = data.get('收盤價', ndays+10)
df = pd.DataFrame({
'今日收盤': prices[slist].iloc[-1]
})
# equality
eq = pd.Series(1, index=pd.to_datetime([prices.index[-1]]))
last_day = prices.index[-1]
data.date = org_date
return df, eq, str(last_day).split()[0] + '晚上的狀況'
# ----------------------
# Dash start 樣式表
# ----------------------
app = dash.Dash()
# ----------------------
# CSS style setting
# ----------------------
# bootstrap CSS
app.css.append_css({"external_url": "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"})
# Dash CSS
app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})
# Loading screen CSS
app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/brPBPO.css"})
# ----------------------
# HTML layout
# ----------------------
app.layout = html.Div(children=[
# 網頁標題
html.H1(children='策略監控台'),
html.Br(),
# 選擇策略
html.H4(children='策略名稱'),
dcc.Dropdown(
id='strategy-picker',
options=[{'label': name, 'value': name} for name, func in strategies.items()],
),
html.Br(),
# 選擇日期
html.H4(children='選股日期'),
dcc.DatePickerSingle(
id='date-picker',
min_date_allowed=datetime.datetime(2014, 8, 1),
max_date_allowed=datetime.datetime(2200, 1, 1),
initial_visible_month=datetime.datetime.now(),
#date=datetime.datetime.now(),
),
html.Br(),
html.Br(),
# 顯示結果
html.Div(id='table'),
], style={'width':'80%', 'margin':'10%'})
# ----------------------
# 使用者互動的邏輯
# ----------------------
@app.callback(
# 此 function 產生的 output 會於 table 顯示
dash.dependencies.Output(component_id='table', component_property='children'),
# 此 function 的 input 是「策略選單」跟「日期選單」
[dash.dependencies.Input(component_id='strategy-picker', component_property='value'),
dash.dependencies.Input('date-picker', 'date')]
)
def update_output_div(input_value, date):
"""
根據所選策略、所選時間,用simulation函式來產生 dataframe equality 和標題
並且顯示於網頁中的 table 上
"""
# 還沒選擇
if date is None or input_value is None:
return html.H4(children='請選擇上方的策略與日期')
try:
# 監測開始
print('start simulation')
date = datetime.datetime.strptime(date.split()[0], '%Y-%m-%d')
data = Data()
# 根據所選策略、所選時間,用simulation函式來產生 dataframe equality 和標題
df, eq, s = simulation(strategies[input_value], data, date.date())
print('end simulation')
df.index.name = '股票代號'
# 產生 html
return html.Div(children=[html.H3(s), html.Br(), dcc.Graph(
id='example-graph-2',
# 畫圖
figure={
'data': [
{'x': eq.index, 'y': eq, 'type': 'line', 'name': 'SF'},
],
'layout': {
}
}
# 用 generate_table 將 dataframe 轉成 HTML table
), generate_table(df.reset_index().round(2), len(df))
])
except:
errorlog = "Unexpected error: " + str(sys.exc_info())
return html.H4(children='遇到了一些問題喔!' + errorlog)
if __name__ == '__main__':
app.run_server(debug=True, processes=1)