Cryptocurrency traders are obsessed with data. Analyzing trade by trade to find the perfect entry or exit point. Looking for the next big breakout. Every data point matters.

That’s why this tutorial will focus on the exchange charting data which most developers need when charting.

Real-time candlestick data is critical for accurate charting, quick decision making, and calculating the perfect trade.

Let’s dig into the different ways we can access candlestick data and how we can use this data to generate historical price charts.

Install Libraries

Before we begin, let’s go ahead and install the necessary libraries for the following examples. Pandas and Plotly will help us chart the data we collect from CCXT and Shrimpy.

pip install ccxt

pip install shrimpy-python

pip install pandas

pip install plotly==4.1.0

CCXT Example

Jumping right into the first example, we will take the LTC/BTC trading pair and collect the OHLCV candles from Binance.

Once we’ve collected the necessary data, we will use a plotting library to graph the candlesticks. The results of this example will be included at the end of the tutorial.

Imports

Import the necessary libraries for this example. In addition to the CCXT library, we will be using the Plotly library to chart the data and datetime to convert the candlestick data to our desired time format.

import ccxt

from datetime import datetime

import plotly.graph_objects as go

Create an Exchange Object

In this example, we will access candlestick data from the Binance exchange. Before we can request candlestick data, we must first create the Binance object that helps us manage the requests to the exchange.

binance = ccxt.binance()

trading_pair = 'LTC/BTC'

Get Data

Access the data by requesting the OHLCV candlestick data from the exchange. This will return the candlesticks based on the interval which is specified in the second argument. In this example, we will collect 1-hour candlesticks.

candles = binance.fetch_ohlcv(trading_pair, '1h')

Format Data

To plot the data from the exchange, we will use the Plotly library. This library requires our data to be formatted differently than what is returned from Binance. As a result, we will need to reformat this data to match the expected data format for the Plotly library.

dates = []

open_data = []

high_data = []

low_data = []

close_data = [] for candle in candles:

dates.append(datetime.fromtimestamp(candle[0] / 1000.0).strftime('%Y-%m-%d %H:%M:%S.%f'))

open_data.append(candle[1])

high_data.append(candle[2])

low_data.append(candle[3])

close_data.append(candle[4])

Plot Candlesticks

Use the plotting library to display the candlesticks on a chart. This chart will be generated in a new browser window where you can click around on the chart and tinker with some of the data.

fig = go.Figure(data=[go.Candlestick(x=dates,

open=open_data, high=high_data,

low=low_data, close=close_data)]) fig.show()