3

Good afternoon,

I would like to see if any of you could help me make a candle chart in minutes. I have managed to graph them in days but I do not know how to do them in minutes.

Attached code.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import dates, ticker
import matplotlib as mpl
from mpl_finance import candlestick_ohlc

mpl.style.use('default')

data = [('2017-01-02 02:00:00', '1.05155', '1.05197', '1.05155', '1.0519'),
    ('2017-01-02 02:01:00', '1.05209', '1.05209', '1.05177', '1.05179'),
    ('2017-01-02 02:02:00', '1.05177', '1.05198', '1.05177', '1.05178'),
    ('2017-01-02 02:03:00', '1.05188', '1.052', '1.05188', '1.052'),
    ('2017-01-02 02:04:00', '1.05196', '1.05204', '1.05196', '1.05203'),
    ('2017-01-02 02:06:00', '1.05196', '1.05204', '1.05196', '1.05204'),
    ('2017-01-02 02:07:00', '1.05205', '1.0521', '1.05205', '1.05209'),
    ('2017-01-02 02:08:00', '1.0521', '1.0521', '1.05209', '1.05209'),
    ('2017-01-02 02:09:00', '1.05208', '1.05209', '1.05208', '1.05209'),
    ('2017-01-02 02:10:00', '1.05208', '1.05211', '1.05207', '1.05209')]

ohlc_data = []

for line in data:
    ohlc_data.append((dates.datestr2num(line[0]), np.float64(line[1]), np.float64(line[2]), np.float64(line[3]), np.float64(line[4])))

fig, ax1 = plt.subplots()
candlestick_ohlc(ax1, ohlc_data, width = 0.5, colorup = 'g', colordown = 'r', alpha = 0.8)

ax1.xaxis.set_major_formatter(dates.DateFormatter('%d/%m/%Y %H:%M'))
ax1.xaxis.set_major_locator(ticker.MaxNLocator(10))

plt.xticks(rotation = 30)
plt.grid()
plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Historical Data EURUSD')
plt.tight_layout()
plt.show()
Luis Portillo
  • 33
  • 1
  • 4

1 Answers1

7

So close, but only trial and error will get you any further. Isn't crappy documentation great?

Simply divide width by the number of minutes in a day. Full code for your copy & paste pleasure below, but all I've done is change width = 0.5 to width = 0.5/(24*60).

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import dates, ticker
import matplotlib as mpl
from mpl_finance import candlestick_ohlc

mpl.style.use('default')

data = [('2017-01-02 02:00:00', '1.05155', '1.05197', '1.05155', '1.0519'),
    ('2017-01-02 02:01:00', '1.05209', '1.05209', '1.05177', '1.05179'),
    ('2017-01-02 02:02:00', '1.05177', '1.05198', '1.05177', '1.05178'),
    ('2017-01-02 02:03:00', '1.05188', '1.052', '1.05188', '1.052'),
    ('2017-01-02 02:04:00', '1.05196', '1.05204', '1.05196', '1.05203'),
    ('2017-01-02 02:06:00', '1.05196', '1.05204', '1.05196', '1.05204'),
    ('2017-01-02 02:07:00', '1.05205', '1.0521', '1.05205', '1.05209'),
    ('2017-01-02 02:08:00', '1.0521', '1.0521', '1.05209', '1.05209'),
    ('2017-01-02 02:09:00', '1.05208', '1.05209', '1.05208', '1.05209'),
    ('2017-01-02 02:10:00', '1.05208', '1.05211', '1.05207', '1.05209')]

ohlc_data = []

for line in data:
    ohlc_data.append((dates.datestr2num(line[0]), np.float64(line[1]), np.float64(line[2]), np.float64(line[3]), np.float64(line[4])))

fig, ax1 = plt.subplots()
candlestick_ohlc(ax1, ohlc_data, width = 0.5/(24*60), colorup = 'g', colordown = 'r', alpha = 0.8)

ax1.xaxis.set_major_formatter(dates.DateFormatter('%d/%m/%Y %H:%M'))
ax1.xaxis.set_major_locator(ticker.MaxNLocator(10))

plt.xticks(rotation = 30)
plt.grid()
plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Historical Data EURUSD')
plt.tight_layout()
plt.show()
Heath Raftery
  • 3,643
  • 17
  • 34
  • 3
    `mpl_finance` is unmaintained, so crappy documentation is what you'll get. If someone who had an interest wanted to take the project over it would be warmly welcomed. – Jody Klymak Jan 03 '18 at 23:56
  • However, the major problem I'm facing is that my 1min intraday OHLC data is not consecutive. For example, 9:00 - 11:30, 13:30 - 15:00. That's the day trading hour, plus a night trading period from 21:00 - 23:30. But I don't want the bar between 11:30 and 13:30 to have a large gap, I want them to display consecutively. How to do that? – StayFoolish Jan 12 '18 at 04:02
  • I doubt the library supports non-consecutive data. I'd either try inserting dummy data or timeshifting it to remove the gap and creating your own x-axis labels. – Heath Raftery Jan 13 '18 at 15:02
  • For me this almost completely worked. There is only one problem, I don't seem to be able to see the green candles. Maybe some graphics bug... I mean I see just one line like it was a doji candle – Giuseppe Salvatore Aug 01 '20 at 21:45