Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken sample in the readme #46

Open
huncholane opened this issue Jan 12, 2023 · 1 comment
Open

Broken sample in the readme #46

huncholane opened this issue Jan 12, 2023 · 1 comment
Assignees

Comments

@huncholane
Copy link

huncholane commented Jan 12, 2023

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
Copy link

PaulGWebster commented Dec 16, 2023

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)

@ISAAC-XXYYZZ ISAAC-XXYYZZ self-assigned this Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants