Getting Started with Binance & Plotly

Binance is one of the cheapest (in terms of fees) crypto trading platforms out there and it offers an API. I love APIs because they allow me to repeat exactly the same thing without my psyche trying to tricking me. And especially when you go into finance that's pretty important — it comes very handily being a technocrat. Plus: taking myself 20 years back in time I keep remembering this quote:

Why program by hand in five days what you can spend five years of your life automating?

from the book: “Language Translation Using PCCTS and C++: A Reference Guide”. That is very much what I love spending my spare time on (when it’s rainy outside). The objective of this article is to walk you through the setup of Binance python and eventually displaying data gathered from this API.

I usually start a new project off in a new virtual python environment. Don’t ask me why — I like using virtualenvwrapper and I usually publish my code on GitHub:

mkvirtualenv binance-python
workon binance-python
mkdir ~/Documents/GitHub/binance-python-eval
cd ~/Documents/GitHub/binance-python-eval

Next, we need to get a few Python packages into the newly created environment:

pip install python-binance
pip install pandas
pip install plotly

I would be tempted to use jupyter-notebooks for this type of project. However, as I would like to build upon the project I am going for proper Python and my IDE of choice is PyCharm.

PyCharm New Project Configuration

If anything is going to change this is the Binance documentation link. Log into your Binance account and go to API Management:

Create an API label:

and verify through phone AND e-mail(I had to do both — within 60s):

and after that you should end up on your API page:

with the important information about your API Key and Secret Key.

The below example is a bit more than hello world but hopefully just what you were looking for. We 1) read the historical data from the Binance API, 2) convert it into a pandas data frame, and 3) plot the result using the Plotly candlestick chart and that’s 25 lines including comments:

from binance.client import Client
import datetime
import pandas as pd
import numpy as np
import plotly.graph_objs as go
import binancekeys

client = Client(binancekeys.APIkey, binancekeys.APIsecret)
granularity = Client.KLINE_INTERVAL_30MINUTE
start_time = "1 March, 2021"
end_time = "1 April, 2021"

# get historical data from binance API
klines = np.array(client.get_historical_klines(symbol, granularity, start_time, end_time))
# reshape date to pandas
df = pd.DataFrame(klines.reshape(-1, 12), dtype=float, 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'))
df['Open Time'] = pd.to_datetime(df['Open Time'], unit='ms')

# plot candlesticks using plotly
fig = go.Figure(data=[go.Candlestick(x=df['Open Time'],

Note: binancekeys is the module containing my API key and secret. You have to replace this with your own key.

Plotly candlestick output for the above code.

And last but not least you can get the code from my GitHub.

Holds a P.h.D. in bioelectromagnetics, leads the research & development of a Zurich based enterprise, and speed-climbs the summits of the alps.