Kucoin / kucoin-futures-python-sdk

MIT License
7 stars 4 forks source link

Broken sample in the readme #46

Open huncholane opened 1 year ago

huncholane commented 1 year ago
import asyncio
from kucoin_futures.client import WsToken
from kucoin_futures.ws_client import KucoinFuturesWsClient

async def main():
    async def deal_msg(msg):
        if msg.get('topic') == '/contractMarket/level2:XBTUSDM':
            print(f'Get XBTUSDM Ticker:{msg["data"]}')
        elif msg.get('topic') == '/contractMarket/level3:XBTUSDTM':
            print(f'Get XBTUSDTM level3:{msg["data"]}')

    # is public
    # client = WsToken()
    # is private
    client = WsToken(is_sandbox=False, url='')
    # is sandbox
    # client = WsToken(is_sandbox=True)
    ws_client = await KucoinFuturesWsClient.create(asyncio.get_running_loop(), client, deal_msg, private=False)
    await ws_client.subscribe('/contractMarket/level2:XBTUSDM')
    await ws_client.subscribe('/contractMarket/level3:XBTUSDM')
    while True:
        await asyncio.sleep(60)

if __name__ == "__main__":
    asyncio.run(main())

The sample needs to change from msg['topic'] to msg.get('topic'). It creates an error that causes the websockets to stop working.

PaulGWebster commented 10 months ago

Also if you do or do not correct it to msg.get you get the following:

Traceback (most recent call last):
  File "/home/paul.webster/Work/Trading/activebook/wsreader/kucoin-websocket.py", line 28, in 
    loop.run_until_complete(main())
  File "/home/paul.webster/local/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/paul.webster/Work/Trading/activebook/wsreader/kucoin-websocket.py", line 23, in main
    await asyncio.sleep(60, loop=loop)
TypeError: sleep() got an unexpected keyword argument 'loop'

Purely non modified example code(except msg.get):

import asyncio
from kucoin_futures.client import WsToken
from kucoin_futures.ws_client import KucoinFuturesWsClient

async def main():
    async def deal_msg(msg):
        if msg.get('topic') == '/contractMarket/level2:XBTUSDM':
            print(f'Get XBTUSDM Ticker:{msg["data"]}')
        elif msg.get('topic') == '/contractMarket/level3:XBTUSDTM':
            print(f'Get XBTUSDTM level3:{msg["data"]}')

    # is public
    # client = WsToken()
    # is private
    client = WsToken(key='', secret='', passphrase='', is_sandbox=False, url='')
    # is sandbox
    # client = WsToken(is_sandbox=True)
    ws_client = await KucoinFuturesWsClient.create(loop, client, deal_msg, private=False)
    await ws_client.subscribe('/contractMarket/level2:XBTUSDM')
    await ws_client.subscribe('/contractMarket/level3:XBTUSDM')
    while True:
        await asyncio.sleep(60, loop=loop)

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

Corrected by changing:

await asyncio.sleep(60, loop=loop)

to:

await asyncio.sleep(60, loop)