'Binance error - Too much request weight used; current limit is 1200 request weight per 1 MINUTE

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex B