Ethereum Historical Market Data from Bitfinex: A Step-by-Step Guide
As an Ethereum developer, you probably know how important it is to have accurate and reliable historical market data to inform your trading strategies. In this article, we will cover how to retrieve historical OHLC (Open, High, Low, Close) data from the Bitfinex API using a step-by-step guide.
Prerequisites
Before diving into the code, make sure you have:
1. Step 2: Set up your Bitfinex API credentials
Log in to your Bitfinex account and go to the “API” section. Create a new application or edit an existing one, then click “Create New API Client”. Copy the Client ID and Client Secret, as you will need them later.
Step 2: Choose a Cryptocurrency Symbol
In your Bitfinex account, select the Ethereum (ETH) cryptocurrency. You can find it in the list of available symbols under the “Symbols” section.
Step 3. Create a new request to retrieve historical OHLC data
Use the following code as an example:
import requests

Set API credentialsclient_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
Define the cryptocurrency symbol and time rangesymbol = 'ETH'
start_time = '2020-01-01 00:00:00'
end_time = '2022-02-26 23:59:59'
Create the API request URLurl = f'
Set API headers (optional)headers = {
'Content-Type': 'application/json',
'x-bitfinex-api-key': client_secret
}
Send a request to retrieve historical OHLC dataresponse = requests.get(url, headers=headers)
Check if the response was successfulif responses.status_code == 200:
Parse the JSON response and store the data in a listdata = response.json()
for item data['data']:
print(item['date'], item['open'], item['high'], item['low'], item['close'])
else:
print(f'Error: {response.status_code}')
Step 4. Handle page allocation and parse the JSON response
If your request returns a response with pages, you will need to handle it accordingly. In this example, we use the variable “data” as a list of OHLC data.
To parse the JSON response and store the data in a more convenient format (e.g., in the Pandas data framework), you can use libraries like pandas. Here is the updated code snippet:
import pandas as pd
Define a function to parse the JSON response and create a Pandas data framedef parse_ohlc_data(data):
Create a dictionary with OHLC datadf = data[0].to_dict()
Create a Pandas DataFrame from the dictionarydf['date'] = [item['date'] for item df]
df['open'] = [item['open'] for item df]
df['high'] = [item['high'] for item df]
df['low'] = [item['low'] for item df]
df['close'] = [item['close'] for item df]
return pd.DataFrame(df)
Parse the JSON response and create a Pandas data framedf = parse_ohlc_data(data)
5. Step 3: Print or Use Your Data
Now that you have retrieved your historical OHLC data, you can print it to the console or save it to a database for further analysis.
That’s it! You have successfully retrieved and parsed Ethereum historical market data from Bitfinex using the Bitfinex API. Don’t forget to keep your API credentials safe and handle page retrieval appropriately when retrieving large data sets. Happy coding!