This is an unofficial Python wrapper for the Kucoin exchanges REST and Websocket API v3. I am in no way affiliated with Kucoin, use at your own risk.
- PyPi
- https://pypi.python.org/pypi/python-kucoin
- Source code
- https://github.com/sammchardy/python-kucoin
- Documentation
- https://python-kucoin.readthedocs.io/en/latest/
- Examples
- https://github.com/sammchardy/python-kucoin/tree/master/examples
- Implementation of REST endpoints
- Spot and Futures
- Sync and Async suport
- Simple handling of authentication
- Response exception handling
- Implement websockets (note only python3.5+)
- Proxy support
- L2 and L3 Local Order Books
Register an account with Kucoin.
pip install python-kucoinfromkucoinimportClientapi_key='<api_key>'api_secret='<api_secret>'api_passphrase='<api_passphrase>'client=Client(api_key, api_secret, api_passphrase)
# get currenciescurrencies=client.get_currencies()
# get market depthdepth=client.get_order_book('KCS-BTC')
# get symbol klinesklines=client.get_kline_data('KCS-BTC')
# get list of marketsmarkets=client.get_markets()
# place a market buy orderorder=client.create_market_order('NEO', Client.SIDE_BUY, size=20)
# get list of active ordersorders=client.get_active_orders('KCS-BTC')fromkucoinimportAsyncClientapi_key='<api_key>'api_secret='<api_secret>'api_passphrase='<api_passphrase>'client=AsyncClient(api_key, api_secret, api_passphrase)
# get currenciescurrencies=awaitclient.get_currencies()Note only for python3.5+
importasynciofromkucoin.clientimportClientfromkucoin.asyncioimportKucoinSocketManagerapi_key='<api_key>'api_secret='<api_secret>'api_passphrase='<api_passphrase>'asyncdefmain():
globalloop# callback function that receives messages from the socketasyncdefhandle_evt(msg):
ifmsg['topic'] =='/market/ticker:ETH-USDT':
print(f'got ETH-USDT tick:{msg["data"]}')
elifmsg['topic'] =='/market/snapshot:BTC':
print(f'got BTC market snapshot:{msg["data"]}')
elifmsg['topic'] =='/market/snapshot:KCS-BTC':
print(f'got KCS-BTC symbol snapshot:{msg["data"]}')
elifmsg['topic'] =='/market/ticker:all':
print(f'got all market snapshot:{msg["data"]}')
elifmsg['topic'] =='/account/balance':
print(f'got account balance:{msg["data"]}')
elifmsg['topic'] =='/market/level2:KCS-BTC':
print(f'got L2 msg:{msg["data"]}')
elifmsg['topic'] =='/market/match:BTC-USDT':
print(f'got market match msg:{msg["data"]}')
elifmsg['topic'] =='/market/level3:BTC-USDT':
ifmsg['subject'] =='trade.l3received':
ifmsg['data']['type'] =='activated':
# must be logged into see these messagesprint(f"L3 your order activated: {msg['data']}")
else:
print(f"L3 order received:{msg['data']}")
elifmsg['subject'] =='trade.l3open':
print(f"L3 order open: {msg['data']}")
elifmsg['subject'] =='trade.l3done':
print(f"L3 order done: {msg['data']}")
elifmsg['subject'] =='trade.l3match':
print(f"L3 order matched: {msg['data']}")
elifmsg['subject'] =='trade.l3change':
print(f"L3 order changed: {msg['data']}")
client=Client(api_key, api_secret, api_passphrase)
ksm=awaitKucoinSocketManager.create(loop, client, handle_evt)
# for private topics such as '/account/balance' pass private=Trueksm_private=awaitKucoinSocketManager.create(loop, client, handle_evt, private=True)
# Note: try these one at a time, if all are on you will see a lot of output# ETH-USDT Market Tickerawaitksm.subscribe('/market/ticker:ETH-USDT')
# BTC Symbol Snapshotsawaitksm.subscribe('/market/snapshot:BTC')
# KCS-BTC Market Snapshotsawaitksm.subscribe('/market/snapshot:KCS-BTC')
# All tickersawaitksm.subscribe('/market/ticker:all')
# Level 2 Market Dataawaitksm.subscribe('/market/level2:KCS-BTC')
# Market Execution Dataawaitksm.subscribe('/market/match:BTC-USDT')
# Level 3 market dataawaitksm.subscribe('/market/level3:BTC-USDT')
# Account balance - must be authenticatedawaitksm_private.subscribe('/account/balance')
whileTrue:
print("sleeping to keep loop open")
awaitasyncio.sleep(20, loop=loop)
if__name__=="__main__":
loop=asyncio.get_event_loop()
loop.run_until_complete(main())For more check out the documentation.
- If you use Binance check out my python-binance library.
- Check out CCXT for more than 100 crypto exchanges with a unified trading API.