Ccxt: Unclosed client session and ddos Error for bitfinex

Created on 28 Nov 2017  ·  3Comments  ·  Source: ccxt/ccxt

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: " and Ddos error. I also tried to pu time.sleep(8) o get rid of it but didnt work. Is there any way to make it work efficiently? appreciate your help on this.

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)

question

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:

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:

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings