Hi,
First, thanks for this amazing work you have done. Second my problem is about below code. I constantly get "Unclosed client session client_session:
timeout = time.time() + 60*60*8
async def bitfinex():
bitfinex = ccxt.bitfinex()
return await bitfinex.fetch_ticker()
while True:
if time.time() > timeout:
break
try:
bitfinexratiolast = float(asyncio.get_event_loop().run_until_complete(ccxt.bitfinex().fetch_ticker('ETH/BTC'))['last'])
bitfinexbtctrylast = float(asyncio.get_event_loop().run_until_complete(ccxt.bitfinex().fetch_ticker('BTC/USD'))['last'])
except ccxt.DDoSProtection:
print('ddos')
continue
print(bitfinexratiolast)
print(bitfinexbtctrylast)
You don't need to do ccxt.bitfinex()
in every line. I suggest you learn more basic Python. Focus on working with classes and objects instances.
This code should work:
import asyncio
import ccxt.async as ccxt
async def run():
bitfinex = ccxt.bitfinex({'enableRateLimit': True})
while True:
try:
bitfinexratio = await bitfinex.fetch_ticker('ETH/BTC')
bitfinexbtctry = await bitfinex.fetch_ticker('BTC/USD')
print('----------------------------------------')
datetimestring = bitfinex.iso8601(bitfinex.milliseconds())
print(datetimestring, bitfinexratio['last'], bitfinexbtctry['last'])
except Exception as e:
print(type(e).__name__ + ' ' + str(e))
asyncio.get_event_loop().run_until_complete(run())
More:
Thanks a lot! now it is working i will look at basic Python courses as well (y)
However, this one is much better:
import asyncio
import ccxt.async as ccxt
async def run():
bitfinex = ccxt.bitfinex({'enableRateLimit': True})
while True:
try:
tickers = await bitfinex.fetch_tickers()
bitfinexratio = tickers['ETH/BTC']
bitfinexbtctry = tickers['BTC/USD']
print('----------------------------------------')
datetimestring = bitfinex.iso8601(bitfinex.milliseconds())
print(datetimestring, bitfinexratio['last'], bitfinexbtctry['last'])
except Exception as e:
print(type(e).__name__ + ' ' + str(e))
asyncio.get_event_loop().run_until_complete(run())
I'd recommend to fetch all tickers in one go like shown above if you need more than one of them.
Most helpful comment
You don't need to do
ccxt.bitfinex()
in every line. I suggest you learn more basic Python. Focus on working with classes and objects instances.This code should work:
More: