3
exchange.load_markets()
while 1:
    try:
        bars = exchange.fetch_ohlcv(ETH/USDT, timeframe='5m', limit=5)                                          
        df = pd.DataFrame(bars[:], columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
        df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')

    except ccxt.BaseError as Error:
        print("[ERROR] ", Error)
        continue

Hello, I am doing algotrade with Python in Binance. I am using ccxt. I am running above code and i am not getting any error message regarding to request usage. However, after i am running 2nd trade bot in same IP with same code i am getting "more than 1200 request usage" error. How can i calculate how many request i am creating in a minute? If i can calculate it, i will schedule to download ohlvc data in 2 or 3 seconds, or how many seconds it needs. Thank you.

euphrates85
  • 87
  • 1
  • 8
  • The canonical source for answers on this matter is the people who run the server. – Charles Duffy May 08 '22 at 15:22
  • That said, have you started by reading https://www.binance.com/en/support/faq/360004492232 (being documentation _written by_ the people who run said server)? It's one of the first things found by a Google search, and looks very relevant. – Charles Duffy May 08 '22 at 15:23
  • (also, https://www.binance.com/en/support/announcement/f3d75a44fc7b4610b080b9c3499ed075 gives an idea of what request weights look like) – Charles Duffy May 08 '22 at 15:27
  • I have seen this but i couldnt understand what is the weight of feftching ohlcv data – euphrates85 May 08 '22 at 15:44
  • The place to start is to read the source to the API you're calling and see which API endpoints it calls and in what pattern. If you don't do that yourself, you're just asking someone else to do it for you. – Charles Duffy May 08 '22 at 15:51

1 Answers1

4

One way you could do this is by retrieving the exchange time and saving it in a variable which you then substract to the current time every time you loop through your code.

This will give you an idea of how many milliseconds there are between loops to see if you are reaching the limit:

import ccxt
import pandas as pd

exchange = ccxt.binance()
exchange.load_markets()

previous_time = 0

while 1:
    try:
        current_time = exchange.fetchTime()
        time_difference = current_time - previous_time
        previous_time = current_time
        print(time_difference)
        
        bars = exchange.fetch_ohlcv('ETH/USDT', timeframe='5m', limit=5)                                          
        df = pd.DataFrame(bars[:], columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
        df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
       # print(df)

    except ccxt.BaseError as Error:
        print("[ERROR] ", Error)
        continue

As far as I can see a request is made every 0.5s to 1s which is well below the 1200 requests per minute / 60 seconds = 20 requests per second. That being said if you are running other bots in parallel then you can quickly reach the limit.

That being said the exact error message is the following:

{"code":-1003,"msg":"Too many requests; current limit is 1200 requests per minute. 
Please use the websocket for live updates to avoid polling the API."}

As the error message suggests, you should really be looking at using the websocket for live updates as the way you are doing it is not the proper way if you want to keep polling the API on very short time frames.

This will also require you learning to use asyncio event loops. A good tutorial can be found here.

Finally, here is a link to a working example using websockets and asyncio to retreive prices on binance.

Alex B
  • 1,092
  • 1
  • 14
  • 39
  • @euphrates85 you're very welcome. In that case can you accept my answer as the correct one for future users please. – Alex B May 15 '22 at 17:23