In this article, I’m going to show how to obtain raw trading data from Binance. Please make sure that ‘Anaconda’ and ‘Python 3.7’ are installed in your computer. Before starting, we need to install ‘python-binance’ module. It can be installed using pip. Go to anaconda prompt and type the following code:
pip install python-binance
Now we can import the ‘python-binance’ module as well as the other modules that we need by typing the following lines of code in your favorite text editor. I personally prefer ‘Visual Studio Code’:
import numpy as np import pandas as pd import datetime from binance.client import Client
In order to use this module, we need an api key and an api secret from your binance account. For this, first, log in to your binance account and navigate to your account settings page. Go to API settings. Name your key and press submit. Copy your API key and secret. Do not store your api secret in a public and unsafe location. The secret is only shown when creating the api key. Therefore, make sure that it is copied and stored. For the details, you may also google it by typing ‘How to create Binance API’. Now we are ready to use our binance client. Type the following lines of code.
api_key = "your api key from binance" api_secret = "your api secret from binance" client = Client(api_key, api_secret)
From the client object, we use ‘get_klines’ method in order to receive the data. In this example, we receive Litecoin (ltc) data as usdt between the dates “10 July, 2019” and “10 Aug, 2019”. The interval is 1 hour. This is done using the following line of code:
start_date = "10 July, 2019" end_date = "10 Aug, 2019" klines = client.get_historical_klines('LTCUSDT', Client.KLINE_INTERVAL_1HOUR, start_date , end_date)
In order to manipulate the data, first, I create a two dimensional numpy array. Then, I load the data into the array by iterating each kline item.
data = np.zeros([len(klines),9]) for i, kline in enumerate(klines): otime, open, high, low, close, vol, ctime, asset_vol, trades, _, _, _ = kline data[i,0] = otime data[i,1] = np.float(open) data[i,2] = np.float(high) data[i,3] = np.float(low) data[i,4] = np.float(close) data[i,5] = np.float(vol) data[i,6] = ctime data[i,7] = np.float(asset_vol) data[i,8] = trades
As you may guess, for our data, the columns are ‘otime’, ‘open’, ‘high’, ‘low’, ‘close’, ‘vol’, ‘ctime’, ‘asset_vol’, and ‘trades’. Since the ‘DataFrame’ of the ‘pandas’ module provides plenty of conveniences for data manipulation, we may convert the numpy array into a pandas data frame by typing the following lines. Note that we also convert the date colums to human readible formats.
columns=['otime', 'open', 'high', 'low', 'close', 'vol', 'ctime', 'asset_vol', 'trades'] df = pd.DataFrame(data,columns=columns) df['otime'] = df['otime'].apply(lambda x: datetime.datetime.fromtimestamp(int(x/1000))) df['ctime'] = df['ctime'].apply(lambda x: datetime.datetime.fromtimestamp(int(x/1000)))
Finally, we store our data frame in a csv file.
That’s all for this post. Enjoy your trading!