質問
現在、BYBIT APIを使った取引の学習をしていますが、websocket接続でリアルタイムに注文情報を出力させようとしても
何故か出力してくれません。
また、python初学者なのでどこが悪いのか全く見当もつきません。
間違いなどをご教示いただければと思い投稿させていただきました。
実現したいこと
発生している問題
Private channel message received: { 'success': True, 'ret_msg': '', 'conn_id': '1203e8fffea16b07-00000001-00001e6b-b43df54fecf53564-af3b7bbf', 'req_id': '', 'op': 'auth' } Private channel message received: { 'success': True, 'ret_msg': '', 'conn_id': '1203e8fffea16b07-00000001-00001e6b-b43df54fecf53564-af3b7bbf', 'req_id': '', 'op': 'subscribe' }
コンソールにはこのように表示されているのに、注文を出してもコンソールに出力されない
該当のソースコード
main.py
1import json 2import hmac 3import asyncio 4import websockets 5import time 6from rich import print 7 8# - Bybit API endpoints 9public_endpoint = "wss://stream-testnet.bybit.com/spot/public/v3" 10private_endpoint = "wss://stream-testnet.bybit.com/spot/private/v3" 11 12# 取引通貨ペア 13coin = 'BTCUSDT' 14private_topic = "order" 15] 16 17# - API & SECRET 18api_key = "****************" 19api_secret = "************************************" 20 21async def handle_public_message(message): 22 print("Public channel message received:", json.loads(message)) 23 24async def handle_private_message(message): 25 print("Private channel message received:", json.loads(message)) 26 27async def connect_to_websocket(endpoint, message_handler, auth=False): 28 async with websockets.connect(endpoint) as ws: 29 if auth: 30 # - Add authentication headers for private channels 31 expires = int((time.time() + 1) * 1000) 32 signature = str(hmac.new( 33 bytes(api_secret, "utf-8"), 34 bytes(f"GET/realtime{expires}", "utf-8"), digestmod="sha256" 35 ).hexdigest()) 36 await ws.send(json.dumps({"op": "auth", "args": [api_key, expires, signature]})) 37 await ws.send(json.dumps({"op": "subscribe", "args": [private_topic]})) 38 39 while True: 40 message = await ws.recv() 41 await message_handler(message) 42 43async def main(): 44 45 # - taskの登録 46 private_task = asyncio.create_task(connect_to_websocket(private_endpoint, handle_private_message, True)) 47 48 await private_task 49if __name__ == '__main__': 50 asyncio.run(main()) 51
試したこと
コードの記述は省略していますが、public_channelは問題なくwebsocket接続できており
リアルタイムで情報が表示されています。
補足情報(FW/ツールのバージョンなど)
BYBIT API V3
python 3.10
windows OS

0 コメント