Skip to content

Instantly share code, notes, and snippets.

@koorukuroo
Created May 12, 2025 03:55
Show Gist options
  • Save koorukuroo/c2905417926a0ae6374287fa3bc9276d to your computer and use it in GitHub Desktop.
Save koorukuroo/c2905417926a0ae6374287fa3bc9276d to your computer and use it in GitHub Desktop.
import asyncio
import websockets
import json
from datetime import datetime
def format_number(val):
try:
return f"{val:,.2f}" if isinstance(val, float) else f"{val:,}"
except:
return "N/A"
async def upbit_ws():
url = "wss://api.upbit.com/websocket/v1"
async with websockets.connect(url) as websocket:
subscribe_data = [
{"ticket": "test"},
{
"type": "ticker",
"codes": ["KRW-BTC"]
},
{"format": "DEFAULT"}
]
await websocket.send(json.dumps(subscribe_data))
while True:
data = await websocket.recv()
parsed = json.loads(data)
# KST 시간 변환
timestamp = parsed.get("timestamp", None)
if timestamp:
timestamp_kst = datetime.fromtimestamp(timestamp / 1000).strftime("%Y-%m-%d %H:%M:%S")
else:
timestamp_kst = "N/A"
print("\n📡 [실시간 업비트 시세 - KRW-BTC]")
print(f"⏱️ 수신 시각 : {timestamp_kst}")
print(f"📊 현재가 : {format_number(parsed.get('trade_price'))} 원")
print(f"📈 고가 / 저가 : {format_number(parsed.get('high_price'))} / {format_number(parsed.get('low_price'))}")
print(f"🧭 시가 / 종가 : {format_number(parsed.get('opening_price'))} / {format_number(parsed.get('prev_closing_price'))}")
print(f"🪙 체결량 : {parsed.get('trade_volume', 0):.4f} BTC")
print(f"💹 거래 방향 : {'매수 (BID)' if parsed.get('ask_bid') == 'BID' else '매도 (ASK)'}")
print(f"🧮 전일 대비 : {format_number(parsed.get('signed_change_price'))} 원 ({parsed.get('signed_change_rate', 0) * 100:.2f}%)")
print(f"📆 체결 일시 : {parsed.get('trade_date')} {parsed.get('trade_time')}")
print(f"💸 누적 거래대금 : {parsed.get('acc_trade_price') / 1_0000_0000:.2f} 억 원")
print(f"💰 누적 거래량 : {parsed.get('acc_trade_volume', 0):.4f} BTC")
print(f"🔁 24H 거래대금 : {parsed.get('acc_trade_price_24h') / 1_0000_0000:.2f} 억 원")
print(f"🔄 24H 거래량 : {parsed.get('acc_trade_volume_24h', 0):.4f} BTC")
print(f"📈 매도호가 : {format_number(parsed.get('ask_price'))} 원")
print(f"📉 매수호가 : {format_number(parsed.get('bid_price'))} 원")
print(f"📦 매도 누적량 : {parsed.get('acc_ask_volume', 0):.4f} BTC")
print(f"📥 매수 누적량 : {parsed.get('acc_bid_volume', 0):.4f} BTC")
print(f"📍 52주 최고가 : {format_number(parsed.get('highest_52_week_price'))} 원 ({parsed.get('highest_52_week_date')})")
print(f"📍 52주 최저가 : {format_number(parsed.get('lowest_52_week_price'))} 원 ({parsed.get('lowest_52_week_date')})")
print(f"📡 마켓 상태 : {parsed.get('market_state')}")
print(f"⚠️ 마켓 경고 : {parsed.get('market_warning')}")
print(f"🚫 거래 정지 여부 : {'O' if parsed.get('is_trading_suspended') else 'X'}")
print("-" * 70)
asyncio.run(upbit_ws())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment