In the domain of cryptocurrency analysis and gaining access to historical data is vital. In this post I describe how to get historical data for a particular trading pair using the Binance API. We will walk through the process of utilising the Python programming language to retrieve and manipulate the data.
1. Importing Libraries
First, what we need to import the necessary libraries which is binance.client
, pandas
, and datetime
.
from binance.client import Client
import pandas as pd
import datetime
**binance.client**
: This library provides the main methods to automate interactions with the Binance.
If you are facing any error in this stage, it means your environment does not have required libraries. So, install libraries with the help of following lines in terminal:
# if you do not have pip run this: python get-pip.py
# then run: pip install [package_name]
pip install python-binance
pip install pandas
2. Setting Up Binance API Credentials
Next, you have to provide your Binance API credentials. Ensure these credentials are kept secure and not exposed in public repositories.
How to generate API credentials on Binance if you do not have it yet:
- Go to your profile:
Account
→API Management
- Click
Create API
button and selectSystem generated
option from key type. - Label your
API Key
as you like e.g.data_fetch
- In the next step you will be asked to complete
Two Step Authentication
. Warning: If your two step authentication is not enabled for your profile you will be asked to activate it before creating API Key. - After verification your
API Key
will be created. CopyAPI Key
andSecret Key
to following lines of code.
api_key = 'your_api_key'
api_secret = 'your_api_secret'
client = Client(api_key, api_secret)
**api_key**
and**api_secret**
: These are placeholders for your Binance API key and secret. Replace'your_api_key'
and'your_api_secret'
with your actual credentials.**Client**
: is an instance is created using the provided API credentials. If you want to get in advanced details of the library and explore list of methods as**get_symbol_info**
,**get_historical_klines**
etc you can visit:## python-binance/binance/client.py at master · sammchardy/python-binance
Binance Exchange API python implementation for automated trading - python-binance/binance/client.py at master ·…
github.com
3. Defining the Trading Pair Symbol and Time Range
Defining the trading pair symbol is the easiest part.
symbol = 'BTCUSDT'
We can set a custom start and end time for our function by parsing the date to date time format.
# format (year, month, day, hour, minute, second)
start_time = datetime.datetime(2024, 3, 15, 0, 0, 0)
end_time = datetime.datetime(2024, 6, 15, 0, 0, 0)
**datetime.datetime**
: Creates datetime object for specified start and end times.
4. Fetching Historical Klines Data
Now, we use the **get_historical_klines**
main function to extract historical candlestick data for our custom time range.
klines = client.get_historical_klines(
symbol=symbol,
interval=Client.KLINE_INTERVAL_1MINUTE,
start_str=str(start_time),
end_str=str(end_time)
)
**interval**
: Time interval it’s set toClient.KLINE_INTERVAL_1MINUTE
for 1-minute intervals. You can useClient.KLINE_INTERVAL_5MINUTE
for 5 minutes,Client.KLINE_INTERVAL_1HOUR
for 1 hour intervals etc.
Note: We did not set
**limit**
argument as it is equal to 1000 by default. You can set it to any desired number up to 1000. The question you might ask: What if I want to fetch more than 1000 data points? Do not worry about it, as you will not go through circle of hell to create loop for fetching greater number of data points. As library handles this itself.
5. Converting Data to DataFrame
With the retrieved data, we can convert it into a Pandas **pd.DataFrame**
for easier manipulation and analysis.
df_M = pd.DataFrame(klines, columns=['Open Time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close Time', 'Quote Asset Volume', 'Number of Trades', 'Taker Buy Base Asset Volume', 'Taker Buy Quote Asset Volume', 'Ignore'])
7. Converting Data Types
It’s vital to convert certain columns to their appropriate data types for accurate analysis and preventing further data type conflicts.
columns_to_convert = ['Open', 'High', 'Low', 'Close', 'Volume', 'Quote Asset Volume', 'Number of Trades', 'Taker Buy Base Asset Volume', 'Taker Buy Quote Asset Volume']
for col in columns_to_convert:
df_M[col] = df_M[col].astype(float)
**astype(float)**
: Parsing the values into float data type.
Full Code
For your convenience here is full code you can copy.
from binance.client import Client
import pandas as pd
import datetime
# Binance API credentials
api_key = 'your_api_key'
api_secret = 'your_api_secret'
client = Client(api_key, api_secret)
# Define the symbol for BTC/USDT pair
symbol = 'BTCUSDT'
# Define custom start and end time
start_time = datetime.datetime(2024, 3, 15, 0, 0, 0)
end_time = datetime.datetime(2024, 6, 15, 0, 0, 0)
klines = client.get_historical_klines(symbol=symbol, interval=Client.KLINE_INTERVAL_1MINUTE, start_str=str(start_time), end_str=str(end_time))
# Convert the data into a pandas dataframe for easier manipulation
df_M = pd.DataFrame(klines, columns=['Open Time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close Time', 'Quote Asset Volume', 'Number of Trades', 'Taker Buy Base Asset Volume', 'Taker Buy Quote Asset Volume', 'Ignore'])
columns_to_convert = ['Open', 'High', 'Low', 'Close', 'Volume', 'Quote Asset Volume', 'Number of Trades', 'Taker Buy Base Asset Volume', 'Taker Buy Quote Asset Volume']
for col in columns_to_convert:
df_M[col] = df_M[col].astype(float)
df_pepe_M
Output:
Key Functions →
Main functions: Client(), get_historical_klines()
Interval: Client.KLINE_INTERVAL_1MINUTE
More from Ja X
Recommended from Medium
[
See more recommendations