-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-tickers.py
43 lines (30 loc) · 1.43 KB
/
async-tickers.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
# -*- coding: utf-8 -*-
import asyncio
import os
import sys
root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(root + '/python')
import ccxt.async as ccxt # noqa: E402
def get_active_symbols(exchange):
return [symbol for symbol in exchange.symbols if is_active_symbol(exchange, symbol)]
def is_active_symbol(exchange, symbol):
return ('.' not in symbol) and (('active' not in exchange.markets[symbol]) or (exchange.markets[symbol]['active']))
async def fetch_ticker(exchange, symbol):
ticker = await exchange.fetchTicker(symbol)
print(exchange.id, symbol, ticker)
return ticker
async def fetch_tickers(exchange):
await exchange.load_markets()
print(exchange.id, 'fetching all tickers by simultaneous multiple concurrent requests')
symbols_to_load = get_active_symbols(exchange)
input_coroutines = [fetch_ticker(exchange, symbol) for symbol in symbols_to_load]
tickers = await asyncio.gather(*input_coroutines, return_exceptions=True)
for ticker, symbol in zip(tickers, symbols_to_load):
if not isinstance(ticker, dict):
print(exchange.id, symbol, 'error')
else:
print(exchange.id, symbol, 'ok')
print(exchange.id, 'fetched', len(list(tickers)), 'tickers')
asyncio.get_event_loop().run_until_complete(fetch_tickers(ccxt.bitfinex({
'enableRateLimit': True, # this option enables the built-in rate limiter
})))