Examples
Example Client
This example will create a Websocket connection, subscribe to a price feed as well as fill and order updates, and place an order.
from zerocap_api import ZerocapWebsocketClient, ZerocapRestClient
import time
api_key = ''
api_secret = ''
zc_rest = ZerocapRestClient(api_key, api_secret, envion="prod")
zc_ws = ZerocapWebsocketClient(api_key, api_secret, envion="prod")
websocket_connection = zc_ws.create_connection()
connect_result = zc_ws.recv(websocket_connection)
print(connect_result)
zc_ws.send({"type": 'price', "symbol": "USDT/AUD"})
zc_ws.send({"type": 'order'})
zc_ws.send({"type": 'trade'})
book = {}
sent_order = False
last_msg_time = 0
while True:
message = zc_ws.recv(websocket_connection)
if message['type'] == 'price' and time.time() - last_msg_time > 5:
data = message['data']
book['bids'] = data['bids']
book['asks'] = data['asks']
if message['type'] == 'order':
print("order", message)
if message['type'] == 'trade':
print("trade", message)
if not sent_order and len(book) > 0:
result = zc_rest.create_order(
symbol='USDT/AUD',
side='buy',
type='limit',
amount='1',
price=float(book['bids'][0][0]) * 0.99)
print(result)
last_msg_time = time.time()
sent_order = True
breakUpdated 10 months ago