banner
stmoonar

stmoonar

无心而为
github
telegram
email
zhihu
x

Binance API Usage (Part Two: Exchange Instances and Data Retrieval)

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 invitation 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 the 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 as follows:

  • 'timeout' is the request timeout period, 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, which can be installed using the command pip install pandas, and then we can import it with import pandas as pd. After that, we can use pd to call the classes and methods within it.

BTC_df= pd.DataFrame(exchange.fetch_ohlcv(symbol='BTC/USDT', timeframe='5m', limit=1000))

Here, we use the fetch_ohlcv() method of exchange to obtain the candlestick data we need, with the following parameters:

  • symbol is a string type, representing the symbol for which we want to obtain data, such as BTC/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 (the 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 (without the since parameter, the default is the most recent limit data points) for Bitcoin against USDT at a 5-minute level.

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 the processing of this data, we use pandas to set the column names:

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:

image

If 1000 data points are not enough, you can use a loop and the since parameter to obtain earlier data. For example, here we can see that for 5-minute level data, the Timestamp between adjacent data points differs by 300000, so we can specify since as BTC_df['Timestamp'][0]-1000*300000 to obtain an earlier 1000 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=BTC_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.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.