-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathasyncio_test.py
83 lines (64 loc) · 2.29 KB
/
asyncio_test.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
#!/usr/bin/env python
import asyncio
import random
import warnings
from arbi import TickerObserver
from bitfinex import BitFinex
from gdax import GDAX
from binance import Binance
from bitmex import BitMEX
from counters import rates_monitor
from wsserver import srv
# TODO :
# kraken
# - bitmex
# - gdax
# - bitfinex
# - binance
obs = None
# obs = TickerObserver()
Exchanges = [
# BitFinex(ticker_observer=obs, markets=BitFinex.all_markets),
# BitMEX(ticker_observer=obs, markets=BitMEX.all_markets),
# BitMEX(ticker_observer=obs, markets=['btc_usd', 'eth_btc', 'ltc_btc']),
# BitFinex(ticker_observer=obs, markets=['btc_usd', 'eth_btc', 'btc_eur', 'eth_usd']),
# GDAX(ticker_observer=obs, markets=['btc_usd', 'btc_eur', 'eth_eur', 'eth_usd', 'eth_btc']),
# GDAX(ticker_observer=obs, markets=GDAX.all_markets),
# GDAX(ticker_observer=obs, markets=['btc_usd']),
Binance(ticker_observer=obs, markets=Binance.all_markets),
# Binance(ticker_observer=obs, markets=['eth_btc', 'ltc_btc', ]),
# Binance(markets=['eth_btc', ])#, 'bnb_btc', 'eos_btc']),
# BitMEX(markets=['btc_usd'], ticker_observer=obs)#, 'ada_btc', 'bch_btc', 'xrp_btc', 'btc_usd', 'eth_btc', 'ltc_btc']),
# Binance(markets=['eth_btc', ])
# GDAX(markets=['ltc_usd', ]),
# CryptoExchange(url="wss://api.bitfinex.com/ws/", symbol="BTCUSD"),
]
async def closer():
while True:
await asyncio.sleep(5)
exchange = random.choice(Exchanges)
name = exchange.__class__.__name__
print('>>> closing', name)
await exchange.close()
print('CLOSED', name)
def main():
# logging.basicConfig(level=logging.DEBUG)
loop = asyncio.get_event_loop()
loop.set_debug(True)
warnings.simplefilter('always', ResourceWarning)
# loop.slow_callback_duration = 0.001
try:
run_all = [exchange.run() for exchange in Exchanges] + [srv.run()] + [rates_monitor()] # + [closer()]
asyncio.gather(*run_all)
loop.run_forever()
except KeyboardInterrupt:
loop.stop()
# Find all running tasks:
pending = asyncio.Task.all_tasks()
# Run loop until tasks done:
loop.run_until_complete(asyncio.gather(*pending))
print("Shutdown complete ...")
finally:
loop.close()
if __name__ == '__main__':
main()