Using CCXT for Python to Access Binance, Lykke, and more crypto-APIs

I explained how to get started with the python-binance library accessing the Binance API in my previous article. Now that I have crypto accounts with Binance, Kraken, Coinbase, Lykke, and a few more, I would have to build an individual interface for all of them. Luckily, with CCXT, there seems to be a common interface to all of them. This article gets you started with CCXT on the examples of Binance and Lykke.

As for every new Python project, I like to dedicate a virtual environment to it:

mkvirtualenv crypto-python
workon crypto-python
pip install ccxt
pip install pandas
pip install plotly

You can read up on how to set up your PyCharm IDE and your Binance account for API use in my other article on the topic. A description of how to obtain your API key for Lykke is available here. Note: with Lykke, you have to create a separate API wall, and you will only have a key — no secret.

I recommend storing your keys in a safe place and, ideally, not your script. Though not extremely safe — I keep my API key in a separate python module(JSON-like) call CryptoAPIKeySecrets. I keep the file one directory down, so I also do not accidentally commit it to GitHub. The file structure I use is like this:


To get started with the CCXT API, the below example fetches the balance from both your Lykke and your Binance wallets and prints the result:

import ccxt
import sys
sys.path.append("../ZZZ_secrets") # to access my secret file one level up
import CryptoAPIKeySecrets #<= see file structure above^

ccxtBinance = ccxt.binance({
'apiKey': CryptoAPIKeySecrets.apiKeySecrets['binance']['apiKey'],
'secret': CryptoAPIKeySecrets.apiKeySecrets['binance']['secret']
ccxtLykke = ccxt.lykke({
'apiKey': CryptoAPIKeySecrets.apiKeySecrets['lykke']['apiKey'],

# see what you have in your wallets

The next step is to reproduce the OHLCV candle-stick chart from my last article with market data from both exchanges. Note: not all exchanges support this, but you can use the CCXT method: has[‘fetchOHLCV’] to check exactly this.

To plot the historical data from both exchanges for the symbol BTC/EUR, add the below code after the “getting started” example. To be compatible with my other article, I performed the conversion from a text date to the UTC timestamp as required by CCXT.

import numpy as np
import pandas as pd
import datetime
import plotly.graph_objs as go

granularity = '30m'
start_date = "1 March, 2021"
utc_start_s = datetime.datetime.strptime(start_date, '%d %B, %Y').replace(tzinfo=datetime.timezone.utc).timestamp()
end_date = "1 April, 2021"
utc_stop_s = datetime.datetime.strptime(end_date, '%d %B, %Y').replace(tzinfo=datetime.timezone.utc).timestamp()
delta_time_s = (utc_stop_s-utc_start_s)
span = delta_time_s/60./30 # number of 30m intervals from start to end

ohlcv_btceur_binance = np.array(ccxtBinance.fetchOHLCV (symbol, timeframe = granularity, since = int(utc_start_s*1000), limit=int(span)))
ohlcv_btceur_lykke = np.array(ccxtLykke.fetchOHLCV (symbol, timeframe = granularity, since = int(utc_start_s*1000), limit=int(span)))

df_ohlcv_btceur_binance = pd.DataFrame(ohlcv_btceur_binance.reshape(-1, 6), dtype=float, columns=('Open Time','Open','High','Low','Close','Volume'))
df_ohlcv_btceur_binance['Open Time'] = pd.to_datetime(df_ohlcv_btceur_binance['Open Time'], unit='ms')

df_ohlcv_btceur_lykke = pd.DataFrame(ohlcv_btceur_lykke.reshape(-1, 6), dtype=float, columns=('Open Time','Open','High','Low','Close','Volume'))
df_ohlcv_btceur_lykke['Open Time'] = pd.to_datetime(df_ohlcv_btceur_lykke['Open Time'], unit='ms')

# plot candlesticks using plotly
fig = go.Figure(data=[go.Candlestick(x=df_ohlcv_btceur_binance['Open Time'],
fig.update_layout(title = "OHLCV from Binance")
fig = go.Figure(data=[go.Candlestick(x=df_ohlcv_btceur_lykke['Open Time'],
fig.update_layout(title = "OHLCV from Lykke")

In the output of the above example, you will notice the first bug in CCXT (or Lykke’s API). In contrast, Binance returns the correct timeframe (1months after the start date). Lykke will only return the latest data.

Candlestick plot of the OHLCV data from Binance
Candlestick plot of the OHLCV data from Lykke. Note — Lykke/CCXT did not return the requested timeframe.

This article illustrated the use of CCXT to harmonize the access of different crypto exchange APIs. The example code is available 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.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store