Using Python to get data from Binance API

Umberto Grando
2 min readFeb 3, 2022

--

Hello World!

For anyone investing in cryptocurrencies, the Binance name will be familiar. For those who don’t know it, Binance is a cryptocurrency exchange which is the largest exchange in the world in terms of daily trading volume of cryptocurrencies.

Today we are going to use the Binance API to retrieve bitcoin trend data over the past 7 days.

First we need to get an API key from Binance:

You can follow the needed steps here. It’s fundamental that you write down the API Key and Secret Key that you create because we are going to use them in the code.

We also need to install the binance library:

pip install python-binance

Now we can start coding:

Let’s have a look step by step:

In this first part we import the libraries we need and we set the keys we retrieved when we’ve created on Binance.

Here we have two functions that are needed to convert the dates in the format we need to plot it in the right way. In the date_to_unix function we also use a timedelta to go back 7 days.

And we also need to choose which crypto we want to track (in this case Bitocoin/USDT).

The final part is where the things get interesting. Here we call the API asking the KLINE data with an interval of 5 minutes starting from 7 days before today.

Then we store those values in the variable values, while also converting the date from unix to datetime.

Finally we store the data in a dataframe and we plot it using matplotlib:

--

--

Umberto Grando

I’m Umberto Grando, an IoT Specialist with a passion for programming, gaming and technology in general.