pip install pybithumb
import pybithumb
tickers = pybithumb.get_tickers()
print(tickers)
print(len(tickers)) #실제로 거래되고 있는 가상화폐의 수 : 51
price = pybithumb.get_current_price("BTC")
print(price)
import pybithumb
import time
while True:
# tickers = pybithumb.get_tickers()
price = pybithumb.get_current_price("BTC")
print(price)
time.sleep(1)
import pybithumb
import time
tickers = pybithumb.get_tickers()
for ticker in tickers:
price = pybithumb.get_current_price(ticker)
print(ticker, " - price : ", price)
time.sleep(0.1)
detail = pybithumb.get_market_detail("BTC")
print(detail)
orderbook = pybithumb.get_orderbook("BTC")
print(orderbook)
for k in orderbook:
print(k)
- orderbook print 결과화면
{'timestamp': '1650196752255', 'payment_currency': 'KRW', 'order_currency': 'BTC', 'bids': [{'price': 50502000.0, 'quantity': 0.2135}, {'price': 50501000.0, 'quantity': 0.7749}, {'price': 50488000.0, 'quantity': 0.1506}, {'price': 50485000.0, 'quantity': 0.0655}, {'price': 50483000.0, 'quantity': 0.4867}], 'asks': [{'price': 50523000.0, 'quantity': 0.0745}, {'price': 50525000.0, 'quantity': 0.2177}, {'price': 50526000.0, 'quantity': 0.031}, {'price': 50527000.0, 'quantity': 0.0188}, {'price': 50534000.0, 'quantity': 0.0409}]}
orderbook = pybithumb.get_orderbook("BTC")
print(orderbook['payment_currency'])
print(orderbook['order_currency'])
ms = int(orderbook['timestamp'])
dt = datetime.datetime.fromtimestamp(ms/1000)
print(dt)
print(orderbook['bids'])
print(orderbook['asks'])
- bids, asks : 파이썬 리스트로 총 다섯개의 딕셔너리가 있음
- 각각 호가(수량과 가격)를 나타냄
[{'price': 50512000.0, 'quantity': 0.0587}, {'price': 50511000.0, 'quantity': 0.0223}, {'price': 50505000.0, 'quantity': 0.1201}, {'price': 50500000.0, 'quantity': 0.004}, {'price': 50499000.0, 'quantity': 0.0332}][{'price': 50515000.0, 'quantity': 0.079}, {'price': 50518000.0, 'quantity': 0.2007}, {'price': 50525000.0, 'quantity': 0.0718}, {'price': 50529000.0, 'quantity': 0.28}, {'price': 50532000.0, 'quantity': 0.6212}]
orderbook = pybithumb.get_orderbook("BTC")
bids = orderbook['bids']
asks = orderbook['asks']
for bid in bids:
price = bid['price']
quant = bid['quantity']
print('매수호가 : ', price, '매수잔량 : ', quant)
for ask in asks:#각각의 가격과 잔고는 'price', 'quantity'라는 키에 저장된 딕셔너리형태
print(ask)
all = pybithumb.get_orderbook("ALL")
for k, v in all.items():
print(k, v)
ENJ {'opening_price': '69', 'closing_price': '66', 'min_price': '66', 'max_price': '71', 'average_price': '68.6902', 'units_traded': '31112008.062182424015', 'volume_1day': '31112008.062182424015', 'volume_7day': '305304299.66063367358992023', 'buy_price': '66', 'sell_price': '67', '24H_fluctate': -3, '24H_fluctate_rate': '-4.34'}
PST {'opening_price': '353', 'closing_price': '339', 'min_price': '336', 'max_price': '362', 'average_price': '351.2558', 'units_traded': '3321414.5559280172538', 'volume_1day': '3321414.5559280172538', 'volume_7day': '27460648.10254002814311293', 'buy_price': '339', 'sell_price': '340', '24H_fluctate': -14, '24H_fluctate_rate': '-3.96'}
all = pybithumb.get_current_price("ALL")
for ticker, data in all.items() :
print(ticker, data['closing_price'])
while True:
price = pybithumb.get_current_price("BTC")
try:
print(price/10) # price=None이 되는 경우 에러발생함
except:
print("에러", price)
time.sleep(0.2)
import pybithumb
btc = pybithumb.get_ohlcv("BTC")
print(btc) #DataFrame 객체 출력
import pybithumb
btc = pybithumb.get_ohlcv("BTC")
c = btc['close']
print(c)#Series 객체 출력
import pybithumb
btc = pybithumb.get_ohlcv("BTC")
c = btc['close']
m5 = c.rolling(5).mean()
print(m5)
import pybithumb
df = pybithumb.get_ohlcv("BTC")
m5 = df['close'].rolling(5).mean()
last_m5 = m5[-2] #전날 이동평균
price = pybithumb.get_current_price("BTC")
if price > last_m5:
print("상승장")
else:
print("하락장")
import pybithumb
def bull_market(ticker):
df = pybithumb.get_ohlcv("BTC")
m5 = df['close'].rolling(5).mean()
last_m5 = m5[-2]
price = pybithumb.get_current_price("BTC")
if price > last_m5:
return True
else:
return False
tickers = pybithumb.get_tickers()
for ticker in tickers:
is_bull = bull_market(ticker)
if is_bull:
print(ticker,"상승장")
else:
print(ticker,"하락장")
1️⃣ tableWidget 메인화면에 끌어다놓기
2️⃣ 항목 편집을 통해 행 이름 추가
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
form_class = uic.loadUiType("bull.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
app = QApplication(sys.argv)
win = MyWindow()
win.show()
app.exec_()
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
from PyQt5.QtCore import *
tickers = ["BTC", "ETH", "BCH", "ETC"]
form_class = uic.loadUiType("bull.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super(MyWindow, self).__init__()
self.setupUi(self)
timer=QTimer(self)
timer.start(500)
timer.timeout.connect(self.timeout) #timeout 발생하면 MyWindow의 timeout 실행되도록 연결함
def timeout(self):
for i, t in enumerate(tickers):
item = QTableWidgetItem(t) #각 티커 얻어옴
self.tableWidget.setItem(i,0,item)
#해당 티커의 인덱스에 맞는 위치에 추가
app = QApplication(sys.argv)
win = MyWindow()
win.show()
app.exec_()
import sys
import pybithumb
from PyQt5.QtWidgets import *
from PyQt5 import uic
from PyQt5.QtCore import *
tickers = ["BTC", "ETH", "BCH", "ETC"]
form_class = uic.loadUiType("bull.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super(MyWindow, self).__init__()
self.setupUi(self)
timer=QTimer(self)
timer.start(500) #0.5초마다 내용 업데이트함
timer.timeout.connect(self.timeout)
def get_market_infos(self, ticker):
df = pybithumb.get_ohlcv(ticker)
m5 = df['close'].rolling(5).mean()
last_m5 = m5[-2]
price = pybithumb.get_current_price("BTC")
state = None
if price > last_m5:
state = '상승장'
else:
state = '하락장'
return price, last_m5, state
def timeout(self):
for i, t in enumerate(tickers):
item = QTableWidgetItem(t)
self.tableWidget.setItem(i,0,item)
price, last_m5, state = self.get_market_infos(t)
self.tableWidget.setItem(i, 1, QTableWidgetItem(str(price)))
self.tableWidget.setItem(i, 2, QTableWidgetItem(str(last_m5)))
self.tableWidget.setItem(i, 3, QTableWidgetItem(str(state)))
app = QApplication(sys.argv)
win = MyWindow()
win.show()
app.exec_()
import sys
import pybithumb
from PyQt5.QtWidgets import *
from PyQt5 import uic
from PyQt5.QtCore import *
tickers = ["BTC", "ETH", "BCH", "ETC"]
form_class = uic.loadUiType("bull.ui")[0]
class Worker(QThread):#스레드 상속 클래스
finished = pyqtSignal(dict) #사용자 정의 시그널 생성
def run(self):#스레드 실행 시 수행되는 코드
while True:
data = {}
for ticker in tickers:
data[ticker] = self.get_market_infos(ticker)
self.finished.emit(data)#finished라는 이벤트를 발생시킴
self.msleep(500) #0.5초 슬립
def get_market_infos(self, ticker):
try:
df = pybithumb.get_ohlcv(ticker)
m5 = df['close'].rolling(5).mean()
last_m5 = m5[-2]
price = pybithumb.get_current_price("BTC")
state = None
if price > last_m5:
state = '상승장'
else:
state = '하락장'
return (price, last_m5, state)
except:
return (None, None,None)
class MyWindow(QMainWindow, form_class):
def __init__(self):
super(MyWindow, self).__init__()
self.setupUi(self)
self.tableWidget.setRowCount(len(tickers))
self.worker = Worker()
self.worker.finished.connect(self.update_table_widget)
self.worker.start()
@pyqtSlot(dict)
def update_table_widget(self, data):
try:
for i, t in data.items():
index = tickers.index(t)
self.tableWidget.setItem(index,0,QTableWidgetItem(t))
self.tableWidget.setItem(index, 1, QTableWidgetItem(str(i[0]))) #현재가
self.tableWidget.setItem(index, 2, QTableWidgetItem(str(i[1]))) #5일 이동평균
self.tableWidget.setItem(index, 3, QTableWidgetItem(str(i[2]))) #상승/하락장
except:
pass
app = QApplication(sys.argv)
win = MyWindow()
win.show()
app.exec_()