loading
open in
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import websocket
import json

ws = websocket.WebSocket()
ws.connect("wss://wsapi.coinut.com")

r = {"request":"inst_order_book","inst_id":490589, "subscribe": True, "nonce":704114}
ws.send(json.dumps(r))

orderbook = {'buy': {}, 'sell': {}}
while True:
    data = json.loads(ws.recv())
    if data['reply'] == 'inst_order_book':
        for buy in data['buy']:
            orderbook['buy'][buy['price']] = buy['qty']

        for sell in data['sell']:
            orderbook['sell'][sell['price']] = sell['qty']

    elif data['reply'] == 'inst_order_book_update':
        qty = data['qty']
        price = data['price']
        count = data['count']
        side = 'buy' if data['side'] == 'BUY' else 'sell'
        if count != 0:
            orderbook[side][price] = qty
        else:
            del orderbook[side][price]

    print "************* orderbook updated *****************"
    print json.dumps(orderbook, indent=2)