sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
6.09k stars 2.21k forks source link

get_historical_klines() takes forever and gives no output #1416

Open DSDecay opened 6 months ago

DSDecay commented 6 months ago

Out of the blue, get_historical_kline() seems to stop working properly:

from binance.client import Client

def get_earliest_kline(client, symbol): start_str = 1210685792000 try: klines = client.get_historical_klines(symbol, Client.KLINE_INTERVAL_1MINUTE, start_str) if klines: print("Data retrieved:", klines) return klines[0] else: print("No data received for the request.") except Exception as e: print(f"An error occurred: {e}") return None

earliest_kline = get_earliest_kline(client, "ETHBTC") if earliest_kline: print(f"Earliest kline timestamp for ETHBTC: {datetime.fromtimestamp(earliest_kline[0] / 1000)}") else: print("No klines found for ETHBTC since 1 Jan, 2008.")

It should have returned first available kline, due to too early startTime (which was very much intentional), but it returns nothing. It does not throw any exceptions, but instantly sends requests.

OUTPUT: DEBUG:urllib3.connectionpool:https://api.binance.com:443 "GET /api/v3/klines?interval=1m&limit=1&startTime=1500004920000&symbol=ETHBTC HTTP/1.1" 200 141 DEBUG:urllib3.connectionpool:https://api.binance.com:443 "GET /api/v3/klines?interval=1m&limit=1&startTime=1500004980000&symbol=ETHBTC HTTP/1.1" 200 141 ...infinite requests of the same type... . . .

Meanwhile, using requests works as expected:

import requests url = "https://api.binance.com/api/v3/klines" params = { 'symbol': 'ETHBTC', 'interval': '1m', 'startTime': 1210685792000, 'limit': 1 } response = requests.get(url, params=params) print(response.json())

OUTPUT: [[1500004800000, '0.08000000', '0.08000000', '0.08000000', '0.08000000', '0.04300000', 1500004859999, '0.00344000', 1, '0.00000000', '0.00000000', '0']]

I tested it in several environments and it's always the same behaviour, as described above. More over, few days ago get_historical_klines() worked and I used it to populate database with timestamps of earlies klines per symbol.

Does someone know something? Maybe Binance changed something on its side?

pcriadoperez commented 3 weeks ago

Hi @DSDecay , yes it's because the API does not go that far back as you can see from the request you are requesting from timestamp 1210685792000 but binance is returning from timestamp 1500004800000. Can you try with a more recent timestamp?

I'll leave this issue open because I believe library should return in this case an error to better inform the user and not just keep you waiting indefinetly