-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket-poloniex-orderbook.py
98 lines (83 loc) · 2.73 KB
/
websocket-poloniex-orderbook.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
import sys
import pprint
import traceback
from copy import deepcopy
pp = pprint.PrettyPrinter(depth=6)
root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(root + '/python')
# import ccxt # noqa: E402
import ccxt.async_support as ccxt # noqa: E402
import asyncio # noqa: E402
loop = asyncio.get_event_loop()
# import txaio
# txaio.start_logging(level='debug')
last_ob = None
async def main():
exchange_id = 'poloniex'
apiKey = 'do not care'
secret = 'do not care'
limit = 100
symbol = 'BTC/USDT'
exchange = getattr(ccxt, exchange_id)({
"apiKey": apiKey,
"secret": secret,
"enableRateLimit": True,
'verbose': False,
'timeout': 5 * 1000,
# 'wsproxy': 'http://185.93.3.123:8080/',
})
@exchange.on('err')
def websocket_error(err, conxid): # pylint: disable=W0612
print(type(err).__name__ + ":" + str(err))
traceback.print_tb(err.__traceback__)
traceback.print_stack()
loop.stop()
@exchange.on('ob')
def websocket_ob(symbol, ob): # pylint: disable=W0612
global last_ob
print("ob updated: " + symbol)
sys.stdout.flush()
#pp.pprint(ob)
if last_ob is not None:
for side in ['bids', 'asks']:
for price, _ in last_ob[side][:10]:
flgFound = False
n = 0
for price2, _ in ob[side]:
n += 1
if price == price2 or n == 100:
flgFound = True
break
if not flgFound:
print('Found removal in top 10 of ' + side + ' for ' + str(price))
print('Old:')
pp.pprint(last_ob)
print('-----')
print('New:')
pp.pprint(ob)
quit()
last_ob = deepcopy(ob)
sys.stdout.flush()
print("subscribe: " + symbol)
sys.stdout.flush()
await exchange.websocket_subscribe('ob', symbol, {'limit': limit})
print("subscribed: " + symbol)
sys.stdout.flush()
ob = await exchange.websocket_fetch_order_book(symbol, limit) # noqa: F841 pylint: disable=W0612
print("ob fetched: " + symbol)
# print(ob)
sys.stdout.flush()
# await asyncio.sleep(15)
# print("unsubscribe: " + symbol)
# sys.stdout.flush()
# await exchange.websocket_unsubscribe('ob', symbol)
# print("unsubscribed: " + symbol)
# sys.stdout.flush()
# await exchange.close()
print("before start")
loop.run_until_complete(main())
loop.run_forever()
# loop.stop()
# loop.close()
print("after complete")