This article is the second part of using the Binance exchange API, mainly introducing how to instantiate the exchange and obtain historical candlestick data using the ccxt
library. If you haven't registered on Binance yet, feel free to use my referral link to register 😘, and you can also get trading fee discounts: https://accounts.binance.com/register?ref=DPVSZVI3
1. ccxt#
Project address: https://github.com/ccxt/ccxt
ccxt is a JavaScript / Python / PHP / C# library for cryptocurrency trading and e-commerce, supporting the APIs of many cryptocurrency exchanges such as Bitcoin/Ethereum.
This tutorial uses Python and can be installed using pip
or conda
.
pip install ccxt
# or
conda install ccxt
Check supported exchanges:
import ccxt
print(ccxt.exchanges) # print a list of all available exchange classes
(Although ccxt
has documentation, it feels poorly written, and I struggled a lot when I first used it, which is why I wanted to write this series of blogs.)
2. Exchange Instance Initialization#
exchange = ccxt.binance({
'apiKey': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'secret': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'timeout': 30000,
'enableRateLimit': True,
'options': {'defaultType': 'future'}
})
exchange.load_markets()
Here, an instance of the ccxt.binance
class is created, using the apiKey
and secret
from the API we created earlier. The meanings of the other parameters are:
'timeout'
is the request timeout in milliseconds. If a response is not received within this time, a timeout error will be thrown.'enableRateLimit'
is a boolean value; if set to True, the ccxt library will respect the exchange's rate limits to avoid sending too many requests.'options'
is a dictionary used to set some additional options. Here,'defaultType': 'future'
indicates that the default trading type is futures or contracts; if you need spot trading, set the value to 'spot'.
The method exchange.load_markets()
is used to load all available market data from the Binance exchange. This method is usually called before starting trading to ensure that the latest market data is available.
3. Obtaining Candlestick Data#
To facilitate data processing, we need to use the pandas
library. Install it using the command pip install pandas
, and then import it with import pandas as pd
, after which you can use pd
to call its classes and methods.
BTC_df= pd.DataFrame(exchange.fetch_ohlcv(symbol='BTC/USDT', timeframe='5m', limit=1000))
Here, the fetch_ohlcv()
method of exchange
is used to obtain the candlestick data we need, with the following parameters:
-
symbol
is a string type, representing the trading pair for which we want to obtain data, such asBTC/USDT
, which indicates the price of Bitcoin against USDT. -
timeframe
is also a string type, indicating the time scale of the candlestick data we want to obtain, for example, 5 minutes —5m
, one day —1d
(default value is 1m). -
limit
is an integer type, indicating the number of data points to obtain; the upper limit seems to be either 1500 or 1000, which I haven't tested, so you can try it out. -
Additionally, there are:
since
: an integer type, indicating the timestamp from which to start obtaining data.params
: a dictionary for storing additional parameters, which are generally not used.
The example code above retrieves the most recent 1000 candlestick data points for Bitcoin against USDT at a 5-minute level (without the since
parameter, it defaults to the most recent limit
data points).
Each data point obtained contains the following entries: 'Timestamp' (timestamp), 'Open' (opening price), 'High' (highest price), 'Low' (lowest price), 'Close' (closing price), 'Vol' (trading volume).
To facilitate processing of this data, we set the column names using pandas
:
BTC_df = pd.DataFrame(exchange.fetch_ohlcv('BTC/USDT', timeframe='5m', limit=1000))
BTC_df.columns = ['Timestamp', 'Open', 'High', 'Low', 'Close', 'Vol']
The data seen in Jupyter Notebook looks like this:
If 1000 data points are not enough, you can use a loop and the since
parameter to obtain earlier data. For example, we can see that for 5-minute level data, the Timestamp between adjacent data points differs by 300000 milliseconds, so we can specify since
as BTC_df['Timestamp'][0]-1000*300000
to obtain 1000 earlier data points and concatenate them with the later data. The code is as follows:
COUNT = 3 # Number of times to obtain data
BTC_df = pd.DataFrame(exchange.fetch_ohlcv(symbol='BTC/USDT', timeframe='5m', limit=1000))
for i in range(COUNT - 1):
BTC_df = pd.concat([pd.DataFrame(exchange.fetch_ohlcv(symbol='BTC/USDT',
timeframe='5m',
since=COIN_df['Timestamp'][0]-1000*300000,
limit=1000)), BTC_df], ignore_index=True)
time.sleep(1)
BTC_df.columns = ['Timestamp', 'Open', 'High', 'Low', 'Close', 'Vol']
By modifying the value of the variable COUNT
, you can obtain the corresponding number of data points.