Skip to content

Instantly share code, notes, and snippets.

@idkravitz
Last active May 21, 2021 04:57
Show Gist options
  • Select an option

  • Save idkravitz/8e25ae614f553a17e289c1083868cf54 to your computer and use it in GitHub Desktop.

Select an option

Save idkravitz/8e25ae614f553a17e289c1083868cf54 to your computer and use it in GitHub Desktop.
import asyncio
from binance import AsyncClient, BinanceSocketManager
from binance.depthcache import DepthCache, DepthCacheManager
from psutil import Process
caches_sum_length = {}
cache_10_bids = {}
cache_10_asks = {}
cache_num_of_zeros = {}
SYMBOLS = ['DOGE', 'BTT', 'HOT', 'WIN', 'AKRO', 'KEY', 'ICP', 'NANO', 'ICX', 'DENT', 'MBL', 'ADA', 'SHIB']
async def run_depth():
client = await AsyncClient.create()
bm = BinanceSocketManager(client)
dcms = {
s: DepthCacheManager(client, s + 'USDT', bm=bm, limit=10)
for s in SYMBOLS
}
async def handlerLoop(dcm:DepthCacheManager):
async with dcm as dcm_socket:
while True:
depth_cache:DepthCache = await dcm_socket.recv()
asks = depth_cache.get_asks()
bids = depth_cache.get_bids()
caches_sum_length[depth_cache.symbol] = len(asks) + len(bids)
cache_10_asks[depth_cache.symbol] = asks[:10]
cache_10_bids[depth_cache.symbol] = bids[:10]
cache_num_of_zeros[depth_cache.symbol] = len([x for x in asks if x[1] == 0.0]) + len(
[x for x in bids if x[1] == 0.0])
futures = [handlerLoop(dcm) for dcm in dcms.values()]
await asyncio.gather(*futures)
async def mon_loop():
while True:
keys = sorted(caches_sum_length.keys())
print("="*80)
print(f"{Process().memory_info().rss / 1024 ** 2:0.4f} MB used")
for k in keys:
print(f"{k}={caches_sum_length[k]}\t", end='')
print()
for k in keys:
if k in cache_num_of_zeros:
print(f"{k}(0)={cache_num_of_zeros[k]}\t", end='')
print()
print("="*80)
await asyncio.sleep(10)
async def main():
await asyncio.gather(run_depth(), mon_loop())
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment