How to get ohlcv data for your exchange with ccxt library?

Analytics Image

Crypto market is obviously volatile not only bitcoin but other tokens too. Crypto assets that are listed in crypto exchanges have been highly correlated with bitcoin and shows its high volatility. Either bullish or bearish movement in the markets those crypto currencies price fluctuate against fiat currencies. Crypto currencies volatility is also driven in large part by varying perceptions of the intrinsic value of the cryptocurrency as a store of value and method of value transfer (especially bitcoin for its purpose). Some crypto currencies are still so speculative without public adoption as money or system use and investors would liquidate their position into fiat currency at some point when the sense of crypto was diminished. So it’s vital to do your own research when you analyze the market and observe the price move.

In this article I’d like to introduce what OHLCV format and why it’s important, how to obtain OHLCV data efficiently from your crypto exchange with ccxt library in Python.

What is OHLCV?

OHLCV stands for Open, High, Low, Close and Volume (Volume is optional). Now you can understand this format data consists of values of Open, High, Low, Close and Volume of your asset. What are Open, High, Low, Close? Long story to short, OHLCV format is a series of 5 data points of the opening price, the highest price, the lowest price, the closing price, the trading volume within a particular interval such as 1 hour or 1 day. In popular OHLC bar chart it appears as vertical lines over the unit of partucular time, each vertical line on the chart shows the price range (the highest and lowest prices). Tick marks project from each side of the line indicating the opening price on the left, and the closing price for that time period on the right.

Why is OHLCV important?

  1. OHLCV data can be used for OHLC bar chart, Candlestick chart that shows a lot of visual patterns for your analysis
  2. OHLCV data can be used to calculate technical indicators to perceive patterns and signals for systematic trading

In this blog I don’t go over the patterns that can be seen on the chart and the technical indicators that we can interpret with the chart. There are a lot of great articles about chart analysis on web if you research. We will look through ccxt library and how to obtain OHLCV data from your exchange.

What is CCXT library?

It is a set of classes of available crypto exchanges and it wraps public and private API methods in it. It supports 124 crypto exchange markets and trading APIs as of writing. So you may be able to find your crypto exchange from the official library link. I’m using Binance so all example would be written for Binance with ccxt in this blog but you can just replace binance part with your exchange name as defined in ccxt.

ccxt Documentation

Installation of ccxt in Python is straightforward.

pip install ccxt

We can list up all available exchanges.

print(ccxt.exchanges)

How to fetch OHLCV data in ccxt?

fetch_ohlcv or fetchOHLCV is the function to obtain a list of array for specified symbol. Initiate an instance of your crypto exchange and call has function. The function returns an associative array containing flags for exchange capabilities like below. If the boolean value for fetchOHLCV is true then the capability is unified in the library natively. If you find it as emulated it means the endpoint is not natively available from your crypto exchange API but reconstructed by the ccxt library from available true-methods.

timeframes function is callable if the exchange has fetchOHLCV property true. This function returns an associative array of timeframes, supported by the fetchOHLCV method of the exchange.

import ccxt
binance = ccxt.binance()
print(binance.has)

The fetchOHLCV (fetch_ohlcv) function is declared as following. symbol needs to be confirmed in each crypto exchange. The easiest way to confirm in ccxt library is to call instance’s load_markets() function. It returns a dictionary of all symbols in the crypto exchange.

fetchOHLCV(symbol, timeframe='1m', since=undefined, limit=undefined, params=
{})

This is the example in the official document to fetch all pairs 1d OHLCV data from a particular exchange. We did not give the since in this example. The since argument is an integer UTC timestamp in milliseconds. If since is not specified fetchOHLCV method it will return the time range as is the default in the exchange itself.

The function returns a list (a flat array) of OHLCV candles represented by the following structure. As this is a flat array of 5 data points (Open, High, Low, Close, Volume) and UTC timestamp in milliseonds, it’s easy to convert this flat array into DataFrame object.

[
    [
        1504541580000, // UTC timestamp in milliseconds, integer
        4235.4, // (O)pen price, float
        4240.6, // (H)ighest price, float
        4230.0, // (L)owest price, float
        4230.7, // (C)losing price, float
        37.72941911 // (V)olume (in terms of the base currency), float
    ],
...
]

I wrote this example how to fetch 5m interval OHLCV data for the last 60 minutes. I must return 12 rows for every 5m interval and each row has 5 data points plus utc timestamp. I set the limit option 12 for 12 candles that are equivalent to an hour (the last 60 minutes).

How to fetch OHLCV efficiently with specific timeframe?

We’ve learned how to obtain OHLCV data in a flat array in ccxt for your exchange by now, however it was not efficient to calculate since and limit options from how far back in time we had to request with your time interval. Here’s just an idea to call fetchOHLCV method with a list of dates in str, specific timeframe and symbol ticker.

We can just generate a list of dates in str and call ohlcv function like below. It will return DataFrame with index column Time that is datetime object from UTC timestamp information.

ohlcv(dt=['20200101', '20200102'], pair='ETH/BTC', period='1h')

One thought on “How to get ohlcv data for your exchange with ccxt library?”

Leave a Reply

Your email address will not be published. Required fields are marked *