-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
336 lines (281 loc) · 15.3 KB
/
main.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import sys
import pandas as pd
from PySide6 import QtWidgets, QtGui, QtCore
from dialog.apikey_input_dialog import APIKeyInputDialog
from dialog.program_info_dialog import ProgramInfoDialog
from user_setting import UserSetting
from util.data_manager import DataManager
from util.thread import Worker
from util.upbit_caller import UpbitCaller
from widget.account_info_widget import AccountInfoWidget
from widget.period_pnl_widget import PeriodPnLWidget
from widget.transaction_history_widget import TransactionHistoryWidget
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
upbit_config = UserSetting().upbit
self.dm = DataManager()
self.upbit = UpbitCaller(upbit_config["access_key"], upbit_config["secret_key"])
# Thread workers
self.asset_thread_worker = Worker(run_func=self.asset_thread_worker_fn, parent=self)
self.orders_thread_worker = Worker(run_func=self.orders_thread_worker_fn, parent=self)
api_key_test_response = self.upbit.api_key_test()
if not api_key_test_response:
QtWidgets.QMessageBox.information(self, 'wollala-upbit 메시지',
f'Upbit 서버의 응답이 없습니다.\n'
f'처음 사용자라면, API key를 등록 후 사용해주세요.\n')
elif not api_key_test_response['ok']:
QtWidgets.QMessageBox.information(self, 'wollala-upbit 메시지',
f'upbit 서버와 API key 인증과정에서 문제가 생겼습니다.\n'
f'처음 사용자라면, API key를 등록 후 사용해주세요.\n'
f'{api_key_test_response["reason"]}')
all_markets_ticker = self.upbit.request_all_markets_ticker()
self.dm.krw_markets = all_markets_ticker['krw_markets']
self.dm.btc_markets = all_markets_ticker['btc_markets']
# Create Tab members
self.main_tab0_widget = self.create_main_tab0()
# Tab widget
tab_widget = QtWidgets.QTabWidget(parent=self)
tab_widget.addTab(self.main_tab0_widget, "투자내역")
# Progress Bar
self.progressbar = QtWidgets.QProgressBar(parent=self)
self.progressbar.setTextVisible(True)
self.progressbar.setAlignment(QtCore.Qt.AlignCenter) # noqa
self.progressbar.setMaximumWidth(500)
self.statusBar().addPermanentWidget(self.progressbar)
self.transaction_history_widget.loading_progress_order_history_changed.connect(self.update_progressbar)
# menu bar
bar = self.menuBar()
# 설정 메뉴
setting_menu = bar.addMenu("설정")
api_key_menu_action = QtGui.QAction("API key 입력", parent=self)
api_key_menu_action.setStatusTip("거래내역을 불러오기 위한 Upbit API key 입력")
api_key_menu_action.triggered.connect(self.api_key_menu_clicked)
setting_menu.addAction(api_key_menu_action)
# 도움말 메뉴
help_menu = bar.addMenu("도움말")
info_menu_action = QtGui.QAction("정보", parent=self)
info_menu_action.setStatusTip("wollala-upbit 정보")
info_menu_action.triggered.connect(self.info_menu_clicked)
help_menu.addAction(info_menu_action)
# API key 입력 dialog
self.apikey_input_dialog = APIKeyInputDialog(parent=self)
self.apikey_input_dialog.upbit_client_updated.connect(self.updated_upbit_client)
self.apikey_input_dialog.hide()
# 프로그램 정보 dialog
self.program_info_dialog = ProgramInfoDialog(parent=self)
self.statusBar()
self.setCentralWidget(tab_widget)
self.setWindowTitle('wollala-upbit')
self.resize(1600, 1000)
@QtCore.Slot()
def api_key_menu_clicked(self, s): # noqa
self.apikey_input_dialog.show()
@QtCore.Slot()
def info_menu_clicked(self, s): # noqa
self.program_info_dialog.show()
@QtCore.Slot()
def model_Updated(self, first_order_day, last_order_day, pnl_krw, pnl_btc):
self.calculate_console_widget.append("======== 수익률 ========")
self.calculate_console_widget.append(f'기간 : <b><font color="#f3f3f4">{first_order_day.strftime("%Y-%m-%d %H:%M:%S")}</font></b> ~ <b><font color="#f3f3f4">{last_order_day.strftime("%Y-%m-%d %H:%M:%S")}</font></b>'
f' ({last_order_day - first_order_day})')
self.calculate_console_widget.append(f'KRW : <b><font color="#f3f3f4">{pnl_krw:>,.0f}</font></b> ₩')
self.calculate_console_widget.append(f'BTC : <b><font color="#f3f3f4">{pnl_btc:>,.8f}</font></b> btc')
@QtCore.Slot()
def krw_sum_finished(self, df, result):
df = df.reset_index(drop=True)
result = f'{result:,.0f}' if result % 1 == 0 else f'{result:,.8f}'
result_str = f'<b><font color="#f3f3f4">{result}</font></b>'
format_str = ''
for idx in df.index:
if not pd.isnull(df.loc[idx]):
format_str = f'{format_str} + {df.loc[idx]:,.0f}'
format_str = format_str[3:]
result_str = f'{result_str} = {format_str}'
self.calculate_console_widget.append("합 계산 결과")
self.calculate_console_widget.append(result_str)
@QtCore.Slot()
def sum_finished(self, df, result):
df = df.reset_index(drop=True)
result = f'{result:,.0f}' if result % 1 == 0 else f'{result:,.8f}'
result_str = f'<b><font color="#f3f3f4">{result}</font></b>'
format_str = ''
for idx in df.index:
if not pd.isnull(df.loc[idx]):
if df.loc[idx] % 1 == 0:
format_str = f'{format_str} + {df.loc[idx]:,.0f}'
else:
format_str = f'{format_str} + {df.loc[idx]:,.8f}'
format_str = format_str[3:]
result_str = f'{result_str} = {format_str}'
self.calculate_console_widget.append("합 계산 결과")
self.calculate_console_widget.append(result_str)
@QtCore.Slot()
def mean_finished(self, trading_volume_df, trading_price_df, result):
trading_volume_df = trading_volume_df.reset_index(drop=True)
trading_price_df = trading_price_df.reset_index(drop=True)
result = f'{result:,.0f}' if result % 1 == 0 else f'{result:,.8f}'
result_str = f'<b><font color="#f3f3f4">{result}</font></b>'
format_str0 = ''
format_str1 = ''
for i in trading_volume_df.index:
format_str0 = f'{format_str0} + {trading_volume_df.loc[i]:,.8f} x {trading_price_df.loc[i]:,.8f}'
for i in trading_volume_df.index:
format_str1 = f'{format_str1} + {trading_volume_df.loc[i]:,.8f}'
format_str0 = format_str0[3:]
format_str1 = format_str1[3:]
result_str = f'{result_str} =' \
f'<b><font color="#cecfd5">(</fond></b>{format_str0}<b><font color="#cecfd5">)</fond></b> / ' \
f'<b><font color="#cecfd5">(</fond></b>{format_str1}<b><font color="#cecfd5">)</fond></b>'
self.calculate_console_widget.append("평단가 계산 결과")
self.calculate_console_widget.append(result_str)
@QtCore.Slot()
def ask_minus_bid_finished(self, ask_df, bid_df, result):
ask_df = ask_df.reset_index(drop=True)
bid_df = bid_df.reset_index(drop=True)
result = f'{result:,.0f}' if result % 1 == 0 else f'{result:,.8f}'
result_str = f'<b><font color="#f3f3f4">{result}</font></b>'
format_str0 = ''
format_str1 = ''
for idx in ask_df.index:
if not pd.isnull(ask_df.loc[idx]):
format_str0 = f'{format_str0} + {ask_df.loc[idx]:,.8f}'
format_str0 = format_str0[3:]
format_str0 = f'<b><font color="#cecfd5">(</fond></b>{format_str0}<b><font color="#cecfd5">)</fond></b>'
for idx in bid_df.index:
if not pd.isnull(bid_df.loc[idx]):
format_str1 = f'{format_str1} + {bid_df.loc[idx]:,.8f}'
format_str1 = format_str1[3:]
format_str1 = f'<b><font color="#cecfd5">(</fond></b>{format_str1}<b><font color="#cecfd5">)</fond></b>'
result_str = f'{result_str} = {format_str0} - {format_str1}'
self.calculate_console_widget.append("매도 - 매수 계산 결과")
self.calculate_console_widget.append(result_str)
@QtCore.Slot()
def bid_minus_ask_finished(self, bid_df, ask_df, result):
ask_df = ask_df.reset_index(drop=True)
bid_df = bid_df.reset_index(drop=True)
result = f'{result:,.0f}' if result % 1 == 0 else f'{result:,.8f}'
result_str = f'<b><font color="#f3f3f4">{result}</font></b>'
format_str0 = ''
format_str1 = ''
for idx in ask_df.index:
if not pd.isnull(ask_df.loc[idx]):
format_str0 = f'{format_str0} + {ask_df.loc[idx]:,.8f}'
format_str0 = format_str0[3:]
format_str0 = f'<b><font color="#cecfd5">(</fond></b>{format_str0}<b><font color="#cecfd5">)</fond></b>'
for idx in bid_df.index:
if not pd.isnull(bid_df.loc[idx]):
format_str1 = f'{format_str1} + {bid_df.loc[idx]:,.8f}'
format_str1 = format_str1[3:]
format_str1 = f'<b><font color="#cecfd5">(</fond></b>{format_str1}<b><font color="#cecfd5">)</fond></b>'
result_str = f'{result_str} = {format_str1} - {format_str0}'
self.calculate_console_widget.append("매도 - 매수 계산 결과")
self.calculate_console_widget.append(result_str)
@QtCore.Slot()
def update_progressbar(self, v):
if v == 100:
self.progressbar.setValue(v)
self.progressbar.setFormat(f'거래내역을 모두 가져왔어요!')
self.progressbar.setEnabled(False)
else:
self.progressbar.setEnabled(True)
self.progressbar.setValue(v)
self.progressbar.setFormat(f'거래내역을 가져오는 중..({self.progressbar.value()} %)')
@QtCore.Slot()
def updated_upbit_client(self):
all_markets_ticker = self.upbit.request_all_markets_ticker()
self.dm.krw_markets = all_markets_ticker['krw_markets']
self.dm.btc_markets = all_markets_ticker['btc_markets']
self.asset_thread_worker.start()
self.orders_thread_worker.start()
def create_main_tab0(self):
# Top widget
# Account Info Widget
self.account_info_widget = AccountInfoWidget(parent=self)
self.account_info_widget.account_info_tableview.sumFinished.connect(self.krw_sum_finished)
self.account_info_widget.refresh_btn.clicked.connect(self.asset_thread_worker.start)
self.asset_thread_worker.finished.connect(self.account_info_widget.updated_asset_df)
self.asset_thread_worker.start()
top_frame = QtWidgets.QFrame(parent=self)
top_frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
top_layout = QtWidgets.QHBoxLayout()
top_layout.addWidget(self.account_info_widget)
top_frame.setLayout(top_layout)
# Mid widget. Mid has 2 tabs
# 거래내역 관련 Widget
self.transaction_history_widget = TransactionHistoryWidget(parent=self)
self.transaction_history_widget.order_history_tableview.sumFinished.connect(self.sum_finished)
self.transaction_history_widget.order_history_tableview.meanFinished.connect(self.mean_finished)
self.transaction_history_widget.order_history_tableview.bidMinusAskFinished.connect(self.bid_minus_ask_finished)
self.transaction_history_widget.order_history_tableview.askMinusBidFinished.connect(self.ask_minus_bid_finished)
self.transaction_history_widget.refresh_btn.clicked.connect(self.orders_thread_worker.start)
self.orders_thread_worker.finished.connect(self.transaction_history_widget.stop_spinner)
self.orders_thread_worker.start()
self.upbit.request_order_info_all_progress_changed.connect(
self.transaction_history_widget.updated_order_history_df)
# 코인별 수익률 Widget
self.period_pnl_widget = PeriodPnLWidget(parent=self)
self.period_pnl_widget.period_pnl_table_view.sumFinished.connect(self.sum_finished)
self.period_pnl_widget.modelUpdated.connect(self.model_Updated)
# Layout
mid_tab_widget = QtWidgets.QTabWidget(parent=self)
mid_tab0_frame = QtWidgets.QFrame(parent=self)
mid_tab0_frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
mid_tab0_layout = QtWidgets.QHBoxLayout()
mid_tab0_layout.addWidget(self.transaction_history_widget)
mid_tab0_frame.setLayout(mid_tab0_layout)
mid_tab1_frame = QtWidgets.QFrame(parent=self)
mid_tab1_frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
mid_tab1_layout = QtWidgets.QHBoxLayout()
mid_tab1_layout.addWidget(self.period_pnl_widget)
mid_tab1_frame.setLayout(mid_tab1_layout)
mid_tab_widget.addTab(mid_tab0_frame, "거래내역")
mid_tab_widget.addTab(mid_tab1_frame, "기간손익")
# Bottom widget.
# 계산결과 출력 Widget
self.calculate_console_widget = QtWidgets.QTextBrowser(parent=self)
self.calculate_console_widget.setStyleSheet(
"background-color: rgb(34, 40, 64);"
"color: rgb(157, 159, 170)"
)
self.calculate_console_widget.setFont(QtGui.QFont('Consolas', 11))
self.calculate_console_widget.setAcceptRichText(True)
bottom_frame = QtWidgets.QFrame(parent=self)
bottom_frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
bottom_layout = QtWidgets.QHBoxLayout()
bottom_layout.addWidget(self.calculate_console_widget)
bottom_frame.setLayout(bottom_layout)
splitter = QtWidgets.QSplitter(QtCore.Qt.Vertical, parent=self)
splitter.addWidget(top_frame)
splitter.addWidget(mid_tab_widget)
splitter.addWidget(bottom_frame)
splitter.setHandleWidth(5)
splitter.setSizes([300, 600, 125])
layout = QtWidgets.QHBoxLayout()
layout.addWidget(splitter)
widget = QtWidgets.QFrame(parent=self)
widget.setLayout(layout)
return widget
def closeEvent(self, e):
# for thread terminate
self.asset_thread_worker.stop()
self.orders_thread_worker.stop()
def asset_thread_worker_fn(self):
account_info_list = self.upbit.request_account_info_list()
markets_string = self.dm.extract_markets_string_in_asset(account_info_list)
krw_price_list = None
btc_price_list = None
if markets_string['krw_markets_string']:
krw_price_list = self.upbit.request_price_list(markets_string['krw_markets_string'])
if markets_string['btc_markets_string']:
btc_price_list = self.upbit.request_price_list(markets_string['btc_markets_string'])
self.dm.asset_coins_price_df = self.dm.create_asset_coins_price_df(krw_price_list, btc_price_list)
self.dm.asset_df = self.dm.create_asset_df(account_info_list, self.dm.asset_coins_price_df)
self.dm.asset_summary_df = self.dm.create_asset_summary_df(self.dm.asset_df)
def orders_thread_worker_fn(self):
self.upbit.request_order_info_all_df()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())