import time
def fetch_historical_data_until_now(exchange, symbol, timeframe, since):
limit = 1000 # Maximum limit per request
all_data = []
while since < exchange.milliseconds():
data = exchange.fetch_ohlcv(symbol, timeframe, since, limit)
if not data: # No more data available
break
all_data.extend(data)
since = data[-1][0] + exchange.parse_timeframe(timeframe) * 1000 # Update the 'since' timestamp
time.sleep(exchange.rateLimit / 1000) # Respect the rate limit
df = pd.DataFrame(all_data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['datetime'] = pd.to_datetime(df['timestamp'], unit='ms')
df = df.reset_index(drop=True)
df['time'] = df.index
return df
# Example usage:
symbol = 'ETH/USDT'
timeframe = '1h'
since = exchange.parse8601('2022-01-01T01:01:00Z')
historical_data = fetch_historical_data_until_now(exchange, symbol, timeframe, since)
historical_data.to_csv("ETH_1h_granularity_check_binance.csv", index=False)