Fetching Historical Cryptocurrency Data from Bitfinex Using Pyth

Eymeric Plaisant

Data Modelling Analyst
Data Engineer
pandas
Python

Prerequisites

Before diving into the code, ensure that you have the necessary tools installed. You’ll need Python and the bitfinex library. Install it using:
pip install bitfinex

Setting Up the Code

Let’s start by examining the Python code you provided:
import bitfinex
import datetime
import time
import pandas as pd
We begin by importing the required libraries: bitfinex for interacting with the Bitfinex API, datetime for handling date and time, time for managing delays, and pandas for data manipulation.
Here, we specify the cryptocurrency pair of interest (pair), the desired timeframe (TIMEFRAME), and the timeframe in seconds (TIMEFRAME_S).
# Define query parameters
pair = 'BTCUSDT'
TIMEFRAME = '1h'
TIMEFRAME_S = 60
Set the start and end dates for the historical data. The dates are converted to milliseconds since the epoch for compatibility with the Bitfinex API.
# Define the start date
t_start = datetime.datetime(2023, 10, 7, 0, 0)
t_start = time.mktime(t_start.timetuple()) * 1000
# Define the end date
t_stop = datetime.datetime(2023, 10, 9, 0, 0)
t_stop = time.mktime(t_stop.timetuple()) * 1000

The fetch_data Function

Now, let’s take a closer look at the fetch_data function:
# Function to fetch data
def fetch_data(start, stop, symbol, interval, TIMEFRAME_S):
limit = 1000
api_v2 = bitfinex.bitfinex_v2.api_v2()
hour = TIMEFRAME_S * 1000
step = hour * limit
data = []
total_steps = (stop - start) / hour
while total_steps > 0:
if total_steps < limit:
step = total_steps * hour
end = start + step
data += api_v2.candles(symbol=symbol, interval=interval, limit=limit, start=start, end=end)
print(pd.to_datetime(start, unit='ms'), pd.to_datetime(end, unit='ms'), "steps left:", total_steps)
start = start + step
total_steps -= limit
time.sleep(1.5)
return data
This function is designed to retrieve historical candlestick data from Bitfinex. It employs a strategy of breaking down the data retrieval into smaller chunks to adhere to Bitfinex’s limit on the number of data points per request.

Fetching Data and Data Processing

Now, let’s fetch the data using the defined parameters:
# Fetching data using the defined parameters
result = fetch_data(t_start, t_stop, pair, TIMEFRAME, TIMEFRAME_S)
The fetch_data function is called with the specified parameters.
# Creating a DataFrame from the fetched data
names = ['Date', 'Open', 'Close', 'High', 'Low', 'Volume']
df = pd.DataFrame(result, columns=names)
# Dropping duplicates, converting Date to datetime, and setting it as the index
df.drop_duplicates(inplace=True)
df['Date'] = pd.to_datetime(df['Date'], unit='ms')
df.set_index('Date', inplace=True)
df.sort_index(inplace=True)
# Save the DataFrame to a CSV file
df.to_csv(f"{pair}_{TIMEFRAME}.csv")
The fetched data is organized into a DataFrame, duplicates are removed, and the data is saved to a CSV file named after the cryptocurrency pair and timeframe.
With this Python code, you can easily retrieve historical data from Bitfinex, enabling you to perform in-depth analyses and gain insights into cryptocurrency price movements. Feel free to customize the code based on your specific needs and explore further with additional data analysis techniques.
This tutorial provides a solid foundation for working with historical cryptocurrency data and opens up possibilities for more advanced analyses. Happy coding!
Partner With Eymeric
View Services

More Projects by Eymeric