Cryptocurrency merchants and fans typically depend on historic information to investigate worth tendencies, determine patterns, and make knowledgeable selections. On this complete tutorial, we are going to discover step-by-step the best way to fetch historic information from Bitfinex utilizing Python, a well-liked programming language, and the Bitfinex API.
Earlier than diving into the code, guarantee that you’ve got the required instruments put in. You’ll want Python and the bitfinex
library. Set up it utilizing:
pip set up bitfinex
Let’s begin by inspecting the Python code you supplied:
import bitfinex
import datetime
import time
import pandas as pd
We start by importing the required libraries: bitfinex
for interacting with the Bitfinex API, datetime
for dealing with date and time, time
for managing delays, and pandas
for information manipulation.
# Outline question parameters
pair = 'BTCUSDT'
TIMEFRAME = '1h'
TIMEFRAME_S = 60
Right here, we specify the cryptocurrency pair of curiosity (pair
), the specified timeframe (TIMEFRAME
), and the timeframe in seconds (TIMEFRAME_S
).
# Outline the beginning date
t_start = datetime.datetime(2023, 10, 7, 0, 0)
t_start = time.mktime(t_start.timetuple()) * 1000# Outline the top date
t_stop = datetime.datetime(2023, 10, 9, 0, 0)
t_stop = time.mktime(t_stop.timetuple()) * 1000
Set the beginning and finish dates for the historic information. The dates are transformed to milliseconds because the epoch for compatibility with the Bitfinex API.
Now, let’s take a better have a look at the fetch_data
operate:
# Operate to fetch information
def fetch_data(begin, cease, image, interval, TIMEFRAME_S):
restrict = 1000
api_v2 = bitfinex.bitfinex_v2.api_v2()
hour = TIMEFRAME_S * 1000
step = hour * restrict
information = []total_steps = (cease - begin) / hour
whereas…