프로젝트

일반

사용자정보

At 업비트 개요창 » 이력 » 버전 1

이태훈, 2025/02/12 13:44

1 1 이태훈
h1. At 업비트 개요창
2
3
h3. 소스코드
4
5
<pre>
6
import sys
7
from PyQt5 import uic
8
from PyQt5.QtWidgets import QWidget, QApplication
9
from PyQt5.QtCore import Qt, QThread, pyqtSignal
10
from pyupbit import WebSocketManager
11
12
class OverViewWorker(QThread):
13
    dataMidSent = pyqtSignal(int, float, float)
14
    data24Sent = pyqtSignal(float, int, float, int, int)
15
16
    def __init__(self, ticker):
17
        super().__init__()
18
        self.ticker = ticker
19
        self.alive = True
20
21
    def run(self):
22
        wm = WebSocketManager("ticker", [f"{self.ticker}"])
23
        while self.alive:
24
            data = wm.get()
25
26
            self.dataMidSent.emit(int(data['trade_price']),  # 현재가
27
                               float(data['signed_change_rate'] * 100),      # 등락률
28
                               float(data['acc_bid_volume'] / data['acc_ask_volume'] * 100))  # 채결강도 Volume Power = Bid Volume/Ask Volume × 100%
29
30
            self.data24Sent.emit(float(data['acc_trade_volume_24h']),  # 거래량
31
                           int(data['high_price']),       # 고가(당일)
32
                           float(data['acc_trade_price_24h']),         # 거래금액
33
                           int(data['low_price']),        # 저가(당일)
34
                           int(data['prev_closing_price']))  # 전일종가
35
36
        wm.terminate() # 해당코드가 없으면 종료가 제대로 이루어지지않음.
37
38
    def close(self):
39
        self.alive = False
40
41
class OverviewWidget(QWidget):
42
    def __init__(self, parent=None, ticker="KRW-BTC"):
43
        super().__init__(parent)
44
        uic.loadUi("resource/overview.ui", self)
45
        self.setWindowTitle("실시간 개요창")
46
47
        self.ticker = ticker
48
49
        self.label_3.setText("거래량(24H)")
50
        self.label_5.setText("고가(당일)")
51
        self.label_7.setText("거래금액(24H)")
52
        self.label_9.setText("저가(당일)")
53
        self.label_11.setText("채결강도(24H)")
54
        self.label_13.setText("전일종가")
55
56
        self.ovw = OverViewWorker(ticker)
57
        self.ovw.dataMidSent.connect(self.fillMidData)
58
        self.ovw.data24Sent.connect(self.fill24Data)
59
        self.ovw.start()
60
61
    def closeEvent(self, event):
62
        self.ovw.close()
63
64
    def fillMidData(self, currPrice, chgRate, volumePower):
65
        self.label_1.setText(f"{currPrice:,}")
66
        self.label_2.setText(f"{chgRate:+.2f}%")
67
        self.label_12.setText(f"{volumePower:.2f}%")
68
        self.__updateStyle()
69
70
    def fill24Data(self, volume, highPrice, value, lowPrice, prevClosePrice):
71
        self.label_4.setText(f"{volume:,.3f} {"BTC"}")
72
        self.label_6.setText(f"{highPrice:,}")
73
        self.label_8.setText(f"{value/100000000:,.1f} 억")
74
        self.label_10.setText(f"{lowPrice:,}")
75
        self.label_14.setText(f"{prevClosePrice:,}")
76
        self.__updateStyle()
77
78
    def __updateStyle(self):
79
        if '-' in self.label_2.text():
80
            self.label_1.setStyleSheet("color:blue;")
81
            self.label_2.setStyleSheet("background-color:blue;color:white")
82
        else:
83
            self.label_1.setStyleSheet("color:red;")
84
            self.label_2.setStyleSheet("background-color:red;color:white")
85
86
if __name__ == "__main__":
87
    app = QApplication(sys.argv)
88
    ob = OverviewWidget()
89
    ob.show()
90
    exit(app.exec_())
91
</pre>
92
93
h3. 결과
94
95
!clipboard-202502122244-9w4yb.png!