-
Notifications
You must be signed in to change notification settings - Fork 3
/
multiob.py
64 lines (54 loc) · 1.72 KB
/
multiob.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
import asyncio
import ccxt.async_support as ccxta # noqa: E402
import time
import os
import sys
import ccxtpro
import pandas as pd
import streamlit as st
symbol = 'ETH/USDT'
def sync_client(exchange_id):
orderbook = None
exchange = getattr(ccxtpro, exchange_id)()
try:
exchange.load_markets()
market = exchange.market(symbol)
orderbook = exchange.watch_order_book(market['symbol'])
except Exception as e:
print(type(e).__name__, str(e))
return { 'exchange': exchange.id, 'orderbook': orderbook }
async def async_client(exchange_id):
orderbook = None
exchange = getattr(ccxtpro, exchange_id)()
try:
await exchange.load_markets()
market = exchange.market(symbol)
orderbook = await exchange.watch_order_book(symbol)
except Exception as e:
print(type(e).__name__, str(e))
await exchange.close()
return { 'exchange': exchange.id, 'orderbook': orderbook }
async def multi_orderbooks(exchanges):
input_coroutines = [async_client(exchange) for exchange in exchanges]
orderbooks = await asyncio.gather(*input_coroutines, return_exceptions=True)
to_fetch = pd.DataFrame(orderbooks)
placeholder = st.empty()
with placeholder:
st.write(to_fetch)
# st.write(orderbooks)
# print(orderbooks)
return orderbooks
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
while True:
# global orderbooks
exchanges = ["ftx", "binance"]
loop.run_until_complete(multi_orderbooks(exchanges))
# st.write(orderbooks)
# ftx_holder = st.empty()
# exchange_dump = orderbooks['orderbooks']
# if exchange_dump == 'FTX':
# orderbooks = ftx_ob
# ftx_ob = orderbooks
# with ftx_holder:
# st.write(ftx_ob)