Skip to content

Instantly share code, notes, and snippets.

@jmoz
Created July 1, 2020 10:52
Show Gist options
  • Save jmoz/a4d124cf9da63e814e8778125fe6aa11 to your computer and use it in GitHub Desktop.
Save jmoz/a4d124cf9da63e814e8778125fe6aa11 to your computer and use it in GitHub Desktop.
asyncio aiohttp FTX prices
import asyncio
import aiohttp
symbols = [
'BTC-PERP',
'ETH-PERP',
'EOS-PERP',
'BCH-PERP',
'LTC-PERP',
'LINK-PERP',
'XRP-PERP',
'KNC-PERP',
'ALT-PERP',
'SHIT-PERP',
]
async def http_fetch(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
result = await response.json()
return result
async def get_symbol_price(symbol):
result = await http_fetch(f"https://ftx.com/api/markets/{symbol}/trades?limit=1")
return symbol, result['result'][0]['price']
async def main():
c = []
for symbol in symbols:
c.append(get_symbol_price(symbol))
result = await asyncio.gather(*c)
for symbol, price in result:
print(f'{symbol}: {price}')
if __name__ == '__main__':
import time
s = time.perf_counter()
asyncio.run(main())
elapsed = time.perf_counter() - s
print(f"{__file__} executed in {elapsed:0.2f} seconds.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment