NoviceCoding NoviceCoding - 1 year ago 502
Python Question

Intraday candlestick charts using MatPlotLib

I've been having some difficulty with MatPlotLib's finance charting. Seems like their candlestick charts work best with daily data and I am having a hard time making them work with intraday (every 5 minutes, between 9:30 and 4pm) data.

I have pasted sample data in pastebin, top is what I get from the database, bottom is tupled with the date formated into an ordinal float for use in matplotlib.

Link to sample data

When I draw my charts there are huge gaps in it, the axes suck, the zoom is equally horrible.
enter image description here
Can anyone guide me through making a nice readable graph out of this data? My ultimate goal is to get a chart that looks remotely like this:
enter image description here The data points can be in various increments from 5minutes to 30 minutes.

I have also made a Pandas dataframe of the data, not sure if pandas has candlestick functionality.

Answer Source

If I understand well, one of your major concern is the gaps between the daily data. To get rid of them, one method is to artificially 'evenly space' your data (but of course you will loose any temporal indication intra-day).

Anyways, doing this way, you will be able to obtain a chart that looks like the one you have proposed as an example.

The commented code and the resulting graph are below.

import numpy as np
import matplotlib.pyplot as plt
import datetime

from import candlestick
from matplotlib.dates import num2date

# data in a text file, 5 columns: time, opening, close, high, low
# note that I'm using the time you formated into an ordinal float
data = np.loadtxt('finance-data.txt', delimiter=',')

# determine number of days and create a list of those days
ndays = np.unique(np.trunc(data[:,0]), return_index=True)
xdays =  []
for n in np.arange(len(ndays[0])):

# creation of new data by replacing the time array with equally spaced values.
# this will allow to remove the gap between the days, when plotting the data
data2 = np.hstack([np.arange(data[:,0].size)[:, np.newaxis], data[:,1:]])

# plot the data
fig = plt.figure(figsize=(10, 5))
ax = fig.add_axes([0.1, 0.2, 0.85, 0.7])
    # customization of the axis
ax.tick_params(axis='both', direction='out', width=2, length=8,
               labelsize=12, pad=8)
    # set the ticks of the x axis only when starting a new day
ax.set_xticklabels(xdays, rotation=45, horizontalalignment='right')

ax.set_ylabel('Quote ($)', size=20)
ax.set_ylim([177, 196])

candlestick(ax, data2, width=0.5, colorup='g', colordown='r')