Skip to content
This repository was archived by the owner on Dec 11, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/.idea
./vscode
*.pyc
/__pycache__
test_code.py
/__pycache__
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.formatting.provider": "black"
}
71 changes: 25 additions & 46 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Features

update
----------
- 2021 12/01
- 2022 06/24

Quick Start
-----------
Expand All @@ -39,56 +39,37 @@ Note:API key can only be generated after logging in.
.. code:: python

# MarketData
from kucoin_futures.client import Market
client = Market(url='https://api-futures.kucoin.com')
from kucoin_futures.client import FuturesApi
client = FuturesApi(url='https://api-futures.kucoin.com')
# client = Market()
# or connect to Sandbox
# client = Market(url='https://api-sandbox-futures.kucoin.com')
# client = Market(is_sandbox=True)

# get l3_order_book
l3_depth = client.l3_order_book('XBTUSDM')
# get order_book `GET /api/v2/order-book`
order_book = client.get_order_book(symbol="BTCUSDTM")
print(order_book)

# get l2_order_book
l2_depth = client.l2_order_book('XBTUSDM')

# get symbol ticker
klines = client.get_ticker("XBTUSDM")

# get symbol ticker
server_time = client.get_server_timestamp()

api_key = '<api_key>'
api_secret = '<api_secret>'
api_passphrase = '<api_passphrase>'
# get Best Maker `GET /api/v2/ticker/bookTicker`
best_maker = client.best_maker(symbol="BTCUSDTM")

# Trade
from kucoin_futures.client import Trade
client = Trade(key='', secret='', passphrase='', is_sandbox=False, url='')
client = FuturesApi(key='', secret='', passphrase='', is_sandbox=False, url='')

# or connect to Sandbox
# client = Trade(api_key, api_secret, api_passphrase, is_sandbox=True)
# client = FuturesApi(api_key, api_secret, api_passphrase, is_sandbox=True)

# place a limit buy order
order_id = client.create_limit_order('XBTUSDM', 'buy', '1', '30', '8600')

# place a market buy order Use cautiously
order_id = client.create_market_order('XBTUSDM', 'buy', '1')

# cancel limit order
client.cancel_order('5bd6e9286d99522a52e458de')

# cancel all limit order
client.cancel_all_limit_order('XBTUSDM')

# User
from kucoin_futures.client import User
client = User(api_key, api_secret, api_passphrase)

# or connect to Sandbox
# client = User(api_key, api_secret, api_passphrase, is_sandbox=True)
order = client.order_placement(
symbol="BTCUSDTM",
# clientOid=uuid.uuid1().hex,
side="BUY",
typ="LIMIT",
price=1,
size=1,
)
print(order)

address = client.get_withdrawal_quota('XBT')

Websockets
----------
Expand All @@ -101,11 +82,10 @@ Websockets


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

# is public
# client = WsToken()
Expand All @@ -114,12 +94,11 @@ Websockets
# 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')
await ws_client.subscribe(test_topic)
while True:
await asyncio.sleep(60, loop=loop)
await asyncio.sleep(60)


if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop = asyncio.get_event_loop_policy().get_event_loop()
loop.run_until_complete(main())
8 changes: 8 additions & 0 deletions examples/examples_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/python
# -*- coding:utf-8 -*-
gloabl_examples_api_config = {
"key": "", # Please set you corresponding API key
"secret": "", # Please set you corresponding API secret
"passphrase": "", # Please set you corresponding API secret
"url": "https://api-v2-futures.kucoin.com", # Environment URL
}
130 changes: 130 additions & 0 deletions examples/test/test_market_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/usr/bin/python
# -*- coding:utf-8 -*-
import time
import unittest
import sys
import os
import json


class TestContract(unittest.TestCase):
def setUp(self) -> None:
current_path = os.path.abspath(os.path.dirname(__file__))
root = os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".")

sys.path.append(root)
from kucoin_futures.client import FuturesApi
from examples_config import gloabl_examples_api_config

self.client = FuturesApi(**gloabl_examples_api_config)

def json_print(self, ret):
print(json.dumps(ret, ensure_ascii=False, indent=2))

def test_get_active_contracts(self): # pass
ret = self.client.get_active_contracts()
self.json_print(ret)

def test_get_a_certain_contract(self): # pass
ret = self.client.get_a_certain_contract(symbol="BTCUSDTM")
self.json_print(ret)

def test_get_risk_limit(self):#pass
ret = self.client.get_risk_limit(symbol="BTCUSDTM")
self.json_print(ret)

def test_get_klines(self): # pass
ret = self.client.get_klines(granularity=5, symbol="BTCUSDTM")
self.json_print(ret)

def test_query_funding_rate_list(self): # pass
ret = self.client.query_funding_rate_list(symbol="BTCUSDTM")
self.json_print(ret)

def test_get_contract_mark_price(self):
ret = self.client.get_contract_mark_price(symbol="BTCUSDTM")
self.json_print(ret)


class TestOrderBook(unittest.TestCase):
def setUp(self) -> None:
current_path = os.path.abspath(os.path.dirname(__file__))
root = os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".")

sys.path.append(root)
from kucoin_futures.client import FuturesApi
from examples_config import gloabl_examples_api_config

self.client = FuturesApi(**gloabl_examples_api_config)

def json_print(self, ret):
print(json.dumps(ret, ensure_ascii=False, indent=2))

def test_get_order_book(self):
ret = self.client.get_order_book(symbol="BTCUSDTM")
self.json_print(ret)

def test_best_maker(self):
ret = self.client.best_maker(symbol="BTCUSDTM")
self.json_print(ret)


class TestQuotesSnapshot(unittest.TestCase):
def setUp(self) -> None:
current_path = os.path.abspath(os.path.dirname(__file__))
root = os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".")

sys.path.append(root)
from kucoin_futures.client import FuturesApi
from examples_config import gloabl_examples_api_config

self.client = FuturesApi(**gloabl_examples_api_config)

def json_print(self, ret):
print(json.dumps(ret, ensure_ascii=False, indent=2))

def test_get_the_latest_transaction_price(self):
ret = self.client.get_the_latest_transaction_price(symbol="BTCUSDTM")
self.json_print(ret)

def test_get_most_recent_record(self):
ret = self.client.get_most_recent_record(symbol="BTCUSDTM")
self.json_print(ret)


class TestDate(unittest.TestCase):
def setUp(self) -> None:
current_path = os.path.abspath(os.path.dirname(__file__))
root = os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".")

sys.path.append(root)
from kucoin_futures.client import FuturesApi
from examples_config import gloabl_examples_api_config

self.client = FuturesApi(**gloabl_examples_api_config)

def json_print(self, ret):
print(json.dumps(ret, ensure_ascii=False, indent=2))

def test_get_server_time(self): # pass
ret = self.client.get_server_time()
self.json_print(ret)


class TestServerStatus(unittest.TestCase):
def setUp(self) -> None:
current_path = os.path.abspath(os.path.dirname(__file__))
root = os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".")

sys.path.append(root)
from kucoin_futures.client import FuturesApi
from examples_config import gloabl_examples_api_config

self.client = FuturesApi(**gloabl_examples_api_config)

def json_print(self, ret):
print(json.dumps(ret, ensure_ascii=False, indent=2))

def test_get_server_status(self): # null
ret = self.client.get_server_status()
self.json_print(ret)
Loading